使用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. Java并发--线程间协作的两种方式:wait、notify、notifyAll和Condition

    在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...

  2. Codeforces 1030E 【暴力构造】

    LINK 题目大意:给你n个数,你可以交换一个数的任意二进制位,问你可以选出多少区间经过操作后异或和是0 思路 充分必要条件: 区间中二进制1的个数是偶数 区间中二进制位最多的一个数的二进制个数小于等 ...

  3. (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切

    if (this == null) Console.WriteLine("this is null"); 这句话一写,大家一定觉得荒谬,然而 if 内代码的执行却是可能的!本文讲介 ...

  4. A Corrupt Mayor's Performance Art

    Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless pain ...

  5. 《DSP using MATLAB》示例 Example6.7

    代码: M = 32; alpha = (M-1)/2; magHk = [1, 1, 1, 0.5, zeros(1, 25), 0.5, 1, 1]; k1 = 0:15; k2 = 16:M-1 ...

  6. bzoj 3779 重组病毒——LCT维护子树信息

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 调了很久……已经懒得写题解了.https://www.cnblogs.com/Zinn ...

  7. 从汇编的角度看待const与#define

    先观察一下的代码: #include<stdio.h> int main(){ ; int y; int *pi=(int*)&i; *pi=; y=*pi; int tempi; ...

  8. Java格式化时间为String类型

    SimpleDateFormat ormater = new SimpleDateFormat("yyyy-MM-dd"); Date date=new Date(); Strin ...

  9. python 多线程要点

    要点整理 多线程 #coding=utf-8 import threading from time import ctime,sleep def music(func): for i in range ...

  10. java Annotation的应用

    一.Annotation 示例 Override Annotation @Override public void onCreate(Bundle savedInstanceState); 二.Ann ...