zoukankan      html  css  js  c++  java
  • 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.4docDBlayout.html获取各个表的详细信息。


    3 创建本体的持久模型

    Jena同时支持内存模型和数据库模型。一般来讲,创建内存模型只需要调用Jena的一些接口,但创建数据库模型,或者打开先前创建的模型,要求一些具体的步骤。

    任何数据库的持久模型通过以下步骤创建:

    1)         加载数据库JDBC驱动

    2)         创建数据库连接

    3)         为数据库创建一个ModelMaker

    4)         为本体创建一个模型

    4 将本体持久化存入MySQL中

    1) 其中数据库的配置文件为:

     
    1 jdbc.drivers=com.mysql.jdbc.Driver  
    2 jdbc.url=jdbc:mysql://localhost:3306/ontologies?useUnicode=true&characterEncoding=UTF-8  
    3 jdbc.username=root  
    4 jdbc.password=root  

    2) 实例类

      1 import java.io.File;  
      2 import java.io.FileInputStream;  
      3 import java.io.FileNotFoundException;  
      4 import java.io.IOException;  
      5 import java.io.InputStreamReader;  
      6 import java.io.UnsupportedEncodingException;  
      7 import java.sql.SQLException;  
      8   
      9 import com.hp.hpl.jena.db.DBConnection;  
     10 import com.hp.hpl.jena.db.IDBConnection;  
     11 import com.hp.hpl.jena.db.RDFRDBException;  
     12 import com.hp.hpl.jena.ontology.OntModel;  
     13 import com.hp.hpl.jena.ontology.OntModelSpec;  
     14 import com.hp.hpl.jena.rdf.model.Model;  
     15 import com.hp.hpl.jena.rdf.model.ModelFactory;  
     16 import com.hp.hpl.jena.rdf.model.ModelMaker;  
     17   
     18 import edu.hrbeu.ontology.util.getDBPropeties;  
     19   
     20 /** 
     21  * @purpose 本体数据库功能 
     22  * @author zhaohongjie 
     23  *  
     24  */  
     25 public class OntologyDBImpl implements IOntologyDB {  
     26   
     27     /**  
     28      * 数据库连接对象  
     29      */  
     30     private IDBConnection conn = null;  
     31     /** 
     32      * 文件输入流对象 
     33      */  
     34     private InputStreamReader in = null;  
     35       
     36     /** 
     37      * 获取数据连接 
     38      * @return 
     39      */  
     40     private IDBConnection getDBConn() {  
     41           
     42         getDBPropeties getdb = new getDBPropeties();  
     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      * @param filePath 
     59      */  
     60     public InputStreamReader getFileStream(String filePath) {  
     61           
     62         FileInputStream inputSreamfile = null;  
     63           
     64         try {  
     65             File file = new File(filePath);  //"./Expert.owl"  
     66             inputSreamfile = new FileInputStream(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      * @param ontoName 
     84      */  
     85     public void toMySQL(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      * OntModelSpec 
     95      * @param maker 
     96      * @return 
     97      */  
     98     private OntModelSpec getModelSpec(ModelMaker maker) {  
     99   
    100          OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);  
    101          spec.setImportModelMaker(maker);  
    102          return spec;  
    103   
    104     }  
    105       
    106     /** 
    107      * 返回本体 
    108      * @param ontoName 
    109      * @return 
    110      */  
    111     private OntModel 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          return newmodel;  
    117   
    118     }  
    119       
    120     /** 
    121      * 获取本体对象 
    122      * @param ontoName 
    123      */  
    124     public OntModel fromMySQL(String ontoName) {  
    125           
    126         OntModel model = getModelFromDB(ontoName);  
    127         return model;  
    128           
    129     }  
    130       
    131     /** 
    132      * 关闭占用资源 
    133      */  
    134     private void closeDBResource() {  
    135         closeFileStream();  
    136         closeDBConn();        
    137     }  
    138       
    139     /** 
    140      * 关闭数据流 
    141      */  
    142     private void closeFileStream() {  
    143         try {  
    144             this.in.close();  
    145         } catch (IOException e) {  
    146             e.printStackTrace();  
    147         }  
    148     }  
    149       
    150     /** 
    151      * 关闭数据库连接 
    152      */  
    153     public void closeDBConn() {  
    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 void main(String[] args) {  
              
            String ruleFile = "file:./expert/Expert.rules";  
      
            IOntologyDB ontoDB = OntologyDBFactory.createDBont();  
            ontoDB.getFileStream(ruleFile);  
            ontoDB.toMySQL("expert");  
      
            ontoDB.closeDBConn();  
    }  
  • 相关阅读:
    jsp 头像上传显示部分代码实现
    Spring Boot
    php好书推荐
    提升PHP编程效率的20个要素
    mysql怎么查询一条记录的前一条记录和后一条记录
    jquery load 和 iframe 比较
    setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式
    每日时间计划表
    Java获取某年第一天和最后一天
    s:if 判断
  • 原文地址:https://www.cnblogs.com/liuchaogege/p/5388844.html
Copyright © 2011-2022 走看看