使用jena开发本体应用程序时,首先需要对我们利用本体构建工具,如protege等,构建的本体文件,如owl、rdf等读取并解析得到本体模型。下面给出相应的代码,不对的地方请指正。

(基于jena 2.13.0,jena 3 需要java 1.8)

方式一:使用RDFDataMgr

    /**
* 使用RDFDataMgr读取RDF文件
*
* @param fileName URI或 file:filename形式路径
* @return
*/
public static OntModel openRDFFile(String fileName)
{
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
RDFDataMgr.read(model, fileName);
return model;
}

方式二:使用FileManager

    /**
* 打开并读取RDF文件
*
* @param fileName
* @return
*/public static OntModel openRdfFile(String fileName)
{
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
InputStream in = FileManager.get().open(fileName);
OntModel baseOntModel = null;
if (in != null)
{
baseOntModel = (OntModel) model.read(in, "");
}
return baseOntModel;
}

上面两种方式都没有处理本体导入(import)问题,也即不能同时读取你在构建本体时引入的其他本体文件。为了处理引用的本体,本人目前发现最好的方式是使用配置文件:location-mapping.ttl

@prefix rdf:        <http://www.w3.org///-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org///rdf-schema#> .
@prefix xsd: <http://www.w3.org//XMLSchema#> .
@prefix lm: <http://jena.hpl.hp.com///location-mapping#> . # 地址映射
# Application location to alternative location mappings.
# + name 指本体本体文件的uri。可以是http开头的url,也可以是file:开头的文件地址。引入文件中的name一般是uri
# + altName 指文件实际的地址,可以是file:开头的本地文件,或网络地址
# + 顺序无关.
#
# The translation algorithm is:
#
# - Exact mappings: these are tried before attempting a prefix match.
# - By prefix: find the longest matching prefix
# - Use the original if no alternative. [] lm:mapping
[
lm:prefix "GTS" ;
lm:altPrefix "http://www.geodataont.cn/support/geology/gts"
],
[
lm:name "http://www.geodataont.cn/support/geology/gts" ;
lm:altName "file:*/GTS.owl" # 请将*替换为文件目录
]

注:prefix部分我暂时没明白啥意思,不一定是对的

prefix部分补充当利用 lm:altName 找不到对应文件时,会查找与lm:name匹配的最长的lm:prefix,利用其lm:altPrefix和lm:altName组合到一起最为地址去查找文件。

若有:

lm:name "http://www.geodataont.cn/support/geology/gts/a.owl" ;
lm:altName "file:*/a.owl"

则可以配置为:

lm:prefix "http://www.geodataont.cn/support/geology/gts" ;
lm:altPrefix "file:*/"
lm:name "http://www.geodataont.cn/support/geology/gts/a.owl" ;
lm:altName "a.owl"

(2016-04-09)

-------------------------------

补充(2017-05-08):Jena中处理prefix的部分代码存在bug,本人已在在stackoverflow指出: http://stackoverflow.com/questions/43680583/how-to-use-prefix-and-altprefix-in-location-mapping-ttl-to-load-owl-file

因此,目前此部分尚不能使用

-------------------------------

相应的代码如下

    /**
* 读取本体,使用location-mapper确定引用文件地址
*
* @param locMapperPath
* @param baseOntPath 读取的本体文件路径
* @return
*/
public static OntModel loadOntModelWithLocMapper(String locMapperPath, String baseOntPath)
{
OntModel model = ModelFactory.createOntologyModel(); LocationMapper locMapper = readLocationMapper(locMapperPath); model.getDocumentManager().setProcessImports(true);
FileManager fileManager = model.getDocumentManager().getFileManager();
fileManager.setLocationMapper(locMapper);
baseOntPath = StringUtil.getUriFilePath(baseOntPath);// 添加file:,替换\
fileManager.readModel(model, baseOntPath);
model.loadImports(); return model;
}

其中getUriFilePath是将文件地址进行转换,方便读取

public static String getUriFilePath(String filepath)
{
if (filepath.startsWith("file:") || filepath.startsWith("http://"))
{
return filepath;
}
else
{
filepath = "file:" + filepath;
filepath = filepath.replaceAll("\\\\", "/");
return filepath;
}
}

目前基本功能就是这样,详细内容参考jena的文档。不对的地方一起讨论

jena读取和解析本体文件的更多相关文章

  1. c++ 读取并解析excel文件方法

    用Cocos开发模型特效工具编辑器,跨Mac和windows,当中有个需求是读取并解析excel文件,但网上的查找的例子几乎都只能是在windows下面使用,再或者是命令行脚本之类的.于是,自己写了一 ...

  2. [Xcode 实际操作]七、文件与数据-(8 )读取和解析Plist文件(属性列表文件)

    目录:[Swift]Xcode实际操作 本文将演示如何读取和解析Plist文件,即属性列表文件. 它是用来存储,串行化后的对象的文件. 在项目名称上点击鼠标右键,弹出右键菜单, 选择[New File ...

  3. [cocos2dx utils] cocos2dx读取,解析csv文件

    在我们的游戏中,经常需要将策划的数值配置成csv文件,所以解析csv文件就是一个很common的logic, 例如如下csv文件: 下面是一个基于cocos2dx 2.2.4的实现类: #ifndef ...

  4. 读取并解析properties文件

    public class SysConfig { private static final Properties properties = new Properties(); static{ Reso ...

  5. Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件

    写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...

  6. 使用DOM4J解析XMl文件与读取XML文件

    XML文件 <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id ...

  7. boost::property_tree读取解析.xml文件

    boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径  boost::property_tree::wptree wpt;    std::locale:: ...

  8. boost::property_tree读取解析ini文件--推荐

    boost::property_tree读取解析ini文件 #include "stdafx.h" #include <iostream> #include <b ...

  9. 【U1结业机试题】新闻内容管理系统:解析XML文件读取Html模版生成网页文件

    一.作业要求: 1.在xml文件中创建新闻节点news,包含标题.作者.日期.正文等信息 2.创建HTML模板文件 3.读取xml中所有新闻信息,并使用新闻信息替换模板文件中占位符,从而为每一条新闻生 ...

随机推荐

  1. Set集合学习

    Java中的Set主要有:HashSet.TreeSet.LinkedHashSet. 一:HashSet HashSet 是一个没有重复元素的无序集合. HashSet由HashMap实现的,不保证 ...

  2. 使用 commander && inquirer 构建专业的node cli

    备注:   比较简单就是使用nodejs 的两个类库帮助我们进行开发而已,具体的使用参考类库文档 1. 项目初始化 a. 安装依赖 yarn init -y yarn add commander in ...

  3. vue a href="tel" 拨打电话

    走默认的方式失败 <a href="tel:{{telPhone}}">{{telPhone}}</a> 走字符串拼接方法成功 <a :href=&q ...

  4. angular的指令独立作用域(以及$watch的使用)

    在编写指令的时候,会有一个独立作用域的问题(scope),他默认的是 scope:false 不创建自己的作用域,直接使用的就是父级的作用域, 问题:容易出现全局的污染,是的指令的重复性使用回出现一些 ...

  5. MOSS 2013研究系列---Win2008R2 建立域控时候,报密码不符合要求解决办法

    今天远程给Win2008R2装AD域控的时候,突然报如下的错误页面: 修改了密码,将密码强度设置复杂了,但是,仍然会弹出这个错误页面,估计是因为远程账号的关系,于是再网上搜下了一下,找到了一个解决方案 ...

  6. Form 总结

    禁止input自动完成下拉 //ie: autocomplete="off" //ff: disableautocomplete <input size="40&q ...

  7. 【转】挟天子以令诸侯博客关于TCP/IP模型与OSI模型的区别

    挟天子以令诸侯 博客园 首页 新随笔 联系 订阅 管理 随笔 - 21  文章 - 0  评论 - 9 TCP/IP四层模型与OSI参考模型   TCP/IP四层模型: 1.链路层(数据链路层/网络接 ...

  8. 初学java记录

    记录一: if语句: if(x < y) System.out.println("x is less than y"); 记录二: 强制转换字符类型赋值的方法: num2= ...

  9. java代码----------实现创建DataInputStream和DataOutputStream进行读写

    总结: 主要是 捕获异常 package com.a.b; import java.io.*; public class testData { public static void main(Stri ...

  10. 分布式事务之:TCC几个框架的测试情况记录

    国内主要的开源TCC分布式事务框架包括 框架名称 Github地址  star数量  tcc-transaction  https://github.com/changmingxie/tcc-tran ...