更早地打印InfModel会在以后更改打印的表示形式吗? [英] printing InfModel earlier changes printed representation later?

查看:92
本文介绍了更早地打印InfModel会在以后更改打印的表示形式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jena 2.5.5(在Linux上)中,我正在使用推理API产生奇怪的效果.以下代码是精简版本.我正在创建一个最初为空的Model和一个通用规则推理器.我为某条语句添加了自反规则.我将推理程序附加到模型上以获得InfModel.然后,我创建匹配的语句并将其添加到模型中.

结果:InfModel包含该语句及其反向语句.到目前为止,这就是应该做的事情.

现在,当我在将匹配语句添加到Model之前System.out.println() InfModel时,结果是完全不同的:该规则似乎没有触发,因此,InfModel最终将不包含原始语句的相反内容./p>

将模型写入控制台如何改变代码的功能?是否记录了这种行为?

import java.util.*;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.rulesys.*;

/**
 * Describe class <code>RuleTest</code> here.
 */
public class RuleTest {
    public static void main(String[] args) throws Exception {
        /* create model */
        Model model = ModelFactory.createDefaultModel();

        /* output model */
        System.out.println("original model : " + model);
        System.out.println("-----");

        /* collect rules */
        List<Rule> rules = new ArrayList<Rule>();
        Rule rule = Rule.parseRule("[ (subject predicate object) -> (object predicate subject) ].");
        rules.add(rule);

        /* create rule reasoner */
        GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);

        /* attach reasoner to model */
        InfModel infModel = ModelFactory.createInfModel(reasoner, model);

        /* output model */
        //-----------------------------------------------//
        // commenting the following line in/out changes  //
        // the output of (*) below in Jena 2.5.5 ?!?!?!  //
        //-----------------------------------------------//
        //System.out.println("inference model: " + infModel);        
        System.out.println("=====");

        /* add facts to original model */
        Resource s = model.createResource("subject");
        Property p = model.createProperty("predicate");
        RDFNode  o = model.createResource("object");
        Statement stmt = model.createStatement(s, p, o);
        model.add(stmt);

        /* output models */
        System.out.println("original model : " + model);
        System.out.println("-----");
        System.out.println("inference model: " + infModel); // (*)
    }
}

解决方案

快速查看相关来源,看来您有两个选择:

  • 如果要对基本 model 进行更改,然后确保将其传播到 infModel ,则必须调用 infModel .rebind()进行更改之后,然后再询问" infModel 任何内容.

  • 您可以直接使用 infModel (而不是 model )来创建和添加语句的元素以及语句本身.

我知道这不能直接回答您的问题,但是可以解决您的问题(顺便说一句,这似乎是由 ModelCom - InfModel 的父类.

I'm getting a strange effect in Jena 2.5.5 (on Linux) where I am playing around with the inference API. The following code is a stripped down version. I am creating an initially empty Model and a generic rule reasoner. I add a reflexivity rule for a certain statement. I attach the reasoner to the model to get an InfModel. Then I create the matching statement and add it to the Model.

Result: InfModel contains both the statement and its reverse. So far so good, that's what it's supposed to do.

Now, when I System.out.println() the InfModel prior to adding the matching statement to Model, the result is completely different: the rule seems not to fire and thus, InfModel will end up not containing the reverse of the original statement.

How does writing the model to the console change the functionality of the code? Is this behavior documented?

import java.util.*;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.rulesys.*;

/**
 * Describe class <code>RuleTest</code> here.
 */
public class RuleTest {
    public static void main(String[] args) throws Exception {
        /* create model */
        Model model = ModelFactory.createDefaultModel();

        /* output model */
        System.out.println("original model : " + model);
        System.out.println("-----");

        /* collect rules */
        List<Rule> rules = new ArrayList<Rule>();
        Rule rule = Rule.parseRule("[ (subject predicate object) -> (object predicate subject) ].");
        rules.add(rule);

        /* create rule reasoner */
        GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);

        /* attach reasoner to model */
        InfModel infModel = ModelFactory.createInfModel(reasoner, model);

        /* output model */
        //-----------------------------------------------//
        // commenting the following line in/out changes  //
        // the output of (*) below in Jena 2.5.5 ?!?!?!  //
        //-----------------------------------------------//
        //System.out.println("inference model: " + infModel);        
        System.out.println("=====");

        /* add facts to original model */
        Resource s = model.createResource("subject");
        Property p = model.createProperty("predicate");
        RDFNode  o = model.createResource("object");
        Statement stmt = model.createStatement(s, p, o);
        model.add(stmt);

        /* output models */
        System.out.println("original model : " + model);
        System.out.println("-----");
        System.out.println("inference model: " + infModel); // (*)
    }
}

解决方案

Had a quick look at the relevant source and it appears that you have two options:

  • If you want to make changes to the base model and then be sure that they propagate to the infModel, then you have to call infModel.rebind() after having made the changes and before you "ask" the infModel anything.

  • You can use the infModel directly (instead of model) to create and add the statement's elements and the statement itself.

I know this does not directly answer your question, but it may solve your problem (which, btw, seems to be caused by reification triggered by the toString() method in ModelCom - the parent class of InfModel).

这篇关于更早地打印InfModel会在以后更改打印的表示形式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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