Search code examples
javaontologyowl-api

How can I get all the classes from a loaded ontology with owlapi?


How can I get all the classes from a loaded ontology with owlapi? Here I have an example on how to get one class, but I'm interested on accessing all the classes defined at the ontology.

    OWLOntologyManager manager = WLManager.createOWLOntologyManager();
    OWLOntology ontology = manager.loadOntology(IRI.create(fileURI));
    OWLDataFactory owlDF = manager.getOWLDataFactory();
//Example to get ONE class, but I want ALL!
    OWLClass stringDocuClass = owlDF.getOWLClass(IRI.create("http://example.com/my_ontology.owl#StringDocu"));

I'm working with java owlapi 4.2.3 (see the API: http://owlapi.sourceforge.net/javadoc/)


Solution

  • For owlapi-v5.0 use:

    ArrayList<OWLClass> classes = new ArrayList<OWLClass>();
    ontology.classesInSignature().forEach(classes::add);
    

    For owlapi-v4.2.3 use:

    Set<OWLClass> classes = ontology.getClassesInSignature();