Search code examples
owl-api

How to analyze a complex class axiom using OWL API


I read the OWL API documentation, most of the examples are about create class axioms and add them to the ontology. Now, I need to retrieve the restriction of a class, and extract the elements in the restriction.

For example, in the pizza.owl, ChessePizza class is defined by the restriction: "Pizza and (hasTopping some CheeseTopping)". I can use the "getEquivalentClassesAxioms" function to get the whole axiom. But I want to know the details of this axiom, such as the object properties (hasTopping) and classes (CheeseTopping) used in this axiom. Is there any method to extract the elements of a axiom?


Solution

  • The best approach to, for example, extract the property for all existential restrictions, is to write an OWLObjectVisitor.

    In a visitor, you implement a visit(OWL... o) for each class that the visitor knows about. For an axiom that defines A equivalentTo p some Q, the visitor would look something like:

    OWLObjectVisitor v = new OWLObjectVisitor() {
        public void visit(OWLEquivalentClassesAxiom ax) {
            // this is an example of recursive visit
            ax.classExpressions().forEach(c->c.accept(v));
        }
    
        public void visit(OWLObjectSomeValuesFrom ce) {
            OWLObjectPropertyExpression p = ce.getProperty();
            // here you can do what you need with the property.
        }
    };
    axiom.accept(v);