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中所有新闻信息,并使用新闻信息替换模板文件中占位符,从而为每一条新闻生 ...
随机推荐
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- 使 WPF 支持触摸板的横向滚动
微软终于开始学苹果一样好好做触摸板了(就是键盘空格键下面那一大块).然而鉴于以前没有好好做,以至于 WPF 程序甚至都没有对触摸板的横向滚动提供支持(竖向滚动是直接使用了 MouseWheel,汗-- ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- @contextmanager
with的作用,类似try...finally...,提供一种上下文机制. 要应用with语句的类,其内部必须提供两个内置函数__enter__以及__exit__ , 前者在主体代码执行前执行, ...
- The SDK platform-tools version ((21)) is too old to check APIs compiled with API 23
android studio是个坑爹的工具,每次打开文件头都出现如上错误提示. 解决方法: Update your android sdk platform-tools to the revision ...
- Spring Boot 入门之持久层篇(三)
原文地址:Spring Boot 入门之持久层篇(三) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之 Web 篇(二)>介绍了 ...
- 引用 LPSTR、LPCSTR、LPTSTR、LPCTSTR、LPWSTR及LPCWSTR的意义及区别
1.ANSI(即MBCS):为多字节字符集,它是不定长表示世界文字的编码方式.ANSI表示英文字母时就和ASCII一样,但表示其他文字时就需要用多字节. 2.Unicode:用两个字节表示一个字符 ...
- CRITICAL:yum.cli:Config Error: Error accessing file for config file:///etc/yum.conf
先试试yum install gcc , 1,下载最新的yum-3.2.28.tar.gz并解压 #wget http://yum.baseurl.org/download/3.2/yum-3.2. ...
- ssi框架学习总结
框架简介: 相信大家对于mvc的三层架构已经灰常熟悉了,在这就不细讲了,个人感觉ssi的框架结构还是比较典型的mvc三层架构,还是比较容易上手的.关于这块的入门我想特别感谢下FrankHui童鞋,在他 ...
- 用活firewalld防火墙中的zone
原文地址:http://www.excelib.com/article/290/show firewalld中zone的含义学生前面已经给大家介绍过了,说白了一个zone就是一套规则集.可是什么时候该 ...