Property paths — reachability without recursion

Traverse chains, inverses and transitive closures in a single pattern.

0/3 done

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.

PathMeaning
:p/:qsequence: :p then :q
:p|:qalternative: :p or :q
^:pinverse: follow :p backwards
:p*zero-or-more :p (reflexive transitive closure)
:p+one-or-more :p (transitive closure)
:p?zero-or-one :p
!:pany 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.

Analogy

A property path is flight connections, not single flights. :flight matches one leg; :flight+ answers 'can I get from Zürich to anywhere, with any number of layovers?'. You learn whether you can arrive — the path operator doesn't hand you the boarding passes for each leg.

Reading in progress · 0 of 3 activities done