解释耶拿的推论 [英] Explain inference in Jena

查看:105
本文介绍了解释耶拿的推论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在耶拿,我使用InfModel类创建了RDFS推理模型:

In Jena, I have created an RDFS inference model using InfModel class:

InfModel infmodel = ModelFactory.createRDFSModel(schema, data);

给定infmodel的推断语句,我们如何获得用于推断该语句的两个语句,类似于Protégé中的解释推断"选项?例如,如果infModel包含语句:a rdf:type :t,我们可能会得到两个用于推断它的语句,例如:a :p :b:p rdfs:domain :t.

Given an inferred statement from infmodel, how do we get the two statements that were used to infer it, similar to the the "explain inference" option in Protégé? For instance, if infModel contains the statement :a rdf:type :t, we might get two statements used to infer it, e.g., :a :p :b and :p rdfs:domain :t.

推荐答案

根据文档(并使用Jena 2.11.1进行测试),您可以访问 RuleDerivation 对象,这些对象公开了有关内部状态的更多信息.

According to the documentation (and testing with Jena 2.11.1) you can get access to a Derivation object which will allow you to create a textual description of what happened. In the following example, we retrieve RuleDerivation objects that expose a little bit more regarding the internal state.

以下是从以下模型开始的文档示例的经过测试的实现:

The following is a tested implementation of the documentation example which begins with the following model:

<urn:eg:C>  <urn:eg:p>  <urn:eg:D> .
<urn:eg:B>  <urn:eg:p>  <urn:eg:C> .
<urn:eg:A>  <urn:eg:p>  <urn:eg:B> .

...以及以下规则:

... and the following rule:

[rule1: (?a urn:eg:p ?b) (?b urn:eg:p ?c) -> (?a urn:eg:p ?c)]

...生成此结果模型:

... to produce this resulting model:

<urn:eg:B>  <urn:eg:p>  <urn:eg:D> , <urn:eg:C> .
<urn:eg:A>  <urn:eg:p>  <urn:eg:D> , <urn:eg:C> , <urn:eg:B> .
<urn:eg:C>  <urn:eg:p>  <urn:eg:D> .

这个基本的传递推理成为下面示例的核心方面.请注意,我们获得了RuleDerivation的实例,这是您达到最终目标的起点.

This basic transitive inference becomes the core aspect of the example to follow. Note that we obtain an instance of RuleDerivation which is a start towards your end goal.

final Resource A = ResourceFactory.createResource("urn:eg:A");
final Resource B = ResourceFactory.createResource("urn:eg:B");
final Resource C = ResourceFactory.createResource("urn:eg:C");
final Resource D = ResourceFactory.createResource("urn:eg:D");
final Property p = ResourceFactory.createProperty("urn:eg:p");

final Model rawData = ModelFactory.createDefaultModel();
rawData.add(A, p, B);
rawData.add(B, p, C);
rawData.add(C, p, D);

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

final 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 (final Iterator<Derivation> id = inf.getDerivation(s); id.hasNext(); ) {
        final RuleDerivation deriv = (RuleDerivation) id.next();
        deriv.printTrace(out, true);
    }
}
out.flush();

此示例的输出是:

Statement is [urn:eg:A, urn:eg:p, urn:eg:D]
Rule rule1 concluded (urn:eg:A urn:eg:p urn:eg:D) <-
    Rule rule1 concluded (urn:eg:A urn:eg:p urn:eg:C) <-
        Fact (urn:eg:A urn:eg:p urn:eg:B)
        Fact (urn:eg:B urn:eg:p urn:eg:C)
    Fact (urn:eg:C urn:eg:p urn:eg:D)

编辑-提示

查看

Check out the internals of RuleDerivation#printTrace(...) if you are looking for an example about how to explore derivations. If you want to convert a triple (from RuleDerivation#getMatches()) back to a statement, use StatementImpl#toStaetment(Triple,ModelCom).

EDIT2-完成假设您使用的是Jena内置的基于规则的推理机之一,则以下代码将允许您探索推理机报告的特定推导的匹配项.

EDIT2 - Done Assuming that you are using one of Jena's built in rule-based reasoners, the following code will allow you to explore the matches for one particular derivation reported by a reasoner.

final StmtIterator input = inf.listStatements(A, p, D);
assert( input.hasNext() );

final Iterator<Derivation> derivations = inf.getDerivation(input.next());
assert( null != derivations );
assert( derivations.hasNext() );

final RuleDerivation oneDerivation = (RuleDerivation) derivations.next();
final ExtendedIterator< Statement > matches = 
        new NiceIterator< Triple >()
        .andThen( oneDerivation.getMatches().iterator())
        .mapWith( new Map1< Triple, Statement >(){
            @Override
            public Statement map1( final Triple t )
            {
                /* Note that it seems that this model doesn't really mean anything. While
                 * the statement will be associated with the infModel, the triple that led
                 * to the match could have been from either the deductions graph or the
                 * raw graph. This does not actually add any triples to the underlying
                 * store.
                 */
                return StatementImpl.toStatement(t, (ModelCom)inf);
            }});

这篇关于解释耶拿的推论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆