Papyrus - OCL 约束验证 属性

Papyrus - OCL constraint to verified property

我的 class 有 2 个属性 (String),它们是两种类型的人员文档编号。为了验证文档是否具有有效数字,实现了计算(验证者数字)。下面是一个如何实现其中之一的一致性的例子:

号码:973.345.650-02(标点符号请忽略)

第一验证码计算

9 * 10 = 90
7 * 9 = 63
3 * 8 = 24
3 * 7 = 21
4 * 6 = 24
5 * 5 = 25
6 * 4 = 24
5 * 3 = 15
0 * 2 = 0
----------
Sum = 286

286 % 11 = 0

If rest < 2 then first digit = 0 
Or if rest >= 2 then first digit = 11 - rest

In this case, rest < 2 (0), then first verifier digit = 0

第二个验证码计算

9 * 11 = 99
7 * 10 = 70
3 * 9 = 27
3 * 8 = 24
4 * 7 = 28
5 * 6 = 30
6 * 5 = 30
5 * 4 = 20
0 * 3 = 0
0 * 2 = 0    ==> FIRST VERIFIER DIGIT
----------
Sum = 328

328 % 11 = 9

Same rule of first verifier digit

If rest < 2 then first digit = 0 
Or if rest >= 2 then first digit = 11 - rest

The rest is greater than 2 (9), then second verifiter digit = 11 - 9 ==> 2

JAVA 方法

public static boolean isValidCPF(String cpf) {
    
    cpf = cpf.replaceAll("[./-]", "");      

    if (cpf.length() < 11) {
        return false;
    }
            
    int equalDigits = 0;
    char compareChar = cpf.charAt(0);
    
    for (int i = 1; i <= 9; i++) {
        if (compareChar == cpf.charAt(i)) {
            equalDigits++;
        } else {
            break;
        }
    }
    
    if (equalDigits == 9) {
        return false;
    }
                    
    int[] digit = new int[2];
    int sum = 0, multiply = 2;
    
    for (int k = 8; k <= 9; k++) {
        
        for (int i = k; i >= 0; i--) {
            sum += Character.getNumericValue(cpf.charAt(i)) * multiply++ ;
        }
    
        digit[k-8] = (sum % 11) < 2 ? 0 : 11 - (sum % 11);
        sum = 0;
        multiply = 2;
        
    }
            
    if (cpf.equals(cpf.substring(0 , 9) + digit[0] + digit[1])) {
        return true;
    }
            
    return false;
}

我想在这种情况下必须使用OCL“序列”(循环数字)将每个转换为整数进行计算并在约束中使用“body”,但我不知道如何。

我想将约束应用于 Papyrus 中的 UML 模型(我知道该怎么做)。

提前致谢。

您必须在聚合中以声明的方式思考才能效仿

   for (int i = 1; i <= 9; i++) {
    if (compareChar == cpf.charAt(i)) {
        equalDigits++;
    } else {
        break;
    }
}

你可以试试

Sequence{2..10}->select(i | cpf->at(i) = compareChar)->size()

NB OCL 索引从 1 开始。