2017-02-16 59 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

希望如果有人面臨這個問題的幫助。

問候

相關問題