If I understand your data model correctly, you have instances of class geo:Municipality
, which belong to an instance of geo:RegionUnit
, which in turn belongs to an instance of geo:Region
, etc, until ultimately they belong to an instance of geo:Country
. And unless I misunderstand, your query tries to get back all these instances for one particular munipicality.
This is quite simply done, and does not even require any RDFS inferencing support.
Let's build up the query one step at a time. First, let's grab the actual municipality itself:
SELECT ?res
WHERE {
?res a geo:Municipality ;
geo:έχει_επίσημο_όνομα "ΔΗΜΟΣ ΧΑΝΙΩΝ" .
}
I'm assuming here (since I don't speak Greek) that geo:έχει_επίσημο_όνομα
is the RDF property that links the Municipality resource with its name label.
The second step is that we want to grab all other resources it belongs to.
SELECT ?res ?belongs
WHERE {
?res a geo:Municipality ;
geo:έχει_επίσημο_όνομα "ΔΗΜΟΣ ΧΑΝΙΩΝ" ;
geo:belongsTo ?belongs .
}
Of course, the above only gets us back the things that it directly belongs to. If we believe your ontology, this will give us back the region unit(s) it belongs to, but nothing else. But we want all of them, N steps removed, so we want to transitively follow the geo:belongsTo
relation. This can be done using a transitive property path:
SELECT ?res ?belongs
WHERE {
?res a geo:Municipality ;
geo:έχει_επίσημο_όνομα "ΔΗΜΟΣ ΧΑΝΙΩΝ" ;
geo:belongsTo+ ?belongs .
}
Notice the +
. This means "one or more times", so the variable ?belongs
will be bound to any values that can be reached by following the geo:belongsTo
property one or more times (the *
that you used in your question, btw, expresses 'zero or more times').
Update
Now, if in addition to getting the individual resources, you also want to get back the classes themselves that they belong to (that is, geo:RegionUnit
, geo:Country
, etc), you can amend the query like so:
SELECT ?res ?belongs ?adminUnit
WHERE {
?res a geo:Municipality ;
geo:έχει_επίσημο_όνομα "ΔΗΜΟΣ ΧΑΝΙΩΝ" ;
geo:belongsTo+ ?belongs .
?belongs a ?adminUnit .
}
This will give you back all administrative units that the given municipality belongs to, and the class of each particular administrative unit. Here, by the way, RDFS inferencing will make a slight difference: because all your admin-unit classes are defined as a rdfs:subClassOf
the class geo:AdministrativeUnit
, you will get back two results for each unit: once the specific class, and once the superclass geo:AdministrativeUnit
.
Update 2 if the problem is that you get all the administrative units back except the country, then the most likely cause of this is that the geo:belongsTo
relation between the specific Decentralised Admin (Let's call it geo:DecAdm
for short) and the Country is missing. If you wish to verify this, you can do the following query:
ASK WHERE {
<http://geo.linkedopendata.gr/gag/id/997> geo:belongsTo [ a geo:Country ] .
}
Replace <http://geo.linkedopendata.gr/gag/id/997>
with the actual id of the geo:DecAdmin
you're interested in. The query will return true
if the relation exists, false
otherwise.
Or if you want to check more generally, you can do the following:
SELECT ?decAdmin ?country
WHERE {
?decAdmin a geo:DecAdm .
OPTIONAL { ?decAdmin geo:belongsTo ?country .
?country a geo:Country .
}
} ORDER BY ?decAdmin
This query will give you an overview of all decentralized administration instances, and the country to which they belong. If there is no country known for a particular ?decAdmin
, the second column in your query result will be empty for that row.