繁体   English   中英

为什么会出现此异常com.hp.hpl.jena.reasoner.rulesys.Rule $ ParserException:在使用Apache Jena Reasoner时?

[英]Why this exception occur com.hp.hpl.jena.reasoner.rulesys.Rule$ParserException: In Using Apache Jena Reasoner?

这是我的代码:

public class FunctionalityCheckTest1 {

    InfModel infModel;
    Model model = ModelFactory.createDefaultModel();
    String NS = "http://myweb.com/vocab#";

    @Test
    public void playingWithJenaReasoner()
    {
        Resource alex = this.model.createResource(NS+"Alex");
        Resource bob = this.model.createResource(NS+"Bob");
        Resource alice = this.model.createResource(NS+"Alice");
        Property isFriendOf = this.model.createProperty(NS,"isFriendOf");
        alex.addProperty(isFriendOf,bob);
        bob.addProperty(isFriendOf,alice);
        StmtIterator stmtIterator1 = this.model.listStatements();
        while (stmtIterator1.hasNext())
        {
            System.out.println(stmtIterator1.next());
        }

        String customRule = "@prefix vocab: <http://myweb.com/vocab#>. " +
                "[rule1: (?a vocab:isFriendOf ?b) (?b vocab:isFriendOf ?c) -> (?a vocab:isFriendOf ?c) ]";

        List<Rule> rules = new ArrayList<>();
        rules.add(Rule.parseRule(customRule));

        GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
        reasoner.setDerivationLogging(false);
        this.infModel = ModelFactory.createInfModel(reasoner, this.model);
        StmtIterator stmtIterator2 = this.infModel.listStatements();
        while (stmtIterator2.hasNext())
        {
            System.out.println(stmtIterator2.next());
        }
    }

}

在执行playingWithJenaReasoner()函数时会引发错误:
com.hp.hpl.jena.reasoner.rulesys.Rule $ ParserException:子句开头应为'(',发现已出现词汇:
从rules.add(Rule.parseRule(customRule))行开始;

如果一切正常,如果我将这些更改添加到上述代码中

PrintUtil.registerPrefix("vocab",NS);
String customRule = "[rule1: (?a vocab:isFriendOf ?b) (?b vocab:isFriendOf ?c) -> (?a vocab:isFriendOf ?c) ]";

那这怎么了

String customRule = "@prefix vocab: <http://myweb.com/vocab#>. " +
                    "[rule1: (?a vocab:isFriendOf ?b) (?b vocab:isFriendOf ?c) -> (?a vocab:isFriendOf ?c) ]";

在此Jena文档中 ,他们提到了带有规则的@prefix。 我在哪里做错了?

我遇到了您今天遇到的相同问题,看来方法

public static List<Rule> parseRules(String source)

不允许在字符串中添加前缀。 我不确定这是错误还是此方法的功能。

但是,如果您在规则文件中声明规则并通过

public static List<Rule> rulesFromURL(String uri)

您应该能够加载包含前缀的规则。

这是一个小例子来测试是否可行。 假定您已将本体存储在文件系统上的某个位置,并且在类路径中有一个名为jena.rule的规则文件:

public class JenaRuleTest {

    public static void main(String[] args) throws UnsupportedEncodingException {

        OntModel model = ModelFactory.createOntologyModel();        
        model.read("C:\\path\\to\\ontology\\ontology.ttl");     

        String ruleResourceStr = JenaRuleTest.class.getResource("/jena.rule").toString();

        Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(ruleResourceStr));
        reasoner.setDerivationLogging(true);

        InfModel inf = ModelFactory.createInfModel(reasoner, model);        
        inf.write(System.out, "TURTLE");        
    }
}

可以在以下位置找到更详细的示例操作方法: http : //tutorial-academy.com/jena-reasoning-with-rules/

Rule类的文档可以在这里找到: https : //jena.apache.org/documentation/javadoc/jena/org/apache/jena/reasoner/rulesys/Rule.html

如果有人遇到这个问题,希望对您有所帮助。

问候

暂无
暂无

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

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