1

I would like to formulate an SPARQL query that asks if there exist a undirected path between node a and d.

My data looks as follows:

<a> <p1> <b>
<c> <p2> <b> 
<c> <p3> <d>

Here is a small ascii drawing of the same data

(a) --> (b) <-- (c) --> (d)

I tried with the following query:

ASK   
WHERE {
  <a>  (<>|^<>)* <d> .
}

How do I make an ASK SPARQL query that returns yes when asked if there is an undirected path between a and d?

  • There is no property `<>` in your data, so how should this match. The common pattern for any edge is `<>|!<>`, i.e. p or not p for a p that does not exists in the data – UninformedUser Sep 20 '16 at 11:02
  • 1
    `((<>|!<>)|^(<>|!<>))*` could work – UninformedUser Sep 20 '16 at 11:04
  • My understanding is that ,, and would match <>, am I misunderstanding something? Why would I match "not p", I only want paths that exist! I tried the pattern on my example data and it works. Would it be possible that you explain why this path solves my problem? – Kim Ahlstrøm Meyn Mathiassen Sep 20 '16 at 11:54
  • 1
    `<>` is not a wildcard but an entity whose URI will be resolved relative to the current base URI. There is no wildcard for property paths, but a workaround is to use a URI that does not exist in the data and then there is always a path between two nodes as either p or not p must be satisfied. This is quite obvious as the URI of a predicate can't for sure be both – UninformedUser Sep 20 '16 at 12:21
  • @AKSW feel free to post an answer, I will accept it. Cheers – Kim Ahlstrøm Meyn Mathiassen Sep 21 '16 at 05:18

1 Answers1

2

There is no property <> in your data, so this won't match. The common pattern for "any edge" is <>|!<>, i.e. a property p or not p for a p that does not exist in the data.

((<>|!<>)|^(<>|!<>))* should work in your example.

UninformedUser
  • 8,397
  • 1
  • 14
  • 23