- 浏览: 59934 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
pkmu8426:
楼主 您好小弟初学Jena 发觉网上文件实在是太少正好看到您这 ...
本体推理的一个例子 -
hadesmile:
确实是个问题,不过一般不这样用,
奇怪的notify()与notifyAll()的执行问题 -
yydcj:
hadesmile 写道10个reduce线程虽然是顺序实例化 ...
一个notify()的实例分析 -
hadesmile:
10个reduce线程虽然是顺序实例化的,但是他们并不是顺序拿 ...
一个notify()的实例分析 -
biganer2008:
你好,按着你的步骤安装后,OWLVizTab标签空白啊,不知道 ...
在 Protege中安装owl-s editor、graphviz插件
jena 操作RDF的一个例子
RDF 越来越被认为是表示和处理半结构化数据的一种极好选择。本文中,Web 开发人员 Philip McCarthy 向您展示了如何使用 Jena Semantic Web Toolkit,以便在 Java 应用程序中使用 RDF 数据模型。
“资源描述框架(Resource Description Framework,RDF)”最近成为 W3C 推荐标准,与 XML 和 SOAP 等 Web 标准并排。
我们从基本操作开始:从头创建模型并向其添加 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
发表评论
-
DAML-S简介
2010-08-02 11:30 850引自:http://hi.baidu.com/fe ... -
OWL-S API指南
2010-07-15 16:13 1202引自http://yoancn.blogbus.com/log ... -
在 Protege中安装owl-s editor、graphviz插件
2010-07-15 15:59 29091.软件安装: 双击install_protege.exe安 ... -
语义Web服务的API使用实例:OWL-S API结合matchmaker、推理机(Jena、Pellet)进行语义转换、匹配、组合及调用web服务
2010-07-15 15:58 1648引自http://www.cnblogs.com/armyme ... -
Jena 环境的配置
2010-07-15 15:55 1161引自 http://www.cnblogs.com/armym ... -
基于OWL-S的语义Web服务发现组装的常用工具
2010-07-15 15:54 1180Protege + OWL-S Editor + ... -
使用jena持久化OWL本体到MySQL
2010-07-15 15:54 1365引自http://www.cnblogs.com/ar ... -
语义Web Service检索服务的工具下载
2010-07-15 15:52 847Eclipse下载: http://www.eclipse.o ... -
本体推理的一个例子
2010-07-15 15:19 2461引自http://hi.baidu.com/wxmsona/b ... -
家族族谱推理规则文件
2010-07-15 15:19 654引自http://hi.baidu.com/wxmsona/b ... -
家族族谱owl文件
2010-07-15 15:17 1014引自http://hi.baidu.com/wxmsona/b ... -
RDF数据查询语言SPARQL
2010-07-15 15:16 1549数据源,一个RDF文件,就是帮助文档中的vc-db-1.rdf ... -
jena持久化到数据库
2010-07-15 15:15 1084Jena 由 HP Labs (http://www.hp ... -
用 SPARQL 搜索 RDF 数据
2010-07-15 15:14 1670随着越来越多的数据使用类似 RSS 的 RDF 格式保存, ... -
Jena的一个例子
2010-07-15 15:12 1162摘自:http://www.crabone.com/index ... -
本体(Ontology)综述
2010-07-15 15:10 999本体作为一种能在语义和知识层次 上描述领域概念的建模工具,其 ... -
语义Web和本体开发相关技术
2010-07-15 15:09 1289摘自:http://iMarine.blog.163.com/ ... -
owl-s API一个简单例子
2010-07-15 15:09 1137owl-s语义Web服务标记语言, 在Tim Berners- ... -
语义Web
2010-07-15 14:48 833语 义Web概念: 1998 ... -
语义Web中语言与本体
2010-07-15 14:43 803随着人们对网络上信 ...
相关推荐
jena RDF API入门主要是学习jena api在RDF应用中的一个帮助文档,里面含有一些典型例子,可以帮助大家入门
使用 Jena API 处理 RDF txt
Jena_RDF_API的译文Jena_RDF_API的译文
《An Introduction to RDF and the Jena RDF API》的译文
An Introduction to RDF and the Jena RDF API是外国人编写的一个有助于jena RDF的帮助文档,并提供了一些典型例子,可供参考练习
提供了一个简单的例子,利于jena的初学者使用
Jena文档《An Introduction to RDF and the Jena RDF API》的译文 RDF和Jena RDF API入门
中文关系搜索:关系搜索(Relationship Search)是一种基于...不过估计不可能,因为关系搜索注定只能够成为名人的专利,自有他们才能够成为大众关注的焦点,大量的重名让准确挖掘普通人关系成为一个不可能完成的人物。
JENA教程(包括生成RDF和写本体到MySQL中,很适合入门,很详细)JENA教程(包括生成RDF和写本体到MySQL中,很适合入门,很详细)JENA教程(包括生成RDF和写本体到MySQL中,很适合入门,很详细)JENA教程(包括生成RDF和写本体...
新手,找到一个jena查询owl的例子供大家分享!
jena的RDF开发API,内含很多实例,英文版
protege学习三部曲 推理篇 入门篇 提高篇 对Jena的简单理解和一个例子
用jena写的一个家庭族谱的小例子 可以练手
基于Jena的分布式RDF数据库实现,可自由伸缩,三元组
文档详细介绍了RDF以及提供一种验证器, 利用Jena搭建了SPARQL对RDF数据进行查询等一些列操作。
此外Jena还提供了非常实用的命令行工具(例如SPARQL查询、批量导入RDF数据等)和一个基于Jena框架构建的Web应用程序Fuseki。 学习Jena框架,一定要结合对于的语义技术的知识,这样才能更加快速地上手。