如何获取链接到耶拿资源的资源列表? [英] How to get a list of resources linked to a resource in Jena?
本文介绍了如何获取链接到耶拿资源的资源列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Jena API创建了一个模型:
I created a Model using the Jena APIs:
public static void main(String[] args) {
Model model = ModelFactory.createDefaultModel();
Resource alice = ResourceFactory.createResource("http://example.org/alice");
Resource bob = ResourceFactory.createResource("http://example.org/bob");
Resource charlie = ResourceFactory.createResource("http://example.org/charlie");
model.add (alice, RDF.type, FOAF.Person);
model.add (alice, FOAF.name, "Alice");
model.add (alice, FOAF.mbox, ResourceFactory.createResource("mailto:alice@example.org"));
model.add (alice, FOAF.knows, bob);
model.add (alice, FOAF.knows, charlie);
model.write(System.out, "RDF/XML-ABBREV");
}
该程序的输出为:
<rdf:RDF xmlns:rdf="w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="xmlns.com/foaf/0.1/">
<j.0:Person rdf:about="example.org/alice">
<j.0:knows rdf:resource="example.org/charlie"/>
<j.0:knows rdf:resource="example.org/bob"/>
<j.0:mbox rdf:resource="mailto:alice@example.org"/>
<j.0:name>Alice</j.0:name>
</j.0:Person>
</rdf:RDF>
现在如何获取链接到特定资源的资源列表?
Now how to get a list of resources linked to a certain resource?
例如:爱丽丝认识鲍勃和查理.爱丽丝,鲍勃和查理是资源,资源爱丽丝知道其他两个资源.现在如何获取名字[Bob,Charlie]?
For example: Alice knows Bob and Charlie. Alice, Bob and Charlie are resources, Resource Alice knows the other two resources. Now how to get the names [Bob, Charlie]?
推荐答案
Jena's Java API documentation shows there is a method just for that:
NodeIterator listObjectsOfProperty(Resource s, Property p)
在您的示例中应该可以使用:
This should work in your example:
NodeIterator friends = model.listObjectsOfProperty(alice, FOAF.knows);
然后您可以遍历friends
与每个朋友(或熟人)一起做某事.
You can then iterate over friends
to do something with each friend (or acquaintance).
这篇关于如何获取链接到耶拿资源的资源列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文