Search code examples
rdfsemantic-webowlowl-apireasoning

Test whether a OWL class is a domain/range of a property


In the example hasProperty from owl-api repository:

To test whether the instances of a class must have a property we create a some values from restriction and then ask for the satisfiability of the class interesected with the complement of this some values from restriction. If the intersection is satisfiable then the instances of the class don't have to have the property, otherwise, they do.

So to check if a class is a domain of an object property, I can use the snippet bellow:

OWLDataFactory dataFactory = manager.getOWLDataFactory();
OWLClassExpression restriction = dataFactory.getOWLObjectSomeValuesFrom(objectProperty, dataFactory.getOWLThing());
OWLClassExpression complement = dataFactory.getOWLObjectComplementOf(restriction);
OWLClassExpression intersection = dataFactory.getOWLObjectIntersectionOf(cls, complement);
boolean hasObjectProperty = !reasoner.isSatisfiable(intersection);

I want to know how to check if a class is a range of an object property, and if it is a domain of a data property. Can I use the following snippet (based on the example above) for checking data property domains?

OWLClassExpression restriction = dataFactory.getOWLDataSomeValuesFrom(dataProperty, dataFactory.getOWLThing());
OWLClassExpression complement = dataFactory.getOWLDataComplementOf(restriction);
OWLClassExpression intersection = dataFactory.getOWLDataIntersectionOf(cls, complement);
boolean hasDataProperty = !reasoner.isSatisfiable(intersection);

Solution

  • The example does not do what you are looking for - it checks whether it is necessary for instances of a class to have a property assertion with a specific property. The condition you are trying to verify is a weaker one - whether, given a property assertion, a class C is inferred to be a type for the subject (or the object, for the range case) of the assertion.

    This can be done in a simpler way (both code and complexity), checking if the domain of the property is a superclass of the class you're interested in - or, if you want to check whether a class C is exactly the domain, you can check if the two classes are equivalent.

    Example:

    OWLOntology o = ... //root ontology for the reasoner
    OWLReasoner r = ...
    OWLObjectProperty p = ...
    for (OWLObjectPropertyDomainAxiom ax: o.getObjectPropertyDomainAxioms(p)) {
        OWLClassExpression c = ax.getDomain();
        NodeSet<OWLClass> allSubClasses = r.getSubClasses(c, false);
        Node<OWLClass> allEquivalentClasses = r.getEquivalentClasses(c);
    }
    

    For domain of data properties you just need to switch from object to data properties in the example, for range of object properties, you'll search for object property range axioms.