jena 操作RDF的一个例子
RDF 越来越被认为是表示和处理半结构化数据的一种极好选择。本文中,Web 开发人员 Philip McCarthy 向您展示了如何使用 Jena Semantic Web Toolkit,以便在 Java 应用程序中使用 RDF 数据模型。
“资源描述框架(Resource Description Framework,RDF)”最近成为 W3C 推荐标准,与 XML 和 SOAP 等 Web 标准并排。
创建简单的 RDF 模型
我们从基本操作开始:从头创建模型并向其添加 RDF 语句。本节,我将说明如何创建描述一组虚构家庭成员之间关系的模型,如图 1 中所示:
将使用来自“关系”词汇表(请参阅 参考资料 )的属性 siblingOf
、 spouseOf
、 parentOf
和 childOf
来描述不同的关系类型。为简单起见,家庭成员用来自虚构名称空间的 URI( http://family/
)进行标识。词汇表 URI 通常以 Jena 代码形式使用,所以将它们声明为 Java 常量会非常有用,减少了错误输入。
Jena 的 ModelFactory
类是创建不同类型模型的首选方式。在这种情况下,您想要空的、内存模型,所以要调用的方法是 ModelFactory.createDefaultModel()
。这种方法返回 Model
实例,您将使用它创建表示家庭中每个成员的 Resource
。创建了资源后,可以编写关于这些资源的语句并添加到模型中。
在 Jena 中,语句的主题永远是 Resource
,谓词由 Property
表示,对象是另一个 Resource
或常量值。常量在 Jena 中通过 Literal
类型表示。所有这些类型共享公共接口 RDFNode
。将需要四个不同的 Property
实例表示家庭树中的关系。这些实例使用 Model.createProperty()
创建。
将语句添加到模型中的最简单方法是通过调用 Resource.addProperty()
。此方法以 Resource
作为主题在模型中创建语句。该方法使用两个参数,表示语句谓词的 Property
和语句的对象。 addProperty()
方法被过载:一个过载使用 RDFNode
作为对象,所以可以使用 Resource
或 Literal
。还有有益过载,它们使用由 Java 原语或 String
表示的常量。在示例中,语句的对象是表示其他家庭成员的 Resource
。
通过使用三元组的主题、谓词和对象调用 Model.createStatement()
,还可以直接在模型上创建语句。注意以此种方式创建 Statement
不将其添加到模型中。如果想将其添加到模型中,请使用创建的 Statement
调用 Model.add()
下面给出一些源代码:
package com.jena.ibm;
import java.util.*;
import com.hp.hpl.jena.rdf.model.*;
public class FamilyModel {
// Namespace declarations
static final String familyUri = "http://family/ ";
static final String relationshipUri = "http://family.org/relationship/ ";
// Jena model representing the family
private Model model;
public FamilyModel() {
// Create an empty Model
model = ModelFactory.createDefaultModel();
}
public void test()
{
// Create the types of Property we need to describe relationships
// in the model
Property childOf = model.createProperty(relationshipUri,"childOf");
Property parentOf = model.createProperty(relationshipUri,"parentOf");
Property siblingOf = model.createProperty(relationshipUri,"siblingOf");
Property spouseOf = model.createProperty(relationshipUri,"spouseOf");
// Create resources representing the people in our model
Resource adam = model.createResource(familyUri+"adam");
Resource beth = model.createResource(familyUri+"beth");
Resource chuck = model.createResource(familyUri+"chuck");
Resource dotty = model.createResource(familyUri+"dotty");
Resource edward = model.createResource(familyUri+"edward");
Resource fran = model.createResource(familyUri+"fran");
Resource greg = model.createResource(familyUri+"greg");
Resource harriet = model.createResource(familyUri+"harriet");
// Add properties to describing the relationships between them
adam.addProperty(siblingOf,beth);
adam.addProperty(spouseOf,dotty);
adam.addProperty(parentOf,edward);
adam.addProperty(parentOf,fran);
beth.addProperty(siblingOf,adam);
beth.addProperty(spouseOf,chuck);
chuck.addProperty(spouseOf,beth);
dotty.addProperty(spouseOf,adam);
dotty.addProperty(parentOf,edward);
dotty.addProperty(parentOf,fran);
// Statements can also be directly created ...
Statement statement1 = model.createStatement(edward,childOf,adam);
Statement statement2 = model.createStatement(edward,childOf,dotty);
Statement statement3 = model.createStatement(edward,siblingOf,fran);
// ... then added to the model:
model.add(statement1);
model.add(statement2);
model.add(statement3);
// Arrays of Statements can also be added to a Model:
Statement statements[] = new Statement[5];
statements[0] = model.createStatement(fran,childOf,adam);
statements[1] = model.createStatement(fran,childOf,dotty);
statements[2] = model.createStatement(fran,siblingOf,edward);
statements[3] = model.createStatement(fran,spouseOf,greg);
statements[4] = model.createStatement(fran,parentOf,harriet);
model.add(statements);
// A List of Statements can also be added
List list = new ArrayList();
list.add(model.createStatement(greg,spouseOf,fran));
list.add(model.createStatement(greg,parentOf,harriet));
list.add(model.createStatement(harriet,childOf,fran));
list.add(model.createStatement(harriet,childOf,greg));
model.add(list);
System.out.println("------------------");
// List everyone in the model who has a child:
ResIterator parents = model.listSubjectsWithProperty(parentOf);
// Because subjects of statements are Resources, the method returned a ResIterator
while (parents.hasNext()) {
// ResIterator has a typed nextResource() method
Resource person = parents.nextResource();
// Print the URI of the resource
System.out.println(person.getURI()); }
// Can also find all the parents by getting the objects of all "childOf" statements
// Objects of statements could be Resources or literals, so the Iterator returned
// contains RDFNodes
NodeIterator moreParents = model.listObjectsOfProperty(childOf);
while(moreParents.hasNext())
{
RDFNode r=moreParents.nextNode();
if(r instanceof Resource){
System.out.println(r);
}
//literal
else System.out.println(r);
}
// To find all the siblings of a specific person, the model itself can be queried
NodeIterator siblings = model.listObjectsOfProperty(edward, siblingOf);
// But it's more elegant to ask the Resource directly
// This method yields an iterator over Statements
StmtIterator moreSiblings = edward.listProperties(siblingOf);
System.out.println("------------------");
model.listStatements(adam,spouseOf,dotty);
// Find all statements with adam as the subject and dotty as the object
model.listStatements(adam,null,dotty);
// Find any statements made about adam
model.listStatements(adam,null,(RDFNode)null);
// Find any statement with the siblingOf property
StmtIterator stmtIter= model.listStatements(null,siblingOf,(RDFNode)null);
while(stmtIter.hasNext())
{
Statement stmt=stmtIter.nextStatement();
System.out.println(stmt);
}
}
public static void main(String args[]) {
// Create a model representing the family
FamilyModel theFamily = new FamilyModel();
theFamily.test();
//System.out.println(theFamily.model);
}
}
参考:http://www.ibm.com/developerworks/cn/java/j-jena/#resources