我正在尝试编写SPARQL查询以返回从源到目标的路径。 下面是代表数据集的Turtle文件。
@prefix node: <http://prism.uvsq.fr/>.
@prefix edge: <http://prism.uvsq.fr#>.
node:a edge:p node:b.
node:a edge:q node:f.
node:a edge:p node:g.
node:b edge:p node:c.
node:c edge:q node:h.
node:c edge:p node:i.
node:c edge:p node:d.
node:d edge:p node:e.
node:f edge:p node:g.
node:f edge:q node:l.
node:f edge:p node:k.
node:g edge:p node:c.
node:g edge:p node:f.
node:h edge:p node:n.
node:i edge:q node:j.
node:j edge:p node:o.
node:j edge:q node:n.
node:k edge:p node:l.
node:l edge:p node:g.
node:m edge:q node:g.
node:n edge:p node:m.
接下来,图像显示相同的信息,以便于查看。
到目前为止我写的查询如下:
prefix graph: <http://prism.uvsq.fr/>
prefix node: <http://prism.uvsq.fr/>
prefix edge: <http://prism.uvsq.fr#>
SELECT * FROM graph: WHERE {
node:a (edge:p|edge:q) ?des.
?des (edge:p|edge:q)* node:h.
}
返回的信息仅显示解决方案的一个级别(它显示了到达目的地的可能的邻居节点)。 在此先感谢您的帮助。 最诚挚的问候
答案 0 :(得分:2)
SPARQL中的属性路径不是您可以直接查询的内容,但您可以使用属性路径来帮助沿两个节点之间的路径提取边。例如,以下查询返回从a到h的路径中的边。基本思想是使用属性路径从一个节点到一个节点u,它有一个边缘到一个节点v,从中有一个到h的路径。值块只是将e的值限制为p或q。
prefix node: <http://prism.uvsq.fr/>
prefix edge: <http://prism.uvsq.fr#>
select distinct ?u ?e ?v where {
values ?e { edge:p edge:q }
node:a (edge:p|edge:q)* ?u .
?u ?e ?v .
?v (edge:p|edge:q)* node:h .
}
----------------------------
| u | e | v |
============================
| node:a | edge:p | node:g |
| node:a | edge:p | node:b |
| node:g | edge:p | node:f |
| node:g | edge:p | node:c |
| node:f | edge:p | node:k |
| node:f | edge:p | node:g |
| node:k | edge:p | node:l |
| node:l | edge:p | node:g |
| node:c | edge:p | node:i |
| node:n | edge:p | node:m |
| node:h | edge:p | node:n |
| node:b | edge:p | node:c |
| node:a | edge:q | node:f |
| node:f | edge:q | node:l |
| node:c | edge:q | node:h |
| node:i | edge:q | node:j |
| node:j | edge:q | node:n |
| node:m | edge:q | node:g |
----------------------------
这并没有给你实际的路径,但它给你所有的,只有从a到h的路径上的边。从那里你可以通过将图形重新组合在一起并执行深度优先遍历来枚举路径来重建路径。