Skip to content

foafpaths

SPARQL 1.1 defines path operators for combining/repeating predicates in triple-patterns.

We overload some Python operators on URIRefs to allow creating path operators directly in Python.

Operator Path
p1 / p2 Path sequence
p1 | p2 Path alternative
p1 * '*' Chain of 0 or more p’s
p1 * '+' Chain of 1 or more p’s
p1 * '?' 0 or 1 p
~p1 p1 inverted, i.e. (s p1 o)(o ~p1 s)
-p1 NOT p1, i.e. any property but p1

These can then be used in property position for s,p,o triple queries for any graph method.

See the docs for paths for the details.

This example shows how to get the name of friends (i.e values two steps away x knows y, y name z) with a single query.

Attributes:

EXAMPLES_DIR module-attribute

EXAMPLES_DIR = parent

g module-attribute

g = Graph()

tim module-attribute

tim = URIRef('http://www.w3.org/People/Berners-Lee/card#i')

w_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

from rdflib import Graph, URIRef
from rdflib.namespace import FOAF

EXAMPLES_DIR = Path(__file__).parent

if __name__ == "__main__":
    g = Graph()
    g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}")

    tim = URIRef("http://www.w3.org/People/Berners-Lee/card#i")

    print("Timbl knows:")

    for o in g.objects(tim, FOAF.knows / FOAF.name):
        print(o)