I have a question about SPARQL. I have an ontology of animals:
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 instances of Animals"? And backwards: How can I know, if a instance (dog1) belongs to Animals?
NOTE: The depth and breadth of the class hierarchy is unknown in advance.
Also the query below will not work
SELECT ?x WHERE {?x rdfs:subClassOf :Animals . ?x :hasСolor "white"}
And the next query (find all Animals, that are 'white') works only if the depth of class hierarchy is known. (So if the hierarchy is known, can I make the specified steps (from top of hierarchy to bottom) to reach 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 next example - "find all instances of Animals"
SELECT ?z WHERE {
?x rdfs:subClassOf :Animals .
?y rdfs:subClassOf ?x .
?z rdf:type ?y .
}
What to do, if the hierarchie is unknown?
The query will be processed with SDB (is a component of Jena).
I want something like :
select ?x where {?x rdfs:subClassOf :Animals . ?x :hasСolor "white"})
UPD. Solution for "find all Animals (instances), that are 'white'" might look like this:
SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals . ?y rdf:type ?x . ?y :hasColor "white"}
You can use transitivity in your SPARQL query (using *) :
SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals .
?y rdf:type ?x .
?y :hasColor "white" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With