2017-02-16 58 views
1

这里是我的代码:

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 Documentation,他们都提到@prefix与规则。我在哪里做错了?

回答

2

我遇到了你今天有同样的问题,它似乎像方法

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

希望如果有人面临这个问题的帮助。

问候

相关问题