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 custom rdflib.Graph.triples(). However, it’s also valid SPARQL to have the VALUES clause outside of the WHERE clause like

SELECT * 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 a VALUES 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 the VALUES clauses appear first, therefore making sure that their bound values are available to the triples function.

Methods Summary

query(query[, initBindings, initNs, base, DEBUG])

Evaluate a SPARQL query on this processor's graph.

Methods Documentation

query(query: str | Query, initBindings=None, initNs=None, base=None, DEBUG=False)[source]

Evaluate a SPARQL query on this processor’s graph.