【问题标题】:How to use the ASK WHERE statement with multiple conditions (sparql)如何使用具有多个条件的 ASK WHERE 语句 (sparql)
【发布时间】:2015-06-22 12:05:25
【问题描述】:

我想检查一个实体是否具有下列类型之一。如果是,则查询必须返回 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}
}

答案:是的

【问题讨论】:

    标签: sparql freebase


    【解决方案1】:

    您编写的查询有点奇怪。没有使用 ?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 的 values 指定变量 ?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
      }
    }
    

    【讨论】:

    • 我认为如果我使用分号,我就不需要使用 x。好像:选择 ?label where {basekb:m.0109yb6 rdf:type ?x; rdfs:label ?label} similar to 选择 ?label where {basekb:m.0109yb6 rdf:type ?x?. ?x rdfs:label ?label}
    • @HaniGoc 你在这些查询中根本没有使用 ?x,不过,这是我的观点。您的查询完全合法;它只是有一些未使用的东西。您不会从 ASK 查询中获得变量绑定,因此 ?X 在您的原始查询中没有任何用途。对于 SELECT 查询,执行select ?label where { values ?x { ... } ?object rdf:type ?x ; rdfs:label ?label } 之类的操作非常有意义。
    猜你喜欢
    • 2013-06-28
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多