2

I want to detect all values that satisfy a condition on the value of a property using a SPARQL query.

For example, imagine that I want to detect all resources in which the value of rdfs:label has type xsd:string.

A definition in logic could be:

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

I have found a way to do it in SPARQL as:

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)
}

which seems to work ok with the following data:

@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 .

However, it does not return the value :x because it does not have rdfs:label and it does not return 0 as the count c0.

Is there a way to count the resources that have some property returning 0 for the resources that don't have that property?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Labra
  • 1,412
  • 1
  • 13
  • 33

1 Answers1

2

Suppose you've got data like this, where some resources have rdfs:label values, and some don't, and of those that do, some have string values, and some have non-string values.

@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 .

You can write a query then that begins by matching all the non-literal resources, and filtering out those that do not meet the specified criteria. That is, you simply filter out those that have a label that's not an 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 |
------

You can of course count the number of results as well:

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  |
------

Alternatively, if you want to select each ?s along with the number of rdfs:labels that it has, you can do that, too, by optionally selecting the labels that an ?s does have, grouping by ?s, and counting the distinct ?label values:

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      |
---------------
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353