
jena 简介.doc
15页Jena 简介通过 Jena Semantic Web Framework 在 Jave 应用程序中使用 RDF 模型Philip McCarthy (phil@), 开发人员, SmartStream Technologies Ltd简介: RDF 越来越被认为是表示和处理半结构化数据的一种极好选择本文中,Web 开发人员 Philip McCarthy 向您展示了如何使用 Jena Semantic Web Toolkit,以便在 Java 应用程序中使用 RDF 数据模型本文的标签: best_practices, jena, :326445431, 应用开发, 项目标记本文!发布日期: 2004 年 7 月 01 日 级别: 初级 访问情况 : 8432 次浏览 评论: 0 (查看 | 添加评论 - 登录)平均分 (22 个评分)为本文评分“资源描述框架(Resource Description Framework,RDF)”最近成为 W3C 推荐标准,与 XML 和 SOAP 等 Web 标准并排RDF 可以应用于处理特殊输入数据(如 CRM)的领域,已经广泛用于社会网络和自助出版软件(如 LiveJournal 和 TypePad)。
Java 程序员将越来越多地得益于具有使用 RDF 模型的技能在本文中,我将带您体验惠普实验室的开放源代码 Jena Semantic Web Framework(请参阅 参考资料)的一些功能您将了解如何创建和填充 RDF 模型,如何将它们持久存储到数据库中,以及如何使用 RDQL 查询语言以程序方式查询这些模型最后,我将说明如何使用 Jena 的推理能力从本体推断模型知识本文假设您已经就图形、三元组和模式等概念方面对 RDF 比较熟悉,并对 Java 编程有基本的了解创建简单的 RDF 模型我们从基本操作开始:从头创建模型并向其添加 RDF 语句本节,我将说明如何创建描述一组虚构家庭成员之间关系的模型,如图 1 中所示:图 1. 虚拟家庭树将使用来自“关系”词汇表(请参阅 参考资料)的属性siblingOf、 spouseOf、 parentOf 和 childOf 来描述不同的关系类型为简单起见,家庭成员用来自虚构名称空间的 URI( http://family/)进行标识词汇表 URI 通常以 Jena 代码形式使用,所以将它们声明为 Java 常量会非常有用,减少了错误输入Schemagen当您通过 Jena 的 API 来使用模型时,为模型词汇表中的每个属性定义常量非常有用。
如果有词汇表的 RDF、DAML 或 OWL 表示,Jena 的 Schemagen 工具可以自动生成这些常量,使您的工作更加容易Schemagen 在命令行中运行,使用的参数包括模式或本体文件的位置、要输出的类的名称和 Java 包然后可以导出生成的 Java 类,其 Property 常量用于访问模型还可以使用 Ant 将 Schemagen 作为构建处理的一部分来运行,保持 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(),如清单 1 所示:清单 1. 创建模型来表示虚构的家庭// URI declarations String familyUri = "http://family/"; String relationshipUri = "http://purl.org/vocab/relationship/"; // Create an empty Model Model model = ModelFactory.createDefaultModel(); // Create a Resource for each family member, identified by their URI Resource adam = model.createResource(familyUri+"adam"); Resource beth = model.createResource(familyUri+"beth"); Resource chuck = model.createResource(familyUri+"chuck"); Resource dotty = model.createResource(familyUri+"dotty"); // and so on for other family members // Create properties for the different types of relationship to represent Property childOf = model.createProperty(relationshipUri,"childOf"); Property parentOf = model.createProperty(relationshipUri,"parentOf"); Property siblingOf = model.createProperty(relationshipUri,"siblingOf"); Property spouseOf = model.createProperty(relationshipUri,"spouseOf"); // Add properties to adam describing relationships to other family members adam.addProperty(siblingOf,beth); adam.addProperty(spouseOf,dotty); adam.addProperty(parentOf,edward); // Can also create statements directly .. . Statement statement = model.createStatement(adam,parentOf,fran); // but remember to add the created statement to the model model.add(statement); 整个代码示例 FamilyModel.java 还说明了语句批量如何一次添加到模型中,或者作为一个数组或者作为 java.util.List。
构建了家庭模型后,我们看一下如何使用 Jena 的查询 API 从模型中提取信息回页首查询 RDF 模型程序化地查询 Jena 模型主要通过 list()方法在 Model 和 Resource 接口中执行可以使用这些方法获得满足特定条件的主题、对象和 Statement它们还返回java.util.Iterator 的特殊化,其具有返回特定对象类型的其他方法我们返回 清单 1 的家庭模型,看一下可以查询它的不同方法,如清单 2 所示:清单 2. 查询家庭模型// 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); // 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); 最通用的查询方法是 Model.listStatements(Resource s, Property p, RDFNode o),下面说明的便利方法都是以其为基础。
所有这些参数都可以保留为null,在这种情况下,它们作为通配符,与任何数据都匹配清单 3 中显示了Model.listStatements()的一些使用示例:清单 3. 使用选择器查询模型// Find the exact statement "adam is a spouse of dotty" 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 。
