這裏是我的代碼:
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與規則。我在哪裏做錯了?