0

This SPARQL query returns true:

ASK {
  wd:Q216665 wdt:P279* wd:Q5185279 .
  }

https://w.wiki/6Mi5

Is there any way to expand the path (i.e. wdt:P279*) to better understand the link between subject and predicate?

Alex W.
  • 119
  • 9
  • getting paths in SPARQL is rather tricky, but possible with property path and an intermediate node to at least get edges: https://stackoverflow.com/questions/18024413/finding-all-steps-in-property-path – UninformedUser Feb 21 '23 at 07:25
  • 1
    a more efficient way on the Blazegraph backend of Wikidata: `#defaultView:Graph PREFIX gas: SELECT DISTINCT ?item ?itemLabel ?linkTo ?linkToLabel WHERE { SERVICE gas:service { gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.SSSP" ; gas:in wd:Q216665 ; gas:traversalDirection "Forward" ; gas:out ?item ; gas:target wd:Q5185279 ; gas:out1 ?depth ; gas:maxIterations 6 ; gas:linkType wdt:P279 . } OPTIONAL { ?item wdt:P279 ?linkTo . } SERVICE wikibase:label {bd:serviceParam wikibase:language "en" } }` – UninformedUser Feb 21 '23 at 07:26
  • That's cool, thank you very much. For who doesn't understand (like myself) what the above query does, here's a page with some hints: https://github.com/blazegraph/database/wiki/RDF_GAS_API – Alex W. Feb 21 '23 at 14:05

1 Answers1

0

If you know in advance that the path's lenght is at most N, then you can hardcode it.

E.g. with N = 4:

SELECT ?start ?node1 ?node2 ?node3 ?end WHERE {
  BIND (wd:Q216665 as ?start)
  BIND (wd:Q5185279 as ?end)
  { ?start wdt:P279 ?node1 . ?node1 wdt:P279 ?end . } UNION
  { ?start wdt:P279 ?node1 . ?node1 wdt:P279 ?node2 . ?node2 wdt:P279 ?end . } UNION
  { ?start wdt:P279 ?node1 . ?node1 wdt:P279 ?node2 . ?node2 wdt:P279 ?node3 . ?node3 wdt:P279 ?end . }
}

or even:

SELECT ?start ?node1 ?node2 ?node3 ?end WHERE {
  BIND (wd:Q216665 as ?start)
  BIND (wd:Q5185279 as ?end)
  ?start wdt:P279* ?end .
  OPTIONAL { ?start wdt:P279 ?node1 . ?node1 wdt:P279+ ?end .
  OPTIONAL { ?node1 wdt:P279 ?node2 . ?node2 wdt:P279+ ?end .
  OPTIONAL { ?node2 wdt:P279 ?node3 . ?node3 wdt:P279+ ?end .
  }}}
}
logi-kal
  • 7,107
  • 6
  • 31
  • 43