6.3 Attributes and Association Ends


This section explains which functions expressions may have when the context is an attribute or the role of one end of an association. The model elements that may be used in this case are all attributes, query operations, states, and associations of the contextual type.

6.3.1 Derivation Rules

An expression whose context is an attribute or association role may be used as a derivation rule . A derivation rule specifies that the value of the context element should always be equal to the value given by the evaluation of the derivation rule. If the context is an attribute, the contextual type is the type that holds the attribute. If the context is an association end, the contextual type is the type at the opposite end of the association. For example, in the R&L model, the context of the association end named participants is the class LoyaltyProgram .

To indicate that the expression is intended to be a derivation rule, the context declaration includes the name of the attribute or the association end, and is followed by the keyword derive and a colon , as shown in the following examples:

  context  LoyaltyAccount::totalPointsEarned : Integer  derive  : transactions->select( oclIsTypeOf( Earning ) )            .points->sum()  context  CustomerCard::myLevel : ServiceLevel  derive  : Membership.currentLevel 

6.3.2 Initial Values

The initial value of an attribute or association role can also be given by an OCL expression. An initial value is the value that the attribute or association end will have at the moment that the contextual instance is created. The context declaration is followed by the keyword init , the name of the attribute, and the expression that gives the initial value, as shown in the following two examples:

  context  CustomerCard::transactions : Set( Transaction )  init  : Set{}  context  CustomerCard::valid : Boolean  init  : true 

Note the difference between an initial value and a derivation rule. A derivation rule states an invariant: The derived element should always have the same value that the rule expresses. An initial value must hold only at the moment when the contextual instance is created. After that moment, the attribute may have a different value at any point in time.



Object Constraint Language, The. Getting Your Models Ready for MDA
The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)
ISBN: 0321179366
EAN: 2147483647
Year: 2003
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net