Jena初体验
1. 打开本体模型
1 // 创建使用OWL语言的内存模型
2 OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
3 ontModel.read("file:E:\\documents\\study\\Ontology\\Creature.owl"); // 读取当前路径下的文件,加载模型
还可以用InputStream。
1 try {
2 File file=new File("E:\\java\\MeOntology.owl");
3 FileInputStream in=new FileInputStream(file);
4 ontModel.read(in,null);
5 }
6 catch (Exception ex) { }
1 String filePath="E:\\java\\MeOntology.owl";
2 try {
3 FileInputStream file = new FileInputStream(filePath);
4 InputStreamReader in = new InputStreamReader(file, "UTF-8");
5 ontModel.read(in,"");
6 in.close();
7 }
8 catch (Exception e) {}
参见:http://bbs.w3china.org/dispbbs.asp?boardID=2&ID=69946
2. 创建类(资源),并添加等价类
1 // 定义一个类作为模型中Animal类的等价类
2 OntClass dwCls = ontModel.createClass("http://www.bluemaplestudio.org/ontologies/2011/7/Dongwu");
3 dwCls.addComment("the EquivalentClass of Animal", "EN");
4
5 // 通过完整的URI获取模型中的Animal类
6 OntClass animalCls = ontModel.getOntClass("http://www.bluemaplestudio.org/ontologies/2011/7/Animal");
7 animalCls.addEquivalentClass(dwCls);
3. 列出所有类,每个类的父类、子类及属性值
1 ontModel.listClasses();
2
3 cls.listSuperClasses();
4
5 cls.listSubClasses();
6
7 cls.listDeclaredProperties();
4. 保存发生改变的模型
File newFile = new File("E:\\documents\\study\\Ontology\\Creature1.owl");
try {
FileOutputStream output = new FileOutputStream(newFile);
ontModel.write(output);
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
5. 源代码:
1 package org.bluemaplestudio;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.Iterator;
8
9 import com.hp.hpl.jena.ontology.OntClass;
10 import com.hp.hpl.jena.ontology.OntModel;
11 import com.hp.hpl.jena.ontology.OntModelSpec;
12 import com.hp.hpl.jena.ontology.OntProperty;
13 import com.hp.hpl.jena.rdf.model.ModelFactory;
14
15 /**
16 * 示例代码,允许任意转载
17 * @author tanyongbin
18 * 2011-7-30
19 * Email: briandottan@gmail.com
20 */
21 public class Ex2 {
22 public static void main(String[] args) {
23 // 创建使用OWL语言的内存模型
24 OntModel ontModel = ModelFactory
25 .createOntologyModel(OntModelSpec.OWL_MEM);
26 ontModel.read("file:E:\\documents\\study\\Ontology\\Creature.owl"); // 读取当前路径下的文件,加载模型
27
28
29 // 定义一个类作为模型中Animal类的等价类
30 OntClass dwCls = ontModel.createClass("http://www.bluemaplestudio.org/ontologies/2011/7/Dongwu");
31 dwCls.addComment("the EquivalentClass of Animal", "EN");
32
33 // 通过完整的URI获取模型中的Animal类
34 OntClass animalCls = ontModel.getOntClass("http://www.bluemaplestudio.org/ontologies/2011/7/Animal");
35 animalCls.addEquivalentClass(dwCls);
36
37 File newFile = new File("E:\\documents\\study\\Ontology\\Creature1.owl");
38 try {
39 FileOutputStream output = new FileOutputStream(newFile);
40 ontModel.write(output);
41 output.close();
42 } catch (FileNotFoundException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 } catch (IOException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49
50 // 迭代显示模型中的类,在迭代过程中完成各种操作
51 for (Iterator<OntClass> clses = ontModel.listClasses(); clses.hasNext();) {
52 OntClass cls = clses.next();
53 if (!cls.isAnon()){
54 //如果不是匿名类,则打印类名
55 System.out.print("Class:");
56 // 获取类的URI并输出,在输出时对URI做了简化(将命名空间前缀省略
57 System.out.println(cls.getModel().getGraph().
58 getPrefixMapping().shortForm(cls.getURI()));
59
60 //处理Animal类
61 if (cls.getLocalName().equals("Animal")){
62 //如果当前类是Animal,输出完整的URI
63 System.out.println(" URI@" + cls.getURI());
64 // 取得它的的等价类并打印
65 System.out.println(" Animal's EquivalentClass is " +
66 cls.getEquivalentClass().getLocalName());
67 // 输出等价类的注释
68 System.out.println(" [Comments: " +
69 cls.getEquivalentClass().getComment("EN") + "]");
70 }//处理Animal类结束
71
72 //迭代显示当前类的直接父类
73 for(Iterator<OntClass> superClses = cls.listSuperClasses(); superClses.hasNext();){
74 OntClass superCls = superClses.next();
75 String str = superCls.getModel().getGraph().
76 getPrefixMapping().shortForm(superCls.getURI());
77 System.out.println(cls.getLocalName() + "的父类为:" + str);
78 }//end of 父类
79
80 //迭代显示当前类的直接子类
81 for(Iterator<OntClass> subClses = cls.listSubClasses(); subClses.hasNext();){
82 OntClass subCls = subClses.next();
83 String str = subCls.getModel().getGraph().
84 getPrefixMapping().shortForm(subCls.getURI());
85 System.out.println(cls.getLocalName() + "的子类为:" + str);
86 }//end of 子类
87
88 //迭代显示与当前类相关的所有属性
89 for(Iterator<OntProperty> properties = cls.listDeclaredProperties(); properties.hasNext();){
90 OntProperty property = properties.next();
91 String str = property.getModel().getGraph().
92 getPrefixMapping().shortForm(property.getURI());
93 System.out.println(cls.getLocalName() + "相关属性:" + str);
94 }//end of 属性
95 }
96 }
97 }
98 }