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

限于喜欢 提交于 2019-12-25 12:53:07

问题


Follwing another post:

I would like to know if OCL allows the following.

Imagine the following UML diagram.

How to use ocl to model: "a person can drinkWater() only if he is from country name 'ABCD'" AND if the population of the country is >= 1000000"

Of course, the example is purely fictive.

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

回答1:


You will need to name the role of Country (create a named attribute inside Person). So let's name that country. This of course can be used "as usual":

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


来源:https://stackoverflow.com/questions/43819795/ocl-write-constraint-on-an-operation-of-class-a-and-attribute-of-class-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!