2

The ontology lies in XML here.

I also tried to ask which are the classes of my world and then tried to check if my resource (the municipality) really belongs to that class, but still Country slips away (although it's fetched when I ask for all the classes, it fails to connect via the property belongs_to to my resource).

I have also enabled Forward chaining for reasoning in Sesame! BTW, I am a beginner, so any tip would be gold to me. What am I missing?


Edit:

New query:

"SELECT ?res ?belongs " +
"WHERE  {" +
         "?res a geo:Mun ;"+
            "geo:hasName \"mun name\" ;"+
            "geo:belongs_to+ ?belongs ."+
        "}";

Output:

[belongs=http://geo.linkedopendata.gr/gag/id/1304;res=http://geo.linkedopendata.gr/gag/id/9325]
[belongs=http://geo.linkedopendata.gr/gag/id/13;res=http://geo.linkedopendata.gr/gag/id/9325]
[belongs=http://geo.linkedopendata.gr/gag/id/997;res=http://geo.linkedopendata.gr/gag/id/9325]
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • @JeenBroekstra I am not really sure how the output should be, but I can tell, from the ontology image I have above, that the `belongs` we are getting are the id's of Region Unit, Region, Dec. Admin, but we are missing one, the id of the Country! Is it clear now? – gsamaras Dec 04 '15 at 00:15
  • 1
    I think so. But if that is the case, the problem is simply that for this municipality, the actual `belongsTo` relation between its Dec. Admin and country is missing. – Jeen Broekstra Dec 04 '15 at 00:20
  • I was trying to check manually, but Eclipse couldn't manage the big dataset, anyway, I'll see what I can do, thanks man! – gsamaras Dec 04 '15 at 00:22
  • 1
    added some useful queries to figure out if there's data missing or not. – Jeen Broekstra Dec 04 '15 at 00:32

1 Answers1

3

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • Jeen, where did we take advantage of the forward chaining here? – gsamaras Dec 04 '15 at 00:00
  • @gsamaras nowhere. Hence my point that you don't really need RDFS inferencing to get the result you asked for in your question. – Jeen Broekstra Dec 04 '15 at 00:01
  • Check my update. I got some id's that our municipality belongs to, but I am targeting on the classes. I keep trying. BUT, again the id of the country *is not there*, the id of the country is 0. Damn it. If I get the id'es, I will find a way to find their classes too! – gsamaras Dec 04 '15 at 00:09
  • 2
    Ah. Sesame makes a distinction between different types of queries, based on the kind of result they produce: TupleQuery (`SELECT`), BooleanQuery (`ASK`), and GraphQuery (`CONSTRUCT`, `DESCRIBE`). Each has its own `prepare` method in the RepositoryConnection interface. – Jeen Broekstra Dec 04 '15 at 00:52