如何将 2 个本体与 OWL API 4 或 3.5 正确合并

How to properly merge 2 ontologies with OWL API 4 or 3.5

在我的硕士论文项目中,我想合并几个具有不同命名空间但共享相同导入和一些个体的本体(仅填充个体)(例如 G ontologyA#Harold & ontology B#哈罗德)。

我正在尝试获得新的合并 ontology,其中:

  1. ontology header 的 2 个本体的信息被保留(就像 Protege 5 中的 OWL API 3.5.1)。
  2. 来自合并本体的个体是 "merged",尽管它们的命名空间不同
  3. 合并后的 ontology 只有 1 个命名空间

我的合并代码

private ArrayList<Ontology> ontologies;
    private OWLOntologyManager man;
    private OWLOntologyMerger merger;
    private String fileName;
    private OWLOntology mergedOntology;

public Merger(ArrayList<Ontology> ontologies, OWLOntologyManager man, String filename){

        this.ontologies = ontologies;
        this.man = man;
        this.fileName = filename;
        //create the OWL Ontology merger which retrievs all loaded ontology from manager
        merger = new OWLOntologyMerger(man);    
        //call the merging process
        mergeOntologies();
    }

private void mergeOntologies(){
        IRI mergedOntologyIRI = IRI.create("http://semanticweb.org/ontology/" + fileName);
        for(Ontology ontology : ontologies){
            try {

                man.loadOntologyFromOntologyDocument(ontology.getFile());

            } catch (OWLOntologyCreationException e) {
                e.printStackTrace();
            }
        }
        try {
            mergedOntology = merger.createMergedOntology(man, mergedOntologyIRI);
        } catch (OWLOntologyCreationException e) {
            e.printStackTrace();
        }   
    }

到目前为止,我只能存档本体个体的简单文本合并。在 Protege 上,ontology header 也被合并,但没有与我的代码合并。

据我所知,我无法合并具有相同命名空间的 2 个本体,OWLOntologyManager 将抛出异常。因此这不是一个选项。

如何实现 "smart" 本体合并? 我做错了什么? 有代码示例吗?


回应伊格纳齐奥: ontology header 我的意思是:

<owl:Ontology rdf:about="http://semanticweb.org/ontologies/Harold/Structural_Context">
        <structure:modeltype>Structural Context</structure:modeltype>
        <structure:modelname>Harold</structure:modelname>
        <structure:adoversion>Version 1.0 4.0</structure:adoversion>
        <structure:date>07.01.2015</structure:date>
        <structure:time>17:49</structure:time>
        <structure:username>alex</structure:username>
        <owl:imports rdf:resource="http://MyServer/HCML/structure"/>
    </owl:Ontology>

OntologyA个人哈罗德长这样:

<owl:NamedIndividual rdf:about="http://semanticweb.org/ontologies/Harold/Structural_Context#Harold">
        <rdf:type rdf:resource="http://MyServer/HCML/structure#Object"/>
    </owl:NamedIndividual>

OntologyB的个人哈罗德长这样:

<owl:NamedIndividual rdf:about="http://semanticweb.org/ontologies/evening_activity/User_Context#Harold">
        <rdf:type rdf:resource="http://MyServer/HCML/structure#Person"/>
        <structure:connection rdf:resource="http://semanticweb.org/ontologies/evening_activity/User_Context#bedroom"/>
        <structure:calling rdf:resource="http://semanticweb.org/ontologies/evening_activity/User_Context#enter_the_living_room"/>
        <structure:executing rdf:resource="http://semanticweb.org/ontologies/evening_activity/User_Context#enter_the_living_room"/>
        <structure:participating rdf:resource="http://semanticweb.org/ontologies/evening_activity/User_Context#enter_the_living_room"/>
    </owl:NamedIndividual>

添加公理:

IRI mergedOntologyIRI = IRI.create("http://semanticweb.org/ontology/" + fileName);
        Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
        for(Ontology ontology : ontologies){
                man.loadOntologyFromOntologyDocument(ontology.getFile());
                axioms.addAll(ontology.getOntology().getAxioms());
                man.removeOntology(ontology.getOntology());
        }
            mergedOntology = man.createOntology(mergedOntologyIRI);
            man.addAxioms(mergedOntology, axioms);

结果与使用OWLOntology合并相同。

手动合并公理、导入并使用 OWLEntityRenamer 我可以获得良好的合并结果。

这里是代码:

private void mergeOntologies(){
IRI mergedOntologyIRI = IRI.create("http://semanticweb.org/ontology/" + fileName);
    //Using HashSet to avoid duplicated entries
    Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
    Set<OWLImportsDeclaration> imports = new HashSet<OWLImportsDeclaration>();
    try{    
        for(Ontology ontology : ontologies){
            man.loadOntologyFromOntologyDocument(ontology.getFile());
            axioms.addAll(ontology.getOntology().getAxioms());
            imports.addAll(ontology.getOntology().getImportsDeclarations());
            man.removeOntology(ontology.getOntology()); 
        } 
        mergedOntology = man.createOntology(mergedOntologyIRI);
        man.addAxioms(mergedOntology, axioms);
    } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
    } 
    //Adding the import declarations
    for(OWLImportsDeclaration decl : imports){
        man.applyChange(new AddImport(mergedOntology, decl));
    }
    //rename individuals names to use the merged ontology's IRI
    renameIRIs(mergedOntologyIRI);
}

  private void renameIRIs (IRI newIRI){
        OWLEntityRenamer renamer = new OWLEntityRenamer(man, man.getOntologies());

        for(Ontology ontology : ontologies){
            for ( OWLEntity individual : ontology.getOntology().getIndividualsInSignature()){
                //replace the individual's old IRI with the new one E.g: http://ontologyOld#name becomes newIRI#name
                IRI individualName = IRI.create(individual.getIRI().toString().replaceFirst("[^*]+(?=#|;)", newIRI.toString()));
                man.applyChanges(renamer.changeIRI(individual.getIRI(), individualName));
            }
        }   
    }

本体的命名空间不是问题 - 问题是当本体具有相同的 ontology id 时。在那种情况下,它们不能合并到同一个管理器中——管理器需要关系 ontologyID->ontology 才能发挥作用。为了能够将相同的 ontology ID 用于新的 ontology,它必须由单独的管理员管理。

请注意,ontology IRI 并未规定其中包含的各个名称的名称空间;解析后,各个 IRI 独立于 ontology,公理可以毫无问题地移动到其他本体。

你能描述一下 ontology headers 的意思吗?

ontology 公理本身可以通过多种方式合并;我能想到的最简单的方法是将两个本体中的所有公理添加到一个集合中,并使用该集合创建一个新的 ontology.