This question already has an answer here:
- Get all properties for a DBpedia class 2 answers
I have a complex OWL ontology with many classes. What SPARQL query do I need to use to obtain data and object properties of one OWL class (e.g., Person class)?
In addition to Jukka Matilainen's answer, there are a few points that should be taken into consideration. OWL is not an object oriented programming language, and the concept of classes and properties are not the same as classes and properties in object oriented programming languages. When we assert that
we're not doing anything that restricts the individuals that
p
can have a value for. That is, it's not inconsistent if we seeand we don't know that
x
is aC
. In fact, what we're actually saying when we say thatp
's domain isC
is that any time we have a triple of the formx p something
, we can infer thatx
is aC
. We can write this as an inference rule on triples:This has some potentially surprising consequences when combined with the inference rules for
rdfs:subClassOf
. In particular, recall that whenC rdfs:subClassOf D
, this means that any time we have an instance ofC
, we can infer that it is also an instance ofD
. As an inference rule:Why does this lead to surprising results? Well, it means that if the domain of
p
isC
, and ifC
is a subclass ofD
, then it's also legal to say thatD
is the (or a) domain ofp
. Why is this the case? Well, suppose thatx p _
, and thatp rdfs:domain C
, and thatC rdfs:subClassOf D
. Well, by rule[1]
above, we have thatx rdf:type C
. But then becauseC
is a subclass ofD
, we also have thatx rdf:type D
. Since thex
is arbitrary, then anytime we havex p _
, we also havex rdf:type D
, but that's exactly what it means to havep rdfs:domain D
.This means that if you're trying to retrieve all properties that have
foaf:Person
as a domain, then you'll need to use an OWL reasoner, not just SPARQL queries, if you want all the results. Just asking for things that have a declared domain offoaf:Person
won't necessarily find all the results.There's an accepted answer with a SPARQL query, but I'd also note that it can be cleaned up a bit by using
values
instead of usingSince the intent is that
?property
has one of two values as anrdf:type
, we can shorten the query to be:As described above, you may well want to include any properties whose declared domain is an subclass of
foaf:Person
, and while we can't compute the entire OWL class hierarchy using SPARQL queries, we can at least do a little bit with property paths:If you want to query an OWL ontology (such as the FOAF ontology) to find out datatype properties and object properties that have a given class (such as foaf:Person) declared as their domain, you could use a SPARQL query like this: