Java questioner
Java questioner

Reputation: 157

get all classess, sub classess from owl in java

I have ontology file. owl from that i need to get all classes, subclasses from owl

File file = new File("myontology.owl");

OWLOntology myontology = manager.loadOntologyFromOntologyDocument(file);
System.out.println("Loaded ontology: " + myontology);

// We can always obtain the location where an ontology was loaded from
IRI documentIRI = manager.getOntologyDocumentIRI(myontology);

OWLDataFactory factory = manager.getOWLDataFactory();
OWLClass clsOwl = factory.getOWLClass(documentIRI);

then what should i write? i checked OWLAPI, but i am very new on semantic. In order to get ,

like following example, using owl2api i need a code ,

String uri = "http://protege.cim3.net/file/pub/ontologies/travel/travel.owl";

OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI(uri);

Collection classes = owlModel.getUserDefinedOWLNamedClasses();
for (Iterator it = classes.iterator(); it.hasNext();) {

    OWLNamedClass cls = (OWLNamedClass) it.next();
    Collection instances = cls.getInstances(false);
    System.out.println("Class " + cls.getBrowserText() + " (" + instances.size() + ")");

    for (Iterator jt = instances.iterator(); jt.hasNext();) {
        OWLIndividual individual = (OWLIndividual) jt.next();
        System.out.println(" - " + individual.getBrowserText());
    }
}

thank you

Upvotes: 3

Views: 2442

Answers (1)

soulcheck
soulcheck

Reputation: 36767

Not sure if that's what you need, but

OwlOntology.getClassesInSignature() 

returns the set of all classes that are used to build axioms in the current ontology.

Also once you have OwlClass you can get it's subclasses using

 OwlClass.getSubClasses(OWLOntology)

Upvotes: 2

Related Questions