![](/img/trans.png)
[英]com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph cannot be cast to org.mindswap.pellet.jena.PelletInfGraph
[英]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.