W3C home > Mailing lists > Public > semantic-web@w3.org > November 2012

Re: Incrementing a literal

From: Andy Seaborne <andy.seaborne@epimorphics.com>
Date: Sun, 11 Nov 2012 17:06:03 +0000
Message-ID: <509FDAFB.3010109@epimorphics.com>
To: semantic-web@w3.org


On 10/11/12 11:54, Melvin Carvalho wrote:
> Is there a pattern for incrementing a literal counter?
>
> Alice stores turtle in http://example.org/counter
>
> The initial operation should generate something like
>
> <#> <#counter> 1.
>
> Then the subsequent operation
>
> <#> <#counter> 2.
>
> And after that.
>
> <#> <#counter> 3.
>
> And so on ...
>
> Is there a neat way to do this in  distributed way?  SPARQL update?
> Maybe using Etags?

SPARQL Update:

DELETE { <#> <#counter> ?c1 }
INSERT { <#> <#counter> ?c2 }
WHERE  { <#> <#counter> ?c1 .
          BIND(?c1+1 AS ?c2) }

There are various ways to deal with the initial condition at the same time:

All in one operation:

DELETE { <#> <#counter> ?c1 }
INSERT { <#> <#counter> ?c2 }
WHERE  {
    OPTIONAL { <#> <#counter> ?c1 . }
    BIND(COALESCE(?c1+1, 1) AS ?c2) }

where the assignment can also be written:

    BIND(IF (BOUND(?c1) , ?c1+1 , 1) AS ?c2)

You can also use two operations in one request: (requests should be 
atomic i.e the two operations together):

# Does a triple for the counter exist?
INSERT { <#> <#counter> 0 }
WHERE   { FILTER NOT EXISTS { <#> <#counter> ?c } }
;

# Increment always - hence initialize to zero above
DELETE { <#> <#counter> ?c1 }
INSERT { <#> <#counter> ?c2 }
WHERE  { <#> <#counter> ?c1 .
          BIND(?c1+1 AS ?c2) }

	Andy
Received on Sunday, 11 November 2012 17:06:32 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 21:45:52 GMT