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();
}