Re: Is there a SPARQL way to...

Leigh Dodds wrote:
>> This should work:
>>
>> SELECT ?x {
>>    ?x a ex:A .
>>    OPTIONAL {
>>      ?x a ?type .
>>      FILTER(?type != ex:A)
>>    }
>>    FILTER(!bound(?type))
>> }
>>
>> The OPTIONAL here tries to find a different type for the same resource
>> (?x). If it succeeds then ?type is bound to a second class, so we filter
>> out that case at the top-level.
> 
> And it does. I just ran it twinkle and it produces the expected
> single result.
> 
> Nice work. I spent a long time scratching my head with that one.
> Didn't occur to me to split the FILTERs into the separate OPTIONAL
> patterns.
> 
> Fun!

I'm happy to see again that the widely hated (by some semantic Web 
purists at least) negation as failure proofs useful here!

SPARQL spec, section 11.4.1:

"One may test that a graph pattern is not expressed by specifying an 
OPTIONAL graph pattern that introduces a variable and testing to see 
that the variable is not bound. This is called Negation as Failure in 
logic programming."

  ;-) still a pitty that there is no more convenient way to express set 
difference in SPARQL, but at least it is possible and we see here that 
people really use this feature.

Axel



-- 
Dr. Axel Polleres
email: axel@polleres.net  url: http://www.polleres.net/

rdf:Resource owl:differentFrom xsd:anyURI .

Received on Friday, 1 February 2008 21:33:11 UTC