def testSimpleGraphPattern(self): from rdfextras.sparql.parser import parse global prolog p = parse("BASE <http://example.com/> SELECT ?ptrec WHERE { GRAPH ?ptrec { ?data :foo 'bar'. } }") prolog = p.prolog p = p.query.whereClause.parsedGraphPattern.graphPatterns if prolog is None: prolog = Prolog(u'',[]) prolog.DEBUG = True assert isinstance(reduce(ReduceToAlgebra,p,None),GraphExpression)
def NormalizeGoals(goals): if isinstance(goals,(list,set)): for goal in goals: yield goal,{} elif isinstance(goals,tuple): yield sparqlQuery,{} else: query=RenderSPARQLAlgebra(parse(goals)) for pattern in query.patterns: yield pattern[:3],query.prolog.prefixBindings
def testSimpleGraphPattern(self): from rdfextras.sparql.parser import parse global prolog p = parse( "BASE <http://example.com/> SELECT ?ptrec WHERE { GRAPH ?ptrec { ?data :foo 'bar'. } }" ) prolog = p.prolog p = p.query.whereClause.parsedGraphPattern.graphPatterns if prolog is None: prolog = Prolog(u'', []) prolog.DEBUG = True assert isinstance(reduce(ReduceToAlgebra, p, None), GraphExpression)
def testExpressions(self): from rdfextras.sparql.parser import parse global prolog for inExpr,outExpr in ExprTests: p = parse(inExpr) prolog = p.prolog p = p.query.whereClause.parsedGraphPattern.graphPatterns if prolog is None: prolog = Prolog(u'',[]) if not hasattr(prolog,'DEBUG'): prolog.DEBUG = False self.assertEquals(repr(reduce(ReduceToAlgebra,p,None)),outExpr)
def testExpressions(self): from rdfextras.sparql.parser import parse global prolog for inExpr, outExpr in ExprTests: p = parse(inExpr) prolog = p.prolog p = p.query.whereClause.parsedGraphPattern.graphPatterns if prolog is None: prolog = Prolog(u'', []) if not hasattr(prolog, 'DEBUG'): prolog.DEBUG = False self.assertEquals(repr(reduce(ReduceToAlgebra, p, None)), outExpr)
def testUnicodeString(self): from rdfextras.sparql.parser import parse from cStringIO import StringIO q = \ u""" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?pred WHERE { rdf:foobar rdf:predicate ?pred. } """ p = parse(q)
def testScoping(self): from rdfextras.sparql.parser import parse from rdfextras.sparql.query import SPARQLQueryResult p = parse(scopingQuery) global prolog prolog = p.prolog if prolog is None: prolog = Prolog(u'', []) prolog.DEBUG = True rt = TopEvaluate(p, self.unionGraph, passedBindings={}, DEBUG=False) rt = SPARQLQueryResult(rt).serialize() #format='python') # print("Len rt", len(rt)) self.failUnless(len(rt) == 1, "Expected 1 item solution set") for ppd in rt: self.failUnless(ppd == URIRef('http://example.org/foaf/aliceFoaf'), "Unexpected ?mbox binding :\n %s" % ppd)
def testScoping(self): from rdfextras.sparql.parser import parse from rdfextras.sparql.query import SPARQLQueryResult p = parse(scopingQuery) global prolog prolog = p.prolog if prolog is None: prolog = Prolog(u'',[]) prolog.DEBUG = True rt = TopEvaluate(p,self.unionGraph,passedBindings = {},DEBUG=False) rt = SPARQLQueryResult(rt).serialize()#format='python') # print("Len rt", len(rt)) self.failUnless(len(rt) == 1,"Expected 1 item solution set") for ppd in rt: self.failUnless(ppd == URIRef('http://example.org/foaf/aliceFoaf'), "Unexpected ?mbox binding :\n %s" % ppd)
def test(self,debug=debug): print testName, label query = urlopen(queryFile).read() factGraph = Graph().parse(urlopen(rdfDoc),format='n3') factGraph.parse(open('SPARQL/W3C/rdfs-axiomatic-triples.n3'),format='n3') self.rules.extend(self.network.setupDescriptionLogicProgramming( factGraph, addPDSemantics=True, constructNetwork=False)) if debug: pprint(list(self.rules)) topDownStore=TopDownSPARQLEntailingStore( factGraph.store, factGraph, idb=self.rules, DEBUG=debug, nsBindings=nsMap, decisionProcedure = BFP_METHOD, identifyHybridPredicates = True, templateMap={ STRING.contains : "REGEX(%s,%s)" }) targetGraph = Graph(topDownStore) parsedQuery=parse(query) for pref,nsUri in (setdict(nsMap) | setdict( parsedQuery.prolog.prefixBindings)).items(): targetGraph.bind(pref,nsUri) # rt=targetGraph.query('',parsedQuery=parsedQuery) actualSolns=[ImmutableDict([(k,v) for k,v in d.items()]) for d in parseResults( targetGraph.query(query).serialize( format='xml'))] expectedSolns=[ImmutableDict([(k,v) for k,v in d.items()]) for d in parseResults(urlopen(result).read())] actualSolns.sort(key=lambda d:hash(d)) expectedSolns.sort(key=lambda d:hash(d)) self.failUnless(set(actualSolns) == set(expectedSolns), "Answers don't match %s v.s. %s"%(actualSolns, expectedSolns) ) if debug: for network,goal in topDownStore.queryNetworks: pprint(goal) network.reportConflictSet(True)
def testGraphEvaluation(self): from rdflib import Literal from rdfextras.sparql.parser import parse p = parse(TEST10) # print TEST10 rt = TopEvaluate(p, self.unionGraph, passedBindings={}) from rdfextras.sparql.query import SPARQLQueryResult rt = SPARQLQueryResult(rt).serialize() #format='python') # print("Len rt", len(rt)) self.failUnless(len(rt) == 1, "Expected 1 item solution set") for mbox, nick, ppd in rt: self.failUnless(mbox == URIRef('mailto:[email protected]'), "Unexpected ?mbox binding :\n %s" % mbox) self.failUnless(nick == Literal("Robert"), "Unexpected ?nick binding :\n %s" % nick) self.failUnless(ppd == URIRef('http://example.org/foaf/bobFoaf'), "Unexpected ?ppd binding :\n %s" % ppd)
def testGraphEvaluation(self): from rdflib import Literal from rdfextras.sparql.parser import parse p = parse(TEST10) # print TEST10 rt = TopEvaluate(p,self.unionGraph,passedBindings = {}) from rdfextras.sparql.query import SPARQLQueryResult rt = SPARQLQueryResult(rt).serialize()#format='python') # print("Len rt", len(rt)) self.failUnless(len(rt) == 1,"Expected 1 item solution set") for mbox,nick,ppd in rt: self.failUnless(mbox == URIRef('mailto:[email protected]'), "Unexpected ?mbox binding :\n %s" % mbox) self.failUnless(nick == Literal("Robert"), "Unexpected ?nick binding :\n %s" % nick) self.failUnless(ppd == URIRef('http://example.org/foaf/bobFoaf'), "Unexpected ?ppd binding :\n %s" % ppd)
def isaBaseQuery(self, queryString, queryObj=None): """ If the given SPARQL query involves purely base predicates it returns it (as a parsed string), otherwise it returns a SPARQL algebra instance for top-down evaluation using this store >>> graph = Graph() # doctest: +SKIP >>> topDownStore = TopDownSPARQLEntailingStore( # doctest: +SKIP ... graph.store, [RDFS.seeAlso], nsBindings={ # doctest: +SKIP ... %(u)s'rdfs':RDFS}) # doctest: +SKIP >>> rt=topDownStore.isaBaseQuery( # doctest: +SKIP ... "SELECT * { [] rdfs:seeAlso [] }") # doctest: +SKIP >>> isinstance(rt, # doctest: +SKIP ... (BasicGraphPattern, AlgebraExpression)) # doctest: +SKIP True >>> rt=topDownStore.isaBaseQuery( # doctest: +SKIP ... "SELECT * { [] a [] }") # doctest: +SKIP >>> isinstance(rt,(Query,basestring)) # doctest: +SKIP True >>> rt=topDownStore.isaBaseQuery( # doctest: +SKIP ... "SELECT * {[] a [] OPTIONAL {[] rdfs:seeAlso []}}") # doctest: +SKIP >>> isinstance(rt,(BasicGraphPattern,AlgebraExpression)) # doctest: +SKIP True """ from rdfextras.sparql.components import Prolog from rdfextras.sparql.parser import parse from rdfextras import sparql as sparqlModule if queryObj: query = queryObj else: query = parse(queryString) if not query.prolog: query.prolog = Prolog(None, []) query.prolog.prefixBindings.update(self.nsBindings) else: for prefix, nsInst in list(self.nsBindings.items()): if prefix not in query.prolog.prefixBindings: query.prolog.prefixBindings[prefix] = nsInst sparqlModule.prolog = query.prolog algebra = RenderSPARQLAlgebra(query, nsMappings=self.nsBindings) return first(self.getDerivedPredicates( algebra, sparqlModule.prolog)) and algebra or query
from rdfextras.sparql.parser import parse # second query from here: # http://www.w3.org/TR/rdf-sparql-query/#GroupPatterns query = """ PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { { ?x foaf:name ?name . } { ?x foaf:mbox ?mbox . } } """ correct = """{ [<SPARQLParser.GraphPattern: [[?x [foaf:name([u'?name'])], ?x [foaf:mbox([u'?mbox'])]]]>] }""" if __name__ == "__main__": p = parse(query) tmp = p.query.whereClause.parsedGraphPattern if str(tmp) == correct: print "PASSED"
def generictest(testFile): func_name = __name__ = __doc__ = id = 'test_sparql.' + \ os.path.splitext(testFile)[0][8:].translate( maketrans('-/','__')) store = plugin.get(STORE,Store)() bootStrapStore(store) store.commit() prefix = testFile.split('.rq')[-1] manifestPath = '/'.join(testFile.split('/')[:-1]+['manifest.n3']) manifestPath2 = '/'.join(testFile.split('/')[:-1]+['manifest.ttl']) queryFileName = testFile.split('/')[-1] store = plugin.get(STORE,Store)() store.open(configString,create=False) assert len(store) == 0 manifestG=ConjunctiveGraph(store) if not os.path.exists(manifestPath): assert os.path.exists(manifestPath2) manifestPath = manifestPath2 manifestG.default_context.parse(open(manifestPath), publicID=URIRef(TEST_BASE), format='n3') manifestData = manifestG.query( MANIFEST_QUERY, processor='sparql', initBindings={'query' : TEST_BASE[queryFileName]}, initNs=manifestNS, DEBUG = False) store.rollback() store.close() for source,testCaseName,testCaseComment,expectedRT in manifestData: if expectedRT: expectedRT = '/'.join(testFile.split('/')[:-1] + \ [expectedRT.replace(TEST_BASE,'')]) if source: source = '/'.join(testFile.split('/')[:-1] + \ [source.replace(TEST_BASE,'')]) testCaseName = testCaseComment and testCaseComment or testCaseName # log.debug("## Source: %s ##"%source) # log.debug("## Test: %s ##"%testCaseName) # log.debug("## Result: %s ##"%expectedRT) #Expected results if expectedRT: store = plugin.get(STORE,Store)() store.open(configString,create=False) resultG=ConjunctiveGraph(store).default_context log.debug("###"*10) log.debug("parsing: %s" % open(expectedRT).read()) log.debug("###"*10) assert len(store) == 0 # log.debug("## Parsing (%s) ##"%(expectedRT)) if not trialAndErrorRTParse(resultG,expectedRT,DEBUG): log.debug( "Unexpected result format (for %s), skipping" % \ (expectedRT)) store.rollback() store.close() continue log.debug("## Done .. ##") rtVars = [rtVar for rtVar in resultG.objects(None,RESULT_NS.resultVariable)] bindings = [] resultSetNode = resultG.value(predicate=RESULT_NS.value, object=RESULT_NS.ResultSet) for solutionNode in resultG.objects(resultSetNode, RESULT_NS.solution): bindingDict = dict([(key,None) for key in rtVars]) for bindingNode in resultG.objects(solutionNode, RESULT_NS.binding): value = resultG.value(subject=bindingNode, predicate=RESULT_NS.value) name = resultG.value(subject=bindingNode, predicate=RESULT_NS.variable) bindingDict[name] = value rbinds = [bindingDict[vName] for vName in rtVars] # print("Rbinds", rbinds) if len(rbinds) > 1 and ( isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(frozenset(rbinds)) elif len(rbinds) == 1 and ( isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(rbinds[0]) else: bindings.append(rbinds) # bindings.append(tuple([bindingDict[vName] for vName in rtVars])) log.debug(open(expectedRT).read()) store.rollback() store.close() if testFile in tests2Skip.keys(): log.debug("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) raise SkipTest("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) query = open(testFile).read() log.debug("### %s (%s) ###" % (testCaseName,testFile)) log.debug(query) p = parse(query)#,DEBUG_PARSE) log.debug(p) if EVALUATE and source: log.debug("### Source Graph: ###") log.debug(open(source).read()) store = plugin.get(STORE,Store)() store.open(configString,create=False) g = ConjunctiveGraph(store) try: g.parse(open(source),format='n3') except: log.debug("Unexpected data format (for %s), skipping" % \ (source)) store.rollback() store.close() continue rt = g.query(query, processor='sparql', DEBUG = False) if expectedRT: try: result = rt.result except AttributeError: result = rt if isinstance(result, Graph): resgraph = open(graphtests[testFile]).read() store = plugin.get(STORE,Store)() store.open(configString,create=False) g = ConjunctiveGraph(store) g.parse(data=resgraph,format="n3") assert result == g, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (g.serialize(format="n3"), result.serialize(format="n3")) else: # result = [r[0] for r in result if isinstance(r, (tuple, list))] def stab(r): if isinstance(r, (tuple, list)): return frozenset(r) else: return r results = set( [stab(r) for r in result]) assert set(bindings).difference(results) == set([]) or set(bindings) == results, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (set(bindings), results) log.debug("### Test Passed: ###") store.rollback()
FOAF = Namespace("http://xmlns.com/foaf/0.1/") MANIFEST_QUERY = \ """ SELECT ?source ?testName ?testComment ?result WHERE { ?testCase mf:action ?testAction; mf:name ?testName; mf:result ?result. ?testAction qt:query ?query; qt:data ?source. OPTIONAL { ?testCase rdfs:comment ?testComment } }""" PARSED_MANIFEST_QUERY = parse(MANIFEST_QUERY) def bootStrapStore(store): rt = store.open(configString,create=False) if rt == NO_STORE: store.open(configString,create=True) else: store.destroy(configString) store.open(configString,create=True) def trialAndErrorRTParse(graph,queryLoc,DEBUG): qstr = StringIO(open(queryLoc).read()) try: graph.parse(qstr,format='n3') return True except Exception, e:
def generictest(testFile): func_name = __name__ = __doc__ = id = 'test_sparql.' + \ os.path.splitext(testFile)[0][8:].translate( maketrans('-/','__')) store = plugin.get(STORE, Store)() bootStrapStore(store) store.commit() prefix = testFile.split('.rq')[-1] manifestPath = '/'.join(testFile.split('/')[:-1] + ['manifest.n3']) manifestPath2 = '/'.join(testFile.split('/')[:-1] + ['manifest.ttl']) queryFileName = testFile.split('/')[-1] store = plugin.get(STORE, Store)() store.open(configString, create=False) assert len(store) == 0 manifestG = ConjunctiveGraph(store) if not os.path.exists(manifestPath): assert os.path.exists(manifestPath2) manifestPath = manifestPath2 manifestG.default_context.parse(open(manifestPath), publicID=TEST_BASE, format='n3') manifestData = manifestG.query( MANIFEST_QUERY, processor='sparql', initBindings={'query': TEST_BASE[queryFileName]}, initNs=manifestNS, DEBUG=False) store.rollback() store.close() for source, testCaseName, testCaseComment, expectedRT in manifestData: if expectedRT: expectedRT = '/'.join(testFile.split('/')[:-1] + \ [expectedRT.replace(TEST_BASE,'')]) if source: source = '/'.join(testFile.split('/')[:-1] + \ [source.replace(TEST_BASE,'')]) testCaseName = testCaseComment and testCaseComment or testCaseName # log.debug("## Source: %s ##"%source) # log.debug("## Test: %s ##"%testCaseName) # log.debug("## Result: %s ##"%expectedRT) #Expected results if expectedRT: store = plugin.get(STORE, Store)() store.open(configString, create=False) resultG = ConjunctiveGraph(store).default_context log.debug("###" * 10) log.debug("parsing: %s" % open(expectedRT).read()) log.debug("###" * 10) assert len(store) == 0 # log.debug("## Parsing (%s) ##"%(expectedRT)) if not trialAndErrorRTParse(resultG, expectedRT, DEBUG): log.debug( "Unexpected result format (for %s), skipping" % \ (expectedRT)) store.rollback() store.close() continue log.debug("## Done .. ##") rtVars = [ rtVar for rtVar in resultG.objects(None, RESULT_NS.resultVariable) ] bindings = [] resultSetNode = resultG.value(predicate=RESULT_NS.value, object=RESULT_NS.ResultSet) for solutionNode in resultG.objects(resultSetNode, RESULT_NS.solution): bindingDict = dict([(key, None) for key in rtVars]) for bindingNode in resultG.objects(solutionNode, RESULT_NS.binding): value = resultG.value(subject=bindingNode, predicate=RESULT_NS.value) name = resultG.value(subject=bindingNode, predicate=RESULT_NS.variable) bindingDict[name] = value rbinds = [bindingDict[vName] for vName in rtVars] # print("Rbinds", rbinds) if len(rbinds) > 1 and (isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(frozenset(rbinds)) elif len(rbinds) == 1 and (isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(rbinds[0]) else: bindings.append(rbinds) # bindings.append(tuple([bindingDict[vName] for vName in rtVars])) log.debug(open(expectedRT).read()) store.rollback() store.close() if testFile in tests2Skip.keys(): log.debug("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) raise SkipTest("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) query = open(testFile).read() log.debug("### %s (%s) ###" % (testCaseName, testFile)) log.debug(query) p = parse(query) #,DEBUG_PARSE) log.debug(p) if EVALUATE and source: log.debug("### Source Graph: ###") log.debug(open(source).read()) store = plugin.get(STORE, Store)() store.open(configString, create=False) g = ConjunctiveGraph(store) try: g.parse(open(source), format='n3') except: log.debug("Unexpected data format (for %s), skipping" % \ (source)) store.rollback() store.close() continue rt = g.query(query, processor='sparql', DEBUG=False) if expectedRT: try: result = rt.result except AttributeError: result = rt if isinstance(result, Graph): resgraph = open(graphtests[testFile]).read() store = plugin.get(STORE, Store)() store.open(configString, create=False) g = ConjunctiveGraph(store) g.parse(data=resgraph, format="n3") assert result == g, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (g.serialize(format="n3"), result.serialize(format="n3")) else: # result = [r[0] for r in result if isinstance(r, (tuple, list))] def stab(r): if isinstance(r, (tuple, list)): return frozenset(r) else: return r results = set([stab(r) for r in result]) assert set(bindings).difference(results) == set([]) or set(bindings) == results, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (set(bindings), results) log.debug("### Test Passed: ###") store.rollback()
u'math': URIRef("http://www.w3.org/2000/10/swap/math#"), } MANIFEST_QUERY = u"""\ SELECT ?status ?premise ?conclusion ?feature ?descr WHERE { [ a otest:PositiveEntailmentTest; otest:feature ?feature; rtest:description ?descr; rtest:status ?status; rtest:premiseDocument ?premise; rtest:conclusionDocument ?conclusion ] }""" PARSED_MANIFEST_QUERY = parse(MANIFEST_QUERY) Features2Skip = [ URIRef('http://www.w3.org/2002/07/owl#sameClassAs'), ] NonNaiveSkip = [ 'OWL/oneOf/Manifest002.rdf', # see Issue 25 'OWL/unionOf/Manifest002.rdf', # support for disjunctive horn logic ] MagicTest2Skip = [ 'OWL/oneOf/Manifest002.rdf', # needs 2nd order predicate derivation 'OWL/oneOf/Manifest003.rdf', # needs 2nd order predicate derivation 'OWL/disjointWith/Manifest001.rdf' # needs 2nd order predicate derivation ]