4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
新闻详情
Jena API 使用介绍_javafreely的专栏-CSDN博客
来自 : CSDN技术社区 发布时间:2021-03-25
m.add( root, P, x ).add( root, P, y ).add( y, Q, z ); System.out.println( # -- no special prefixes defined m.write( System.out ); System.out.println( # -- nsA defined //设置Namespace nsA 的前缀为“nsA” m.setNsPrefix( nsA , nsA ); m.write( System.out ); System.out.println( # -- nsA and cat defined //设置Namespace nsB 的前缀为“cat” m.setNsPrefix( cat , nsB ); m.write( System.out );


该程序首先调用 Model 的createProperty 和createResource 生成属性和资源。然后调用Model.add 想model 中增加3个Statement。add 的三个参数分别是三元组的主语、谓语和客体。想Model 中增加内容实际上就是增加三元组。

Model 的 setNsPrefix 函数用于设置名字空间前缀。该程序的输出如下

# -- no special prefixes defined rdf:RDF xmlns:rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#  xmlns:j.0 http://nowhere/else#  xmlns:j.1 http://somewhere/else#  rdf:Description rdf:about http://somewhere/else#y  j.0:Q rdf:resource http://somewhere/else#z /  /rdf:Description  rdf:Description rdf:about http://somewhere/else#root  j.1:P rdf:resource http://somewhere/else#y /  j.1:P rdf:resource http://somewhere/else#x /  /rdf:Description  /rdf:RDF # -- nsA defined rdf:RDF xmlns:rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#  xmlns:j.0 http://nowhere/else#  xmlns:nsA http://somewhere/else#  rdf:Description rdf:about http://somewhere/else#y  j.0:Q rdf:resource http://somewhere/else#z /  /rdf:Description  rdf:Description rdf:about http://somewhere/else#root  nsA:P rdf:resource http://somewhere/else#y /  nsA:P rdf:resource http://somewhere/else#x /  /rdf:Description  /rdf:RDF # -- nsA and cat defined rdf:RDF xmlns:rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#  xmlns:cat http://nowhere/else#  xmlns:nsA http://somewhere/else#  rdf:Description rdf:about http://somewhere/else#y  cat:Q rdf:resource http://somewhere/else#z /  /rdf:Description  rdf:Description rdf:about http://somewhere/else#root  nsA:P rdf:resource http://somewhere/else#y /  nsA:P rdf:resource http://somewhere/else#x /  /rdf:Description  /rdf:RDF 

如果我们没有为RDF 指定namespace 前缀 则jena 会自动为其生成名为 j.0, j.1 的名字空间。

7. jena 的 Model 访问

上面介绍了jena 用来创建、读、写 RDF Model 本部分将主要用来访问RDF Model 的信息 对Model 的内容进行操作。

看下面一个例子

import com.hp.hpl.jena.rdf.model.Model;import com.hp.hpl.jena.rdf.model.ModelFactory;import com.hp.hpl.jena.rdf.model.Resource;import com.hp.hpl.jena.rdf.model.StmtIterator;import com.hp.hpl.jena.vocabulary.VCARD;public class ModelAccess { public static void main(String[] args){ String personURI http://somewhere/JohnSmith  String givenName John  String familyName Smith  String fullName givenName familyName; Model model ModelFactory.createDefaultModel(); Resource johnSmith model.createResource(personURI); johnSmith.addProperty(VCARD.FN, fullName); johnSmith.addProperty(VCARD.N,  model.createResource() .addProperty(VCARD.Given, givenName) .addProperty(VCARD.Family, familyName)); // 从 Model 获取资源 Resource vcard model.getResource(personURI); // 获取N 属性的值 用属性的 getObject()方法  Resource name (Resource) vcard.getProperty(VCARD.N) .getObject(); // 如果知道属性的值是资源 可以使用属性的getResource 方法 Resource name vcard.getProperty(VCARD.N) .getResource(); // 属性的值若是 literal 则使用 getString 方法 fullName vcard.getProperty(VCARD.FN) .getString(); // 增加两个 NICKNAME 属性 vcard.addProperty(VCARD.NICKNAME, Smithy ) .addProperty(VCARD.NICKNAME, Adman  System.out.println( The nicknames of \\  fullName \\ are:  // 列出两个NICKNAME 属性 使用资源的 listProperties 方法 StmtIterator iter vcard.listProperties(VCARD.NICKNAME); while (iter.hasNext()) { System.out.println( iter.nextStatement() .getObject() .toString());

本例子中主要使用了以下内容

Model 的 getResource 方法 该方法根据参数返回一个资源对象。Resource 的 getProperty 方法 根据参数返回一个属性对象。Property 的 getObject 方法 返回属性值。使用时根据实际类型是 Resource 还是 literal 进行强制转换。Property 的 getResource 方法 返回属性值的资源。如果属性值不是Resource 则报错。Property 的 getString 方法 返回属性值的文本内容。如果属性值不是文本 则报错。Resource 的 listProperties 方法 列出所找到符合条件的属性。 8. 对 Model 的查询

Jena 和核心 API 仅支持有限的查询操作。我们这里进行简单介绍。

Model.listStatements(): 列出Model 所有的Statements。Model.listSubjects(): 列出所有具有属性的资源。Model.listSubjectsWithProperty(Property p, RDFNode o): 列出所有具有属性p 且其值为 o 的资源。

上面所述的几种查询都是对 Model.listStatements(Selector s) 进行了一些包装得到的。如

Selector selector new SimpleSelector(subject, predicate, object). 这个选择器选择所有主语符合 subject、谓语符合 predicate、客体符合 object 的Statement。

下面分别使用两种方式查询具有 fullName 的资源。

1. 使用 Model.listSubjectsWithProperty 查询

import java.io.InputStream;import com.hp.hpl.jena.rdf.model.Model;import com.hp.hpl.jena.rdf.model.ModelFactory;import com.hp.hpl.jena.rdf.model.ResIterator;import com.hp.hpl.jena.util.FileManager;import com.hp.hpl.jena.vocabulary.VCARD;public class RDFQuery { public static String inputFileName resources.rdf  public static void main(String[] args){ Model model ModelFactory.createDefaultModel(); InputStream in FileManager.get().open( inputFileName ); if (in null) { throw new IllegalArgumentException( File: inputFileName not found  model.read(in, null); //使用 listResourcesWithProperty ResIterator iter model.listResourcesWithProperty(VCARD.FN); if(iter.hasNext()){ System.out.println( The database contains vcard for:  while(iter.hasNext()){ System.out.println( iter.nextResource().getProperty(VCARD.FN).getString()); }else{ System.out.println( No vcards were found in the database }

 

2. 使用 Selector 查询

import java.io.InputStream;import com.hp.hpl.jena.rdf.model.Model;import com.hp.hpl.jena.rdf.model.ModelFactory;import com.hp.hpl.jena.rdf.model.RDFNode;import com.hp.hpl.jena.rdf.model.SimpleSelector;import com.hp.hpl.jena.rdf.model.StmtIterator;import com.hp.hpl.jena.util.FileManager;import com.hp.hpl.jena.vocabulary.VCARD;public class RDFQuery1 { public static String inputFileName resources.rdf  public static void main(String[] args){ Model model ModelFactory.createDefaultModel(); InputStream in FileManager.get().open( inputFileName ); if (in null) { throw new IllegalArgumentException( File: inputFileName not found  model.read(in, null); //使用 Selector StmtIterator iter model.listStatements(new SimpleSelector(null, VCARD.FN, (RDFNode)null)); if(iter.hasNext()){ System.out.println( The database contains vcard for:  while(iter.hasNext()){ System.out.println( iter.nextStatement().getString()); }else{ System.out.println( No vcards were found in the database }

本例中使用resources.rdf 资源。上面两例的输出均为

The database contains vcard for: Becky Smith Matt Jones Sarah Jones John Smith
9. 对Model 的增删操作

我们知道 对数据库的操作主要包括增、删、改、查等。对RDF 我们同样可以实现这几种操作。查询操作我们已经介绍过 本节将介绍RDF Model 的增删操作。我们可以对一个RDF 增加或者删除 Statement。由于 RDF Model完全是由 Statements 构成的 因此我们可以据此实现资源和属性等的增删。改动操作可以通过删除后再添加来实现。

看下面这个例子

import com.hp.hpl.jena.rdf.model.Model;import com.hp.hpl.jena.rdf.model.ModelFactory;import com.hp.hpl.jena.rdf.model.RDFNode;import com.hp.hpl.jena.rdf.model.Resource;import com.hp.hpl.jena.vocabulary.VCARD;

本文链接: http://jena.immuno-online.com/view-736486.html

发布于 : 2021-03-25 阅读(0)