Apache Jena框架的推理示例存在问题

时间:2014-07-16 16:30:31

标签: java rdf jena reasoning jena-rules

我有一个严重的问题要让任何推理器运行起来。 此外,文档中的示例:https://jena.apache.org/documentation/inference/ 在这里不起作用。 我将示例转移到单元测试中,以便更容易再现问题。

推理仅限于某种环境,如空间JDK等,或者我遇到了什么问题?

由于

这里是示例代码(作为java单元测试):

import static org.junit.Assert.assertNotNull;
import java.io.PrintWriter;
import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;

import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Derivation;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.vocabulary.RDFS;

public class ReasonerTest {

    String NS = "urn:x-hp-jena:eg/";

    // Build a trivial example data set
    Model model = ModelFactory.createDefaultModel();
    InfModel inf;

    Resource A = model.createResource(NS + "A");
    Resource B = model.createResource(NS + "B");
    Resource C = model.createResource(NS + "C");
    Resource D = model.createResource(NS + "D");

    Property p = model.createProperty(NS, "p");
    Property q = model.createProperty(NS, "q");


    @Before
    public void init() {

        // Some small examples (subProperty)
        model.add(p, RDFS.subPropertyOf, q);
        model.createResource(NS + "A").addProperty(p, "foo");

        String rules = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]";
        GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
        reasoner.setDerivationLogging(true);
        inf = ModelFactory.createInfModel(reasoner, model);

        // Derivations
        A.addProperty(p, B);
        B.addProperty(p, C);
        C.addProperty(p, D);
    }


    @Test
    public void subProperty() {
        Statement statement =  A.getProperty(q);
        System.out.println("Statement: " + statement);
        assertNotNull(statement);
    }


    @Test
    public void derivations() {
        String trace = null;
        PrintWriter out = new PrintWriter(System.out);
        for (StmtIterator i = inf.listStatements(A, p, D); i.hasNext(); ) {
            Statement s = i.nextStatement();
            System.out.println("Statement is " + s);
            for (Iterator id = inf.getDerivation(s); id.hasNext(); ) {
                Derivation deriv = (Derivation) id.next();
                deriv.printTrace(out, true);
                trace += deriv.toString();
            }
        }
        out.flush();
        assertNotNull(trace);
    }

    @Test
    public void listStatements() {
        StmtIterator stmtIterator = inf.listStatements();
        while(stmtIterator.hasNext()) {
            System.out.println(stmtIterator.nextStatement());
        }
    }
}

1 个答案:

答案 0 :(得分:5)

前缀例如:不是您认为的:

规则中的eg:前缀不会扩展到您的想法。我将规则字符串修改为

String rules = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)] [rule2: -> (<urn:ex:a> eg:foo <urn:ex:b>)]";

这样规则2将始终将三元组 urn:ex:a eg:foo urn:ex:b 插入图形中。然后,测试的输出包括:

[urn:ex:a, urn:x-hp:eg/foo, urn:ex:b]
[urn:x-hp-jena:eg/C, urn:x-hp-jena:eg/p, urn:x-hp-jena:eg/D]

第一行显示我的 rule2 插入的三元组,而第二行使用您手动输入的前缀。我们发现eg:前缀是urn:x-hp:eg/的缩写。如果您使用String NS = "urn:x-hp:eg/";相应地更改了NS字符串,那么派生测试将会通过。

您需要问正确的模型

subProperty 测试失败有两个原因。首先,它正在检查错误的模型。

您正在查看A.getProperty(q)

Statement statement =  A.getProperty(q);
System.out.println("Statement: " + statement);
assertNotNull(statement);

A是您为模型model而非模型inf创建的资源,所以当您要求A.getProperty(q)时,它实际上是在询问{{1}对于语句,所以你不会在model中看到推论。您可以使用infinModel中获取A“,以便inf查看正确的模型:

getProperty

或者,您也可以直接询问Statement statement = A.inModel(inf).getProperty(q); 是否包含inf形式的三倍:

A q <something>

或者你可以列举所有这些陈述:

inf.contains( A, q, (RDFNode) null );

您也需要RDFS推理

即使您正在查询正确的模型,您的推理模型仍然需要进行RDFS推理以及使属性p传递的自定义规则。为此,我们可以从RDFS推理器中提取规则,将规则添加到该列表的副本,然后使用新的规则列表创建自定义推理器:

StmtIterator stmts = inf.listStatements( A, q, (RDFNode) null );
assertTrue( stmts.hasNext() );
while ( stmts.hasNext() ) { 
  System.out.println( "Statement: "+stmts.next() );
}

完整的结果

这是修改后的代码,一起用于轻松复制和粘贴。所有的测试都通过了。

// Get an RDFS reasoner
GenericRuleReasoner rdfsReasoner = (GenericRuleReasoner) ReasonerRegistry.getRDFSReasoner();
// Steal its rules, and add one of our own, and create a
// reasoner with these rules
List<Rule> customRules = new ArrayList<>( rdfsReasoner.getRules() );
String customRule = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]";
customRules.add( Rule.parseRule( customRule ));
Reasoner reasoner = new GenericRuleReasoner( customRules );