0

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

4

1 回答 1

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);
于 2015-07-27T14:22:39.347 回答