Search code examples
javaowlowl-api

How to retrieve subclasses of an OWL class using owlapi 5.1?


I am rewriting a java program which reads an OWL file and builds a graph database. The program uses an older version of OWLAPI and many get methods are now deprecated. I have refactored my code to use Stream. Right now I am trying to retrieve the subclasses for each class in my OWL file.

Using OWLSubClassOfAxiom I can retrieve the subclasses I need but I still need to filter the result to only grab the subclass

    final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLOntology ontology = load(manager);

    //--create a reasoner to check that the ontology is consistent
    OWLReasonerFactory reasonerFactory = new 
    StructuralReasonerFactory();
    OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
    reasoner.precomputeInferences();
    boolean consistent = reasoner.isConsistent();


    if (consistent) {
      //--get all classes in the ontology
      for (OWLClass oc : ontology.classesInSignature().collect(Collectors.toSet())) {
          System.out.println( "Class: " + oc.toString() );
          //--get all the SubClassOfAxiom of each class
          for (OWLSubClassOfAxiom sca: ontology.subClassAxiomsForSuperClass(oc).collect(Collectors.toSet())) {
            System.out.println( "    Subclass: " + sca.toString() );
          }
        }
    }

A sample of the output is as follows:

Class: <http://www.nist.gov/el/ontologies/kitting.owl#PoseLocation>
    Subclass: SubClassOf(<http://www.nist.gov/el/ontologies/kitting.owl#PoseLocationIn> <http://www.nist.gov/el/ontologies/kitting.owl#PoseLocation>)

In this example, using owlapi 5.1, how can I retrieve PoseLocationIn, which is a subclass of PoseLocation ?


Solution

  • Use the Searcher class, it is meant to be a convenient replacement for methods that were deleted moving from OWLAPI 3 to 5. Searcher::getSubClasses does the same job.