• 使用jena持久化OWL本体到MySQL

    普通类
    • 支持
    • 批判
    • 提问
    • 解释
    • 补充
    • 删除
    • 默认段落标题(请修改)

    /* 连接数据库 */
    public static IDBConnection connectDB(String DB_URL, String DB_USER,
    String DB_PASSWD, String DB_NAME) {
    return new DBConnection(DB_URL, DB_USER, DB_PASSWD, DB_NAME);
    }

    /* 从文件读取本体并将其存入数据库 */
    public static OntModel createDBModelFromFile(IDBConnection con, String name,
    String filePath) {
    ModelMaker maker = ModelFactory.createModelRDBMaker(con);
    Model base = maker.createModel(name);
    OntModel newmodel = ModelFactory.createOntologyModel(
    getModelSpec(maker), base);
    newmodel.read(filePath);
    return newmodel;
    }

    /* 从数据库中得到已存入本体 */
    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;
    }

    下面是测试的代码,先从文件中读取,让后存入数据库中,再从数据库中读出。
    public static void test() {
    String DB_URL = "jdbc:mysql://localhost/expert";
    String DB_USER = "root";
    String DB_PASSWD = "root";
    String DB = "MySQL";
    String DB_DRIVER = "com.mysql.jdbc.Driver";

    try {
    Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }

    String filePath = "file:C://expert//Expert.rdf-xml.owl";
    IDBConnection con = JaneUtils.connectDB(DB_URL,DB_USER, DB_PASSWD, DB);
    System.out.println(con);

    JaneUtils.createDBModelFromFile(con, "expert",filePath);
    OntModel model = JaneUtils.getModelFromDB(con, "expert");
    JaneUtils.SimpleReadOntology(model);
    }

    /* 简单读取本体中的各个class */
    public static void SimpleReadOntology(OntModel model) {
    for (Iterator i = model.listClasses(); i.hasNext();) {
    OntClass c = (OntClass) i.next();
    System.out.println(c.getLocalName());
    }
    }

    • 标签:
    • ontmodel
    • expert
    • static
    • string
    • owl
    • 学习元
    • maker
    • 持久化
    • public
    • filepath
    • mysql
    • model
    • 数据库
    • jena
    • base
  • 加入的知识群:
    学习元评论 (0条)

    评论为空
    聪明如你,不妨在这 发表你的看法与心得 ~



    登录之后可以发表学习元评论
      
暂无内容~~
顶部