使用 OWLAPI 添加表达式编辑器的公理

Add Axioms of Expression Editor using OWLAPI

我有一个 API,它接收 JSON 文档,其中包含 ontology 的 classes、属性和公理。该文件如下所示:

{
    "id": "https://onto4alleditor.com/pt/idDoProjeto",
    "filetype": "OWL",
    "classes": [{
                    "Name": "Homem",
                    "SubClassOf": ["Pessoa"],
                    "DisjointWith": ["Mulher"],
                    "Annotation": [{"Annotation":"Homo sapiens do sexo masculino.", "Language":"pt"},
                                   {"Annotation": "Male homo sapiens.", "Language": "en"}]
                },
                {
                    "Name": "Mulher",
                    "Constraint": ["Mulher EquivalentTo: (NOT Homem)"],
                    "SubClassOf": ["Pessoa"]
                }
                ]
 }

变量Constraint是从Protegé的表达式编辑器中获取的表达式。我试图将此表达式添加到我的 OWLOntology 对象中,但我收到以下错误:

Encountered Mulher at line 1 column 1. Expected one of:
    Class name
    Object property name
    Data property name
    inv
    Functional
    inverse
    InverseFunctional
    (
    Asymmetric
    Transitive
    Irreflexive
    {
    Symmetric
    Reflexive

Mulher 是我 ontology 的 class,因此错误没有意义。我不确定为什么会这样,因为我相信我的代码正在正确初始化解析器(我正在使用曼彻斯特解析器来完成这项工作)。下面是我的代码:

private void loadClassConstraints(JSONArray constraints){
    ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser();
    parser.setDefaultOntology(this.ont);
    for (int i = 0; i < constraints.length(); i++) {
        parser.setStringToParse(constraints.getString(i));
        this.ont.add(parser.parseAxiom());
    }
}

出了什么问题?我无法提前在互联网上找到足够的适用于我的 problem.Thanks 的文档来寻求任何帮助。

这里的问题是解析公理依赖于 OWLEntityChecker 实现将字符串转换为 class 或 属性 表达式,在本例中 Mulher 为您声明的实体ontology,例如,urn:test:Mulher

在 OWLAPI 跟踪器中有一个关于如何配置它的示例: https://github.com/owlcs/owlapi/issues/507

Protege 使用类似的机制,但它有自己的 OWLEntityChecker 实现,如果您只使用 OWLAPI,则无法使用。

下面的示例使用了 OWLEntityChecker 的简单实现,它将使用 ontology 中所有实体的 IRI 片段来识别实体;这是有限的,因为在真实的本体中,IRI 片段很容易发生冲突或丢失。但是,如果错误跟踪器上的示例不够好,它可能会有所帮助。

    OWLOntologyManager m = OWLManager.createOWLOntologyManager();
    OWLDataFactory df = m.getOWLDataFactory();
    OWLOntology ont = m.createOntology(IRI.create("urn:test:test"));
    ont.add(df.getOWLDeclarationAxiom(df.getOWLClass("urn:test:Mulher")));
    ont.add(df.getOWLDeclarationAxiom(df.getOWLClass("urn:test:Homem")));
    ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser();
    parser.setDefaultOntology(ont);
    final Map<String, OWLEntity> map = new HashMap<>();
    ont.signature().forEach(x -> map.put(x.getIRI().getFragment(), x));
    parser.setOWLEntityChecker(new OWLEntityChecker() {
        private <T> T v(String name, Class<T> t) {
            OWLEntity e = map.get(name);
            if (t.isInstance(e)) {
                return t.cast(e);
            }
            return null;
        }

        @Override
        public OWLObjectProperty getOWLObjectProperty(String name) {
            return v(name, OWLObjectProperty.class);
        }

        @Override
        public OWLNamedIndividual getOWLIndividual(String name) {
            return v(name, OWLNamedIndividual.class);
        }

        @Override
        public OWLDatatype getOWLDatatype(String name) {
            return v(name, OWLDatatype.class);
        }

        @Override
        public OWLDataProperty getOWLDataProperty(String name) {
            return v(name, OWLDataProperty.class);
        }

        @Override
        public OWLClass getOWLClass(String name) {
            return v(name, OWLClass.class);
        }

        @Override
        public OWLAnnotationProperty getOWLAnnotationProperty(String name) {
            return v(name, OWLAnnotationProperty.class);
        }
    });
    parser.setStringToParse("Mulher EquivalentTo: (NOT Homem)");
    OWLAxiom parseAxiom = parser.parseAxiom();
    System.out.println("of.main() " + parseAxiom);
    ont.add(parseAxiom);