Jena学习笔记(2)——利用数据库保存本体

注明:本文档是使用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) 其中数据库的配置文件为:

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) 实例类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.db.RDFRDBException;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;

import edu.hrbeu.ontology.util.getDBPropeties;

/**
 * @purpose 本体数据库功能
 * @author zhaohongjie
 * 
 */
public class OntologyDBImpl implements IOntologyDB {

	/** 
	 * 数据库连接对象 
	 */
	private IDBConnection conn = null;
	/**
	 * 文件输入流对象
	 */
	private InputStreamReader in = null;
	
	/**
	 * 获取数据连接
	 * @return
	 */
	private IDBConnection getDBConn() {
		
		getDBPropeties getdb = new getDBPropeties();
		
		try {
			this.conn = new DBConnection(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...");
		}
		
		return this.conn;
	}
	
	/**
	 * 从数据流获取本体
	 * @param filePath
	 */
	public InputStreamReader getFileStream(String filePath) {
		
		FileInputStream inputSreamfile = null;
		
		try {
            File file = new File(filePath);  //"./Expert.owl"
            inputSreamfile = new FileInputStream(file);
       } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("Ontology File is not available...");
       }
       
       try {
           this.in = new InputStreamReader(inputSreamfile, "UTF-8");
      } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
      }
      
      return this.in;
	}
	
	/**
	 * 将本体存入数据库
	 * @param ontoName
	 */
	public void toMySQL(String ontoName) {
		ModelMaker maker = ModelFactory.createModelRDBMaker(getDBConn());
		Model defModel = maker.createModel(ontoName); //"expert"
		defModel.read(in,null);
		defModel.commit();
		closeDBResource();
	}
	
	/**
	 * OntModelSpec
	 * @param maker
	 * @return
	 */
	private OntModelSpec getModelSpec(ModelMaker maker) {

	     OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
	     spec.setImportModelMaker(maker);
	     return spec;

	}
	
	/**
	 * 返回本体
	 * @param ontoName
	 * @return
	 */
	private OntModel getModelFromDB(String ontoName) {

	     ModelMaker maker = ModelFactory.createModelRDBMaker(getDBConn());
	     Model base = maker.getModel(ontoName);
	     OntModel newmodel = ModelFactory.createOntologyModel(getModelSpec(maker), base);
	     return newmodel;

	}
	
	/**
	 * 获取本体对象
	 * @param ontoName
	 */
	public OntModel fromMySQL(String ontoName) {
		
		OntModel model = getModelFromDB(ontoName);
		return model;
		
	}
	
	/**
	 * 关闭占用资源
	 */
	private void closeDBResource() {
		closeFileStream();
		closeDBConn(); 		
	}
	
	/**
	 * 关闭数据流
	 */
	private void closeFileStream() {
		try {
            this.in.close();
		} catch (IOException e) {
            e.printStackTrace();
		}
	}
	
	/**
	 * 关闭数据库连接
	 */
	public void closeDBConn() {
		try {
			if (this.conn != null) {
				this.conn.close();
			}
		} catch (SQLException sqlEx) {
			sqlEx.printStackTrace();
		}
	}
	
}

3) mian函数

public static void main(String[] args) {
		
		String ruleFile = "file:./expert/Expert.rules";

		IOntologyDB ontoDB = OntologyDBFactory.createDBont();
		ontoDB.getFileStream(ruleFile);
		ontoDB.toMySQL("expert");

		ontoDB.closeDBConn();
}



你可能感兴趣的:(Jena学习笔记(2)——利用数据库保存本体)