1

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.

2

2 Answers 2

0

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
}
3
  • 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." This is not strictly true. If there's a unique path, you can extract it. See my answer to another question. Mar 6, 2015 at 15:16
  • No, it is strictly true (there's no equivalent of parentheses in regular expressions to extract the matched property path) but there are ways to try to work out what the path was, as I said and your answer attempts. Paths simply aren't first class things in sparql results.
    – user205512
    Mar 6, 2015 at 16:19
  • "Paths simply aren't first class things in sparql results." I agree completely. My point is just that in many (but certainly not all), it is possible to extract the edges of a single path using a SPARQL query. Mar 6, 2015 at 16:58
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.