SPARQL 递归:获取 class 的 属性,或父 class,或祖父 class,等等
SPARQL recursion: Get property of class, or parent class, or grand-parent class, etc
- 我有 Grumpy Cat Wikidata item, and I want to get the icon property of the item it is an instance of, which is cat。
- 如果cat does not have an icon, then I want the icon of cat's parent class, which is domesticated animal。
- 如果domesticated animal does not have an icon, then I want the icon of domesticated animal's parent class, which is animal
- 等等,递归地,直到找到一个图标或直到不再有父级class。
如何在 SPARQL 中获取它?
特别是,如果猫和动物都有图标,那么我想得到猫的图标而不是动物的图标,因为它更接近不爽猫。
如果该项目是多个 class 的实例,或者如果 class 是多个 class 的子 class,那就太好了如果可以探索所有分支,并选择最接近的图标。性能很重要,我希望查询可以 运行 在 https://query.wikidata.org 上不到一秒。我曾尝试编写基于叠层式 MINUS 子句的查询,但它并不是真正的递归。
如果出于测试目的需要,这里是 many Wikidata items with icons。
您不能递归地在 SPARQL 中查找某些内容,然后在找到第一个时停止。您可以做的是获取所有图标、一个随机图标,或者定义您经历的有限数量的 类。
您可以使用一系列选项来获取最近的图标(在您定义静态深度的情况下):
# SAMPLE is used here because 1) the resource might have multiple classes,
# and 2) because of the fallback below
SELECT ?item ?itemLabel ?cls ?clsLabel (SAMPLE(?icon) AS ?icon) WHERE {
BIND(wd:Q103474 AS ?item)
?item wdt:P31 ?cls.
OPTIONAL { ?cls wdt:P2910 ?icon. }
OPTIONAL { ?cls (wdt:P279/wdt:P2910) ?icon. }
OPTIONAL { ?cls (wdt:P279/wdt:P279/wdt:P2910) ?icon. }
OPTIONAL { ?cls (wdt:P279/wdt:P279/wdt:P279/wdt:P2910) ?icon. }
# You can continue the optionals with longer property paths,
# here we just fall back to trying the whole class path
# (returning all icons in the path in an arbitrary order).
OPTIONAL { ?cls (wdt:P279+/wdt:P2910) ?icon. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?itemLabel ?cls ?clsLabel
第一个成功的可选将绑定 ?icon
,因此该变量将包含 "closest" 图标。 SAMPLE
将确保查询只有 returns 个图标。
- 我有 Grumpy Cat Wikidata item, and I want to get the icon property of the item it is an instance of, which is cat。
- 如果cat does not have an icon, then I want the icon of cat's parent class, which is domesticated animal。
- 如果domesticated animal does not have an icon, then I want the icon of domesticated animal's parent class, which is animal
- 等等,递归地,直到找到一个图标或直到不再有父级class。
如何在 SPARQL 中获取它?
特别是,如果猫和动物都有图标,那么我想得到猫的图标而不是动物的图标,因为它更接近不爽猫。
如果该项目是多个 class 的实例,或者如果 class 是多个 class 的子 class,那就太好了如果可以探索所有分支,并选择最接近的图标。性能很重要,我希望查询可以 运行 在 https://query.wikidata.org 上不到一秒。我曾尝试编写基于叠层式 MINUS 子句的查询,但它并不是真正的递归。
如果出于测试目的需要,这里是 many Wikidata items with icons。
您不能递归地在 SPARQL 中查找某些内容,然后在找到第一个时停止。您可以做的是获取所有图标、一个随机图标,或者定义您经历的有限数量的 类。
您可以使用一系列选项来获取最近的图标(在您定义静态深度的情况下):
# SAMPLE is used here because 1) the resource might have multiple classes,
# and 2) because of the fallback below
SELECT ?item ?itemLabel ?cls ?clsLabel (SAMPLE(?icon) AS ?icon) WHERE {
BIND(wd:Q103474 AS ?item)
?item wdt:P31 ?cls.
OPTIONAL { ?cls wdt:P2910 ?icon. }
OPTIONAL { ?cls (wdt:P279/wdt:P2910) ?icon. }
OPTIONAL { ?cls (wdt:P279/wdt:P279/wdt:P2910) ?icon. }
OPTIONAL { ?cls (wdt:P279/wdt:P279/wdt:P279/wdt:P2910) ?icon. }
# You can continue the optionals with longer property paths,
# here we just fall back to trying the whole class path
# (returning all icons in the path in an arbitrary order).
OPTIONAL { ?cls (wdt:P279+/wdt:P2910) ?icon. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?itemLabel ?cls ?clsLabel
第一个成功的可选将绑定 ?icon
,因此该变量将包含 "closest" 图标。 SAMPLE
将确保查询只有 returns 个图标。