Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL 1.1: how to use the replace function?

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?

like image 582
jpp1 Avatar asked May 22 '12 19:05

jpp1


2 Answers

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

like image 88
RobV Avatar answered Nov 11 '22 13:11

RobV


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?

like image 20
Alex Miller Avatar answered Nov 11 '22 14:11

Alex Miller