Richard Newman wrote:

  is it possible to write  a SPARQL query that given this data

<snip>

  returns only all subjects which belong only to class A?
  So in my example, only "ex:1" should  be returned.
  Ah,  and  resources can have more than two classes.

I haven't run this, but this might do the trick...

SELECT ?x {
  ?x a ex:A .
  OPTIONAL {
    ?x a ?bar .
  }
  FILTER (!bound(?x) || sameTerm(?bar, ex:A))
}

(I haven't run it either.) I think you meant !bound(?bar), but I don't think that's quite right since it will still return resources that are typed as ex:A and something else (since there will be one binding in which ?bar binds to ex:A). 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.

Let's try it. I put the data from the original mail at:

http://thefigtrees.net/lee/sw/data/single-class.ttl

Using Joseki/ARQ at http://sparql.org, I go to http://sparql.org/sparql.html and paste in

PREFIX ex: <http://example.org/>
SELECT ?x
FROM <http://thefigtrees.net/lee/sw/data/single-class.ttl>
{
  ?x a ex:A .
  OPTIONAL {
    ?x a ?type .
    FILTER(?type != ex:A)
  }
  FILTER(!bound(?type))
}
oh cripes, sparql.org is down right now. But I think this query is right.

2) Is there any kind of collection on the web of common SPARQL problems and
queries that solve it? Maybe some wiki page?

Lee runs the SPARQL FAQ:

http://thefigtrees.net/lee/sw/sparql-faq


...which needs some updating, but has a few of these sorts of things... As always, please mail me if you have any suggestions for things to add to the FAQ.

Lee

Reply via email to