I am trying to write a SPARQL query which should give me all foaf:Agents which aren’t foaf:Persons.
I can’t see a way to apply this OPTIONAL/BOUND construct to this problem, because all the properties like rdfs:subClassOf and rdf:type are transitive and reflexive.
I tried this:
SELECT * WHERE {
?x rdf:type foaf:Agent
OPTIONAL { ?y rdf:type foaf:Person }
FILTER ( !BOUND(?y) ) }
But rdf:type seems to be transitive, at least with JENA/ARQ/SDB.
The reason this isn’t working is because you have two separate variable bindings (
?xand?y) which are unrelated in your query. So?xmust be bound to appear in the result set (which is what you want), but if?yis unbound, you have not learned anything new about?x.Update: In an ideal query, there would be no need for
?yat all; you could test the incoming/outgoing edegs of?xdirectly. This is difficult (impossible?) to do in SPARQL 1.0 when you want to check if an edge does not exist on a given variable binding. However, SPARQL 1.1 will provide support for negation:@Kingsley Idehen’s approach (using third-party SPARQL extensions) should help you solve the problem in the short run.