语义网技术:jena的使用(1)——环境以及实例

来源:互联网 发布:淘宝卖家的支付宝红包 编辑:程序博客网 时间:2024/05/15 23:47

大四毕设做关于语义网技术的研究,其中需要学习关于protege,jena等工具,以及owl和rdf的技术,网上的资料比较少,在这里给自己做一个学习的记录。


关于jena和protege

简单说,写个式子:(jena→java)    =    (protege→用户)

不难理解,jena对编程来说就像protege对用户的关系一样,这个是网上的资料的普遍说法,另外这个只做了解,这个系列的blog不出意外只做jena的相关研究,说白了,jena的道理都懂,其实大家就是缺API和介绍。

附:Jena官网地址:http://jena.apache.org/


开始

1、第一部分显然是环境问题,jena有相关的资料,下面提供官网下载,同时提供官网下载网址,以方便可以下载最新的jena文件

jena官网下载链接(.zip):apache-jena-2.13.0.zip

官网下载页地址(.zip以及tar.gz):http://jena.apache.org/download/index.cgi


2、之后就不用说了,导包。

解压上述压缩文件后,在lib里面,为方便操作,全部导入即可。


3、例子

解压目录下的\apache-jena-2.13.0\src-examples\jena\examples\rdf会有以下示例,导入即可



4、示例1的运行:

默认大家都有对rdf的基本了解,main里面就三条语句,用ModelFactory里的方法创建model,对该model添加资源,对该model添加属性。

/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package jena.examples.rdf;import com.hp.hpl.jena.rdf.model.*;import com.hp.hpl.jena.vocabulary.*;/** * Tutorial 1 creating a simple model */public class Tutorial01 extends Object {// some definitionsstatic String personURI = "http://somewhere/JohnSmith";static String fullName = "John Smith";public static void main(String args[]) {// create an empty modelModel model = ModelFactory.createDefaultModel();// create the resourceResource johnSmith = model.createResource(personURI);// add the propertyjohnSmith.addProperty(VCARD.FN, fullName);}}
运行,咦?没结果,当然没结果,没写输出语句!


在“johnSmith.addProperty(VCARD.FN, fullName);”后添加一句:

model.write(System.out);

运行:

输出rdf格式:

<rdf:RDF    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" >   <rdf:Description rdf:about="http://somewhere/JohnSmith">    <vcard:FN>John Smith</vcard:FN>  </rdf:Description></rdf:RDF>
如上,第一个jena实例运行成功,并得到相应rdf格式的输出。



(本系列未完待续)


0 0