如何将2个本体与OWL API 4或3.5正确合并 [英] How to properly merge 2 ontologies with OWL API 4 or 3.5

查看:91
本文介绍了如何将2个本体与OWL API 4或3.5正确合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的硕士论文项目中,我想合并具有不同名称空间但共享相同的导入和一些个人(例如,本体OnA,本体B和本体Har)的几种本体(仅填充有个人).

in my master thesis project I want to merge several ontologies (filled only with individuals) having different namespaces, but sharing the same imports and some individuals(e.G ontologyA#Harold & ontologyB#Harold).

我正在尝试获得一种新的合并本体,其中:

I'm trying to gain a new merged ontology where:

  1. 将保留2个本体的本体头信息(例如在带有OWL API 3.5.1的Protege 5中).
  2. 尽管命名空间不同,但合并本体中的个人仍被合并"
  3. 合并后的本体只有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上,本体头也被合并,但是不与我的代码合并.

So far I could only archive a simple textual merge of the individuals of the ontologies. On Protege also the ontology headers are merged,but not with my code.

据我了解,我无法合并具有相同名称空间的2个本体,OWLOntologyManager将引发异常.因此,这不是一个选择.

As I understand I cannot merge 2 ontologies having the same namespace, the OWLOntologyManager will throw an Exception. Therefore it's not an option.

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

How can I achieve a "smart" merging of ontologies? What I'm doing wrong? Is there any code example?

在回应Ignazio时: 对于本体头,我的意思是:

In response to Ignazio: With ontology header I mean:

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

个体A的本体Harold看起来像:

The individual Harold of Ontology A looks like:

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

个体本体Harold B看起来像:

The individual Harold of Ontology B looks like:

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


添加公理:


Adding the axioms:

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可以达到良好的合并结果.

The result is the same as using the OWLOntologyMerger.

Merging manually the axioms, imports and using the OWLEntityRenamer I could achieve a good merging result.

代码在这里:

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

推荐答案

本体的名称空间不是问题-问题是本体具有相同的本体ID.在这种情况下,它们不能在同一管理器中合并-管理器需要关系ontologyID->ontology起作用.为了能够对新的本体使用相同的本体ID,必须由单独的管理员对其进行管理.

The namespace for the ontologies is not the issue - the problem is when the ontologies have the same ontology id. In that case, they cannot be merged in the same manager - the manager needs the relation ontologyID->ontology to be functional. To be able to use the same ontology id for a new ontology, it must be managed by a separate manager.

请注意,本体IRI并不为其中包含的各个名称指定名称空间;解析后,各个IRI便独立于本体,并且公理可以毫无问题地移至其他本体.

Note that the ontology IRI does not dictate the namespace for the individual names contained in it; once parsed, the individual IRIs are independent of the ontology, and the axioms can be moved to other ontologies without issues.

您能描述本体标头的意思吗?

Can you describe what you mean by ontology headers?

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

The ontology axioms themselves can be merged in multiple ways; the simplest I can think of is adding all axioms from both ontologies to a set, and using the set to create a new ontology.

这篇关于如何将2个本体与OWL API 4或3.5正确合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆