获取传递关系中的所有节点 [英] Get all nodes in a transitive relation

查看:38
本文介绍了获取传递关系中的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过与 SPARQL 的传递关系获得连接的节点列表?我有以这种方式连接的元素:

Is it somehow possible to get the list of nodes connected through a transitive relation with SPARQL? I have elements that are connected in this way:

?a g:eastOf ?b
?b g:eastOf ?c
…

并非所有节点都相互连接,因为有些节点位于更靠南的地方.只有当节点垂直在同一平面上时,才可能存在 g:eastOf 关系.这意味着有几组节点没有相互连接.

Not all nodes are connected with each other, because some are further down south. Only when nodes are vertically in the same plane there might be a g:eastOf relationship. This means there are several groups of nodes that are not connected with each other.

我想获得所有这些节点组(基本上是一个列表列表).有没有办法在 SPARQL 中做到这一点?SELECT 语句需要有限数量的变量,并且不能以某种方式表达某物的列表".

I'd like to get all these groups of nodes (basically a list of lists). Is there a way to do that in SPARQL? The SELECT statement requires a finite number of variables and can't somehow express "a list of" something.

我只对从西到东时所有节点的 xsd:integer 属性上升的列表感兴趣,但是在我有了第一个解决方案后,这应该相对容易问题.

I'm only interested in the lists where an xsd:integer property of all nodes is ascending when going from west to east, but that should be relatively easy after I have the solution for my first problem.

推荐答案

您至少可以使用 SPARQL 1.1 完成其中的一些工作.

You can do at least some of this with SPARQL 1.1.

假设您有数据,其中有两组基于 :eastOf 的点形成一行:

Assume you have the data in which there are two groups of points that form a row based on :eastOf:

@prefix : <http://stackoverflow.com/q/4056008/1281433/>

:a :eastOf :b .
:b :eastOf :c .
:c :eastOf :d .

:e :eastOf :f .
:f :eastOf :g .
:g :eastOf :h .

然后你可以像这样使用查询:

Then you can use query like this:

prefix : <http://stackoverflow.com/q/4056008/1281433/>

select (group_concat(strafter(str(?westernpoint),str(:));separator=", ") as ?colatitudinalPoints)
 where {
  ?easternmost :eastOf* ?westernpoint .
  filter not exists { ?easternmoster :eastOf ?easternmost }
}
group by ?easternmost

获得这些结果:

-----------------------
| colatitudinalPoints |
=======================
| "e, f, g, h"        |
| "a, b, c, d"        |
-----------------------

有一些字符串处理

group_concat(strafter(str(?westernpoint),str(:));separator=", ") as ?colatitudinalPoints

你可能不需要;关键是 ?westernpoint?easternmost 以东以西的点的 IRI(实际上包括 ?eastmost,因为我们在属性路径中使用了 *),然后我们需要以某种方式将它们连接在一起.这里我做了一些字符串处理,使结果更漂亮.你也可以轻松做到

that you might not need; the point is that ?westernpoint is the IRI of the points west of the east of the ?easternmost (which actually includes ?easternmost, since we used * in the property path), and then we need to concatenate those together somehow. Here I did some string processing to make the results prettier. You can just as easily do

prefix : <http://stackoverflow.com/q/4056008/1281433/>

select (group_concat(?westernpoint;separator=", ") as ?colatitudinalPoints)
 where {
  ?easternmost :eastOf* ?westernpoint .
  filter not exists { ?easternmoster :eastOf ?easternmost }
}
group by ?easternmost

并得到

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| colatitudinalPoints                                                                                                                                                                      |
============================================================================================================================================================================================
| "http://stackoverflow.com/q/4056008/1281433/e, http://stackoverflow.com/q/4056008/1281433/f, http://stackoverflow.com/q/4056008/1281433/g, http://stackoverflow.com/q/4056008/1281433/h" |
| "http://stackoverflow.com/q/4056008/1281433/a, http://stackoverflow.com/q/4056008/1281433/b, http://stackoverflow.com/q/4056008/1281433/c, http://stackoverflow.com/q/4056008/1281433/d" |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

确保节点上升

您的其他要求更具挑战性:

Ensuring that the nodes are ascending

Your other requirement is a bit more of a challenge:

我只对 xsd:integer 属性的列表感兴趣从西到东时,节点正在上升,但这应该是有了第一个问题的解决方案后,相对容易了.

I'm only interested in the lists where an xsd:integer property of all nodes is ascending when going from west to east, but that should be relatively easy after I have the solution for my first problem.

也就是说,如果你能在类似

That said, if you can clarify exactly what you want in the case of something like

w --eastof-> x --eastof-> y --eastof-> z
|            |            |            |
5            6            2            3

例如,你想拒绝整个链,因为节点不是all升序,还是你想得到两个子链wxyz 其中值是升序的?两者都有可能……

E.g., do you want to reject the whole chain because the nodes aren't all ascending, or do you want to get the two subchains w x and y z wherein the values are ascending? It might be possible to do both…

这篇关于获取传递关系中的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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