在没有sparql的情况下检索主题的对象和谓词

时间:2015-07-23 21:02:25

标签: rdf jena triples

我使用getSubject()通过模型检索了主题,对于这个主题,我想为它们各自的对象和谓词创建一个关系。如何通过jena和没有sparql检索特定主题的对象和谓词?

1 个答案:

答案 0 :(得分:0)

获取给定模型m的特定主题的所有谓词和对象:

// The resource you already had:
Resource subject; // = m.getResource(NAMESPACE + "subject");

// This creates a 'list' (iterator) over all the satements containing your subject
StmtIterator stmtIterator = m.listStatements(subject, null, (RDFNode) null);

// While you have not processed all these statements:
while (stmtIterator.hasNext()){

     // Grab the next statement
     Statement s = stmtIterator.next();

     // Retrieve the predicate(property) and the object from the statement
     Property predicate = s.getPredicate();
     Resource object = s.getObject();

     // Do something with your predicate
     // Do something with your object
}

但是,如果您的意思是想要从模型中获取谓词和主题,则将其添加到您检索到的主题中:

Property property = m.getProperty(NAMESPACE + "propertyName");
Resource object = m.getResource(NAMESPACE + "objectName");
subject.addProperty(property, object);