Search code examples
ontologyowl-api

How to get subclasses, supperclasses after update OWL API 3.X to 4.X?


Using version 3.X we used to get classexpressions like this

cls.getSuperClasses(ont)

Updating version 4.X we try to use EntitySearcher but it return empty sets.

EntitySearcher.getSuperClasses(cls, ontology)

Full code:

public static void test() throws OWLOntologyCreationException {

    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = manager.getOWLDataFactory();
    OWLOntology ontology;

    File file = new File("assets/ontologies/zebra.owl");
    ontology = manager.loadOntologyFromOntologyDocument(file);

    OWLClass cls = factory.getOWLClass(IRI.create(ontology.getOntologyID().getOntologyIRI() + "#" + "Color"));
    Set<OWLClassExpression> parentClasses = collect(EntitySearcher.getSuperClasses(cls, ontology).iterator());

    System.out.println(parentClasses.size());
}

public static Set<OWLClassExpression> collect(Iterator<OWLClassExpression> i) {

    Set<OWLClassExpression> set = new HashSet<OWLClassExpression>();
    while (i.hasNext()) {
        OWLClassExpression res = i.next();
        set.add(res);
    }
    return set;
}

Used ontology is zebra.owl /Zebra Riddle or Einstein riddle - DB link

Expected result (3.X or Protege):

inverse (has_color) some House

Solution

  • The problem is here:

    ontology.getOntologyID().getOntologyIRI()

    In OWLAPI 4 getOntologyIRI does not return an IRI but an Optional<IRI> to represent the fact that an ontology might not have an IRI.

    If you change the code to

    ontology.getOntologyID().getOntologyIRI().get()

    your code works and prints 1.

    I've added this fix to the migration suggestions