Skip to content

custom_eval

This example shows how a custom evaluation function can be added to handle certain SPARQL Algebra elements.

A custom function is added that adds rdfs:subClassOf “inference” when asking for rdf:type triples.

Here the custom eval function is added manually, normally you would use setuptools and entry_points to do it: i.e. in your setup.py::

entry_points = {
    'rdf.plugins.sparqleval': [
        'myfunc = mypackage:MyFunction',
        ],
}

Functions:

  • customEval

    Rewrite triple patterns to get super-classes

Attributes:

EXAMPLES_DIR module-attribute

EXAMPLES_DIR = parent

g module-attribute

g = Graph()

inferred_sub_class module-attribute

inferred_sub_class = subClassOf * '*'

customEval

customEval(ctx, part)

Rewrite triple patterns to get super-classes

Source code in examples/custom_eval.py
def customEval(ctx, part):  # noqa: N802
    """
    Rewrite triple patterns to get super-classes
    """

    if part.name == "BGP":
        # rewrite triples
        triples = []
        for t in part.triples:
            if t[1] == RDF.type:
                bnode = rdflib.BNode()
                triples.append((t[0], t[1], bnode))
                triples.append((bnode, inferred_sub_class, t[2]))
            else:
                triples.append(t)

        # delegate to normal evalBGP
        return evalBGP(ctx, triples)

    raise NotImplementedError()