Re: Filtering statements in a given named-graph

Thomas FRANCART wrote:
> Hi SPARQL gurus
> 
> I have a set of statements separated in 2 named graphs :
> 
> - some statements appear in named-graph A
> - some statements appear in named-graph A and named-graph B
> 
> I would like to make a query on statements that are in named-graph A but 
> _not_ in named-graph B. Is it possible, and how can I do that ?
> 
> Thanks
> Thomas

Hi Thomas,

It's possible, but it's not pretty. :-)

Something like this, maybe? (untested)

PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ?p ?o }
FROM NAMED ex:A
FROM NAMED ex:B
WHERE {
   GRAPH ex:A {
     ?s ?p ?o .     # match any triple in A
   }
   OPTIONAL {
     GRAPH ex:B {
       ?s2 ?p ?o .  # match the same triple in B, introduce a new var
       FILTER(sameTerm(?s, ?s2)) . # constrain the new var
     }
   }
   FILTER(!bound(?s2)) .   # test that the new var (?s2) was not found
}

You may also be able to get away with just FILTER(?s = ?s2), but I think 
using sameTerm is slightly more correct in the case that you happen to 
have extended datatypes and an engine that knows about them.

You could also make the graph pattern inside the GRAPH ex:B { ... } clause:

    ?s ?p ?o, ?o2 .

...and then change the !bound(?s2) to !bound(?o2). Again, untested.

Lee

Received on Monday, 9 March 2009 15:15:21 UTC