注明:本文档是使用Jena2.6.4,数据库为MySQL,数据库驱动版本为mysql-connector-java-5.1.13-bin.jar。
1 Jena的数据库接口
Jena提供了将RDF数据存入关系数据库的接口,Model、Resource、Query等接口可以用于访问和维护数据库里的RDF数据。在处理数据时,应用程序不必直接操作数据(而
是通过Jena的API),也不必知道数据库的模式。Jena提供了支持MySQL、HSQLDB、PostgreSQ、Oracle和Microsoft SQL Server的程序接口。有些第三方提供其他数据库接
口的支持。可以参考Jena数据库文档获得数据库版本以及对应的JDBC驱动说明。
2 Jena的数据库模式
关系数据库存储RDF数据的一般模式是“三元组”,表有三列(主体、谓词、客体)每个RDF陈述(sataement)占用一行。有时候,添加第四列以表示客体是字符常量还是
URI。Jena 2采用一种denormalized的三元组存储方法,是存储空间和访问时间的一种权衡方法(a space-time trade-off)。Jena使用两类七个表存储本体,第一类是
asserted statements,第二类reified statements。
Statement Tables 陈述表:
1) Asserted Statement Table (Jena_GiTj_Stmt):存储本体数据
2) Reified Statement Table (Jena_GiTj_Reif):经过处理的本体数据。System Tables 系统表:存储元数据和陈述表中使用的较长的文字或者资源
3) System Statement Table (Jena_Sys_Stmt):存储系统元数据
4) Long Literals Table (Jena_Long_Lit):存储陈述表中不便于直接存储的长字符创常量(Literals)
5) Long Resources Table (Jena_Long_URI):存储陈述表中不便于直接存储的长资源URI
6) Prefixes Table (Jena_Prefix):存储URI的前缀。前缀只存储一次,节省空间。
7) Graph Table (Jena_Graph):存储每一个用户图的名字和唯一标志符。
8) Lock Table (Jena_Mutex):一个没有内容的表。如果该表存在,在一定时间段里数据库被锁定。
可以参照\\Jena-2.6.4\doc\DB\layout.html获取各个表的详细信息。
3 创建本体的持久模型
Jena同时支持内存模型和数据库模型。一般来讲,创建内存模型只需要调用Jena的一些接口,但创建数据库模型,或者打开先前创建的模型,要求一些具体的步骤。
任何数据库的持久模型通过以下步骤创建:
1) 加载数据库JDBC驱动
2) 创建数据库连接
3) 为数据库创建一个ModelMaker
4) 为本体创建一个模型
4 将本体持久化存入MySQL中
1) 其中数据库的配置文件为:
1 jdbc.drivers=com.mysql.jdbc.Driver2 jdbc.url=jdbc\:mysql\://localhost\:3306/ontologies?useUnicode\=true&characterEncoding\=UTF-8
3 jdbc.username=root4 jdbc.password=root
2) 实例类
1 importjava.io.File;2 importjava.io.FileInputStream;3 importjava.io.FileNotFoundException;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.io.UnsupportedEncodingException;7 importjava.sql.SQLException;8
9 importcom.hp.hpl.jena.db.DBConnection;10 importcom.hp.hpl.jena.db.IDBConnection;11 importcom.hp.hpl.jena.db.RDFRDBException;12 importcom.hp.hpl.jena.ontology.OntModel;13 importcom.hp.hpl.jena.ontology.OntModelSpec;14 importcom.hp.hpl.jena.rdf.model.Model;15 importcom.hp.hpl.jena.rdf.model.ModelFactory;16 importcom.hp.hpl.jena.rdf.model.ModelMaker;17
18 importedu.hrbeu.ontology.util.getDBPropeties;19
20 /**
21 * @purpose 本体数据库功能22 *@authorzhaohongjie23 *24 */
25 public class OntologyDBImpl implementsIOntologyDB {26
27 /**
28 * 数据库连接对象29 */
30 private IDBConnection conn = null;31 /**
32 * 文件输入流对象33 */
34 private InputStreamReader in = null;35
36 /**
37 * 获取数据连接38 *@return
39 */
40 privateIDBConnection getDBConn() {41
42 getDBPropeties getdb = newgetDBPropeties();43
44 try{45 this.conn = new DBConnection(getdb.getUrl(), getdb.getUser(), getdb.getPassword(), "MySQL");46 Class.forName(getdb.getClassDrive());47 } catch(RDFRDBException e) {48 System.out.println("Exceptions occur...");49 } catch(ClassNotFoundException e) {50 System.out.println("ClassNotFoundException, Driver is not available...");51 }52
53 return this.conn;54 }55
56 /**
57 * 从数据流获取本体58 *@paramfilePath59 */
60 publicInputStreamReader getFileStream(String filePath) {61
62 FileInputStream inputSreamfile = null;63
64 try{65 File file = new File(filePath); //"./Expert.owl"
66 inputSreamfile = newFileInputStream(file);67 } catch(FileNotFoundException e) {68 e.printStackTrace();69 System.out.println("Ontology File is not available...");70 }71
72 try{73 this.in = new InputStreamReader(inputSreamfile, "UTF-8");74 } catch(UnsupportedEncodingException e) {75 e.printStackTrace();76 }77
78 return this.in;79 }80
81 /**
82 * 将本体存入数据库83 *@paramontoName84 */
85 public voidtoMySQL(String ontoName) {86 ModelMaker maker =ModelFactory.createModelRDBMaker(getDBConn());87 Model defModel = maker.createModel(ontoName); //"expert"
88 defModel.read(in,null);89 defModel.commit();90 closeDBResource();91 }92
93 /**
94 * OntModelSpec95 *@parammaker96 *@return
97 */
98 privateOntModelSpec getModelSpec(ModelMaker maker) {99
100 OntModelSpec spec = newOntModelSpec(OntModelSpec.OWL_MEM);101 spec.setImportModelMaker(maker);102 returnspec;103
104 }105
106 /**
107 * 返回本体108 *@paramontoName109 *@return
110 */
111 privateOntModel getModelFromDB(String ontoName) {112
113 ModelMaker maker =ModelFactory.createModelRDBMaker(getDBConn());114 Model base =maker.getModel(ontoName);115 OntModel newmodel =ModelFactory.createOntologyModel(getModelSpec(maker), base);116 returnnewmodel;117
118 }119
120 /**
121 * 获取本体对象122 *@paramontoName123 */
124 publicOntModel fromMySQL(String ontoName) {125
126 OntModel model =getModelFromDB(ontoName);127 returnmodel;128
129 }130
131 /**
132 * 关闭占用资源133 */
134 private voidcloseDBResource() {135 closeFileStream();136 closeDBConn();137 }138
139 /**
140 * 关闭数据流141 */
142 private voidcloseFileStream() {143 try{144 this.in.close();145 } catch(IOException e) {146 e.printStackTrace();147 }148 }149
150 /**
151 * 关闭数据库连接152 */
153 public voidcloseDBConn() {154 try{155 if (this.conn != null) {156 this.conn.close();157 }158 } catch(SQLException sqlEx) {159 sqlEx.printStackTrace();160 }161 }162
163 }
3) mian函数
public static voidmain(String[] args) {
String ruleFile= "file:./expert/Expert.rules";
IOntologyDB ontoDB=OntologyDBFactory.createDBont();
ontoDB.getFileStream(ruleFile);
ontoDB.toMySQL("expert");
ontoDB.closeDBConn();
}