您好,欢迎来到榕意旅游网。
搜索
您的当前位置:首页jena学习小结

jena学习小结

来源:榕意旅游网
一、 创建RDF语言本体:

static String personURI = \"http://somewhere/JohnSmith\"; static String fullName = \"John Smith\"; // create an empty Model

Model model = ModelFactory.createDefaultModel();○1 // create the resource

Resource johnSmith = model.createResource(personURI);○2 // add the property

johnSmith.addProperty(VCARD.FN, fullName);○3

1处是用模型工程创建一个空的本体模型 ○

2处是创建一个资源,主体是○

\"http://somewhere/JohnSmith\"。

3处是给○2出创建的资源增加属性和客体(即属性的值),属性为VCARD.FN,○

其中VCARD是RDFS中定义好的,客体为fullName,是字符串,也就是literal。

johnSmith,其对应URI为

○2和○3合起来就是创建了一个三元组,是组成本体的基本单元,可以简写

成如下:Resource johnSmith =

model.createResource(personURI) .addProperty(VCARD.FN, fullName);

二、

对本体中的陈述(即三元组)的操作

StmtIteratoriter = model.listStatements();○1

// print out the predicate, subject and object of each statement while (iter.hasNext()) {○2

Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate 4RDFNode object = stmt.getObject(); // get the object ○

System.out.print(subject.toString());○3 System.out.print(\" \" + predicate.toString() + \" \"); if (object instanceof Resource) { System.out.print(object.toString()); } else {

// object is a literal

System.out.print(\" \\\"\" + object.toString() + \"\\\"\"); }

System.out.println(\" .\"); }

1处是列出model本体模型中的所有的陈述。 ○

2处是对所有陈述逐个进行操作,○stmt是得到该陈述,subject是得到陈述主题,predicate是得到陈述的属性,object是得到陈述的属性值,记住各个函数的用法功能。还有打印三元组的各个部分时,.toString函数是打印出完整的URI,而.getlocalname则是URI的

3处打印为:http://somewhere/JohnSmith,若○3处最后的本地名字部分。例如○

改为subject.localname(),则打印为:JohnSmith。

4处,因为属性值可以为resource和literal,所以用其父类RDFNode做返○

回值,其后再用object instanceof Resource语句判断到底是资源,还是literal。

三、

写RDF本体

model.write(System.out);

model.write(System.out, \"RDF/XML-ABBREV\");

model.write(new FileOutputStream(new File(文件路径))); 第一句是默认本体表示语言的写,默认是用in triple form,用三元组形式的RDF

表示法。

第二句是用RDF/XML-ABBREV表示形式输出本体。

第三句则是把本体写入文件中 四、

读RDF本体

// create an empty model

Model model = ModelFactory.createDefaultModel(); // use the FileManager to find the input file

InputStream in = FileManager.get().open( inputFileName );○1 if (in == null) {

throw new IllegalArgumentException(

\"File: \" + inputFileName + \" not found\"); }

// read the RDF/XML file model.read(in, null);○2

// write it to standard out model.write(System.out);

1创建一个文件管理器,利用此函数就可以读进本体读就比较麻烦。首先用○

文件到InputStream,然后用model的read函数读进来记住就好了,别管那么多了,反正可以用的。

五、

前缀操作

Model m = ModelFactory.createDefaultModel(); String nsA = \"http://somewhere/else#\"; String nsB = \"http://nowhere/else#\";

Resource root = m.createResource(nsA + \"root\" ); Property P = m.createProperty(nsA + \"P\" ); Property Q = m.createProperty(nsB + \"Q\" ); Resource x = m.createResource(nsA + \"x\" ); Resource y = m.createResource(nsA + \"y\" ); Resource z = m.createResource(nsA + \"z\" );

m.add( root, P, x ).add( root, P, y ).add( y, Q, z );○1 System.out.println( \"# -- no special prefixes defined\" ); m.write(System.out );

System.out.println( \"# -- nsA defined\" ); m.setNsPrefix( \"nsA\○2

m.write(System.out );

System.out.println( \"# -- nsA and cat defined\" ); m.setNsPrefix( \"cat\m.write(System.out );

1处展现了创建陈述的一个更简介的写法,记住 ○

○2处的这个函数在使用前,在把本体写入输出流的时候,它会自己创建前缀,前缀名随即一个合法的前缀,使用后,则可以指定前缀名,此处指定为msA例如: 没有使用○2时输出为:

使用了之后输出为:

六、

本体查询操作

1. Resource name = vcard.getProperty(VCARD.N) .getResource();

返回第一个主体是vcard,谓词是VCARD.N的客体,若没有找到符合条件的客体将抛出异常。

// set up the output

System.out.println(\"The nicknames of \\\"\"

+ fullName + \"\\\" are:\"); // list the nicknames

StmtIteratoriter = vcard.listProperties(VCARD.NICKNAME);○1 while (iter.hasNext()) {

System.out.println(\" \" + iter.nextStatement() .getObject() .toString());○2 }

若要找出所有符合条件的客体,就用○1这种操作。这是一个功能很强大的函数,有了他,想要知道一个个体的所属类别,只要把他的参数改成Property(type),就可以了,要知道一个类是哪一个类的子类,只要用Property(subClassof)就可以了,所以功能很强大,要切记这个函数。

2. ResIteratoriter =

model.listSubjectsWithProperty(VCARD.FN);○1

while (iter.hasNext()) {

Resource r = iter.nextResource(); ... }

1查询出所有属性是VCARD.FN的主体。 ○

3. model.listStatements(Selector s).

是功能最强大的查询,它查询出所有查询子查询出的所有三元组。而查询子是可以用户自己定义的,定义方法如下:Selector selector = new SimpleSelector(null, null, null);它匹配出所有,主体是给定主体,谓词是给定谓词,客体是给定客体的所有陈述,如果为null,就符合anything。给出例子,瞬间就懂得了:

// select all the resources with a VCARD.FN property // whose value ends with \"Smith\"

StmtIteratoriter = model.listStatements(

newSimpleSelector(null, VCARD.FN, (RDFNode) null) { publicboolean selects(Statement s)

{returns.getString().endsWith(\"Smith\");}

});

这个查询应用了匿名类。功能就是,匹配所有谓词是VCARD.FN,且陈述是以Smith结尾的所有陈述,并返回,这个查询很高级的,可以非常灵活的应用,且几乎可以应用到任何目的的查询,记住,少年。

七、

本体操作

model1.read(new InputStreamReader(in1), \"\"); model2.read(new InputStreamReader(in2), \"\");

// merge the Models

Model model = model1.union(model2);○2 // print the Model as RDF/XML

model.write(system.out, \"RDF/XML-ABBREV\");

2处把两个本体模型执行并操作,它会是两个本体模型合并,当然对共有○

的部分只保留一份。就像集合的并一样。其他的.intersection(Model)交 and .difference(Model)补一样的操作。

八、 本体容器(Containers)

Bag smiths = model.createBag();○1

// select all the resources with a VCARD.FN property // whose value ends with \"Smith\"

StmtIteratoriter = model.listStatements(

newSimpleSelector(null, VCARD.FN, (RDFNode) null) { publicboolean selects(Statement s) { returns.getString().endsWith(\"Smith\"); } });

// add the Smith's to the bag while (iter.hasNext()) {

smiths.add(iter.nextStatement().getSubject());○2 }

NodeIterator iter2 = smiths.iterator();○3 if (iter2.hasNext()) {

System.out.println(\"The bag contains:\"); while (iter2.hasNext()) { System.out.println(\" \" +

((Resource) iter2.next())

.getProperty(VCARD.FN) .getString()); } } else {

System.out.println(\"The bag is empty\"); }

1处就创建了一个包容器,○2处把符合条件的陈述加到包中,○3处○

对包里的陈述执行遍历。其他容器比如seq顺序,alt替换也是一样的用法。其实按我的理解,本体里的包没什么区别,就是seq顺序有影

响,alt第一个和不是第一个有影响,bag则顺序没影响。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- nryq.cn 版权所有 赣ICP备2024042798号-6

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务