我想检查一个实体是否具有下面列出的类型之一。如果是,则查询必须返回true。
PREFIX basekb:<http://rdf.basekb.com/ns/>
basekb:music.release_track
basekb:book.written_work
basekb:book.book
basekb:music.release
basekb:music.album
basekb:tv.tv_series.episode
basekb:music.composition
basekb:music.recording
basekb:film.film
basekb:fictional_universe.fictional_character
实体m.0109yb6具有以下类型。 sparql查询应返回 true
http://rdf.basekb.com/ns/common.topic http://rdf.basekb.com/ns/music.recording
我出来了以下查询。有没有更好的方法来解决这个问题?
PREFIX basekb:<http://rdf.basekb.com/ns/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdf1:<http://www.w3.org/2000/01/rdf-schema#>
ASK where
{
{basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:music.release_track}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:book.written_work}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:book.book}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:music.release}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:music.album}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:tv.tv_series.episode}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:music.composition}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:music.recording}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:film.film}
UNION {basekb:m.0109yb6 rdf:type ?x; rdf:type basekb:fictional_universe.fictional_character}
}
答案:TRUE
答案 0 :(得分:2)
您撰写的查询有点奇怪。没有使用?x
变量,因此您可以立即将其简化为:
ASK where
{
{basekb:m.0109yb6 rdf:type basekb:music.release_track}
UNION {basekb:m.0109yb6 rdf:type basekb:book.written_work}
#-- ...
UNION {basekb:m.0109yb6 rdf:type basekb:fictional_universe.fictional_character}
}
但是,那仍然有很多重复的代码。您可以使用SPARQL 1.1的值指定变量?type
的允许值,然后询问您的实体是否具有rdf:type
的值之一:
prefix basekb:<http://rdf.basekb.com/ns/>
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
ask where {
basekb:m.0109yb6 rdf:type ?type
values ?type {
basekb:music.release_track
basekb:book.written_work
basekb:book.book
basekb:music.release
basekb:music.album
basekb:tv.tv_series.episode
basekb:music.composition
basekb:music.recording
basekb:film.film
basekb:fictional_universe.fictional_character
}
}