java为什么会出现这种异常。惠普。hpl。杰娜。推理者。rulesys。规则$ParserException:在使用ApacheJena推理机时?
这是我的代码:
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。惠普。hpl。杰娜。推理者。rulesys。Rule$ParserException:在子句的开头应该是“(”找到vocab:
来自行规则。添加(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。我哪里做错了
# 1 楼答案
我遇到了你今天遇到的同样的问题,这似乎是一种方法
public static List<Rule> parseRules(String source)
不允许在字符串中使用前缀。我不确定这是一个错误还是这个方法的一个特点
但是,如果在规则文件中声明规则并通过
public static List<Rule> rulesFromURL(String uri)
您应该能够加载包含前缀的规则
下面是一个小例子来测试这是否有效。它假设您的本体存储在文件系统的某个地方,并且在类路径中有一个名为
jena.rule
的规则文件:这里有一个更详细的例子:http://tutorial-academy.com/jena-reasoning-with-rules/
可以在这里找到
Rule
类的文档:https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/reasoner/rulesys/Rule.html如果有人面临这个问题,希望这能有所帮助
问候