Property Path
Also Property Paths
A property path is a pattern over predicates, written in the predicate position of a SPARQL query triple, that matches a whole path of edges from subject to object; `p1/p2` is an edge p1 followed by an edge p2, and `p*` is zero or more edges p.
Properties#
- Sequence = hidden intermediate variable.
?s p1/p2 ?omatches the same as the two triples?s p1 ?x . ?x p2 ?o, except that the intermediate node?xgets no column in the result. *has no fixed-size equivalent. A path of unbounded length can’t be written as a fixed number of triples, so*is needed for transitive relations such as “subclass of”.- Duplicates and
DISTINCT. The lecture’s query yields one result per path. If a city is reachable fromwd:Q515via several paths, it appears several times, henceSELECT DISTINCT. - Typical Wikidata use.
wdt:P31/wdt:P279*(instance of, then any number of subclass-of edges) finds all instances of a class including instances of its subclasses (Wikidata Prefix System). - Property paths also save typing for plain chains, e.g.
?building geo:hasGeometry/geo:asWKT ?wktin the Spatial Join example.
Example#
Toy knowledge graph:
<Freiburg> <instance_of> <University_Town> .
<University_Town> <subclass_of> <Town> .
<Town> <subclass_of> <Settlement> .
<Ulm> <instance_of> <Town> .
SELECT ?x WHERE { ?x <instance_of>/<subclass_of>* <Settlement> . }
Freiburg:instance_oftoUniversity_Town, then 2subclass_ofedges.Ulm:instance_oftoTown, then 1subclass_ofedge.
With <instance_of> <Settlement> alone, the result would be empty.
Lecture query (Wikidata, Slide 17):
SELECT DISTINCT ?city ?name ?population WHERE {
?city wdt:P31/wdt:P279* wd:Q515 . # instance of (a subclass of) city
?city wdt:P17 wd:Q183 . # country: Germany
?city wdt:P1082 ?population .
?city rdfs:label ?name FILTER (LANG(?name) = "de")
}
ORDER BY DESC(?population)
Sources#
- Slides 01, p. 17 (
/,*, DISTINCT), p. 24 (geo:hasGeometry/geo:asWKT) - ADIS_VL01 > Property Paths