两种资源之间的路径 [英] path between two resources

查看:61
本文介绍了两种资源之间的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以计算通过SPARQL查询连接两个实例的边的数量?我想找到一条路径.

Is it possible to count the number of the edges that connect two instance with a SPARQL query? I want to find a path.

推荐答案

您可以使用SPARQL的属性路径和聚合函数来计算唯一路径中的边数.例如,使用这样的数据,其中包含我们关心的两条路径(具有两个边的 a c ,以及 d g (具有三个边缘):

You count the number of edges in a unique path using SPARQL's property paths and aggregate functions. For instance, with data like this, which contains two paths that we care about (a to c with two edges, and d to g with three edges):

@prefix : <https://stackoverflow.com/questions/19587520/sparql-path-between-two-instance/> .

:a :p :b .  # a to c is a path of length 2
:b :p :c .  

:d :p :e .  # d to g is a path of length 3
:e :p :f .
:f :p :g . 

您可以使用类似以下查询的查询.注意,我使用了特定的属性:p,而不是变量.这是必需的,因为SPARQL 1.1中的 9.1属性路径语法规范不允许在属性路径中使用变量.

you can use a query like the following one. Notice that I've used the specific property :p, rather than a variable. This is necessary, because 9.1 Property Path Syntax from the SPARQL 1.1 specification doesn't allow variables in property paths.

prefix : <https://stackoverflow.com/questions/19587520/sparql-path-between-two-instance/>

select ?start ?end (count(?mid) as ?length)
where {
  values (?start ?end) { (:a :c) (:d :g) }
  ?start :p+ ?mid .
  ?mid :p* ?end .
}
group by ?start ?end 

并获得如下结果:

$ sparql --query query.rq --data data.n3
------------------------
| start | end | length |
========================
| :d    | :g  | 3      |
| :a    | :c  | 2      |
------------------------

有关此处发生的情况的完整说明,请参见:

A fuller description of what's happening here can be found in:

  • Calculate length of path between nodes? (which is actually look at paths in a tree)
  • this answer to Finding all steps in property path (Note that the accepted answer says you can't do this, but the linked answer shows that you actually can); and
  • the accepted answer to Is it possible to get the position of an element in an RDF Collection in SPARQL?.

但是,基本思想是,如果您具有从?start?end的路径,那么对于?mid的许多不同值,您也可以获得从?start?mid的路径. ?mid和从?mid?end的路径.您可以为?mid选择的不同值的数字(如果您允许一个端点,而不允许另一个端点)恰好是路径的长度.

The basic idea, though, is that if you have a path from ?start to ?end, then you've also got, for a bunch of different values of ?mid, a path from ?start to ?mid and a path from ?mid to ?end. The number of different values that you can pick for ?mid (if you allow one of the endpoints, and disallow the other) is exactly the length of the path.

这篇关于两种资源之间的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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