更新OWLAPI3.X到4.X后如何获取子类、超类?

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

使用版本 3.X 我们曾经得到这样的类表达式

cls.getSuperClasses(ont)

更新版本 4.X 我们尝试使用 EntitySearcher,但它 return 空集。

EntitySearcher.getSuperClasses(cls, ontology)

完整代码:

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;
}

使用的ontology是zebra.owl/斑马之谜或爱因斯坦之谜-DB link

预期结果(3.X 或 Protege):

inverse (has_color) some House

问题出在这里:

ontology.getOntologyID().getOntologyIRI()

在 OWLAPI 4 中,getOntologyIRI 没有 return 一个 IRI 而是一个 Optional<IRI> 来表示一个 ontology 可能没有 IRI 的事实。

如果将代码更改为

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

您的代码可以运行并打印 1

我已将此修复程序添加到 the migration suggestions