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) 其中数据库的配置文件为:
jdbc.drivers=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/ontologies?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=root
2) 实例类
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
importjava.sql.SQLException;
importcom.hp.hpl.jena.db.DBConnection;
importcom.hp.hpl.jena.db.IDBConnection;
importcom.hp.hpl.jena.db.RDFRDBException;
importcom.hp.hpl.jena.ontology.OntModel;
importcom.hp.hpl.jena.ontology.OntModelSpec;
importcom.hp.hpl.jena.rdf.model.Model;
importcom.hp.hpl.jena.rdf.model.ModelFactory;
importcom.hp.hpl.jena.rdf.model.ModelMaker;
importedu.hrbeu.ontology.util.getDBPropeties;
/**
* @purpose 本体数据库功能
* @author zhaohongjie
*
*/
publicclassOntologyDBImplimplementsIOntologyDB {
/**
* 数据库连接对象
*/
privateIDBConnection conn =null;
/**
* 文件输入流对象
*/
privateInputStreamReader in =null;
/**
* 获取数据连接
* @return
*/
privateIDBConnection getDBConn() {
getDBPropeties getdb = newgetDBPropeties();
try{
this.conn =newDBConnection(getdb.getUrl(), getdb.getUser(), getdb.getPassword(),"MySQL");
Class.forName(getdb.getClassDrive());
} catch(RDFRDBException e) {
System.out.println("Exceptions occur...");
} catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException, Driver is not available...");
}
returnthis.conn;
}
/**
* 从数据流获取本体
* @param filePath
*/
publicInputStreamReader getFileStream(String filePath) {
FileInputStream inputSreamfile = null;
try{
File file = newFile(filePath);//"./Expert.owl"
inputSreamfile = newFileInputStream(file);
} catch(FileNotFoundException e) {
e.printStackTrace();
System.out.println("Ontology File is not available...");
}
try{
this.in =newInputStreamReader(inputSreamfile,"UTF-8");
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
returnthis.in;
}
/**
* 将本体存入数据库
* @param ontoName
*/
publicvoidtoMySQL(String ontoName) {
ModelMaker maker = ModelFactory.createModelRDBMaker(getDBConn());
Model defModel = maker.createModel(ontoName); //"expert"
defModel.read(in,null);
defModel.commit();
closeDBResource();
}
/**
* OntModelSpec
* @param maker
* @return
*/
privateOntModelSpec getModelSpec(ModelMaker maker) {
OntModelSpec spec = newOntModelSpec(OntModelSpec.OWL_MEM);
spec.setImportModelMaker(maker);
returnspec;
}
/**
* 返回本体
* @param ontoName
* @return
*/
privateOntModel getModelFromDB(String ontoName) {
ModelMaker maker = ModelFactory.createModelRDBMaker(getDBConn());
Model base = maker.getModel(ontoName);
OntModel newmodel = ModelFactory.createOntologyModel(getModelSpec(maker), base);
returnnewmodel;
}
/**
* 获取本体对象
* @param ontoName
*/
publicOntModel fromMySQL(String ontoName) {
OntModel model = getModelFromDB(ontoName);
returnmodel;
}
/**
* 关闭占用资源
*/
privatevoidcloseDBResource() {
closeFileStream();
closeDBConn();
}
/**
* 关闭数据流
*/
privatevoidcloseFileStream() {
try{
this.in.close();
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* 关闭数据库连接
*/
publicvoidcloseDBConn() {
try{
if(this.conn !=null) {
this.conn.close();
}
} catch(SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
}
3) mian函数
publicstaticvoidmain(String[] args) {
String ruleFile = "file:./expert/Expert.rules";
IOntologyDB ontoDB = OntologyDBFactory.createDBont();
ontoDB.getFileStream(ruleFile);
ontoDB.toMySQL("expert");
ontoDB.closeDBConn();
}