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` –
- `g` –
- `inferred_sub_class` –

## `` 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`

    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54

    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()

source: false show_docstring_attributes: false show_docstring_functions: false show_docstring_modules: false show_docstring_classes: false show_signature: false show_signature_annotations: false show_signature_type_parameters: false show_docstring_other_parameters: false show_docstring_parameters: false show_docstring_raises: false show_docstring_receives: false show_docstring_returns: false summary: false show_if_no_docstring: false

    from pathlib import Path

    import rdflib
    from rdflib.namespace import FOAF, RDF, RDFS
    from rdflib.plugins.sparql.evaluate import evalBGP

    EXAMPLES_DIR = Path(__file__).parent

    inferred_sub_class = (
        RDFS.subClassOf * "*"  # type: ignore[operator]
    )  # any number of rdfs.subClassOf

    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()

    if __name__ == "__main__":
        # add function directly, normally we would use setuptools and entry_points
        rdflib.plugins.sparql.CUSTOM_EVALS["exampleEval"] = customEval

        g = rdflib.Graph()
        g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}")

        # Add the subClassStmt so that we can query for it!
        g.add((FOAF.Person, RDFS.subClassOf, FOAF.Agent))

        # Find all FOAF Agents
        for x in g.query(
            f"""
            PREFIX foaf: <{FOAF}>

            SELECT *
            WHERE {{
                ?s a foaf:Agent .
            }}
            """
        ):
            print(x)