Apache Jena (2) ---- An Introduction to RDF and the Jena RDF API
2012-08-22 16:46
573 查看
Introduction
资源描述框架(RDF)是描述资源的标准(W3C)。资源是我们能够想到的任何事物。在本文中,将使用RDF representation of VCARDS, 一个简单的vcard表现为RDF的形式可如图1所示,图1. A simple vcard in RDF
图中的椭圆中的John Smith,是以Uniform Resource Identifier (URI)标识的资源。
资源是有是有属性的,在图1中,只有一个属性(John Smith's 全名)。 属性在图中用弧表示,property作为该弧的标签。
Statements
Statement就是RDF三元组(主语 谓语 宾语)Jena框架的Model接口定义了listStatement()方法,该方法会返回Model中所有Statements的StmtIterator(java Iterator的子类型)。StmtIterator的nextStatement()方法返回当前statement的下一个statement。
列出指定Model的所有statements
//list the statements in the model StmtIterator iter = model.listStatements(); while(iter.hasNext()) { Statement stmt = iter.nextStatement(); //get the subject, predicate, object of this statement. Resource subject = stmt.getSubject(); Property predicate = stmt.getPredicate(); RDFNode object = stmt.getObject(); // RDFNode是Resource和Literal的父类。 }
Writing RDF
Jena提供了以XML格式读写RDF的方法。model.write(System.out); model.write(System.out, "RDF/XML-ABBREV"); model.write(System.out, "N-TRIPLE");
Reading RDF
Model model = ModelFactory.createDefaultModel(); InputStream in = FileManager.get().open(inputFileName); if (in == null) { throw new IllegalArgumentException("FIle:" + InputFileName + " not found"); } //read the RDF/XML file model.read(in, null);
Controlling Prefixes
前缀定义包括两种方式:①显式前缀定义
②隐式前缀定义
Jena RDF Packages
com.hp.hpl.jena.rdf.model是关键的RDF包,这个包囊括了RDF领域的概念:models, resources, properties, literals, statements等等,同时还包括了创建models的ModelFactory.com.hp.hpl.jena.tutorial
com.hp.hpl.jena...impl是实现类包
Navigating a Model
前面赘述的都是关于对RDF Model的创建、读和写。这章主要介绍如何访问Model中的信息。我们能够根绝给出的Resource URI 从model中检索出我们想要的Resource对象,
//Model.getResource(String uri);
// retrieve the John Smith vcard resource from the model Resource vcard = model.getResource(johnSmithURI);
如果这个对象存在,则返回该对象,反之,则创建该对象。
Resrouce接口定义了一系列用来访问Resource Properties的方法。
//Resource.getProperty(Property p); //retrieve the value of the N property Resrouce name = (Resrouce) vcard.getProperty(VCARD.N).getObject();一般地,一个statement的object可以是一个Resource或者一个Literal。因此,应用程序需要知道Resource的类型,才能准确的声明Object返回的类型。为了解决这个问题,Jena提供了不需要在编译阶段声明和类型检查的方法。因此上述代码可以简述为:
// retrieve the value of the FN property Resource name = vcard.getProperty(VCARD.N).getResource();
如果object是literal类型的,则使用代码:
// retrieve the given name property String fullName = vcard.getProperty(VCARD.FN).getString();上述的实例中,vcard resource只有一个vcard:FN 和一个vcard:N 属性。RDF允许Resource的属性有多个值。例如,Adam可以有两个昵称。
// add two nickname properties to vcard vcard.addProperty(VCARD.NICKNAME, "Smithy").addProperty(VCARD.NICKNAME, "Adman");Jena的RDF Model是由一系列的statements组成的,因此向model中添加已经存在的<subject,predicate,object>的statement是无效的。在property可能出现多次的情况下,Jena的Resource.listProperties(Property p)方法能够穷举出当前Model中所持有的Property的value。
// set up the output System.out.println("The nicknames of \"" + fullName + "\" are:"); // list the nicknames StmtIterator iter = vcard.listProperties(VCARD.NICKNAME); while (iter.hasNext()) { System.out.println(" " + iter.nextStatement() .getObject() .toString()); }
Querying a Model
Querying a Model顾名思义就是搜索一个Model。Jena Core提供的API只提供了有限的查询方法。(更全面的查询可查看 RDQL查询语言)
Jena中查询Model的关键接口为selector,当前Jean版本下,com.hp.hpl.jena.rdf.model包中的类SimpleSelector实现了该接口。 SimpleSelector有三个参数:
Selector selector = new SimpleSelector(subject, predicate, object)上述代码将会返回所有subject,predicate,object与给定参数都匹配的所有statements.
Selector selector = new SimpleSelector(null, null, null);上述代码将会返回model中所有的statements。
Selector selector = new SimpleSelector(null, VCARD.FN, null);上述代码将会返回Predicate为VCARD.FN的statements.
listStatement (S, P, O) is equivalent to listStatements(new SimpleSelector(S, P, O))
Operations on Models
相关文章推荐
- An Introduction to RDF and the Jena RDF API
- Jena文档《An Introduction to RDF and the Jena RDF API》的译文
- Jena文档《An Introduction to RDF and the Jena RDF API》的译文
- Jena文档《An Introduction to RDF and the Jena RDF API》的译文
- An Introduction to RDF and the Jena RDF API
- The Essential Guide to User Interface Design: An Introduction to GUI Design Principles and Technique
- An Introduction to the Java Logging API (part)
- An Introduction to the JAIN SIP API
- C#数据结构和算法[An Introduction to Collections, Generics, and the Timing Class]
- C#数据结构和算法[An Introduction to Collections, Generics, and the Timing Class]
- Game Sound: An Introduction to the History, Theory, and Practice of Video Game Music and Sound Desig
- An Introduction to Wavelets and the Haar Transform
- Understanding Windows CardSpace: An Introduction to the Concepts and Challenges of Digital Identitie
- Minds and Computers: An Introduction to the Philosophy of Artificial Intelligence
- An Introduction to the Internet Explorer Developer Toolbar
- Introduction to Languages and the Theory of Computation--Chapter 1 学习笔记
- 用Python和OpenCV创建一个图片搜索引擎的完整指南 The complete guide to building an image search engine with Python and
- Intro to the Apache table API
- iOS9 getting error “an ssl error has occurred and a secure connection to the server cannot be made”
- Configure PHP and Django (Python) to run on the same Apache site.