7

I'm trying to use the IN and NOT IN statements (that were if I understand correctly, introduced in SPARQL 1.1) on the python implementation of SPARQL (now in rdfextras) but it seems that the syntax is not recognised.

Let's consider two sets (A and B). I want to output what is in Set A, removing what is in Set B.

SELECT ?title WHERE {
   some logic defining ?item and ?SetB
   FILTER (?item NOT IN ?SetB)
}

Maybe this particular thing was added in SPARQL 1.1 and is not supported by rdfextra, in which case I would love to have a workaround (or how to do it without using the NOT IN keyword)

Alexis Métaireau
  • 10,767
  • 5
  • 24
  • 34

2 Answers2

7

I have tried a similar query and also got a parsing exception. I have gone through rdflib's SPARQL parser code and it doesn't seem to exist a rule to handle IN or NOT IN. I would assume that this functionality is not implemented.

Anyway, I am not sure you are using it correctly. Having a look at the NOT IN definition in the SPARQL 1.1 spec ... it defines the IN operator to be used against a list of expressions. Therefore, you'd do:

FILTER (?item NOT IN (?SetB))

And I am not completely sure if you can use variables in the right-hand side because all the examples in the spec use terms. edit: see RobV message , it's possible to use variables in the RLH

workaround with one query

One possible solution, that might work for you is to use OPTIONAL and bound (both supported in rdflib). Something like ...

SELECT ?title WHERE {
   some logic defining ?item
   OPTIONAL {
   some logic defining ?SetB
   }
   FILTER (bound(?SetB) && ?setB != ?item)
}

Without knowing more about your query I cannot advice better with this case.

workaround with two queries

The easiest way to solve this with rdlib is to use filters and two queries, the first query retrieves all the posible values for ?SetB. Ant in the second query you dynamically create a filter:

SELECT ?title WHERE {
   some logic defining ?item
   FILTER (?item != <setb_val1> && ?item != <setb_val2> &&
   ... && ?item != <setb_val2>)
}
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56
  • Great that's what I was looking for. – Alexis Métaireau Apr 27 '11 at 09:00
  • It should be legal to use variables in the RHS of a IN/NOT IN expression, the RHS is defined to be an `ExpressionList` in the 1.1 grammar which permits any list of `Expression` productions i.e. any valid expression and thus this should include variables – RobV Apr 27 '11 at 12:17
  • good to know, thanks Rob. The examples in the spec are a bit misleading. – Manuel Salvadores Apr 27 '11 at 12:51
4

Difficult to answer without specifics, but it sounds like you want MINUS:

SELECT ?title WHERE {
    ?item ... ITEM CRITERIA ...
    MINUS { ?item ... SET CRITERIA ... }
}

for example:

SELECT ?title WHERE {
    ?item ex:colour "red" .       # item is red
    MINUS { ?item ex:size "big" } # but not in set of big things
}

NOT IN is a bit misleading: as far as I can tell it operates over a list expression, not a list you can define.

user205512
  • 8,798
  • 29
  • 28