Information Technology Reference
In-Depth Information
Fig. 8.6 UML class diagram
as an example for context
definition (Introduction to
OCL 2005 )
inv: transactions.card.owner->size() = 1
init: 0
LoyaltyAccount
points: Integer
number: Integer
earn(i : Integer)
burn(i : Integer)
isEmpty() : Boolean
pre: i > 0
1 account
body: points = 0
transactions 0..*
Transaction
init: Set{ }
points: Integer
date : Date
program() :
LoyaltyProgram
When it is evaluated, the contextual instance is the instance of LoyaltyAccount for
which the operation has been called. The initial value (init: 0) has context as the
attribute points. The contextual instance will be the instance of LoyaltyAccount
that is newly created.
8.5.2.4 Invariants on Attributes
The simplest constraint is an invariant on an attribute. Suppose our model contains
a class Customer with an attribute age, then the following constraint restricts the
value of the attribute:
context Customer inv:
age [ = 18
8.5.2.5 Invariants on Associations
One may also put constraints on associated objects. Suppose our model contains
the class Customer that has an association to class Salesperson, with the role name
salesrep and multiplicity 1, then the following constraint restricts the value of the
attribute knowledgelevel of the associated instance of Salesperson:
Context Customer inv:
salesrep.knowledgelevel [ = 5
 
Search WWH ::




Custom Search