getting a graph path using SPARQL [duplicate]

荒凉一梦 提交于 2019-12-31 03:43:10

问题


We have the following turtle dataset representing a graph where we want to observe some properties.

@prefix v1: <http://localhost:9091/graphe/> .
@prefix v2: <http://localhost:9091/graphe#> .
v1:a v2:p v1:b.
v1:a v2:q v1:f.
v1:a v2:p v1:g.
v1:b v2:p v1:c.
v1:c v2:q v1:h.
v1:c v2:p v1:i.
v1:c v2:p v1:d.
v1:d v2:p v1:e.
v1:f v2:p v1:g.
v1:f v2:q v1:l.
v1:f v2:p v1:k.
v1:g v2:p v1:c.
v1:g v2:p v1:f.
v1:h v2:p v1:n.
v1:i v2:q v1:j.
v1:j v2:p v1:o.
v1:j v2:q v1:n.
v1:k v2:p v1:l.
v1:l v2:p v1:g.
v1:m v2:q v1:g.
v1:n v2:p v1:m. 

The query we are expecting to write must print all the full paths between a source and a destination node.

PREFIX g: <http://localhost:9091/graphe-ttl-1>
PREFIX t: <http://localhost:9091/graphe#>
PREFIX o: <http://localhost:9091/graphe/>
SELECT * WHERE {
GRAPH g: {
      o:a t:p{*} o:o .
}}

Here is the query we wrote so far but we are getting no output since we dont have any variables expression. Thank you for your help.


回答1:


There's no way to do that with sparql property paths: you just find out whether there is a path, not what the path is.

You could try:

o:a t:p{*} ?middle .
?middle t:p{*} o:o .

but I'm not convinced that will work.

Edit: ok, that does work (I thought * would be greedy), although you won't get the properties. I use !ex:nothing* to indicated 'any property' (there must be a better way):

prefix ex: <http://invalid.org/>
select * {
  o:a !ex:nothing* ?middle .
  ?middle !ex:nothing* o:o
}



回答2:


This will give u path between a & n nodes. select ?start ?end (count(?mid) as ?length) where {values (?start ?end) { (:a :n) } ?start :p+ ?mid . ?mid :p* ?end . }group by ?start ?end



来源:https://stackoverflow.com/questions/28897701/getting-a-graph-path-using-sparql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!