0

If I have to define an invariant to state that the age of a person requesting a loan should be greater than 18 for the attached class diagram would this be

context Person
inv: age > 18

or

context Person
inv: self.age > 18

and what is the difference between the two?

enter image description here Thank you for your guidance

CompilerSaysNo
  • 415
  • 3
  • 14

2 Answers2

1

Both invariants in your example are exactly the same (the "self" is implicit in the first). Nevertheless, there you just say that all "person" objects must have an age value of at least 18 which is not exactly the invariant you described (in your invariant, it seemed that it was possible to have people under 18 in the system, it´s just that people under 18 could not be linked to a loan)

Jordi Cabot
  • 8,058
  • 2
  • 33
  • 39
1

As Jordi already said they are semantically equivalent. The first includes the self implicitly.

As he mentioned either, your OCL invariant does not match the textual representation you gave us. In your invariant, people must be older than 18. However, the owner of the load should be greater than 18. So your invariant should be formulated on Loan.

context Loan inv: owner.age > 18

which also has implicit self, so the second is semantically equivalent to the first one:

context Loan inv: self.owner.age > 18

lschuetze
  • 1,218
  • 10
  • 25