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

时间:2015-06-17 10:07:07

标签: java merge ontology owl-api

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

我试图获得一个新的合并本体,其中:

  1. 保留了2个本体的本体标题信息(如使用OWL API 3.5.1的Protege 5)。
  2. 来自合并本体的个人"合并"尽管名称空间不同
  3. 合并的本体只有1个命名空间
  4. 我的合并代码

    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上也会合并本体标题,但不能与我的代码合并。

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

    我怎样才能实现&#34; smart&#34;合并本体? 我做错了什么? 有代码示例吗?

    回应Ignazio: 使用本体标题我的意思是:

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

    Ontology A的Harold个人看起来像:

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

    Ontology B的个人Harold看起来像:

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

    结果与使用OWLOntologyMerger相同。

    手动合并公理,导入和使用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));
                }
            }   
        }
    

1 个答案:

答案 0 :(得分:1)

本体的命名空间不是问题 - 问题是当本体具有相同的本体id时。在这种情况下,它们不能在同一个管理器中合并 - 管理器需要关系ontologyID->ontology才能运行。为了能够为新的本体使用相同的本体id,它必须由一个单独的管理器管理。

请注意,本体IRI并未规定其中包含的各个名称的命名空间;一旦解析,单个IRI独立于本体,并且公理可以移动到其他本体而没有问题。

您能描述一下本体标题的含义吗?

本体公理本身可以多种方式合并;我能想到的最简单的方法是将所有公理从本体添加到集合中,并使用集合创建新的本体。