4

Consider the following snippet

ASK WHERE { wd:Q734774 wdt:P31 wd:Q3918. }

This works fine in Wikidata. I want to use some of the path syntax in the this snippet. Specifically I want to limit the number of the times "wdt:P31" used in the path. According to the guidelines this should be the right syntax:

ASK WHERE { wd:Q734774 wdt:P31{,3} wd:Q3918. }

But it's giving me weird error messages. Any ideas?

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Daniel
  • 5,839
  • 9
  • 46
  • 85
  • The query is right; it (the same format) works in DBPedia. Perhaps some issue with the parser on wikidata? – Kiran.B Dec 28 '16 at 05:25
  • 4
    No, you're wrong. You read a draft (from 2010) of the property path syntax. The restriction of the length of a property path did not make it to the final version (which was 3 years later): https://www.w3.org/TR/sparql11-query/#propertypaths – UninformedUser Dec 28 '16 at 09:12
  • @AKSW I see ... then how can I simulate such behavior? (other than querying multiple times, and once for fixed path length, obviously) – Daniel Dec 30 '16 at 10:42
  • You can't really do it efficiently via SPARQL. That's the limitation of property paths. As you said, the only way would be to build the query by hand. Another option would be to compute the path length directly in the SPARQL query as suggested by JohuaTaylor here: http://stackoverflow.com/questions/5198889/calculate-length-of-path-between-nodes But this might not scale to big resp. deep graphs. – UninformedUser Dec 30 '16 at 11:48
  • @AKSW - Not quite correct... See my answer. – TallTed Dec 30 '16 at 15:29
  • You're right, sorry. I'll delete my comment – UninformedUser Dec 30 '16 at 17:06

1 Answers1

6

The final version of SPARQL 1.1 Property Paths lets you do this with the following query --

ASK WHERE 
  { wd:Q734774 
       wdt:P31? / wdt:P31? / wdt:P31? 
          wd:Q3918 
  }

For clarity, I've put the full Property Path Predicate (wdt:P31? / wdt:P31? / wdt:P31?) on a separate line between Subject (wd:Q734774) and Object (wd:Q3918). The trailing ? asks for one-or-zero instances of the wdt:P31 predicate, and the / asks for a sequence, so this full path asks for a sequence of zero-or-one-or-two-or-three instances.

TallTed
  • 9,069
  • 2
  • 22
  • 37
  • Thanks @TallTed for the answer. One question though. Suppose `ASK WHERE { wd:Q734774 wdt:P31* wd:Q3918. }`returns true. How can I query the length of the path which connects the two entities `wd:Q3918` and `wd:Q734774` together? – Daniel Jan 03 '17 at 20:18
  • 1
    That's a very different question, and requires [a very different answer](http://stackoverflow.com/a/41121465/241164). – TallTed Jan 03 '17 at 20:27