Search code examples
owl-api

Retrieve just root classes/classes with no asserted subclassof parent class


Is there an easy way to retrieve just the root classes from an OWL ontology in OWLAPI? Here I mean named classes with no asserted parent class. Added complexity, trying not to use reasoner because ontologies are huge and reasoner is not completing in fast enough time (typically). Thanks as always!


Solution

  • Actually using a reasoner would make the task trivial because the only named class without a parent would be owl:Thing.

    Jokes aside, you'd need to take all the classes in the ontology signature and, for each one, ensure it does not appear in a subclass axiom as subclass (or, if it does, that the class on the other side is owl:Thing.

    The code would look like this:

        OWLDataFactory df=OWLManager.getOWLDataFactory();
        OWLClass thing=df.getOWLThing();
        OWLOntology o = ...
        o.classesInSignature()
                       // all asserted superclasses
            .filter(c->o.subClassAxiomsForSubClass(c)
                                   // skip owl:Thing 
                        .filter(s->!thing.equals(s.getSuperClass()))
                        // only keep the ones with no superclass
                        .count()==0)
            .forEach(System.out::println);
    

    This example prints out the IRIs for those classes.