In previous examples, we queried RDF data by following relationships in their natural direction. For example, if a person is linked to an actor through the :portrayedBy predicate, we can easily retrieve the actor associated with each character by traversing the path from the person to the actor.
However, SPARQL property paths also allow us to navigate relationships in the opposite direction. This is useful when we want to start from the target node and work backwards without explicitly rewriting the data model.
In this post, we'll explore the inverse path operator (^), which lets us follow a predicate in the reverse direction.
Using our Marvel dataset, we'll retrieve people and the actors who portray them, first using the normal direction and then using the inverse path.
Let's start with a straightforward query that retrieves Marvel characters and the actors who portray them.
PREFIX : <http://example.org/marvel/> SELECT ?personName ?actorName WHERE { ?person :name ?personName ; :portrayedBy ?actor . ?actor :name ?actorName . }
How it works?
Here, we're following the :portrayedBy relationship in its natural direction:
Tony Stark ──portrayedBy──► Robert Downey Jr.
Steve Rogers ──portrayedBy──► Chris Evans
Bruce Banner ──portrayedBy──► Mark Ruffalo
Running the query returns results similar to:
|
personName |
actorName |
|
Steve Rogers |
Chris Evans |
|
Peter Quill |
Chris Pratt |
|
Bruce Banner |
Mark Ruffalo |
|
Tony Stark |
Robert Downey Jr. |
|
Natasha Romanoff |
Scarlett Johansson
|
Following the relationship backwards
Now suppose we want to start from the actor and navigate back to the person they portray.
Instead of traversing:
Person ──portrayedBy──► Actor
we want to traverse:
Actor ◄──portrayedBy── Person
SPARQL provides the inverse path operator (^) for exactly this purpose. We simply place the caret (^) in front of the predicate.
PREFIX : <http://example.org/marvel/> SELECT ?personName ?actorName WHERE { ?actor ^:portrayedBy ?person . ?actor :name ?actorName . ?person :name ?personName . }
Above Query results following output.
|
personName |
actorName |
|
Bruce Banner |
Mark Ruffalo |
|
Natasha Romanoff |
Scarlett Johansson |
|
Peter Quill |
Chris Pratt |
|
Steve Rogers |
Chris Evans |
|
Tony Stark |
Robert Downey Jr. |
Understanding the inverse path
The pattern:
?actor ^:portrayedBy ?person .
means find all resources where ?person :portrayedBy ?actor. In other words, SPARQL automatically reverses the direction of the predicate.
This expression is equivalant to
?person :portrayedBy ?actor .
but can be much more convenient when constructing larger property paths.
Even though we got the similar results, the order is different. Let's apply ORDER BY clause to sort the results.
Query 1:
PREFIX : <http://example.org/marvel/> SELECT ?personName ?actorName WHERE { ?person :name ?personName ; :portrayedBy ?actor . ?actor :name ?actorName . }ORDER BY ?personName
Query 2:
PREFIX : <http://example.org/marvel/> SELECT ?personName ?actorName WHERE { ?actor ^:portrayedBy ?person . ?actor :name ?actorName . ?person :name ?personName . } ORDER BY ?personName
Why use inverse paths?
Inverse paths become especially valuable when:
· You need to navigate relationships from the opposite direction.
· You're building complex property paths involving multiple predicates.
· You don't want to rewrite graph patterns solely to reverse traversal.
· You want a more natural way to express a query from a particular starting point.
For example, if you start with actors and want to discover the characters they portray, the inverse path often reads more naturally than reversing the entire triple pattern.
In summary,
· The inverse path operator (^) allows SPARQL to traverse a predicate in the opposite direction.
· ^:predicate means "follow the :predicate relationship backwards.
· The pattern "?a ^:predicate ?b" is equivalent to "?b :predicate ?a"
· Inverse paths do not change the underlying data, they simply provide an alternative way to navigate the RDF graph.
· They become particularly powerful when combined with more advanced SPARQL property path expressions.
Previous Next Home
No comments:
Post a Comment