1

I am trying to write a SPARQL query to return a path from a source to a destination. Below is the Turtle file representing the data set.

@prefix node: <http://prism.uvsq.fr/>.
@prefix edge: <http://prism.uvsq.fr#>.
node:a edge:p node:b.
node:a edge:q node:f.
node:a edge:p node:g.
node:b edge:p node:c.
node:c edge:q node:h.
node:c edge:p node:i.
node:c edge:p node:d.
node:d edge:p node:e.
node:f edge:p node:g.
node:f edge:q node:l.
node:f edge:p node:k.
node:g edge:p node:c.
node:g edge:p node:f.
node:h edge:p node:n.
node:i edge:q node:j.
node:j edge:p node:o.
node:j edge:q node:n.
node:k edge:p node:l.
node:l edge:p node:g.
node:m edge:q node:g.
node:n edge:p node:m.

The image next presents the same information, for easier visualization.

Graph nodes and edges

The query I wrote so far is the following:

prefix graph: <http://prism.uvsq.fr/>
prefix node: <http://prism.uvsq.fr/>
prefix edge: <http://prism.uvsq.fr#>
SELECT * FROM graph: WHERE {
   node:a (edge:p|edge:q) ?des.
   ?des (edge:p|edge:q)* node:h.
}

The returned information only shows one level of the solution (it shows the possible neighbor nodes for reaching the destination). Thanks in advance for your help. Best Regards

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Ali Masri
  • 67
  • 1
  • 11
  • That's because `?des` can only be one edge away from `?node`, since you used the pattern `node:a (edge:p|edge:q) ?des.`. Did you mean `node:a (edge:p|edge:q)* ?des.` (with a star)? Then `?des` can be any node along a path between a and h. – Joshua Taylor Mar 06 '15 at 15:06
  • [Finding all steps in property path](http://stackoverflow.com/q/18024413/1281433) may help. The accepted answer says that this isn't possible, but [my answer](http://stackoverflow.com/a/18032019/1281433) shows that you actually can do this in some cases. – Joshua Taylor Mar 06 '15 at 15:20
  • 1
    This seems very similar to [getting a graph path using SPARQL](http://stackoverflow.com/q/28897701/1281433). Is this a class assignment? – Joshua Taylor Mar 06 '15 at 15:29
  • I tried it also with a * but the results are not comprehensive at all. I am searching for a way to get all the path from a to h. Thanks for the Links I will have a look at them. And no this is not a class assignment, I am a PhD student and I am seeking for such way to integrate it with my work. – Ali Masri Mar 06 '15 at 15:47
  • It's OK to ask about assignments (though it's usually good to mention if it is). Do you happen to know if one of your colleagues posted the other question (from about 2 hours before yours): http://stackoverflow.com/q/28897701/1281433. It's almost identical, after all, down to the data (using p and q as edges, and nodes labeled from a to o), and the desired result. Given the timing, it's very hard to imagine that these aren't related. – Joshua Taylor Mar 06 '15 at 15:51
  • Yes he is my supervisor we were seeking for an answer to this question. – Ali Masri Mar 06 '15 at 16:13
  • so an answer to either question would be fine, since you'll both get an answer. I'm going to flag the other as a duplicate of this one, since I think that this one is nicer (it has the picture), and starts with a better query. – Joshua Taylor Mar 06 '15 at 16:28
  • Yes an asnwer to any of us is good.Ok thank you very much. – Ali Masri Mar 06 '15 at 18:23

1 Answers1

3

Property paths in SPARQL are not things that you can query directly, but you can use property paths to help extract the edges along a path between two nodes. For instance, the following query returns the edges in paths from a to h. The basic idea is to use a property path to from a to some node u which has an edge to some node v from which there is a path to h. The values block just limits the value of e to be either p or q.

prefix node: <http://prism.uvsq.fr/>
prefix edge: <http://prism.uvsq.fr#>

select distinct ?u ?e ?v where {
  values ?e { edge:p edge:q }
  node:a (edge:p|edge:q)* ?u .
  ?u ?e ?v .
  ?v (edge:p|edge:q)* node:h .
}
----------------------------
| u      | e      | v      |
============================
| node:a | edge:p | node:g |
| node:a | edge:p | node:b |
| node:g | edge:p | node:f |
| node:g | edge:p | node:c |
| node:f | edge:p | node:k |
| node:f | edge:p | node:g |
| node:k | edge:p | node:l |
| node:l | edge:p | node:g |
| node:c | edge:p | node:i |
| node:n | edge:p | node:m |
| node:h | edge:p | node:n |
| node:b | edge:p | node:c |
| node:a | edge:q | node:f |
| node:f | edge:q | node:l |
| node:c | edge:q | node:h |
| node:i | edge:q | node:j |
| node:j | edge:q | node:n |
| node:m | edge:q | node:g |
----------------------------

That doesn't give you the actual paths, but it gives you all and only the edges that are on paths from a to h. From that you can reconstruct paths by putting the graph back together and performing a depth first traversal to enumerate the paths.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • So I think there is no way till now to fully support our needs. The technique you showed is interesting. Many thanks for your efforts and your help it is appreciated. – Ali Masri Mar 10 '15 at 09:30