0

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() |                  
================                   
Community
  • 1
  • 1
S12000
  • 3,345
  • 12
  • 35
  • 51

1 Answers1

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
qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Hello Thomas. Thanks a lot for your answer. Just a question: what do you mean by "Enough" ? – S12000 May 06 '17 at 18:58
  • `Enough` is the name of the constraint. It could be left away, but I thought this name would fit here. – qwerty_so May 06 '17 at 20:02
  • It is helpful, but not necessary, to explicitly provide a Person::country Property. All associations are always navigable in OCL (in both directions for Objects). For unambiguous cases, the target type defines an implicit role, so just access the "Country" property. For ambiguous cases the ambiguity can be resolved by qualifying with the opposite which in this unrealistically totally unnamed example must be "Country[Person]" – Ed Willink May 21 '18 at 07:41
  • @EdWillink Thanks for the clarification. I have to confess that my knowledge of OCL itself is only very rudimentary. – qwerty_so May 21 '18 at 08:06