耶拿规则不会在预期时开火

时间:2013-07-01 10:00:01

标签: rdf jena rule-engine jena-rules

我已经开始使用Jena并测试了基于规则的reasoners,但我没有看到我期望的结果。我列出了所有单独的声明,这些声明来自我的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]

我想选择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')
]

但规则不会触发。我对规则文件做错了什么? (我已经测试了

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

它有效。

1 个答案:

答案 0 :(得分:3)

正如Dave Reynolds pointed out在Jena用户的邮件列表中,rdf:typerdf:Type不同,(约定是对谓词使用小写,对类使用大写) )。因此,规则应该是:

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

replied收到该消息:

  

我已将rdf:Type更改为rdf:type,但结果相同。是否只需要一个人匹配此规则(在这种情况下,有2个人)?

规则匹配尽可能多的实例,因此,匹配?use的资源数量无关紧要,规则应针对所有实例触发。如果还有其他问题,它似乎没有出现在您向我们展示的任何代码中。对该主题的更多讨论表明,在这种情况下,您的完整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;
}

虽然上面的rdf:type / rdf:Type问题导致初始规则在预期时不会触发,但上面的代码也存在processRules("data/rules/rules.txt", infmodel);仅返回带有规则的推理模型的问题,但实际上并没有开始与它们相关的推理。添加语句processRules.prepare();会导致推理模型运行规则,从而产生预期结果。

相关问题