简体   繁体   English

如何将Jena规则添加到OntModel

[英]How to add Jena rule to OntModel

I am trying to add a Jena rule submitted as string to the below method. 我正在尝试将以字符串形式提交的Jena规则添加到以下方法。 However the rule is not being added as I can verify no new change on the ontology written to E://1_1_1 Could someone help me how to do this. 但是未添加规则,因为我可以验证写入E:// 1_1_1的本体没有新变化。有人可以帮助我执行此操作吗? Other questions on SO that may be related are 26292160 , 349652 ; 关于SO的其他问题可能与26292160、349652 有关 both of which are similar to my case. 两者都与我的情况相似。

public String ValidateAndConfigureRule(String string) {     

    try{
        GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.parseRules(string));
        Model oModel = m.getOntology();

        reasoner.setDerivationLogging(true);            
        reasoner.setOWLTranslation(true);
        reasoner.setTraceOn(true);
        reasoner.setTransitiveClosureCaching(true);

        InfModel inf = ModelFactory.createInfModel(reasoner, oModel);

        inf.write(new FileWriter("E://1_1_1"));

        Model baseModel = ModelFactory.createDefaultModel();            
        baseModel.add(inf);

        final OntModel model  = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, baseModel);          

        ValidityReport validity = model.validate();
    }
}

The rule itself is in the format [ r1: (?x :objProp1 :ind_x) -> (?x :objProp2 :ind_y) ] where objProp 's are object properties and ind_x , ind_y are individuals along with the necessary prefix for ":" in the rule string. 规则本身的格式为[ r1: (?x :objProp1 :ind_x) -> (?x :objProp2 :ind_y) ] ,其中objProp是对象属性, ind_xind_y是带有“:的必要前缀”的个体: ”在规则字符串中。

Andy's answer captures the most important parts here: when you define a rule-based inference model, the contents of the model are the asserted and inferred triples; Andy的答案涵盖了这里最重要的部分:定义基于规则的推理模型时,模型的内容是声明的和推理的三元组; the rules aren't written into the model. 规则未写入模型。 The rules are part of the reasoner , not the data . 规则是推理程序的一部分,而不是数据

This is actually in contrast to SWRL rules, which have defined serialization formats so that SWRL rules can be written alongside OWL data. 这实际上与定义了序列化格式的SWRL规则相反,因此SWRL规则可以与OWL数据一起写入。 You might consider using SWRL rules and a reasoner that supports them (eg, Pellet, HermiT) instead of Jena rules, since the rules could be written along with the data. 您可能会考虑使用SWRL规则以及支持它们的推理机(例如Pellet,HermiT)而不是Jena规则,因为这些规则可以与数据一起编写。

As a workaround, you could also store the text of your Jena rules as the value of an annotation property on your OWL ontology and read them in again when you load the ontology. 作为一种解决方法,您还可以将Jena规则的文本存储为OWL本体上的批注属性的值,并在加载本体时再次读取它们。 That is, you might end up with something like: 也就是说,您可能会得到类似以下内容的结果:

@prefix : <http://example.org/my-ontology/>

<http://example.org/my-ontology>
        a                owl:Ontology ;
        rdfs:comment     "My ontology with some Jena rules"@en ;
        :hasJenaRules    "...rule content here..." .

Then, when you load the ontology, you can check whether there are triples with the property :hasJenaRules , and if there is, you can take their objects, parse them as a Jena rules, and create a reasoner with the rules. 然后,当您加载本体时,可以检查是否具有属性:hasJenaRules的三元组,如果存在,则可以获取它们的对象,将它们解析为Jena规则,并使用该规则创建推理器。 I think that would be the easiest way to store your rules alongside your data with Jena. 我认为这是通过Jena将规则与数据一起存储的最简单方法。

When you write an InfModel it writes the base data - which has not changed. 编写InfModel时,它会写入基础数据-尚未更改。 Rules don't get written. 规则不成文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM