首页 文章

SPARQL zick-zack模式查询

提问于
浏览
1

我有一个SPARQL查询模仿zick-zack模式,如下所示 .

?p1 :infector ?p.
?p2 :infector ?p1.
?p3 :infector ?p2.
?p4 :infector ?p3.
?p5 :infector ?p4
.................

基本上,在一个三元组的模式主题中用作下一个的对象 . 有没有办法概括这种模式?因此,我不需要在模式中使用一长串变量(?p-?p5) . 另外,在运行查询多次之前,我不知道需要多少这样的变量 . 因此,我无法想出一组已定义的变量 . 我需要通用的东西 . 如果您有任何想法使这个查询通用,请告诉我 . 我将非常感谢任何帮助 .

澄清:

我有一个如下的RDF图 .

<http://ndssl.bi.vt.edu/chicago/dendrogram/experiment_id#7385/cell_id#86304/infectee_pid#446734805/iteration#0> <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infector_pid> <http://ndssl.bi.vt.edu/chicago/person/pid#449563560>.

<http://ndssl.bi.vt.edu/chicago/dendrogram/experiment_id#7385/cell_id#86304/infectee_pid#446734805/iteration#0> <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infectee_pid> <http://ndssl.bi.vt.edu/chicago/person/pid#446734805>

<http://ndssl.bi.vt.edu/chicago/dendrogram/experiment_id#7385/cell_id#86304/infectee_pid#446753456/iteration#0> <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infector_pid> <http://ndssl.bi.vt.edu/chicago/person/pid#446734805>.

<http://ndssl.bi.vt.edu/chicago/dendrogram/experiment_id#7385/cell_id#86304/infectee_pid#446753456/iteration#0> <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infectee_pid> <http://ndssl.bi.vt.edu/chicago/person/pid#446753456>.

.......................................................................

以下SPARQL查询可以获取上面提到的现有链RDF图 .

select * from <http://ndssl.bi.vt.edu/chicago/> where
where { 
{
?s <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infector_pid> ?o1.
?s <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infectee_pid> ?o2
}

{
?s1 <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infector_pid> ?o2.
?s1 <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infectee_pid> ?o3
}
 ..........................................................................

}

此链类型的查询包含两个部分,其中来自查询的第一部分的感染者ID用作第二部分的感染者 . 在我的查询中,我有很多部分 . 有没有什么方法可以概括它?因此,使用这么多部件的插入,我可以使用一个部分并获得结果 . BTW我也需要路径长度和中间节点信息 .

1 回答

  • 2

    基本上,在一个三元组的模式主题中用作下一个的对象 . 有没有办法概括这种模式?

    首先,请注意,如果你考虑在另一个方向上的三重模式,那么它不是一个曲折,而只是一个链:

    ?p5 :infector ?p4 .
    ?p4 :infector ?p3 .
    ?p3 :infector ?p2 .
    ?p2 :infector ?p1 .
    ?p1 :infector ?p0 .
    

    使用重复属性路径很容易捕获:

    ?p5 :infector* ?p0
    

    如果要查看,可以反转方向?首先通过反转属性路径的方向在查询文本中显示p0:

    ?p0 ^:infector* ?p5
    

    我还需要知道路径长度和中间节点信息 .

    因为你说的是"path length",听起来你想要一个最大路径 . 这会让事情变得有点棘手,但你仍然可以做到这一点 . 您可以从Is it possible to get the position of an element in an RDF Collection in SPARQL?应用该方法 . 要获得从?begin到?end的路径长度,您可以执行以下操作:

    select ?begin ?end (count(?mid) as ?length) {
      ?end :infector* ?mid .
      ?mid :infector* ?begin .
    }
    group by ?begin ?end
    

    这将找到每个:infector路径的长度 . 如果您只想要最大路径,则需要确保路径不能从?begin或?end向任一方向扩展:

    select ?begin ?end (count(?mid) as ?length) {
      ?end :infector* ?mid .
      ?mid :infector* ?begin .
    
      filter not exists { ?begin :infector ?beginEx }
      filter not exists { ?endEx :infector ?end }
    }
    group by ?begin ?end
    

    这需要对?mid变量进行分组,因此您可以获得长度,但是当您没有获得长度时,您可以获得有关中间节点的信息:

    select * {
      ?end :infector* ?mid .
      ?mid :infector* ?begin .
    
      filter not exists { ?begin :infector ?beginEx }
      filter not exists { ?endEx :infector ?end }
    
      #-- information about ?mid, e.g,. 
      #-- ?mid rdfs:label ?midLabel . 
    }
    

相关问题