【问题标题】:Count values that satisfy a constraint and return 0 for those that don't satisfy it in SPARQL在 SPARQL 中计算满足约束的值并为不满足约束的值返回 0
【发布时间】:2014-08-14 19:44:25
【问题描述】:

我想使用 SPARQL 查询检测所有满足属性值条件的值。

例如,假设我想检测rdfs:label 的值类型为xsd:string 的所有资源。

逻辑上的定义可以是:

∀x(stringLabel(x) ≡ ∀y(rdfs:label(x,y)→xsd:string(y)))

我在 SPARQL 中找到了一种方法:

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix : <http://example.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>

select ?s where {

  # Count arcs with property rdfs:label 
  { SELECT ?s (COUNT(*) AS ?c0) {
      ?s rdfs:label ?o . 
     } GROUP BY ?s
  } 
  # Count arcs with property rdfs:label and value xsd:string
  { SELECT ?s (COUNT(*) AS ?c1) {
     ?s rdfs:label ?o . FILTER ((isLiteral(?o) && datatype(?o) = xsd:string)) 
    } GROUP BY ?s
  }
  # filter out those resources that have rdfs:label 
  # with values not in xsd:string
  FILTER (?c0 = ?c1)
}

这似乎适用于以下数据:

@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://example.org/> .

:peter rdfs:label "Peter" ;  foaf:knows :anna .

:anna rdfs:label "Anna" .

:r22 rdfs:label 22 .

:x foaf:knows :r22 .

但是,它不返回值 :x,因为它没有 rdfs:label,并且它不返回 0 作为计数 c0

对于没有该属性的资源,有没有办法计算具有某些属性返回 0 的资源?

【问题讨论】:

    标签: rdf sparql


    【解决方案1】:

    假设您有这样的数据,其中一些资源具有 rdfs:label 值,而有些则没有,而在那些具有的资源中,一些具有字符串值,而另一些具有非字符串值。

    @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix : <http://stackoverflow.com/q/25316358/1281433/> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    
    # a non-string label
    :a rdfs:label 22 .
    
    # one or more string labels
    :b rdfs:label "hello"^^xsd:string .
    :c rdfs:label "hello"^^xsd:string , "goodbye"^^xsd:string .
    
    # no labels at all
    :d rdfs:seeAlso :b .
    

    您可以编写一个查询,首先匹配所有非文字资源,然后过滤掉那些不符合指定条件的资源。也就是说,您只需过滤掉那些标签不是 xsd:string 的标签。

    prefix foaf: <http://xmlns.com/foaf/0.1/>
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    prefix : <http://stackoverflow.com/q/25316358/1281433/>
    prefix xsd: <http://www.w3.org/2001/XMLSchema#>
    
    select ?s where {
      #-- initially select all non-literals
      ?s :? ?s 
      filter ( !isLiteral(?s) )
    
      #-- but filter out anything that has a
      #-- a non-string label
      filter not exists {
        ?s rdfs:label ?label 
        filter ( datatype(?label) != xsd:string )
      }
    }
    
    ------
    | s  |
    ======
    | :c |
    | :d |
    | :b |
    ------
    

    您当然也可以计算结果的数量:

    prefix foaf: <http://xmlns.com/foaf/0.1/>
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    prefix : <http://stackoverflow.com/q/25316358/1281433/>
    prefix xsd: <http://www.w3.org/2001/XMLSchema#>
    
    select (count(distinct ?s) as ?ns) where {
      ?s :? ?s 
      filter ( !isLiteral(?s) )
      filter not exists {
        ?s rdfs:label ?label 
        filter ( datatype(?label) != xsd:string )
      }
    }
    

    ------
    | ns |
    ======
    | 3  |
    ------
    

    或者,如果您想选择每个 ?s 以及它具有的 rdfs:labels 的数量,您也可以这样做,方法是选择性地选择 ?s 具有的标签,按 ?s 分组,然后计算不同的 ?label 值:

    prefix foaf: <http://xmlns.com/foaf/0.1/>
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    prefix : <http://stackoverflow.com/q/25316358/1281433/>
    prefix xsd: <http://www.w3.org/2001/XMLSchema#>
    
    select ?s (count(distinct ?label) as ?nlabel) where {
      ?s :? ?s 
      filter ( !isLiteral(?s) )
      filter not exists {
        ?s rdfs:label ?label 
        filter ( datatype(?label) != xsd:string )
      }
    
      #-- get the label for counting
      optional { 
        ?s rdfs:label ?label 
      }
    }
    group by ?s
    
    ---------------
    | s  | nlabel |
    ===============
    | :d | 0      |
    | :b | 1      |
    | :c | 2      |
    ---------------
    

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      相关资源
      最近更新 更多