jena读取和解析本体文件
使用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读取和解析本体文件的更多相关文章
- c++ 读取并解析excel文件方法
用Cocos开发模型特效工具编辑器,跨Mac和windows,当中有个需求是读取并解析excel文件,但网上的查找的例子几乎都只能是在windows下面使用,再或者是命令行脚本之类的.于是,自己写了一 ...
- [Xcode 实际操作]七、文件与数据-(8 )读取和解析Plist文件(属性列表文件)
目录:[Swift]Xcode实际操作 本文将演示如何读取和解析Plist文件,即属性列表文件. 它是用来存储,串行化后的对象的文件. 在项目名称上点击鼠标右键,弹出右键菜单, 选择[New File ...
- [cocos2dx utils] cocos2dx读取,解析csv文件
在我们的游戏中,经常需要将策划的数值配置成csv文件,所以解析csv文件就是一个很common的logic, 例如如下csv文件: 下面是一个基于cocos2dx 2.2.4的实现类: #ifndef ...
- 读取并解析properties文件
public class SysConfig { private static final Properties properties = new Properties(); static{ Reso ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
- 使用DOM4J解析XMl文件与读取XML文件
XML文件 <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id ...
- boost::property_tree读取解析.xml文件
boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径 boost::property_tree::wptree wpt; std::locale:: ...
- boost::property_tree读取解析ini文件--推荐
boost::property_tree读取解析ini文件 #include "stdafx.h" #include <iostream> #include <b ...
- 【U1结业机试题】新闻内容管理系统:解析XML文件读取Html模版生成网页文件
一.作业要求: 1.在xml文件中创建新闻节点news,包含标题.作者.日期.正文等信息 2.创建HTML模板文件 3.读取xml中所有新闻信息,并使用新闻信息替换模板文件中占位符,从而为每一条新闻生 ...
随机推荐
- POJ1733 Parity game 【扩展域并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- Codeforces 382E Ksenia and Combinatorics 【组合计数】*
Codeforces 382E Ksenia and Combinatorics Ksenia has her winter exams. Today she is learning combinat ...
- XML专题:使用NSXMLParser解析xml文件
使用NSXMLParser解析xml文件 1. 设置委托对象,开始解析 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data ...
- 正则同时获取a标签里的href,text 2项
Regex regex2 = new Regex(@"<a[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<hre ...
- python模块--os模块的用法
os.getcwd() 获取当前工作的目录,即当前python脚本工作的目录路径 os.phdir("dirname") 改变当前脚本的工作目录:相当于shell下cd os.cu ...
- Python猴子补丁
属性在运行时的动态替换,叫做猴子补丁(Monkey Patch). 为什么叫猴子补丁 属性的运行时替换和猴子也没什么关系,关于猴子补丁的由来网上查到两种说法: 1,这个词原来为Guerrilla Pa ...
- nginx 使用ctx实现数据共享,修改上下文
环境: init_worker_by_lua, set_by_lua, rewrite_by_lua, access_by_lua, content_by_lua, header_filter_by_ ...
- DiscuzX 3. 3搭建和学习
Discuz!全局变量$_G详解 http://jingyan.baidu.com/article/cb5d610516048c005c2fe0c8.html UCenter uc_user_synl ...
- oracle 之 控制oracle RAC 进行并行运算
RAC的一大优点就是可以跨节点进行并行计算,那么如何控制并行运算?这就是这篇文章要讨论的内容. 10 g 中: 合理设置跨节点并行,需要先设置一些参数:instance_groups:这个参数主要是设 ...
- jsp中取两位小数
var d=1.11111111; var c = d.toFixed(2); alert(c);