Thank you very much! (For my future reference: https://www.w3.org/TR/rdf-sparql-query/#emptyGroupPattern)
> Sent: Monday, June 17, 2019 at 10:55 AM > From: "Andy Seaborne" <a...@apache.org> > To: users@jena.apache.org > Subject: Re: INSERT if not exists > > On 16/06/2019 21:01, Laura Morales wrote: > > I would like to add a new node if and only if another node with the same > > properties does not exists. Something like this: > > > > > > INSERT { > > ex:item-X a ex:Item; > > ex:serial "XYZ" . > > } if and only if there is not already another node with (ex:serial > > "XYZ") > > > > > > After trial and error I got this working > > > > > > INSERT { > > ex:item-X a ex:Item; > > ex:serial "XYZ" . > > } > > WHERE { > > FILTER NOT EXISTS { > > [] a ex:Item; > > ex:serial "XYZ" . > > } > > } > > > > > > my problem is that I don't understand why it's working. First of all, is > > the query correct? > > Yes. > > > Second, how does Jena compute this query? > > Try running as "SELECT * WHERE" to see that it returns either one row, > (no variables) if the pattern does not exist or no rows when something > exists. > > INSERT is a loop on the rows of the WHERE > > When there is one row, INSERT does soemthing. > When there are no rows, INSERT does not happen. > > > Why does that work, but not this one? > > > > WHERE { > > ?s ?p ?o . > > > > FILTER NOT EXISTS { > > [] a ex:Item; > > ex:serial "XYZ" . > > } > > } > > > > Tested on an empty graph? > > This does not work on the empty graph because ?s ?p ?o does to match. > > There is an implicit empty pattern in the first update and the empty > pattern matches (one row, no variables) even on the empty graph. > > Andy >