OWL, Protege: Getting from a DefaultOWLObjectProperty value to the class of an individual

StackOverflow https://stackoverflow.com/questions/18740480

  •  28-06-2022
  •  | 
  •  

Question

I'm trying to use the Stanford OWL API, and I find the documentation a bit unclear. Using Java, I load an ontology which has been prepared by some user via Protégé, and get to a DefaultOWLObjectProperty. The value of that property is meant to be an individual in some class in the ontology. How can I find the class? Code snippet below:

OWLNamedClass cls = (OWLNamedClass) it.next();

Collection instances = cls.getInstances(false);
for (Iterator jt = instances.iterator(); jt.hasNext();) {
    OWLIndividual individual = (OWLIndividual) jt.next();
    Collection props = individual.getRDFProperties();
    for (Object prop : props) {
        DefaultOWLObjectProperty obj = (DefaultOWLObjectProperty) prop;
        Object val = individual.getPropertyValue(obj);
        DefaultRDFIndividual valInd = (DefaultRDFIndividual) val;
…
}

I'd like to get the class of valInd.

Was it helpful?

Solution

There are two methods in OWLIndividual that will make this easier for you. Let's assume you've got your OWLOntology as ontology. Then, using getObjectPropertyValues(OWLOntology), you can get a map that maps a property expression to the set of individuals that are related to individual by that property. You can iterate over the entries of that map, and then iterate over the set of individuals. Then, for each of those individuals, you can use getTypes(OWLOntology) to get the set of OWLClassExpressions that are its types. (You get a set of these rather than a single type, because OWL individuals can, and usually do, have more than one type.)

If you're just interested in the values of certain properties, then you can use the more specialized getObjectPropertyValues(OWLObjectPropertyExpression,OWLOntology) to get just the values of a specific property for an individual.

In general, I'd suggest at least skimming over all the methods that the OWLIndividual interface provides, just to have a general awareness of what you can do with it. You don't need to memorize all the details, but when you are approaching a problem, you'll have at least a vague thought that "I think the interface has something like that…" and you'll know where to look. This is good practice with any API or tool, not just the OWL API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top