2

Jena で、InfModel クラスを使用して RDFS 推論モデルを作成しました。

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

infmodel から推論されたステートメントが与えられた場合、Protégé の「推論の説明」オプションと同様に、それを推論するために使用された 2 つのステートメントを取得するにはどうすればよいでしょうか? たとえばinfModel、ステートメント が含まれている場合、:a rdf:type :tそれを推論するために使用される 2 つのステートメント (例: と ) が得られる可能性が:a :p :b あり:p rdfs:domain :tます。

4

1 に答える 1

2

ドキュメント(および Jena 2.11.1 でのテスト) によるとDerivation、何が起こったのかをテキストで説明できるオブジェクトにアクセスできます。RuleDerivation次の例では、内部状態に関してもう少し公開するオブジェクトを取得します。

以下は、次のモデルで始まるドキュメントの例のテスト済みの実装です。

<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> .

...そして次のルール:

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

... この結果のモデルを生成するには:

<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最終目標への出発点となるインスタンスを取得することに注意してください。

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)

編集 - ヒント

RuleDerivation#printTrace(...)派生を調べる方法の例を探している場合は、の内部を確認してください。RuleDerivation#getMatches()トリプル ( から) をステートメントに変換したい場合は、 を使用しますStatementImpl#toStaetment(Triple,ModelCom)

EDIT2 - 完了Jena のビルトイン ルールベース推論の 1 つを使用していると仮定すると、次のコードにより、推論によって報告された 1 つの特定の派生の一致を調べることができます。

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);
            }});
于 2014-04-21T12:47:49.557 に答える