Jena将owl文件持久化到数据库中

  1. package cn.edu.shu.db;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  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.rdf.model.Model;  
  12. import com.hp.hpl.jena.rdf.model.ModelFactory;  
  13. import com.hp.hpl.jena.rdf.model.ModelMaker;  
  14.   
  15. /** 
  16.  *  
  17.  * <p> 
  18.  * ClassName OWLFile2DB 
  19.  * </p> 
  20.  * <p> 
  21.  * Description :该类可以将owl文件持久化到数据库中,并自动创建生成的表,注意会生成多个表;<br/> 
  22.  * 依赖的jar包是jena-2.6.0.jar/iri-0.7.jar/icu4j-3.4.4.jar/mysql驱动包 
  23.  * </p> 
  24.  *  
  25.  * @author wangxu wangx89@126.com 
  26.  *         <p> 
  27.  *         Date 2014-10-11 下午08:32:34 
  28.  *         </p> 
  29.  * @version V1.0 
  30.  *  
  31.  */  
  32.   
  33. public class OWLFile2DB {  
  34.     private static final String DB = "MySQL";  
  35.     String CLASSNAME = "com.mysql.jdbc.Driver";  
  36.     String DBURL = "jdbc:mysql://localhost:3306/ontologyIR?useUnicode=true&characterEncoding=UTF8";  
  37.     String DBUSER = "root";  
  38.     String DBPWD = "admin";  
  39.   
  40.     public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {  
  41.         new OWLFile2DB().createTable();  
  42.         System.out.println("succeed");  
  43.     }  
  44.   
  45.     public void createTable() throws ClassNotFoundException, IOException, SQLException {  
  46.         Class.forName(CLASSNAME);  
  47.         IDBConnection conn = new DBConnection(DBURL, DBUSER, DBPWD, DB);// 获取连接  
  48.         ModelMaker maker = ModelFactory.createModelRDBMaker(conn);// 创建ModelMaker对象  
  49.         Model base = maker.createModel("ontologyIR");  
  50.         FileInputStream inputStream = null;  
  51.         File file = new File("Creature.owl");  
  52.         inputStream = new FileInputStream(file);  
  53.         InputStreamReader in = null;  
  54.         in = new InputStreamReader(inputStream, "UTF-8");  
  55.   
  56.         base.read(in, null);  
  57.         in.close();  
  58.         base.commit();// 持久化到数据库中  
  59.         conn.close();  
  60.     }  
  61.   
  62. }  

用到的owl文件

[plain]  view plain  copy
  1. <?xml version="1.0"?>  
  2.   
  3.   
  4. <!DOCTYPE rdf:RDF [  
  5.     <!ENTITY owl "http://www.w3.org/2002/07/owl#" >  
  6.     <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >  
  7.     <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >  
  8.     <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >  
  9. ]>  
  10.   
  11.   
  12. <rdf:RDF xmlns="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#"  
  13.      xml:base="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16"  
  14.      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"  
  15.      xmlns:owl="http://www.w3.org/2002/07/owl#"  
  16.      xmlns:xsd="http://www.w3.org/2001/XMLSchema#"  
  17.      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">  
  18.     <owl:Ontology rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16"/>  
  19.       
  20.   
  21.   
  22.     <!--   
  23.     ///  
  24.     //  
  25.     // Object Properties  
  26.     //  
  27.     ///  
  28.      -->  
  29.   
  30.       
  31.   
  32.   
  33.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#beEated -->  
  34.   
  35.     <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#beEated">  
  36.         <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  37.         <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  38.         <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>  
  39.     </owl:ObjectProperty>  
  40.       
  41.   
  42.   
  43.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#eat -->  
  44.   
  45.     <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#eat">  
  46.         <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  47.         <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal"/>  
  48.         <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal"/>  
  49.         <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal"/>  
  50.         <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>  
  51.     </owl:ObjectProperty>  
  52.       
  53.   
  54.   
  55.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#mainEat -->  
  56.   
  57.     <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#mainEat">  
  58.         <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  59.         <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  60.         <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal"/>  
  61.         <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>  
  62.     </owl:ObjectProperty>  
  63.       
  64.   
  65.   
  66.     <!--   
  67.     ///  
  68.     //  
  69.     // Classes  
  70.     //  
  71.     ///  
  72.      -->  
  73.   
  74.       
  75.   
  76.   
  77.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal -->  
  78.   
  79.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal">  
  80.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>  
  81.     </owl:Class>  
  82.       
  83.   
  84.   
  85.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Branch -->  
  86.   
  87.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Branch">  
  88.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree"/>  
  89.     </owl:Class>  
  90.       
  91.   
  92.   
  93.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature -->  
  94.   
  95.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>  
  96.       
  97.   
  98.   
  99.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Grass -->  
  100.   
  101.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Grass">  
  102.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>  
  103.     </owl:Class>  
  104.       
  105.   
  106.   
  107.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal -->  
  108.   
  109.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal">  
  110.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  111.     </owl:Class>  
  112.       
  113.   
  114.   
  115.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Leaf -->  
  116.   
  117.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Leaf">  
  118.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree"/>  
  119.     </owl:Class>  
  120.       
  121.   
  122.   
  123.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal -->  
  124.   
  125.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal">  
  126.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  127.     </owl:Class>  
  128.       
  129.   
  130.   
  131.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal -->  
  132.   
  133.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal">  
  134.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>  
  135.     </owl:Class>  
  136.       
  137.   
  138.   
  139.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant -->  
  140.   
  141.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant">  
  142.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>  
  143.     </owl:Class>  
  144.       
  145.   
  146.   
  147.     <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree -->  
  148.   
  149.     <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree">  
  150.         <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>  
  151.     </owl:Class>  
  152. </rdf:RDF>  
  153.   
  154.   
  155.   
  156. <!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值