耶拿TDB经过原因然后更新 [英] Jena TDB after reason then update

查看:83
本文介绍了耶拿TDB经过原因然后更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jena,我想将新的本体更新到我的tdb中. 例如. 我的本体中有100行,添加一些规则并运行推理程序后,现在有105行. 而且我需要在我的tdb中更新这5个额外的行. 我该怎么办呢?

I am using Jena and I want to update the new ontology into my tdb. For example. I have 100 rows in my ontology , after I add some rules and run the reasoner, there are 105 rows now. And I need to update these 5 additional rows in my tdb. How can I get this done ?

我尝试用Google搜索它,发现了两种方法.一种是使用sparql进行更新,另一种是截断tdb并将新模型添加到其中.

I try to google it and I found two ways. One is using sparql to update , another is truncating the tdb and add the new model into it.

还有其他更好的解决方案吗?

Is there any other better solution?

谢谢

-

代码

void after_reasoner(Model m) {

    String yago = "http://yago-knowledge.org/resource/";

    Reasoner reasoner = new GenericRuleReasoner(
            Rule.rulesFromURL("file:./rules/act.rule"));

    InfModel inf1 = ModelFactory.createInfModel(reasoner, m);

    PrintUtil.registerPrefix("yago", "http://yago-knowledge.org/resource/");

    }

同样,我的问题是如何处理我的tdb中的新"infmodel". 我只想将新事实更新到其中.

So again , my problem is how to deal with the new "infmodel" to my tdb. I want to update only new fact into it.

这是我从tdb获取模型的方法.

Here is my method to get model from tdb.

Model tdb_write_return() {
    String directory = "./tdb";
    Dataset dataset = TDBFactory.createDataset(directory);

    dataset.begin(ReadWrite.WRITE);
    String ns = "http://www.darrell.com.tw/ontologies/";

    Model model = dataset.getNamedModel(ns);
    dataset.commit();
    dataset.end();
    dataset.close();

    return model;
}

推荐答案

注意,从TDBFactory获取的Datset的生存期应长于你从中得到.如果您在该数据集上调用Dataset.close(),则与返回的模型的交互可能会突然遇到错误.

Note that the lifetime of the Datset that you get from TDBFactory should be longer than the Model objects that you get from it. If you call Dataset.close() on that dataset, then interactions with the returned model may suddenly experience errors.

对扣除额进行量化

关于您的问题,如果要保留推断的三元组,则需要将它们重新添加到基础模型中.这样可以利用现有图形而不是替换任何图形.

Regarding your question, if you want to retain your inferred triples, you will need to add them back to the underlying model. This can utilize an existing graph rather than replacing anything.

final String directory = "./tdb";
final String ns = "http://www.darrell.com.tw/ontologies/";

final Dataset dataset = TDBFactory.createDataset(directory);

dataset.begin(ReadWrite.WRITE);
try {
   Model model = dataset.getNamedModel(ns);

   final Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL("file:./rules/act.rule"));
   final InfModel infModel = ModelFactory.createInfModel(reasoner, m);
   infModel.prepare()
   model.add(infModel.getDeductionsModel()); // #1

   dataset.commit();
}
catch(final Exception e) {
   dataset.abort();
}
finally {
   dataset.end();
}

在上面的示例中,注释为#1的行采用了(正向)推论的结果,并将其插回到基础模型中.要获得所有目的,您可以执行以下操作:

In the above example the line with comment #1 takes the results of your (forward) deductions and inserts them back into the underlying model. To get all dedudctions you can do something like:

model.add(infModel);

推理者可以/应该考虑新的三元组并尝试进行新的演绎,但是尝试推理的结果应该是无操作".

The reasoner could/should consider the new triples and attempt to make new deductions, but the result of that attempted reasoning should be a no-op.

说明

如果将infModel添加到基础模型,则尝试将原始模型的并集,向后推理和向前推理全部添加到原始模型.您在评论中提到了这一点.需要说明的是,由于RDF具有设置的语义,尝试添加已经存在的三元组不会导致数据发生任何变化.在RDF中,每个四元组/三元组(或行,考虑到关系类比)都是唯一的.

If you add the infModel to the underlying model, you attempt to add the union of your original model, backwards inference, and forward inference, all to the original model. You called this out in your comment. To clarify, due to the set semantics of RDF, attempting to add triples that already exist doesn't result in any changes to the data. In RDF, every quad/triple (or row, considering a relational analogy), is unique.

如果将infModel.getDeductionsModel()添加到原始模型,则所有正向推理都将重新插入到图形中.通常,这在最低上比添加infModel效率更高,但是如果您依赖于反向链接推论,则不适用.

If you add infModel.getDeductionsModel() to the original model, all forward-chaining inferences are inserted back into the graph. This should be in general at least a little more efficient than adding infModel, but it is not applicable if you depend on backwards-chaining inferences.

这篇关于耶拿TDB经过原因然后更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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