SPARQL: How to get an idea of ​​the ontology if the depth of the class hierarchy is unknown?

I have a question about SPARQL. I have an animal ontology:

Animals (is a superclass with object property <hasColor>) ------ Mammals (subclass of Animals) ------------- Dog (subclass of Mammals) ---------------- dog1 (a instance with property <hasColor>="white") ---------------- dog2 (a instance with property <hasColor>="red" ) ------ Bird (subclass of Animals) 

Is it possible to find with SPARQL β€œall Animals that areβ€œ white ”orβ€œ all animal instances ”? And back: how can I find out if an instance (dog1) belongs to animals?

NOTE The depth and width of the class hierarchy are unknown in advance.

Also the request below will not work

 SELECT ?x WHERE {?x rdfs:subClassOf :Animals . ?x :hasolor "white"} 

And the next query (to find all Animals that are "white") only works if the depth of the class hierarchy is known. (So, if the hierarchy is known, can I take the indicated steps (from the top of the hierarchy down) to achieve the goal: in this case, 2 steps.

 SELECT ?z WHERE { ?x rdfs:subClassOf :Animals . ?y rdfs:subClassOf ?x . ?z rdf:type ?y . ?z :hasColor "white" } 

The same is true for the following example - "find all instances of Animals"

 SELECT ?z WHERE { ?x rdfs:subClassOf :Animals . ?y rdfs:subClassOf ?x . ?z rdf:type ?y . } 

What if the hierarchy is unknown?

The request will be processed using the SDB (a component of Jena ).

I need something like: select ?x where {?x rdfs:subClassOf :Animals . ?x :hasolor "white"}) select ?x where {?x rdfs:subClassOf :Animals . ?x :hasolor "white"})

UPD The solution for finding all animals (specimens) that are "white" might look like this:

SELECT? y WHERE {? x rdfs: subClassOf *: Animals.? y rdf: type? x.? have: hasColor "white"}

+6
source share
1 answer

You can use transitivity in a SPARQL query (using *):

 SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals . ?y rdf:type ?x . ?y :hasColor "white" } 
+10
source

Source: https://habr.com/ru/post/912045/


All Articles