Hi, I have been playing around with filter in SPARQL and found a small bug in sparql-p. A test case is attached.
I looked at sparql.py and found that in SPARLQ.queryObject() the algorithm expands the top-level patterns first and applies the global constraints (in this case BOUND(?date)) on the respective result nodes. After that, the optional patterns are expanded on the bindings from each top-level pattern. But, since here the constraint says that ?date must be bound, the optional patterns are never expanded (self.clash == True for both result nodes). The BOUND constraint should be applied after the OPTIONAL clause. Please let me know if anyone has an idea how to fix this? Cheers, Mikael
from rdflib import ConjunctiveGraph, plugin from rdflib.store import Store from StringIO import StringIO import unittest test_data=""" @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . _:a foaf:givenName "Alice". _:b foaf:givenName "Bob" . _:b dc:date "2005-04-04T04:04:04Z"^^xsd:dateTime . """ class FilterTest(unittest.TestCase): def _query(self, query): graph = ConjunctiveGraph(plugin.get('IOMemory',Store)()) graph.parse(StringIO(test_data), format="n3") return graph.query(query) def testFilterBound(self): test_query = """ PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?name ?date WHERE { ?x foaf:givenName ?name . OPTIONAL { ?x dc:date ?date . } FILTER ( BOUND(?date) ) } # regex(?name, "^ali", "i") """ res = self._query(test_query) # print [r for r in res] self.assertEqual([str(name[0]) for name in res], ["Bob"]) def main(): unittest.main() if __name__ == '__main__': main()
_______________________________________________ Dev mailing list [email protected] http://rdflib.net/mailman/listinfo/dev
