What Are Property Paths?
In SPARQL, property paths let you express chains or patterns of relationships between resources, without manually specifying each join.
Example idea:
Movie → Director → foaf:name
Instead of multiple triple lines, a single property path can express it:
?movie ex:director/foaf:name ?directorName
It’s like saying: “Start from a movie, follow the ex:director
edge, then the foaf:name
edge.”
Supported Property Path Syntax in AGS
AGS supports most of the W3C SPARQL 1.1 Property Path syntax.
Syntax Form | Description | Supported in AGS |
---|
uri
| A direct predicate (single hop). | ✅ |
^elt
| Inverse path (object to subject). | ✅ |
(elt)
| Group path (brackets control precedence). | ✅ |
elt1 / elt2
| Sequence — follow elt1 then elt2 . | ✅ |
`elt1 | elt2` | Alternative — follow either elt1 or elt2 . |
elt*
| Zero or more occurrences (Kleene star). | ✅ |
elt+
| One or more occurrences. | ✅ |
elt?
| Zero or one occurrence. | ✅ |
elt{n,m}
| Between n and m occurrences. | ❌ |
elt{n}
| Exactly n occurrences. | ❌ |
elt{n,}
| n or more occurrences. | ❌ |
elt{,n}
| Between 0 and n occurrences. | ❌ |
Example 1 - Simple Path Traversal
Goal: Get each movie’s director’s name in a single line.
Instead of two triple patterns, use /
to chain relationships.
PREFIX ex: <http://example.org/movie/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?title ?directorName
WHERE {
?m a ex:Movie ;
ex:title ?title ;
ex:director/foaf:name ?directorName .
}
ORDER BY ?title
Output:
Title | Director |
---|
The Shawshank Redemption | Frank Darabont |
The Godfather | Francis Ford Coppola |
Inception | Christopher Nolan |
The Dark Knight | Christopher Nolan |
Pulp Fiction | Quentin Tarantino |
✅ This is the most common and efficient use of property paths in AGS.
Example 2 - Inverse Paths
Goal: Find all directors and which movies they directed (reverse traversal).
Use the ^
operator to go from object → subject.
PREFIX ex: <http://example.org/movie/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?directorName ?title
WHERE {
?m ex:director ?d .
?d foaf:name ?directorName .
?d ^ex:director ?m .
?m ex:title ?title .
}
ORDER BY ?directorName
Alternatively (simpler form):
PREFIX ex: <http://example.org/movie/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?directorName ?title
WHERE {
?d foaf:name ?directorName .
?m ^ex:director ?d ;
ex:title ?title .
}
Output:
Director | Movie |
---|
Christopher Nolan | The Dark Knight |
Christopher Nolan | Inception |
Francis Ford Coppola | The Godfather |
Frank Darabont | The Shawshank Redemption |
Quentin Tarantino | Pulp Fiction |
Example 3 - Alternative Paths
Goal: Get all people (directors OR actors) associated with movies.
Use |
to try multiple property alternatives.
PREFIX ex: <http://example.org/movie/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?movieTitle ?personName
WHERE {
?m a ex:Movie ;
ex:title ?movieTitle ;
(ex:director|ex:hasActor)/foaf:name ?personName .
}
ORDER BY ?movieTitle
Output:
Movie | Person |
---|
The Shawshank Redemption | Frank Darabont |
The Shawshank Redemption | Tim Robbins |
The Godfather | Francis Ford Coppola |
The Godfather | Marlon Brando |
The Dark Knight | Christopher Nolan |
The Dark Knight | Christian Bale |
Inception | Christopher Nolan |
Inception | Leonardo DiCaprio |
Pulp Fiction | Quentin Tarantino |
Pulp Fiction | John Travolta |
Example 4 - Recursive Paths (One or More Steps)
Goal: Show all people connected to Christopher Nolan through any number of actor/director hops.
Use the +
operator for one or more relationships.
PREFIX ex: <http://example.org/movie/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?connectedName
WHERE {
ex:Christopher_Nolan ( ^ex:director | ^ex:hasActor | ex:director | ex:hasActor )+ ?person .
?person foaf:name ?connectedName .
}
Output:
Connected Name |
---|
Christopher Nolan |
Christian Bale |
Leonardo DiCaprio |
Why it’s useful:
You can explore connected entities recursively — e.g., co-actors, collaborators, or hierarchical relationships — without predefining path length.
Caution: Recursive paths (*
or +
) can be expensive on large graphs — always test with LIMIT
.
Example 5 - Optional Path
Goal: Return all movies and their director names, but still include movies missing a director relationship.
Use the ?
operator (zero or one occurrence).
PREFIX ex: <http://example.org/movie/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?title ?directorName
WHERE {
?m a ex:Movie ;
ex:title ?title ;
(ex:director/foaf:name)? ?directorName .
}
Output:
Same as Example 1 - but if any movie lacked a director triple, it would still appear with a blank ?directorName
Unsupported Property Path Forms in AGS
These forms from the W3C draft are not implemented in AGS:
Syntax | Meaning | Status |
---|
elt{n,m}
| Between n and m hops | ❌ Not supported |
elt{n}
| Exactly n hops | ❌ Not supported |
elt{n,}
| n or more hops | ❌ Not supported |
elt{,n}
| 0 to n hops | ❌ Not supported |
These fixed/variable repetition paths are rarely used in practical graph queries and were never finalized in the W3C SPARQL 1.1 draft (which remains unpublished).
AGS follows the stable subset of property path syntax.
Developer Best Practices
Best Practice | Why It Helps |
---|
Limit recursion depth | Prevent runaway traversal on large graphs |
Use VALUES or filters to narrow subjects before applying paths | Reduces work |
Combine WITH and property paths | Precompute complex paths for reuse |
Prefer / and ` | ` patterns for most traversals |
Avoid * unless necessary | Can blow up results exponentially |
Test with small datasets first | Debug easier, verify logic |