W3C home > Mailing lists > Public > public-sparql-dev@w3.org > January to March 2009

Re: Filtering statements in a given named-graph

From: Lee Feigenbaum <lee@thefigtrees.net>
Date: Mon, 09 Mar 2009 11:14:27 -0400
Message-ID: <49B53253.9050108@thefigtrees.net>
To: Thomas FRANCART <thomas.francart@mondeca.com>
CC: public-sparql-dev@w3.org
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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 9 March 2009 15:15:22 GMT