14

How can one use the replace function in SPARQL 1.1, especially in update commands?

For example, if I have a number of triples ?s ?p ?o where ?o is a string and for all triples where ?o contains the string "gotit" I want to insert an additional triple where "gotit" is replaced by "haveit", how could I do this? I am trying to achieve this is Sesame 2.6.0.

I tried this naive approach:

INSERT { ?s ?p replace(?o,"gotit","haveit","i") . }
WHERE { ?s ?p ?o . FILTER(regex(?o,"gotit","i")) }

but this caused a syntax error.

I also failed to use replace in the result list of a query like so:

SELECT ?s ?p (replace(?o,"gotit","haveit","i") as ?r) WHERE { .... }

The SPARQL document unfortunately does not contain an example of how to use this function.

Is it possible at all to use functions to create new values and not just test existing values and if yes, how?

jpp1
  • 2,019
  • 3
  • 22
  • 43

2 Answers2

16

You can't use an expression directly in your INSERT clause like you have attempted to do. Also you are binding ?name with the first triple pattern but then filtering on ?o in the FILTER which is not going to give you any results (filtering on an unbound variable will give you no results for most filter expressions).

Instead you need to use a BIND in your WHERE clause to make the new version of the value available in the INSERT clause like so:

INSERT 
{
  ?s ?p ?o2 .
}
WHERE 
{ 
  ?s ?p ?o .
  FILTER(REGEX(?o, "gotit", "i"))
  BIND(REPLACE(?o, "gotit", "haveit", "i") AS ?o2)
}

BIND assigns the result of an expression to a new variable so you can use that value elsewhere in your query/update.

The relevant part of the SPARQL specification you are interested in is the section on Assignment

RobV
  • 28,022
  • 11
  • 77
  • 119
  • Thank you for pointing out the error of using ?name instead of ?o .. I tried to simplify an actual query for here and forgot to change the ?name into ?o. I tried the query using BIND with my SESAME installation but it also gives me a syntax error ... I will have to figure out if this is supported in the newest version! – jpp1 May 23 '12 at 20:13
  • I just tested this and it works great in Sesame version 2.6.5 with an OWLIM-Lite repository. – jpp1 May 23 '12 at 20:48
6

The usage of replace looks correct afaict according to the spec. I believe REPLACE was just added to the last rev of the spec relatively recently - perhaps Sesame just doesn't support it yet?

If you just do SELECT ?s ?p ?o WHERE { ?s ?p ?name . FILTER(regex(?name,"gotit","i")) } does your query return rows?

Alex Miller
  • 69,183
  • 25
  • 122
  • 167
  • Thank you for that reassurance that it should work like I imagined! The regex query indeed works just fine. I will report back here as soon as I manage to upgrade to a newer version of Sesame. – jpp1 May 23 '12 at 09:52
  • I now tested with Sesame version 2.6.5 and it turns out that while the query works the insert command does not work and produces a syntax error. The approach suggested by RobV below using BIND does work with Sesame 2.6.5. – jpp1 May 23 '12 at 20:52