Information Technology Reference
In-Depth Information
8.5.2.6 Collections of Objects
In most of the cases, the multiplicity of an association is not 1, but more than 1.
Evaluating a constraint in these cases will result in a collection of instances of the
associated class. Constraints can be put on either the collection itself, e.g. limiting
the size, or on the elements of the collection. Suppose in our model the association
between Salesperson and Customer has the role name clients and multiplicity 1..*
on the side of the Customer class, then we might restrict this relationship by the
following constraint:
context Salesperson inv:
clients- [ size() \= 100 and clients- [ forAll(c: Customer | c.age [ = 40)
8.5.2.7 Pre- and Postconditions
In pre- and postconditions, the parameters of the operation may be used. Fur-
thermore, there is a special keyword called result which denotes the return value of
the operation. It can be used in the postconditions only. As an example we have
added an operation sell to the Salesperson class:
context Salesperson::sell(item: Thing): Real
pre: self.sellableItems- [ includes(item)
post: not self.sellableItems- [ includes(item) and result = item.price
8.5.2.8 Derivation Rules
Models often define derived attributes and associations. A derived element does not
stand alone. The value of a derived element must always be determined from other
(base) values in the model. Omitting the way to derive the element value will result in
an incomplete model. Using OCL, the derivation can be expressed in a derivation
rule. In the following example, the value of a derived element usedServices is defined
to be all services that have generated transactions on the account:
context LoyaltyAccount::usedServices : Set(Services)
derive: transactions.service- [ asSet()
8.5.2.9 Initial Values
In the model information, the initial value of an attribute or association role can be
specified by an OCL expression. In the following examples, the initial value for the
attribute points is 0, and for the association end transactions, it is an empty set:
context LoyaltyAccount::points : Integer
init:0
Search WWH ::




Custom Search