Apache Jena不知道是我了解的少还是咋的,可能这个领域本身就比较冷门,网上的资料少之甚少,只能靠自己一点点摸索了
LiteralRequiredException
今天写bug的时候蹦出来一个新的错误
Exception in thread "main" org.apache.jena.rdf.model.LiteralRequiredException: http://www.daml.org/services/owl-s/1.1/Process.owl#Input
at org.apache.jena.rdf.model.impl.StatementImpl.getLiteral(StatementImpl.java:113)
查阅Jena javadoc后得到如下英文
Exception to throw when an RDFNode required to be a Literal isn't, or when a literal Node is required but a non-literal Node supplied.
结合我出错的语句
System.out.println("statement.getLiteral():"+statement.getLiteral());
可以看出getLinteral()
这个函数要求满足条件是对应的Statement对象是Literal类型的,而本次调用的statement的类型如下所示
statement.getClass():class org.apache.jena.rdf.model.impl.StatementImpl
很明显并不是Literal类型
最后再放上getLiteral()函数的定义
getLiteral
Literal getLiteral()
Return the object of the statement.
An exception will be thrown if the object is not a Literal.
Returns:
The Literal which is the object of the statement.
有类似问题的函数还有
- getBoolean()
- getByte()
- getShort()
- getLong()
- getChar()
- getFloat()
- getString()
- getInt()
- getLanguage()
getSubject() , getPredicate(), getObject()
学过英语的人应该对这三个词语不陌生
Subject即主语,Predicate即谓语, Object即宾语
我们都知道三元组
形式其实和主谓宾结构类似
因此这三个函数就是从statement中抽取“主语”,“谓语”和“宾语”
Nt数据statement
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#平均负荷, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.daml.org/services/owl-s/1.1/Process.owl#Input]
定义三个不同类型变量如下
Resource subject=statement.getSubject();
Property property=statement.getPredicate();
RDFNode objectNode=statement.getObject();
分别输出三个变量.toString()如下
Resource:file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#平均负荷
Property:http://www.w3.org/1999/02/22-rdf-syntax-ns#type
object:http://www.daml.org/services/owl-s/1.1/Process.owl#Input
解析OWL文件
本次解析OWL文件来自以下地址
数据来源
首先将OWL数据存放在.owl文件中
还真够多的
随后进入eclipse开始撸代码
- 创建模型
Model model3 = ModelFactory.createDefaultModel();
- 读取程序
是一个返回模型的函数
在函数体中创建模型Model
随后创建输入流
InputStream inputStream = FileManager.get().open(FileName);
然后直接使用Model类型的read方法即可,返回模型
完整代码
Model FileRead(String FileName)
{
Model model2 = ModelFactory.createDefaultModel();
InputStream inputStream = FileManager.get().open(FileName);
if(inputStream==null) {
throw new IllegalArgumentException("File"+inputStream+"not found");
}
model2.read(inputStream,null);
return model2;
}
至此该文件中的内容已经被读取到所建立的模型中了
接下来就是测试读取效果
3. 输出函数
StmtIterator list = model3.listStatements(null,null,(RDFNode)null);
while(list.hasNext())
System.out.println(list.next());
建立Statement迭代器list,随后直接输出,结果如下
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最低负荷, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.daml.org/services/owl-s/1.1/Process.owl#Input]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最高负荷, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.daml.org/services/owl-s/1.1/Process.owl#Input]
[2e9ea962-4d4f-4ade-b11a-79aa937cc93f, http://www.daml.org/services/owl-s/1.1/Process.owl#process, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess]
[2e9ea962-4d4f-4ade-b11a-79aa937cc93f, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.daml.org/services/owl-s/1.1/Process.owl#Perform]
[c8f03d9b-cab1-4f53-b619-1210272964f3, http://www.daml.org/services/owl-s/1.1/Process.owl#components, cfb2c7df-a47b-43d9-aa1a-968d9c67305c]
[c8f03d9b-cab1-4f53-b619-1210272964f3, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.daml.org/services/owl-s/1.1/Process.owl#Sequence]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess, http://www.daml.org/services/owl-s/1.1/Process.owl#hasOutput, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#负荷预警信息]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess, http://www.daml.org/services/owl-s/1.1/Process.owl#composedOf, c8f03d9b-cab1-4f53-b619-1210272964f3]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess, http://www.daml.org/services/owl-s/1.1/Process.owl#hasInput, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最低负荷]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess, http://www.daml.org/services/owl-s/1.1/Process.owl#hasInput, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最高负荷]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess, http://www.daml.org/services/owl-s/1.1/Service.owl#describes, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadService]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess, http://www.daml.org/services/owl-s/1.1/Process.owl#hasResult, 93bb8120-b7db-41d3-8c9a-f32d7c62073e]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProcess, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.daml.org/services/owl-s/1.1/Process.owl#CompositeProcess]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProfile, http://www.daml.org/services/owl-s/1.1/Profile.owl#textDescription, "Composite Service"]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProfile, http://www.daml.org/services/owl-s/1.1/Service.owl#presentedBy, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadService]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProfile, http://www.daml.org/services/owl-s/1.1/Profile.owl#hasInput, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最高负荷]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProfile, http://www.daml.org/services/owl-s/1.1/Profile.owl#hasInput, file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最低负荷]
[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#PayloadProfile, http://www.w3.org/2000/01/rdf-schema#label, "Composite Service"]
数据解析成功
随后可以用上面提到的三个提取主谓宾的方法来提取信息
statement:[file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最高负荷, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.daml.org/services/owl-s/1.1/Process.owl#Input]
ResourceParse
Resource:file:/D:/WorkSpace/CreateOWL-SFile/ontologyFiles/jiaxidianli1.owl#最高负荷
Property:http://www.w3.org/1999/02/22-rdf-syntax-ns#type
object:http://www.daml.org/services/owl-s/1.1/Process.owl#Input
statement.getClass():class org.apache.jena.rdf.model.impl.StatementImpl
注意
在执行函数
StmtIterator list = model3.listStatements(null,null,(RDFNode)null);
第三个参数处的null务必要强制类型转换成RDFNode,否则会报编译错误
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method listStatements(Resource, Property, RDFNode) is ambiguous for the type Model
Q.E.D.
Comments | 1 条评论