OCL 对 class A 的操作和 class B 的属性的写约束

OCL write constraint on an operation of class A and attribute of class B

关注

我想知道 OCL 是否允许以下​​内容。

想象一下下面的 UML 图。

如何使用 ocl 建模:"a person can drinkWater() only if he is from country name 'ABCD'" 并且如果国家/地区的人口 >= 1000000

当然,这个例子纯属虚构。

================                  ================
|    Person    |                  |    Country   |
|--------------|                  |--------------|
|- name        |------------------|- id          |
|- age         |                  |- name        |
|--------------|                  |- population  |
|+ drinkWater()|                  ================
|+ drinkBeer() |                  
================                   

您需要为 Country 的角色命名(在 Person 中创建一个命名属性)。因此,让我们将其命名为 country。这个当然可以用"as usual":

context Person::drinkWater()
pre Enough: self.country.name == "ABCD" and self.country.population >= 1000000