Rdf 使用SPARQL和Jena检索OWL类层次结构中的所有路径

Rdf 使用SPARQL和Jena检索OWL类层次结构中的所有路径,rdf,semantic-web,jena,breadth-first-search,Rdf,Semantic Web,Jena,Breadth First Search,我有一个RDF图,它的层次结构有三层。我想检索从类层次结构的根(即,owl:Thing)到第三级中的类的所有路径,而不使用推理器。例如,我想要路径 C1和右箭头; C2&右箭头; C3 是一条路,每一条路 词 是层次结构第i级的类 我需要使用广度优先搜索算法检索RDF图中的所有路径,而不考虑图中的对象属性。给定一些类似的数据(其中类名的长度表示类在层次结构中的深度): 使用相机本体 这种方法适用于注释中提到的,尽管查询需要一些扩展来处理更深层的类路径。因此: PREFIX rdfs: <h

我有一个RDF图,它的层次结构有三层。我想检索从类层次结构的根(即,
owl:Thing
)到第三级中的类的所有路径,而不使用推理器。例如,我想要路径 C1和右箭头; C2&右箭头; C3 是一条路,每一条路 词 是层次结构第i级的类

我需要使用广度优先搜索算法检索RDF图中的所有路径,而不考虑图中的对象属性。

给定一些类似的数据(其中类名的长度表示类在层次结构中的深度):

使用相机本体 这种方法适用于注释中提到的,尽管查询需要一些扩展来处理更深层的类路径。因此:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX : <http://www.xfront.com/owl/ontologies/camera/#>

select * where { 
  values ?c1 { owl:Thing } 
  ?c1 ^rdfs:subClassOf ?c2 .
  OPTIONAL { 
    ?c2 ^rdfs:subClassOf ?c3 .
    OPTIONAL { 
      ?c3 ^rdfs:subClassOf ?c4 .
    }
  }
}
order by ?c4 ?c3 ?c2
使用Jena API 虽然上面的SPARQL查询按照广度优先遍历的顺序生成路径,但实际上无法保证ARQ如何生成结果。我们还可以直接实现广度优先搜索,使用Jena模型API检索子类。下面是一个简单的实现:

import java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Queue;
导入com.hp.hpl.jena.rdf.model.model;
导入com.hp.hpl.jena.rdf.model.ModelFactory;
导入com.hp.hpl.jena.rdf.model.Resource;
导入com.hp.hpl.jena.rdf.model.stmtilerator;
导入com.hp.hpl.jena.词汇表.OWL;
导入com.hp.hpl.jena.词汇表.RDFS;
公共类BFSInRDFWithJena{
公共静态列表BFS(最终模型、最终队列、最终整数深度){
最终列表结果=新建ArrayList();
而(!queue.isEmpty()){
最终列表路径=queue.poll();
结果。添加(路径);
if(path.size()<深度){
final Resource last=path.get(path.size()-1);
final StmtIterator stmt=model.listStatements(null,RDFS.subcassof,last);
while(stmt.hasNext()){
最终列表extPath=newarraylist(路径);
add(stmt.next().getSubject().asResource());
queue.offer(extPath);
}
}
}
返回结果;
}
公共静态void main(最终字符串[]args)引发IOException{
最终模型=ModelFactory.createDefaultModel();
try(final InputStream in=BFSInRDFWithJena.class.getClassLoader().getResourceAsStream(“camera.owl”)){
model.read(in,null);
}
//设置初始队列
最终队列=新建LinkedList();
最终列表thingPath=new ArrayList();
thingPath.add(OWL.Thing);
queue.offer(thingPath);
//获取路径并显示它们
最终列表路径=BFS(模型,队列,4);
用于(列表路径:路径){
System.out.println(路径);
}
}
}

你把“东西”放在括号里。我是否理解正确:您有一个具有某个根的类层次结构,并且您希望找到从根到子类的所有路径(最大深度为3)?你能提供一个你想要得到的数据和结果的例子吗?这将有助于制定解决方案。是的,我有一个类层次结构,但只有一个根,这是一个根类。以下面的本体为例:我想要的路径应该是Thing-PurchaseableItem-Camera-Digital、Thing-PurchaseableItem-Camera-Large Format等等……以及其他路径。谢谢,谢谢。但这不是我想要的。以下面的本体论为例:我想要的路径应该看起来像Thing PurchaseableItem Camera Digital等等…@user2502144是的,所以你用
owl:Thing
替换了
:a
,而且似乎你想要四层类(因为你包括了
owl:Thing
),你再添加一个
可选的
模式以转到
?c4
@user2502144我已经更新了答案,以显示如何将此查询与链接到的
摄像机.owl
一起使用。Joshua答案是正确的,但这不是我需要检索这些路径的方法。我需要使用Jena中的广度优先搜索算法检索RDF图中的所有路径。谢谢。好的,你的意思是你真的想使用Jena模型或OntModelAPI来编写遍历?看看我发布的关于使用Jena的API在RDF图上执行深度优先搜索的文章。假设您已经对如何实现广度优先和深度优先搜索有了一定的了解,那么这个示例应该充分展示了Jena特定的API,您需要进行BFS。
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?c1 ?c2 ?c3 where { 
  values ?c1 { :a }
  ?c1 ^rdfs:subClassOf ?c2 .
  OPTIONAL {
    ?c2 ^rdfs:subClassOf ?c3 .
  }
}
order by ?c3 ?c2 ?c1
-------------------
| c1 | c2  | c3   |
===================
| :a | :ac |      |
| :a | :aa | :aaa |
| :a | :aa | :aab |
| :a | :aa | :aac |
| :a | :ab | :aba |
| :a | :ab | :abb |
-------------------
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX : <http://www.xfront.com/owl/ontologies/camera/#>

select * where { 
  values ?c1 { owl:Thing } 
  ?c1 ^rdfs:subClassOf ?c2 .
  OPTIONAL { 
    ?c2 ^rdfs:subClassOf ?c3 .
    OPTIONAL { 
      ?c3 ^rdfs:subClassOf ?c4 .
    }
  }
}
order by ?c4 ?c3 ?c2
-----------------------------------------------------------
| c1        | c2                | c3      | c4            |
===========================================================
| owl:Thing | :Money            |         |               |
| owl:Thing | :Range            |         |               |
| owl:Thing | :Window           |         |               |
| owl:Thing | :PurchaseableItem | :Body   |               |
| owl:Thing | :PurchaseableItem | :Lens   |               |
| owl:Thing | :PurchaseableItem | :Camera | :Digital      |
| owl:Thing | :PurchaseableItem | :Camera | :Large-Format |
-----------------------------------------------------------
[http://www.w3.org/2002/07/owl#Thing]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Window]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Range]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Money]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Camera]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Lens]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Body]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Camera, http://www.xfront.com/owl/ontologies/camera/#Digital]
[http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Camera, http://www.xfront.com/owl/ontologies/camera/#Large-Format]