One of the most powerful features of SPARQL is its support for property paths, which allow us to traverse relationships across multiple hops in a graph.
In traditional SQL databases, finding indirect relationships often requires multiple self-joins. In SPARQL, however, a single property path expression can traverse a graph recursively, making it easy to answer questions such as:
· Who knows Krishna directly or indirectly?
· Who can eventually reach Krishna through a chain of friendships?
· Which people are connected through friends of friends?
· How can we navigate hierarchical or network structures without knowing the path length in advance?
In this post, we'll explore recursive paths using a simple social network example.
Consider the following RDF graph:
socialNetwork.turtle
@prefix : <http://example.org/social/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . ################################################# # People ################################################# :Ram rdf:type :Person ; :name "Ram" . :Hari rdf:type :Person ; :name "Hari" . :Krishna rdf:type :Person ; :name "Krishna" . :Rahim rdf:type :Person ; :name "Rahim" . :Priya rdf:type :Person ; :name "Priya" . :John rdf:type :Person ; :name "John" . :Anita rdf:type :Person ; :name "Anita" . :Vikram rdf:type :Person ; :name "Vikram" . :Neha rdf:type :Person ; :name "Neha" . :Suresh rdf:type :Person ; :name "Suresh" . :Meera rdf:type :Person ; :name "Meera" . :David rdf:type :Person ; :name "David" . :Alex rdf:type :Person ; :name "Alex" . ################################################# # Friendship Network ################################################# :Ram :knows :Hari . :Hari :knows :Krishna . :Krishna :knows :Rahim . ################################################# # Branch 1 ################################################# :Priya :knows :Hari . :John :knows :Priya . ################################################# # Branch 2 ################################################# :Anita :knows :Krishna . :Vikram :knows :Anita . :Neha :knows :Vikram . ################################################# # Branch 3 ################################################# :Suresh :knows :Hari . :Meera :knows :Suresh . ################################################# # Another route to Rahim ################################################# :David :knows :Rahim . ################################################# # Small cycle ################################################# :Alex :knows :John . :John :knows :Alex .
1. Finding Direct Connections
Let's start with a simple query. Suppose we want to find people who directly know Rahim.
PREFIX : <http://example.org/social/> SELECT ?personName WHERE { ?person :knows :Rahim ; :name ?personName. }
Output
personName David Krishna
Only Krishna and David have direct :knows relationships to Rahim. However, this does not tell the whole story.
Ram can reach Rahim through Hari and Krishna. Similarly, John can reach Rahim through Priya, Hari, and Krishna. To discover those indirect relationships, we need recursive paths.
2. Property Paths
A property path allows SPARQL to repeatedly follow a predicate. The two most commonly used recursive path operators are summarized in following table.
|
Operator |
Meaning |
|
* |
Zero or more occurrences |
|
+ |
One or more occurrences |
2.1 Zero or More Occurrences (*)
The following query asks "Who can reach Rahim through zero or more :knows relationships?"
PREFIX : <http://example.org/social/> SELECT ?personName WHERE { ?person :knows* :Rahim ; :name ?personName. } ORDER BY ?personName
Output
personName Alex Anita David Hari John Krishna Meera Neha Priya Rahim Ram Suresh Vikram
At first glance, one result may seem surprising.
Why is Rahim included?
The reason is that the * operator means zero or more occurrences. A path of length zero is valid.
Therefore, SPARQL considers: Rahim → Rahim without traversing any edge. This behavior is mathematically correct, but it is not always what users expect.
2.2 One or More Occurrences (+)
Most of the time, we want people who actually know Rahim either directly or indirectly. In that case, we should use the + operator.
PREFIX : <http://example.org/social/> SELECT ?personName WHERE { ?person :knows+ :Rahim ; :name ?personName. } ORDER BY ?personName
Output
personName Alex Anita David Hari John Krishna Meera Neha Priya Ram Suresh Vikram
Notice that Rahim no longer appears in the result set. The + operator requires at least one traversal of the :knows relationship. This is usually the preferred option when searching for friends, connections, dependencies, or lineage relationships.
2.3 Select all the persons that Hari knows
PREFIX : <http://example.org/social/> SELECT DISTINCT ?personName WHERE { ?Hari :knows+ ?person . ?person :name ?personName. } ORDER BY ?personName
Output
personName Alex Anita Hari John Krishna Priya Rahim Suresh Vikram
2.4 Finding Everyone Connected to Hari
PREFIX : <http://example.org/social/> SELECT DISTINCT ?personName WHERE { ?person :knows+ ?Hari . ?person :name ?personName. } ORDER BY ?personName
Output
personName Alex Anita David Hari John Krishna Meera Neha Priya Ram Suresh Vikram
In summary, Property paths are one of the most powerful features of SPARQL. They allow us to navigate graph relationships without knowing the path length in advance.
Remember these two operators:
· *: Zero or more occurrences
· +: One or more occurrences
Use * when zero-length paths should be included. Use + when at least one relationship traversal is required.
Whenever you need to answer questions involving friends-of-friends, organizational hierarchies, dependencies, network connectivity, or data lineage, recursive property paths provide a concise and elegant solution.
Previous Next Home
No comments:
Post a Comment