Question

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)?

Was it helpful?

Solution

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

p rdfs:domain C

we're not doing anything that restricts the individuals that p can have a value for. That is, it's not inconsistent if we see

x p something

and we don't know that x is a C. In fact, what we're actually saying when we say that p's domain is C is that any time we have a triple of the form x p something, we can infer that x is a C. We can write this as an inference rule on triples:

 x p _     p rdfs:domain C
 ------------------------- [1]
       x rdf:type C

This has some potentially surprising consequences when combined with the inference rules for rdfs:subClassOf. In particular, recall that when C rdfs:subClassOf D, this means that any time we have an instance of C, we can infer that it is also an instance of D. As an inference rule:

x rdf:type C    C rdfs:subClassOf D
----------------------------------- [2]
        x rdf:type D

Why does this lead to surprising results? Well, it means that if the domain of p is C, and if C is a subclass of D, then it's also legal to say that D is the (or a) domain of p. Why is this the case? Well, suppose that x p _, and that p rdfs:domain C, and that C rdfs:subClassOf D. Well, by rule [1] above, we have that x rdf:type C. But then because C is a subclass of D, we also have that x rdf:type D. Since the x is arbitrary, then anytime we have x p _, we also have x rdf:type D, but that's exactly what it means to have p 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 of foaf: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 using

{ ?property a owl:DatatypeProperty } UNION { ?property a owl:ObjectProperty }

Since the intent is that ?property has one of two values as an rdf:type, we can shorten the query to be:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl:  <http://www.w3.org/2002/07/owl#>

SELECT ?property
FROM <http://xmlns.com/foaf/spec/index.rdf>
WHERE {
  values ?propertyType { owl:DatatypeProperty owl:ObjectProperty }
  ?property a ?propertyType ;
            rdfs:domain foaf:Person .
}

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:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl:  <http://www.w3.org/2002/07/owl#>

SELECT ?property
FROM <http://xmlns.com/foaf/spec/index.rdf>
WHERE {
  values ?propertyType { owl:DatatypeProperty owl:ObjectProperty }
  ?property a ?propertyType ;
            rdfs:domain/rdfs:subClassOf* foaf:Person .
}

OTHER TIPS

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:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl:  <http://www.w3.org/2002/07/owl#>

SELECT ?property
FROM <http://xmlns.com/foaf/spec/index.rdf>
WHERE {
  { ?property a owl:DatatypeProperty } UNION { ?property a owl:ObjectProperty }
  ?property rdfs:domain foaf:Person 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top