Routes 如何以编程方式列出本体中两个实例之间的关系?

Routes 如何以编程方式列出本体中两个实例之间的关系?,routes,jena,ontology,instances,Routes,Jena,Ontology,Instances,我有一个本体论,它由以下关系组成 Company1 :hasSubsidary sub1 :hasDepartment Cars1 :hasSubdepartment Manufacturing :isinBuilding area1 :hasUnit PrecisionMaching :hasMachine LatheMachine1 我有一个本体模型,其中创建了这些个体并描述了它们之间的关系 如果输入参数是Company1,Lathemachine1,

我有一个本体论,它由以下关系组成

Company1 
  :hasSubsidary sub1 
  :hasDepartment Cars1 
  :hasSubdepartment Manufacturing 
  :isinBuilding  area1 
  :hasUnit PrecisionMaching 
  :hasMachine LatheMachine1
我有一个本体模型,其中创建了这些个体并描述了它们之间的关系


如果输入参数是
Company1
Lathemachine1
,使用Jena或任何其他API,我怎么能用语法列出所有关系路径呢?

你没有说你的本体的名称空间是什么,所以我猜测它是
http://example.com/ontologies/test#
。鉴于此,您有两个基本选择:使用SPARQL,或直接使用Jena RDF API

在第一种情况下,您的查询非常简单:

prefix ex: <http://example.com/ontologies/test#>
select distinct ?relationship where { ex:Company1 ?relationship ex:LatheMchine1 }

如果您确实需要所有路径,可以使用
findShortestPath
中的代码作为模板进行简单的广度优先搜索。

是的,所有路径都在同一名称空间下,但我的问题比这更复杂。RDF文件中定义的关系如下。车床和公司之间没有直接关系。更清楚地说,公司1:有附属sub1,sub1有部门Cars1,Cars1在建筑区域1,区域1有单元精密加工,精密加工有机床1。我想找出这条路1号公司和车床是如何联系的。谢谢你的回复,虽然我本应该更好地解释我的问题。
Model m = ... your RDF model ... ;
String NS = "http://example.com/ontologies/test#";
Resource company1 = m.getResource( NS + "Company1" );
Resource lathe1 = m.getResource( NS + "LatheMachine1" );

Set<Property> relationships = new HashSet<Property>();
for (StmtIterator i = m.listStatements( company1, null, lathe1 ); i.hasNext();) {
  Statement s = i.next();
  relationships.add( s.getPredicate() );
}
Path p = OntTools.findShortestPath( m, company1, lathe1, Filter.any() );