Theory
A property path lets one triple pattern match a route through the graph instead of a single edge. This is SPARQL's answer to recursive SQL CTEs — expressed in a few characters.
| Path | Meaning |
|---|---|
:p/:q | sequence: :p then :q |
:p|:q | alternative: :p or :q |
^:p | inverse: follow :p backwards |
:p* | zero-or-more :p (reflexive transitive closure) |
:p+ | one-or-more :p (transitive closure) |
:p? | zero-or-one :p |
!:p | any predicate except :p |
# Everyone reachable up an ancestor chain of any length:
SELECT ?descendant WHERE {
?descendant :ancestorOf+ :Splinter .
}
Where paths shine, and where they bite
- Reachability for free.
:ancestorOf+,rdfs:subClassOf*,:partOf+answer 'is X reachable from Y?' without materialised transitive triples or a reasoner. */+find existence, not the path itself. A path query tells you that a route exists, not which nodes it passed through. If you need the intermediate steps, you need a different approach (enumerated hops or a graph engine).- Unbounded
*/+over a large graph can be expensive. Anchor at least one end with a constant (as above) so the engine doesn't explore the whole graph.