Search code examples
javaowlontologyowl-api

get subclasses of a class OwlApi


There is a way to find all named subclasses of a class without using a reasoner for (OWLClass child : reasoner.getSubClasses(clazz, true).getFlattened() ) and without doing inference just by using axioms? Thank you


Solution

  • Using owl-api the ontology can be query to get all subClasses axioms. Then you filter the result to retain only the named classes.

    for (final OWLSubClassOfAxiom subClasse : ontology.getAxioms(AxiomType.SUBCLASS_OF))
    {
        if (subClasse.getSuperClass() instanceof OWLClass 
             && subClasse.getSubClass() instanceof OWLClass)
        {
            System.out.println(subClasse.getSubClass() 
                 + " extends " + subClasse.getSuperClass());
        }
    }
    

    Using Jena, you can list statement, add look for the "subClassOf" predicate, then as in owl-api you filter to get only non-annoymous objects.

    final StmtIterator it = model.listStatements();
    while (it.hasNext())
    {
        final Statement s = it.next();
        if (s.getPredicate().equals(RDFS.subClassOf) && !s.getObject().isAnon())
                System.out.println(s.getSubject() + " extends " + s.getObject());
    }