以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 RSS/FOAF/Dublin Core/CIM/PRISM/Gene Ontology 』  (http://bbs.xml.org.cn/list.asp?boardid=3)
----  怎么对存储到MySQL数据库的OWL本体文件进行查询  (http://bbs.xml.org.cn/dispbbs.asp?boardid=3&rootid=&id=117151)


--  作者:星火辉煌
--  发布时间:8/27/2011 2:55:00 PM

--  怎么对存储到MySQL数据库的OWL本体文件进行查询

[转发]实现将OWL本体文件存储到MySQL数据库
首 先配置好开发环境,我采用的是Eclipse + mysql-essential-5.1.51-win32,对于MySQL的JDBC选择的是 mysql-connector-java-5.0.8。我把JDBC在D盘的根目录下解压缩,在环境变量中将JDBC的所在地址加入原有的 classpath中,然后开始存储本体文件工作。
1. 利用MySQL创建一个数据库:create database jena;
2. 打开Eclipse,新建一个Java工程,起名为Persistent。
3. 新建工程的同时,分别导入Jena包和MySQL的JDBC。
4. 在工程Persistent\src\目录下新建一个Java文件,名字为Persistent.java;
5. 开始编写以下代码:
import java.io.*;
import java.sql.SQLException;
import java.util.Iterator;

import com.hp.hpl.jena.db.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;

public class Persistent {
  public static final String strDriver = "com.mysql.jdbc.Driver";
  public static final String strURL = "jdbc:mysql://localhost:3306/jena";
 // localhost的后面要直接写冒号,再写3306;
  public static final String strUser = "root";
  public static final String strPassword = "hejunhui";
  public static final String strDB = "MySQL";
 

  public static void main(String[] args){
   try {
    DBConnection connection = new DBConnection(strURL, strUser, strPassword, strDB);
    // 创建连接时,第四个参数需要指定所用的数据库类型;也就是说strDB的值应该是“MySQL”
    try {
     Class.forName("com.mysql.jdbc.Driver");
     System.out.println("驱动程序已经安装。");
    } catch (ClassNotFoundException e){
     System.out.println("ClassNotFoundException, Driver is not available");
     }
    System.out.println("数据库连接成功。");
    
    // 从此处开始读入一个OWL文件并且存储到数据库中;
    ModelMaker maker = ModelFactory.createModelRDBMaker(connection);
 // 使用数据库连接参数创建一个模型制造器
    Model defModel = maker.createModel("国画");
      // 创建一个默认模型,命名为国画,因为我要存入的OWL文件名是国画
    System.out.println("检查点");
    FileInputStream read = null;
    try{
     File file = new File("F:/国画/国画.owl");
     
     read = new FileInputStream(file);
    }catch (FileNotFoundException e){
     e.printStackTrace();
     System.out.println("未找到要存储的本体文件,请检查文件地址及名称");
    }
    System.out.println("已将本体文件转换为字节流文件。");
 
    InputStreamReader in = null;
    try {
     in = new InputStreamReader((FileInputStream)read, "UTF-8");
    } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
     System.out.println("不支持上述字符集。");
    }
    System.out.println("已将字节流文件转换为UTF-8编码。");
    
    defModel.read(in,null);
    try {
     in.close();
    } catch (IOException e){
     e.printStackTrace();
     System.out.println("无法关闭字节流文件。");
    }
    System.out.println("已将字节流文件关闭。");
    
    defModel.commit();
    System.out.println("数据转换执行完毕,已将本体文件存入数据库。");
    try{
     connection.close();
    } catch (SQLException e){
     e.printStackTrace();
     System.out.println("文件无法关闭。");
    }
   } catch (RDFRDBException e){
    System.out.println("出现异常");
    }
   System.out.println("已将本体文件持久化到数据库中");
   
   
   }
  /* 从数据库中得到已存入本体 */
  public static OntModel getModelFromDB(IDBConnection con, String name) {
      ModelMaker maker = ModelFactory.createModelRDBMaker(con);
      Model base = maker.getModel(name);
      OntModel newmodel = ModelFactory.createOntologyModel(
      getModelSpec(maker), base);
      return newmodel;
  }
  public static OntModelSpec getModelSpec(ModelMaker maker) {
      OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
      spec.setImportModelMaker(maker);
      return spec;
  }
  /* 简单读取本体中的各个class */
  public static void SimpleReadOntology(OntModel model) {
      for (Iterator i = model.listClasses(); i.hasNext();) {
          OntClass c = (OntClass) i.next();
          System.out.println(c.getLocalName());
      }
  }

}

我是通过这个学习的
初次学习,按部就班
就是没有对应的查询,网上好多介绍查询的,就是很少有例子,能帮忙提供点资料吗??非常感谢


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
2,496.094ms