首页 > 代码库 > Jena将owl文件持久化到数据库中

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

package cn.edu.shu.db;

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

import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;

/**
 * 
 * <p>
 * ClassName OWLFile2DB
 * </p>
 * <p>
 * Description :该类可以将owl文件持久化到数据库中,并自动创建生成的表,注意会生成多个表;<br/>
 * 依赖的jar包是jena-2.6.0.jar/iri-0.7.jar/icu4j-3.4.4.jar/mysql驱动包
 * </p>
 * 
 * @author wangxu wangx89@126.com
 *         <p>
 *         Date 2014-10-11 下午08:32:34
 *         </p>
 * @version V1.0
 * 
 */

public class OWLFile2DB {
	private static final String DB = "MySQL";
	String CLASSNAME = "com.mysql.jdbc.Driver";
	String DBURL = "jdbc:mysql://localhost:3306/ontologyIR?useUnicode=true&characterEncoding=UTF8";
	String DBUSER = "root";
	String DBPWD = "admin";

	public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
		new OWLFile2DB().createTable();
		System.out.println("succeed");
	}

	public void createTable() throws ClassNotFoundException, IOException, SQLException {
		Class.forName(CLASSNAME);
		IDBConnection conn = new DBConnection(DBURL, DBUSER, DBPWD, DB);// 获取连接
		ModelMaker maker = ModelFactory.createModelRDBMaker(conn);// 创建ModelMaker对象
		Model base = maker.createModel("ontologyIR");
		FileInputStream inputStream = null;
		File file = new File("Creature.owl");
		inputStream = new FileInputStream(file);
		InputStreamReader in = null;
		in = new InputStreamReader(inputStream, "UTF-8");

		base.read(in, null);
		in.close();
		base.commit();// 持久化到数据库中
		conn.close();
	}

}

用到的owl文件

<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#"
     xml:base="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16"/>
    


    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#beEated -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#beEated">
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:ObjectProperty>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#eat -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#eat">
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal"/>
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:ObjectProperty>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#mainEat -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#mainEat">
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
        <rdfs:domain rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal"/>
        <rdfs:range rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:ObjectProperty>
    


    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Branch -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Branch">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Grass -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Grass">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#GrassAnimal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Leaf -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Leaf">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MeatAnimal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#MixeatAnimal">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Animal"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Creature"/>
    </owl:Class>
    


    <!-- http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree -->

    <owl:Class rdf:about="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Tree">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/wangxu/ontologies/2014/9/untitled-ontology-16#Plant"/>
    </owl:Class>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->


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