boundary for arbitrary property path in SPARQL 1.1
Asked Answered
D

1

6

Is it possible to bound the length of property path? For example getting all the triples with lengths that are between (m,n) or all that are not between this range? For instance, how could this be done with the following query?

select ?x ?y
where {?x p* ?y}
Desberg answered 27/5, 2016 at 17:40 Comment(0)
C
7

Some endpoints support this directly

Some SPARQL engines support a method for doing this directly, with a regular-expression-like syntax. E.g.,

?s :p{n,m} ?o

would be a path with a length between n and m. That syntax is described in SPARQL 1.1 Property Paths: W3C Working Draft 26 January 2010. There is also support for exact lengths, minimum lengths, and maximum lengths. For better or for worse, that syntax didn't make it into the final SPARQL 1.1 standard. Some SPARQL endpoints will still accept it though, so it's worth trying.

A general workaround

But there is a workaround. The idea is to split the candidate path into two parts. By checking how many ways it can be split into two parts, you can find the length of the path. That is, you do something like this to, for instance, find ?s and ?p where they are joined by a path of length ten:

select ?s ?o {
  ?s :p* ?mid .
  ?mid :p* ?o .
}
group by ?s ?o
having (count(?mid) = 10)

Be sure to check the actual counts if you use this approach. It's easy to get an off-by-one (or -two) error depending on how you want to calculate length. There are a few options (whether to count the properties or the nodes, whether to count the endpoints or not, etc.), so a little bit of experimentation is worth while.

References and Examples

For some more examples of how you can use this pattern, have a look at:

Clactonian answered 27/5, 2016 at 18:56 Comment(2)
Thanks. Your query seem to be supported by Jena. It is as well very costy. Why not using + instead of * ?Desberg
@JavaDeveloper You can use + if you like, but if you're ever looking for paths of length 1, be sure that you haven't done ?s :p+ ?mid . ?mid :p+ ?o, because that will require that the path is of length at least 2. I think Jena might support the {n,m} notation too, but you might have to put it into an "extensions-enabled" mode.Clactonian

© 2022 - 2024 — McMap. All rights reserved.