diff options
Diffstat (limited to 'examples/custom_eval.py')
-rw-r--r-- | examples/custom_eval.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/examples/custom_eval.py b/examples/custom_eval.py index 36998087..f8dfd390 100644 --- a/examples/custom_eval.py +++ b/examples/custom_eval.py @@ -16,12 +16,19 @@ i.e. in your setup.py:: } """ -import rdflib -from rdflib.plugins.sparql.evaluate import evalBGP +from pathlib import Path + +import rdflib from rdflib.namespace import FOAF, RDF, RDFS +from rdflib.plugins.sparql.evaluate import evalBGP -inferredSubClass = RDFS.subClassOf * "*" # any number of rdfs.subClassOf +EXAMPLES_DIR = Path(__file__).parent + + +inferred_sub_class = ( + RDFS.subClassOf * "*" # type: ignore[operator] +) # any number of rdfs.subClassOf def customEval(ctx, part): @@ -36,7 +43,7 @@ def customEval(ctx, part): if t[1] == RDF.type: bnode = rdflib.BNode() triples.append((t[0], t[1], bnode)) - triples.append((bnode, inferredSubClass, t[2])) + triples.append((bnode, inferred_sub_class, t[2])) else: triples.append(t) @@ -47,12 +54,11 @@ def customEval(ctx, part): 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("foaf.n3") + 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)) @@ -60,11 +66,11 @@ if __name__ == "__main__": # Find all FOAF Agents for x in g.query( f""" - PREFIX foaf: <{FOAF}> - - SELECT * + PREFIX foaf: <{FOAF}> + + SELECT * WHERE {{ - ?s a foaf:Agent . + ?s a foaf:Agent . }} """ ): |