我在本体literature中定义了一个多值对象属性hasAuthor。有一个单独的book-1,它的hasAuthor是writer-1和writer-2。
Resource r; // r represents the individual book-1
r.getRequiredProperty(literature.hasAuthor).getObject().toString();或
r.getPropertyResourceValue(literature.hasAuthor).toString();但它们都只返回第一个值writer-1,而忽略了writer-2。
我应该如何修改我的代码以获取所有作者?
发布于 2013-05-28 16:26:07
通常,get*操作获取单个项,list*返回多个对象的迭代器。
使用.listProperties(属性) -> StmtIterator。
发布于 2013-05-28 23:32:41
一个Jena Resource有一个listProperties方法,您可以使用它来迭代将资源作为主题并具有给定属性的语句。下面是一个描述RDF Primer及其两个编辑器(为了与您的示例一致,在本例中称为authors )的示例。
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]https://stackoverflow.com/questions/16784357
复制相似问题