0
votes

I try to use the SPARQL Update query such that it will delete triples if its in the graph and afterwards insert new triples even if no delete happened.

I have this query as example in the Virtuoso SPARQL endpoint after creating the graph http://example.de/:

WITH <http://example.de/>
DELETE {
  <http://example.de/Rheinmetall+AG> 
                  <http://dbpedia.org/property/locationStreet> 
                                    ?0 .
}
INSERT {
  <http://example.de/Rheinmetall+AG> 
                  <http://dbpedia.org/property/locationStreet> 
                                    <http://dbpedia.org/resource/Rheinmetall+Platz+1> .
}
WHERE
  { OPTIONAL
      { <http://example.de/Rheinmetall+AG>
                  <http://dbpedia.org/property/locationStreet>  
                                    ?0
      }
  } 

It doesn't insert any triples:

Modify <http://example.de/>, delete 0 (or less) and insert 0 (or less) triples -- done 

If I leave the WHERE clause empty it will insert one triple. And afterwards the original query will also delete and insert one triple. But I also want it to work if the triple is not in the graph yet. Is it possible to overcome that problem in an update query or do I have to call delete and insert separately?

Thanks for any help!

1
it would be possible but I don't understand why. You can also put multiple SPARQL update statements into a single request, usually comma separated. Did you try this? Just do INSERT DATA for the triple that you always want to add. And then ; and then DELETE ... WHERE statement - UninformedUser
and the OPTIONAL didn't work because it's basically a left-join on an empty table - UninformedUser
The reason it works in the default graph and not in the WITH case is rooted in SPARQL Query and the definition of GRAPH <nosuchurui> { pattern }. See answer. The WHERE pattern of { OPTIONAL {...} } is a left-join on the unit table which is why it works in the default graph case. - AndyS
@UninformedUser - I think your query order is backward. OP wants to DELETE all matching triples if present, and then INSERT one triple. Doing the INSERT first would mean that the triple in the INSERT query would then immediately be caught in the DELETE query. - TallTed
@AndyS - The OP never tried the default graph case. The WITH clause is always present, in all described tests. The change they made is in the WHERE clause. - TallTed

1 Answers

0
votes

A solution (already mentioned) is have two operations in a single request. This also means the OPTIONAL is not necessary and the DELETE can be a DELETE WHERE: WITH DELETE {} WHERE {} would also work.

DELETE WHERE { 
   GRAPH <http://example.de/> {
     <http://example.de/Rheinmetall+AG>
                  <http://dbpedia.org/property/locationStreet>  
                                    ?0
        }
   } ;

INSERT DATA { 
    GRAPH <http://example.de/> {
        <http://example.de/Rheinmetall+AG> 
                  <http://dbpedia.org/property/locationStreet> 
                                    <http://dbpedia.org/resource/Rheinmetall+Platz+1>
   }
}