简体   繁体   English

杰娜(Jena)规则未如预期般触发

[英]Jena rule not firing when expected

I've started to use Jena and tested its rules-based reasoners, but I'm not seeing the results that I expect. 我已经开始使用Jena并测试了其基于规则的推理机,但没有看到预期的结果。 I listed all of individual statement and these are from my OWL files: 我列出了所有单独的语句,这些都来自我的OWL文件:

[http://www.semanticweb.org/ontologies/2012/6/Ontology1342794465827.owl#CreditCardPayment,        http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Resource]

[http://www.semanticweb.org/ontologies/2012/6/Ontology1342794465827.owl#UseCase, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Resource]

I want to select to select CreditCardPayment , so I wrote this rule: 我想选择选择CreditCardPayment ,因此我编写了以下规则:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
@prefix base: <http://www.semanticweb.org/ontologies/2012/6/Ontology1342794465827.owl#>
[rule1:
(?use rdf:Type rdfs:Resource)
->
print('test')
]

but the rule doesn't fire. 但该规则不会生效。 What I did wrong with rule file? 我对规则文件做错了什么? (I've already tested on (我已经在

[rule1:
print('test')
->
print('test')
])

and it works. 而且有效。

As Dave Reynolds pointed out on the Jena users' mailing list, rdf:type is not the same as rdf:Type , (the convention is to use lower case for predicates and upper case for classes). 正如Dave Reynolds在Jena用户的邮件列表中指出的那样rdf:typerdf:Type (约定是对谓词使用小写字母,对类使用大写字母)。 As a result the rule should be: 因此,规则应为:

[rule1: (?use rdf:Type rdfs:Resource) -> print('test') ]

You replied to that message: 回复了该消息:

I've changed rdf:Type to rdf:type, but I got the same result. 我已经将rdf:Type更改为rdf:type,但是得到了相同的结果。 Does it required only one individual to match this rule (In this case, there are 2 individuals)? 是否只需要一个人即可匹配此规则(在这种情况下,有2个人)?

A rule matches as many instances as possible, so it does not matter how many resources match ?use , the rule should fire for all of them. 规则会尽可能地匹配实例,因此匹配多少资源都无关紧要?use ,规则应为所有实例触发。 If there is another problem, it does not appear to be in any of the code that you've shown us. 如果还有其他问题,那么您显示给我们的任何代码中都不会出现该问题。 More discussion on that thread revealed that your full Java code in this case was: 关于该线程的更多讨论表明,在这种情况下,您的完整Java代码为:

public class Main {
    public static void main(String[] args) throws MalformedURLException, IOException {
    // create a basic RAW model that can do no inferencing
    Model rawModel = FileManager.get().loadModel("file:data/design_pattern.owl");

    // create an InfModel that will infer new facts.
    OntModel infmodel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, rawModel);
        StmtIterator i = infmodel.listStatements();
        System.out.println("====Begin=====");
        while (i.hasNext()) {
            Statement indi = i.next();
            System.out.println(indi);
        }
        System.out.println("=====End=====");
        InfModel processRules = (InfModel) processRules("data/rules/rules.txt", infmodel);
    }
}

public static Model processRules(String fileloc, InfModel modelIn) {
    Model m = ModelFactory.createDefaultModel();
    Resource configuration = m.createResource();
    configuration.addProperty(ReasonerVocabulary.PROPruleSet, fileloc);
    Reasoner reasoner = GenericRuleReasonerFactory.theInstance().create(configuration);
    InfModel infmodel = ModelFactory.createInfModel(reasoner, modelIn);
    return infmodel;
}

While the rdf:type / rdf:Type issue above caused the initial rule to not fire when expected, the code above also has the issue that processRules("data/rules/rules.txt", infmodel); 尽管上面的rdf:type / rdf:Type问题导致初始规则在预期时不会触发,但是上面的代码也存在processRules("data/rules/rules.txt", infmodel); only returns an inference model with the rules, but does not actually start the reasoning associated with them. 仅返回带有规则的推理模型,但实际上不启动与规则相关联的推理。 Adding the statement processRules.prepare(); 添加语句processRules.prepare(); causes the inference model to run the rules, producing the expected results. 使推理模型运行规则,产生预期结果。

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

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