如何克隆或复制Jena-Ontology-Model(OntModel)以应用临时更改? [英] How to clone or copy a Jena-Ontology-Model (OntModel) to apply temporary changes?

查看:128
本文介绍了如何克隆或复制Jena-Ontology-Model(OntModel)以应用临时更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在寻找解决方案,并且由于Apache Jena上没有论坛,我不得不在stackoverflow上创建一个帐户来询问.
我的问题是我需要一个已经加载的本体的临时(深层)副本,即OntModelImpl的对象(带有一些导入,但没有任何附加的推理机).其目的是在模型上应用一些验证步骤,该步骤需要一些SPARQL UPDATE查询来首先添加一些自动生成的三元组.这些额外的三元组仅用于验证,不应在原始模型中找到它们!
对象本身没有复制或克隆方法,通过我的多次搜索,我无法在其他任何地方找到任何静态(?)方法来实现此目的.而是在OntModelImpl上有某种复制构造函数"(未真正标记为此类),它带有一个规范(OntModelSpec)和一个Model.我试图通过这种方式使用它:

I've been searching for a solution for days now and since there is no forum at Apache Jena I had to create an account at stackoverflow to ask.
My problem is that I need a temporary (deep) copy of an already loaded ontology, namely an object of OntModelImpl (with some imports but without any attached inference machine). Its purpose is to apply some validation steps on the model which require some SPARQL UPDATE queries to add some automatically generated triples first. These additional triples are only intended for the validation and should not find their way into the original model!
There is no copy- or clone-method on the object itself and through my many searches I couldn’t find any static (?) methods anywhere else to fulfil this purpose. Instead there is some kind of "copy constructor" (not really marked as such) on OntModelImpl which takes a specification (OntModelSpec) and a Model. I tried to use it this way:

workingModel = new OntModelImpl(ontModel.getSpecification(), ontModel);

其中ontModelOntModelImpl,它是先前通过调用JenaOWLModel.getOntModel()创建的. JenaOWLModel本身是Protégé-3-API的一部分,是通过调用OwlProjectFromReaderCreator.getOwlModel()创建的.该创建者(连同必要的存储库)用于加载带有导入的原始本体. workingModel表示我要创建的工作副本.
显示的代码行的最初问题是,它抛出多个DoesNotExistException,下面有十几个或更多调用级别.这些表示找不到导入本体????如前所述,原始模型已经加载,包括所有导入. (我在这里跳过了一些细节以简化故事.)
后来我发现我可以通过设置来抑制异常

Where ontModel is an OntModelImpl which was previously created through a call of JenaOWLModel.getOntModel(). The JenaOWLModel itself is part of the Protégé-3-API and was created through a call to OwlProjectFromReaderCreator.getOwlModel(). This creator (together with a necessary repository) was used to load the original ontology with its imports. workingModel denotes my working copy I am trying to create.
The initial problem with the shown code line was that it throws multiple DoesNotExistExceptions, a dozen or more call levels beneath. These denote that the import ontologies couldn't be found??? As I mentioned before, the original model is already loaded, inclusive all imports. (I skip some details here to shorten the story.)
Later I found out that I can suppress the exceptions by setting

ontModel.getSpecification().getDocumentManager().setProcessImports(false);

在构造函数调用之前.从这一点来看,一切看起来都很好,没有例外了,我在两个不同的图中得到了两个具有相同三元组计数的不同模型对象(我对以确保它是深层副本).但是今天我知道了一些奇怪的事情:第一次添加验证三元组时没有问题-我在SPARQL-UPDATE-INSERT请求前后将三元组计数写入stdout.但是当我第二次调用验证时,原始模型已经包含了额外的三元组 !!
这意味着我无法看到两个模型的链接,或者Jena使用某种全局链接-也许是我不知道的缓存机制,哪种机制试图使所有具有相同URI的模型彼此保持一致,或者完全保持某种一致性.不同.

所以我真的需要这里的帮助!如何获取OntModel(OntModelImpl)的副本以应用随后要丢弃的临时更改?

before the constructor-call. From this point everything looked fine, no exceptions anymore, I got two different model objects with the same triple counts in their different graphs (I did a quick compare of the object IDs of all first and second level attributes of the two instances of OntModelImpl to make sure it is a deep copy). But today I got aware of something strange: There is no problem when adding the validation triples the first time - I write the triple count to stdout before and after the SPARQL-UPDATE-INSERT-requests. But when I call the validation for the second time, the original model does already contain the additional triples!?
That means either the two models are linked somehow I couldn't see or Jena uses some kind of global linking-, maybe caching-mechanism I am not aware of which tries to hold all models with the same URI consistent to each other or something totally different.

So I really need help here! How can I get a copy of an OntModel (OntModelImpl) to apply temporary changes which I want to throw away afterwards?

问候和感谢您的光临

推荐答案

@ dr0i的答案对我不起作用.

The answer from @dr0i did not work for me.

这有效: Model copy = ModelFactory.createDefaultModel().add(originalModel);

ModelFactory也会创建Model子类:createInfModel()createOntologyModel().

我按照以下逻辑进行了测试.

I tested with the following logic.

int size = originalModel.size();
Model copy = ModelFactory.createDefaultModel().add(originalModel);
copy.removeAll();
assert originalModel.size() == size;

这篇关于如何克隆或复制Jena-Ontology-Model(OntModel)以应用临时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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