Re: Incrementing a literal

On 11 November 2012 18:06, Andy Seaborne <andy.seaborne@epimorphics.com>wrote:

>
>
> 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) }
>

Awesome, thanks!


>
>         Andy
>
>

Received on Sunday, 11 November 2012 17:20:59 UTC