1

hasAuthor我在我的本体中定义了一个多值对象属性literature。有一个人book-1hasAuthorwriter-1writer-2。如果我想获得 的作者book-1,我可以写类似

Resource r;  // r represents the individual book-1
r.getRequiredProperty(literature.hasAuthor).getObject().toString();

或者

r.getPropertyResourceValue(literature.hasAuthor).toString();

但它们都只返回第一个值writer-1,并被writer-2忽略。

我应该如何修改我的代码以获取所有作者?

4

2 回答 2

2

通常,get* 操作获取单个项目,而 list* 返回多个事物的迭代器。

使用 .listProperties(property) -> StmtIterator。

于 2013-05-28T08:26:07.337 回答
2

JenaResource有一个方法listProperties,您可以使用该方法迭代以资源为主题并具有给定属性的语句。这是一个描述RDF Primer及其两个编辑器的示例(在此示例中称为作者,以便与您的示例保持一致)。

public class MultipleProperties {
  public static void main(String[] args) {
    String ns = "http://www.example.com/";
    Model model = ModelFactory.createDefaultModel();
    model.setNsPrefix( "", ns );
    Property hasAuthor = model.createProperty( ns + "hasAuthor" );

    Resource rdfPrimer = model.createResource( "http://www.w3.org/TR/rdf-primer/" );
    Resource fm = model.createResource( ns + "FrankManola" );
    Resource em = model.createResource( ns + "EricMiller" );

    rdfPrimer.addProperty( hasAuthor, fm );
    rdfPrimer.addProperty( hasAuthor, em );

    System.out.println( "== The Model ==" );
    model.write( System.out, "N3" );

    System.out.println( "\n== The Properties ==" );
    StmtIterator it = rdfPrimer.listProperties( hasAuthor );
    while( it.hasNext() ) {
      Statement stmt = it.nextStatement();
      System.out.println( " * "+stmt.getObject() );
      System.out.println( "   * "+stmt );
    }
  }
}

输出:

== The Model ==
@prefix :        <http://www.example.com/> .

<http://www.w3.org/TR/rdf-primer/>
      :hasAuthor :EricMiller , :FrankManola .

== The Properties ==
 * http://www.example.com/EricMiller
   * [http://www.w3.org/TR/rdf-primer/, http://www.example.com/hasAuthor, http://www.example.com/EricMiller]
 * http://www.example.com/FrankManola
   * [http://www.w3.org/TR/rdf-primer/, http://www.example.com/hasAuthor, http://www.example.com/FrankManola]
于 2013-05-28T15:32:40.537 回答