在 SPARQL 中查询 Graph 路径 [英] Querying a Graph path in SPARQL

查看:48
本文介绍了在 SPARQL 中查询 Graph 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写 SPARQL 查询以返回从源到目标的路径.下面是代表数据集的 Turtle 文件.

I am trying to write a SPARQL query to return a path from a source to a destination. Below is the Turtle file representing the data set.

@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.

接下来的图像显示了相同的信息,以便于可视化.

The image next presents the same information, for easier visualization.

到目前为止我写的查询如下:

The query I wrote so far is the following:

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.
}

返回的信息只显示了解决方案的一个级别(它显示了到达目的地的可能邻居节点).在此先感谢您的帮助.最好的问候

The returned information only shows one level of the solution (it shows the possible neighbor nodes for reaching the destination). Thanks in advance for your help. Best Regards

推荐答案

SPARQL 中的属性路径不是您可以直接查询的东西,但您可以使用属性路径来帮助提取沿两个节点之间路径的边.例如,以下查询返回路径中从 a 到 h 的边.基本思想是使用从 a 到某个节点 u 的属性路径,该路径具有到某个节点 v 的边,从该节点 v 有一条到 h 的路径.values 块只是将 e 的值限制为 p 或 q.

Property paths in SPARQL are not things that you can query directly, but you can use property paths to help extract the edges along a path between two nodes. For instance, the following query returns the edges in paths from a to h. The basic idea is to use a property path to from a to some node u which has an edge to some node v from which there is a path to h. The values block just limits the value of e to be either p or 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 的路径上的边.据此,您可以通过将图重新组合在一起并执行深度优先遍历以枚举路径来重建路径.

That doesn't give you the actual paths, but it gives you all and only the edges that are on paths from a to h. From that you can reconstruct paths by putting the graph back together and performing a depth first traversal to enumerate the paths.

这篇关于在 SPARQL 中查询 Graph 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆