检索作为链中的实例或子实例或子子实例的所有项目

Retrieving all items that are instances or sub-instances or sub-sub-instances in a chain

我需要关于一个项目或其父项(实例父项或子类父项)的所有有效语句的逻辑 OR

例子:Q957653Q3184121的实例,最后还有?item P17 Q155。所以满足链条Q957653 P31 Q3184121 P17 Q155...所以我需要一些东西

    ?item P17 Q155
    | ?item P31 ?x P17 Q155
    | ?item P31 ?x P31 ?y P17 Q155
    | ?item P279 ?x P17 Q155
    | ?item P279 ?x P31 ?y P17 Q155
    | ?item P279 ?x P279 ?y P17 Q155

所有可能的 P31 或 P279 依赖链的大逻辑 "or"。


实例

我需要一个项目列表,其中包含一些 属性(例如 ?item wdt:P402 _:b0.)并且是具有其他 属性 的项目的实例或子类,例如。 wdt:P17 wd:Q155.

?item wdt:P17 wd:Q155 的 "first level" 工作正常,

SELECT DISTINCT ?item ?osm_relid ?itemLabel 
WHERE {
  ?item wdt:P402 _:b0.
  ?item wdt:P17 wd:Q155.
  OPTIONAL { ?item wdt:P1448 ?name. }
  OPTIONAL { ?item wdt:P402 ?osm_relid .}
  SERVICE wikibase:label { 
      bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]". 
  }
}

但是如何表达所有其他可能依赖的(或逻辑"or")?

Edit/Notes

假设?item wdt:P31*/wdt:P279* wd:Qxx .将是所有"chain dependences of something Qxx",因为我需要...但是Qxx也是一个查询,
?item wdt:P31*/wdt:P279* (?xx wdt:P17 wd:Q155) ..

...似乎有一个解决方案 (!)

SELECT  (COUNT(DISTINCT ?item) AS ?count) 
WHERE {
  ?item wdt:P402 _:b0.
  ?item  (wdt:P31*|wdt:P279*)/wdt:P17 wd:Q155 .
}

但我无法检查,因为这很耗时。

... "feasible solution" 附近可能是
?item wdt:P31*/wdt:P279*/wdt:P31*/wdt:P17 wd:Q155 .
...在测试可行性后,似乎 wdt:P31*/wdt:P279*/wdt:P17 是唯一没有超时问题的 "optimal"。

为了提高性能,可以使用Blazegraphquery hints。查询提示允许修改自动生成的查询执行计划。

SELECT DISTINCT ?item ?itemLabel ?osm_relid ?name {
  ?itemi wdt:P17 wd:Q155 .
  hint:Prior hint:runFirst true .
  ?item (wdt:P31|wdt:P279)* ?itemi .
  ?item wdt:P625 [].
  OPTIONAL { ?item wdt:P1448 ?name. }
  OPTIONAL { ?item wdt:P402 ?osm_relid .}
  SERVICE wikibase:label { 
      bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]". 
  }
}

Try it!

This 是查询执行计划的样子(只需将 &explain 添加到查询 URL 并向下滚动)。

请注意,您在使用标签服务时使用了原始评论中的hint:Prior hint:runLast true:在任何图形模式组中只能有一个这样的提示。