paths
This module implements the SPARQL 1.1 Property path operators, as defined in: http://www.w3.org/TR/sparql11-query/#propertypaths
In SPARQL the syntax is as follows:
| Syntax | Matches |
|---|---|
iri |
An IRI. A path of length one. |
^elt |
Inverse path (object to subject). |
elt1 / elt2 |
A sequence path of elt1 followed by elt2. |
elt1 \| elt2 |
An alternative path of elt1 or elt2 (all possibilities are tried). |
elt* |
A path that connects subject and object by zero or more matches of elt. |
elt+ |
A path that connects subject and object by one or more matches of elt. |
elt? |
A path that connects subject and object by zero or one matches of elt. |
!iri or !(iri1 \| ... \| irin) |
Negated property set. An IRI not among iri1 to irin. !iri is short for !(iri). |
!^iri or !(^iri1 \| ... \| ^irin) |
Negated reverse property set. Excludes ^iri1 to ^irin as reverse paths. !^iri is short for !(^iri). |
!(iri1 \| ... \| irij \| ^irij+1 \| ... \| ^irin) |
A combination of forward and reverse properties in a negated property set. |
(elt) |
A grouped path elt, where parentheses control precedence. |
This module is used internally by the SPARQL engine, but the property paths can also be used to query RDFLib Graphs directly.
Where possible the SPARQL syntax is mapped to Python operators, and property path objects can be constructed from existing URIRefs.
>>> from rdflib import Graph, Namespace
>>> from rdflib.namespace import FOAF
>>> ~FOAF.knows
Path(~http://xmlns.com/foaf/0.1/knows)
>>> FOAF.knows/FOAF.name
Path(http://xmlns.com/foaf/0.1/knows / http://xmlns.com/foaf/0.1/name)
>>> FOAF.name|FOAF.givenName
Path(http://xmlns.com/foaf/0.1/name | http://xmlns.com/foaf/0.1/givenName)
Modifiers (?, *, +) are done using * (the multiplication operator) and the strings ‘*‘, ‘?’, ‘+’, also defined as constants in this file.
The path objects can also be used with the normal graph methods.
First some example data:
>>> g=Graph()
>>> g=g.parse(data='''
... @prefix : <ex:> .
...
... :a :p1 :c ; :p2 :f .
... :c :p2 :e ; :p3 :g .
... :g :p3 :h ; :p2 :j .
... :h :p3 :a ; :p2 :g .
...
... :q :px :q .
...
... ''', format='n3') # doctest: +ELLIPSIS
>>> e = Namespace('ex:')
Graph contains:
Graph generator functions, triples, subjects, objects, etc. :
>>> list(g.objects(e.c, (e.p3*OneOrMore)/e.p2)) # doctest: +NORMALIZE_WHITESPACE
[rdflib.term.URIRef('ex:j'), rdflib.term.URIRef('ex:g'),
rdflib.term.URIRef('ex:f')]
A more complete set of tests:
>>> list(eval_path(g, (None, e.p1/e.p2, None)))==[(e.a, e.e)]
True
>>> list(eval_path(g, (e.a, e.p1|e.p2, None)))==[(e.a,e.c), (e.a,e.f)]
True
>>> list(eval_path(g, (e.c, ~e.p1, None))) == [ (e.c, e.a) ]
True
>>> list(eval_path(g, (e.a, e.p1*ZeroOrOne, None))) == [(e.a, e.a), (e.a, e.c)]
True
>>> list(eval_path(g, (e.c, e.p3*OneOrMore, None))) == [
... (e.c, e.g), (e.c, e.h), (e.c, e.a)]
True
>>> list(eval_path(g, (e.c, e.p3*ZeroOrMore, None))) == [(e.c, e.c),
... (e.c, e.g), (e.c, e.h), (e.c, e.a)]
True
>>> list(eval_path(g, (e.a, -e.p1, None))) == [(e.a, e.f)]
True
>>> list(eval_path(g, (e.a, -(e.p1|e.p2), None))) == []
True
>>> list(eval_path(g, (e.g, -~e.p2, None))) == [(e.g, e.j)]
True
>>> list(eval_path(g, (e.e, ~(e.p1/e.p2), None))) == [(e.e, e.a)]
True
>>> list(eval_path(g, (e.a, e.p1/e.p3/e.p3, None))) == [(e.a, e.h)]
True
>>> list(eval_path(g, (e.q, e.px*OneOrMore, None)))
[(rdflib.term.URIRef('ex:q'), rdflib.term.URIRef('ex:q'))]
>>> list(eval_path(g, (None, e.p1|e.p2, e.c)))
[(rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:c'))]
>>> list(eval_path(g, (None, ~e.p1, e.a))) == [ (e.c, e.a) ]
True
>>> list(eval_path(g, (None, e.p1*ZeroOrOne, e.c))) # doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:c')),
(rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:c'))]
>>> list(eval_path(g, (None, e.p3*OneOrMore, e.a))) # doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a')),
(rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a'))]
>>> list(eval_path(g, (None, e.p3*ZeroOrMore, e.a))) # doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:a')),
(rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a')),
(rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a'))]
>>> list(eval_path(g, (None, -e.p1, e.f))) == [(e.a, e.f)]
True
>>> list(eval_path(g, (None, -(e.p1|e.p2), e.c))) == []
True
>>> list(eval_path(g, (None, -~e.p2, e.j))) == [(e.g, e.j)]
True
>>> list(eval_path(g, (None, ~(e.p1/e.p2), e.a))) == [(e.e, e.a)]
True
>>> list(eval_path(g, (None, e.p1/e.p3/e.p3, e.h))) == [(e.a, e.h)]
True
>>> list(eval_path(g, (e.q, e.px*OneOrMore, None)))
[(rdflib.term.URIRef('ex:q'), rdflib.term.URIRef('ex:q'))]
>>> list(eval_path(g, (e.c, (e.p2|e.p3)*ZeroOrMore, e.j)))
[(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:j'))]
No vars specified:
>>> sorted(list(eval_path(g, (None, e.p3*OneOrMore, None)))) #doctest: +NORMALIZE_WHITESPACE
[(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a')),
(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:g')),
(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:h')),
(rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
(rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:h')),
(rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a'))]
Classes:
-
AlternativePath– -
InvPath– -
MulPath– -
NegatedPath– -
Path–Base class for all property paths.
-
PathList– -
SequencePath–
Functions:
-
evalPath– -
eval_path– -
inv_path–inverse path
-
mul_path–cardinality path
-
neg_path–negated path
-
path_alternative–alternative path
-
path_sequence–sequence path
Attributes:
-
OneOrMore– -
ZeroOrMore– -
ZeroOrOne–
AlternativePath
Bases: Path
Methods:
Attributes:
Source code in rdflib/paths.py
__repr__
eval
eval(graph: Graph, subj: Optional[_SubjectType] = None, obj: Optional[_ObjectType] = None) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]
Source code in rdflib/paths.py
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
InvPath
Bases: Path
Methods:
Attributes:
-
arg–
__repr__
eval
eval(graph: Graph, subj: Optional[_SubjectType] = None, obj: Optional[_ObjectType] = None) -> Generator[Tuple[_ObjectType, _SubjectType], None, None]
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
MulPath
Bases: Path
Methods:
Attributes:
Source code in rdflib/paths.py
__repr__
eval
eval(graph: Graph, subj: Optional[_SubjectType] = None, obj: Optional[_ObjectType] = None, first: bool = True) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]
Source code in rdflib/paths.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
NegatedPath
NegatedPath(arg: Union[AlternativePath, InvPath, URIRef])
Bases: Path
Methods:
Attributes:
Source code in rdflib/paths.py
__repr__
eval
Source code in rdflib/paths.py
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
Path
Bases: ABC
Base class for all property paths.
Methods:
Attributes:
-
__invert__(Callable[[Path], InvPath]) – -
__mul__(Callable[[Path, str], MulPath]) – -
__neg__(Callable[[Path], NegatedPath]) – -
__or__(Callable[[Path, Union[URIRef, Path]], AlternativePath]) – -
__truediv__(Callable[[Path, Union[URIRef, Path]], SequencePath]) –
__eq__
__hash__
__lt__
eval
abstractmethod
eval(graph: Graph, subj: Optional[_SubjectType] = None, obj: Optional[_ObjectType] = None) -> Iterator[Tuple[_SubjectType, _ObjectType]]
n3
abstractmethod
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
PathList
Bases: list
SequencePath
Bases: Path
Methods:
Attributes:
Source code in rdflib/paths.py
__repr__
eval
eval(graph: Graph, subj: Optional[_SubjectType] = None, obj: Optional[_ObjectType] = None) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]
Source code in rdflib/paths.py
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
evalPath
evalPath(graph: Graph, t: Tuple[Optional[_SubjectType], Union[None, Path, _PredicateType], Optional[_ObjectType]]) -> Iterator[Tuple[_SubjectType, _ObjectType]]
Source code in rdflib/paths.py
eval_path
inv_path
mul_path
neg_path
neg_path(p: Union[URIRef, AlternativePath, InvPath]) -> NegatedPath
path_alternative
alternative path
path_sequence
sequence path