是否可以在SPARQL中的RDF集合中获取元素的位置?

是否可以在SPARQL中的RDF集合中获取元素的位置?

假设我有以下Turtle声明:


@prefix : <http://example.org#> .


:ls :list (:a :b :c)

有没有办法获得集合中元素的位置?


例如,使用此查询:


PREFIX :     <http://example.org#>

PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 


SELECT ?elem WHERE {

 ?x :list ?ls .

 ?ls rdf:rest*/rdf:first ?elem .

}

我明白了:


--------

| elem |

========

| :a   |

| :b   |

| :c   |

--------

但我想要一个查询来获取:


--------------

| elem | pos |

==============

| :a   |  0  |

| :b   |  1  |

| :c   |  2  |

--------------

可能吗?


眼眸繁星
浏览 489回答 3
3回答

湖上湖

纯SPARQL 1.1解决方案我扩展了数据以使问题变得更难。让我们在列表中添加一个重复元素,例如,:a最后添加一个元素:@prefix : <http://example.org#> .:ls :list (:a :b :c :a) .然后我们可以使用这样的查询来提取每个列表节点(及其元素)以及列表中节点的位置。我们的想法是,我们可以匹配列表中的所有单个节点[] :list/rdf:rest* ?node。但是,每个节点的位置是列表头部之间的中间节点的数量?node。我们可以通过将模式分解为来匹配每个中间节点[] :list/rdf:rest* ?mid . ?mid rdf:rest* :node .然后,如果我们分组?node,则不同?mid绑定的数量是?node列表中的位置。因此,我们可以使用以下查询(它还抓取rdf:first与每个节点关联的元素)来获取列表中元素的位置:prefix : <https://stackoverflow.com/q/17523804/1281433/>prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?element (count(?mid)-1 as ?position) where {&nbsp;&nbsp; [] :list/rdf:rest* ?mid . ?mid rdf:rest* ?node .&nbsp; ?node rdf:first ?element .}group by ?node ?element----------------------| element | position |======================| :a&nbsp; &nbsp; &nbsp; | 0&nbsp; &nbsp; &nbsp; &nbsp; || :b&nbsp; &nbsp; &nbsp; | 1&nbsp; &nbsp; &nbsp; &nbsp; || :c&nbsp; &nbsp; &nbsp; | 2&nbsp; &nbsp; &nbsp; &nbsp; || :a&nbsp; &nbsp; &nbsp; | 3&nbsp; &nbsp; &nbsp; &nbsp; |----------------------这是有效的,因为RDF列表的结构是这样的链接列表(其中?head是列表的开头(对象:list),并且是?mid因为模式的另一个绑定[] :list/rdf:rest* ?mid):RDF列表的图形表示与Jena ARQ扩展的比较该问题的提问者还发布了一个使用Jena的ARQ扩展来处理RDF列表的答案。该答案中公布的解决方案是PREFIX :&nbsp; &nbsp; &nbsp;<http://example.org#>PREFIX rdf:&nbsp; <http://www.w3.org/1999/02/22-rdf-syntax-ns#>&nbsp;PREFIX list: <http://jena.hpl.hp.com/ARQ/list#>SELECT ?elem ?pos WHERE {&nbsp;?x :list ?ls .&nbsp;?ls list:index (?pos ?elem).}这个答案取决于使用Jena的ARQ和启用扩展,但它更简洁和透明。不明显的是一个人是否有明显更好的表现。事实证明,对小名单,差别不是特别显著,但对于大名单中,ARQ扩展有很多更好的性能。纯SPARQL查询的运行时间变得非常长,而使用ARQ扩展的版本几乎没有差异。-------------------------------------------| num elements | pure SPARQL | list:index |===========================================|&nbsp; &nbsp; &nbsp; 50&nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; 1.1s&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; 0.8s&nbsp; &nbsp; ||&nbsp; &nbsp; &nbsp;100&nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; 1.5s&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; 0.8s&nbsp; &nbsp; ||&nbsp; &nbsp; &nbsp;150&nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; 2.5s&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; 0.8s&nbsp; &nbsp; ||&nbsp; &nbsp; &nbsp;200&nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; 4.8s&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; 0.8s&nbsp; &nbsp; ||&nbsp; &nbsp; &nbsp;250&nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; 9.7s&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; 0.8s&nbsp; &nbsp; |-------------------------------------------这些具体值明显会因您的设置而异,但总体趋势应该可以在任何地方观察到。由于将来可能会发生变化,这里是我正在使用的ARQ的特定版本:$ arq --versionJena:&nbsp; &nbsp; &nbsp; &nbsp;VERSION: 2.10.0Jena:&nbsp; &nbsp; &nbsp; &nbsp;BUILD_DATE: 2013-02-20T12:04:26+0000ARQ:&nbsp; &nbsp; &nbsp; &nbsp; VERSION: 2.10.0ARQ:&nbsp; &nbsp; &nbsp; &nbsp; BUILD_DATE: 2013-02-20T12:04:26+0000因此,如果我知道我必须处理非平凡大小的列表并且我有ARQ可用,我会使用扩展名。

动漫人物

简短的回答不是没有超出标准,除非你的名单长度有限,那么你可以做一些像脏的事情:{&nbsp;?x&nbsp;:list&nbsp;(:a)&nbsp;BIND(1&nbsp;AS&nbsp;?length)&nbsp;}UNION{&nbsp;?x&nbsp;:list&nbsp;([],&nbsp;:a)&nbsp;BIND(2&nbsp;AS&nbsp;?length)&nbsp;}UNION{&nbsp;?x&nbsp;:list&nbsp;([],&nbsp;[],&nbsp;:a)&nbsp;BIND(3&nbsp;AS&nbsp;?length)&nbsp;}...等等某些RDF查询引擎具有可在RDF列表上运行的非标准功能,但您必须查阅系统的文档。答案很长这是RDF列表的一个症状,具有可怕的结构和定义。不知何故,我们最终得到了两种表示列表的方式,这两种方式都很难用!如果您控制数据,请使用更合理的表示,例如<x>&nbsp;:member&nbsp;[ &nbsp;&nbsp;&nbsp;rdf:value&nbsp;:a&nbsp;; &nbsp;&nbsp;&nbsp;:ordinal&nbsp;1&nbsp;;],&nbsp;[ &nbsp;&nbsp;&nbsp;rdf:value&nbsp;:b&nbsp;; &nbsp;&nbsp;&nbsp;:ordinal&nbsp;2&nbsp;;],&nbsp;[ &nbsp;&nbsp;&nbsp;rdf:value&nbsp;:c&nbsp;; &nbsp;&nbsp;&nbsp;:ordinal&nbsp;3&nbsp;;]...然后你可以查询:{&nbsp;<x>&nbsp;:member&nbsp;[&nbsp;rdf:value&nbsp;:a&nbsp;;&nbsp;:ordinal&nbsp;?position&nbsp;]&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP