【问题标题】:Iterating multiple reasoned literals from the same property从同一属性迭代多个推理文字
【发布时间】:2018-01-12 03:08:33
【问题描述】:

标题可能有点混乱,但基本上这就是问题所在:我正在使用 JenaPellet reasoner 从名为 的资源中生成属性文字Patient_Doug。三元组看起来像这样: Patient_Doug-> hasSuggestion-> Literal 推断建议。

问题在于,Protege Pellet reasonerDoug 提出了三个建议,因为 Doug 在医院的情况非常糟糕. Protege reasoner 建议 Doug 需要一个 Hi-Lo 床、一个 RF ID 波段 和一个 靠近护士站的床。不幸的是,在 Jena,我只能得到 Hi-lo bed 来打印。 3 种文字中的一种。

这里是一些代码。

    OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC );

    String ns = "http://altervista.org/owl/unit.owl#";
    String inputFile = "c:\\jena\\acuity.owl";  
    InputStream in = FileManager.get().open(inputFile);
    if (in == null) {
        throw new IllegalArgumentException("File: " + inputFile + " not found");
    }
    model.read(in,"");

    model.prepare();

    //inf and reasoner wont run unless i use hp libraries!

    //asserted data properties
    Individual ind = model.getIndividual(ns+"Patient_Doug");
    OntProperty abcValue = model.getOntProperty("http://example.org/hasABCValue");


    //inferred data properties
    OntProperty suggestion = model.getOntProperty(ns+"hasSuggestion");

    //print asserted data properties
    System.out.println("Properties for patient "+ind.getLocalName().toString());
    System.out.println( abcValue.getLocalName()+"= "+ind.getPropertyValue(abcValue).asLiteral().getInt());

    //print inferenced data properties      
    StmtIterator it = ind.listProperties(suggestion);

     //this iterator only prints one suggestion in an infinite loop
    while (it.hasNext()) {
        System.out.println("A posible suggestion= "+ind.getPropertyValue(suggestion).asLiteral().getString());
    }

    }

代码运行良好,但最后的迭代器仅在无限循环中打印一个子集

如有任何建议,我将不胜感激。 谢谢。

【问题讨论】:

  • 好吧,你应该再读一遍“如何使用迭代器” ...it.hasNext() 只是返回是否有“下一个”条目进行迭代。 it.next() 将接受下一个条目。你从来没有“使用”过迭代器的值......现在轮到你了......
  • 感谢 AKSW,我总是可以指望您的启发性答复。
  • 请在找到解决问题的方法后添加答案。可能也会对其他人有所帮助(尽管在 Stackoverflow 上肯定有关于“如何在 Java 中使用迭代器”的重复)
  • 我想我必须在颗粒推理时进行迭代。所以,我正在寻找像“listInferredPropertyValues”这样的方法。 Pellet 正在为具有挑战性的 SAME PROPERTY 推理三个不同的值。我将考虑将推理器放入带有迭代器的 TRY 中。我仍然需要迭代器中的 next() 。我会弄清楚并在这里发布。
  • 不确定我是否理解,但您应该使用ind.listPropertyValues(suggestion) 而不是ind.listPropertyValues(suggestion) 来获取值(而不是整个语句)。然后只需遍历这些值。这是微不足道的 Java 基础知识。不知道你认为这里有什么复杂的。顺便说一句,推理是隐式进行的,因为您使用的是OntModel。不需要一些额外的方法。

标签: jena reasoning pellet


【解决方案1】:

此代码用于迭代和打印许多推断的 hasSuggestions。 hasSuggestion SWRL 规则在 OWL 本体中

    OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC );

String ns = "http://altervista.org/owl/unit.owl#";
String inputFile = "c:\\jena\\acuity.owl";  
InputStream in = FileManager.get().open(inputFile);
if (in == null) {
    throw new IllegalArgumentException("File: " + inputFile + " not found");
}
model.read(in,"");

model.prepare();

//inf and reasoner wont run unless i use hp libraries!

//asserted data properties
Individual ind = model.getIndividual(ns+"Patient_Doug");
OntProperty abcValue = model.getOntProperty("http://example.org/hasABCValue");


//inferred data properties
OntProperty suggestion = model.getOntProperty(ns+"hasSuggestion");

//print asserted data properties
System.out.println("Properties for patient "+ind.getLocalName().toString());
System.out.println( abcValue.getLocalName()+"= "+ind.getPropertyValue(abcValue).asLiteral().getInt());

    for (StmtIterator j = ind.listProperties(suggestion); j.hasNext(); ) {
            Statement s = j.next();
            //System.out.println( "    " + s.getPredicate().getLocalName() + " -> " );
            System.out.println( "A possible suggestion... " + s.getLiteral().getLexicalForm());

        }

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 2023-03-17
    • 2020-10-31
    • 1970-01-01
    • 2014-03-11
    • 2015-09-20
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多