I am trying to find the Luhn algorithm in OCL to check the validity of ISIN. Can anyone provide a code example would it be great!
-
I need it too, please write one! – Lars Jun 02 '20 at 14:50
3 Answers
In pure OCL using the example from https://en.wikipedia.org/wiki/Luhn_algorithm
let s = Sequence{7,9,9,2,7,3,9,8,7,1} in
(Sequence{1..s->size()}
->collect(i |
let t = s->at(i) in
if i.mod(2) = 1
then t
else let tt = 2 * t in tt.div(10) + tt.mod(10)
endif)
->sum()*9)
.mod(10)

- 1,205
- 7
- 8
In the German Wikipedia article about the Luhns algorithm (https://de.wikipedia.org/wiki/Luhn-Algorithmus) you can find an example to calculate the value for an ident of 446-667-651. The algorithm below calculates the correct value of 40.
let list = '446667651'.ToCharArray.strToInt in
Sequence{1..list->size}
->Collect(i|
if (list->size-i).Mod(2)=0 then
list.at(i)
else
(list.at(i)*2).Mod(9)
endif)
->Sum
Maybe you need some adaptions for calculating the value for ISINs.

- 85
- 5
-
1
-
This answer is not OCL which requires parentheses on function calls and has no ToCharArray or strToInt functions. mod is not camelcase. The collection operation at() must be invoked using ->. – Ed Willink Jun 07 '20 at 07:22
Create a class:
Attribute NextMult1Or2:Integer
Method MultWith2Or1SumBiggerThan10(v:Integer):Integer
Method GetCheckSum(input:String):Integer
The first method MultWith2Or1SumBiggerThan10:
let res=v*self.NextMult1Or2 in
(
if self.NextMult1Or2=2 then
self.NextMult1Or2:=1
else
self.NextMult1Or2:=2
endif;
if res>10 then
res-9
else
res
endif
)
And the second method GetCheckSum(input:String):Integer
self.NextMult1Or2:=2;
input.ToCharArray->collect(c|
let i=Integer.Parse(c) in (
self.MultWith2Or1SumBiggerThan10(i)
)
)->sum
To calculate the checksum - send in all but check digit to GetCheckSum - the check digit is (((res+10) / 10).Truncate*10)-res
(ie the diff from nearest 10:th above)
To check a sequence send in all including check digit to GetCheckSum - if res.Mod(10)= 0
it has passed

- 2,275
- 1
- 15
- 15
-
This answer is not OCL which has no ":=" operator, ";" terminator Attributes or Methods. – Ed Willink Jun 02 '20 at 16:18
-
In standard ocl there is no ":=" and no ";" operators that is correct - the answered used MDriven Action language that introduce these. But standard ocl does allow you to call functions that has no side effects @EdWillink – Hans Karlsen Jun 05 '20 at 14:06
-