MappingServiceSPARQLProcessor
- class MappingServiceSPARQLProcessor(graph)[source]
Bases:
SPARQLProcessor
A custom SPARQL processor that optimizes the query based on https://github.com/RDFLib/rdflib/pull/2257.
Why is this necessary? Ideally, we get queries like
SELECT * WHERE { VALUES ?s { :a :b ... } ?s owl:sameAs ?o }
This is fine, since the way that RDFLib parses and constructs an abstract syntax tree, the values for
?s
get bound properly when calling a customrdflib.Graph.triples()
. However, it’s also valid SPARQL to have theVALUES
clause outside of theWHERE
clause likeSELECT * WHERE { ?s owl:sameAs ?o } VALUES ?s { :a :b ... }
Unfortunately, this trips up RDFLib since it doesn’t know to bind the values before calling
triples()
, therefore thwarting our custom implementation that dynamically generates triples based on the bound values themselves.This processor, originally by Jerven Bolleman in https://github.com/RDFLib/rdflib/pull/2257, adds some additional logic between parsing + constructing the abstract syntax tree and evaluation of the syntax tree. Basically, the abstract syntax tree has nodes with two or more children. Jerven’s clever code (see
_optimize_node()
below) finds Join nodes that have aVALUES
clause in the second of its two arguments, then flips them around. It does this recursively for the whole tree. This gets us to the goal of having theVALUES
clauses appear first, therefore making sure that their bound values are available to thetriples
function.Methods Summary
query
(query[, initBindings, initNs, base, DEBUG])Evaluate a SPARQL query on this processor's graph.
Methods Documentation