6.2 Classes and Other Types


This section explains which function expressions may have when the context is a type, i.e., a class, interface, datatype, or component. The model elements that may be used in this case are all attributes, query operations, states, and associations of the type.

6.2.1 Invariants

The first way in which an expression with a type as context can be used is as an invariant. An invariant is described using a boolean expression that evaluates to true if the invariant is met. The invariant must be true upon completion of the constructor and completion of every public operation, but not necessarily during the execution of operations. Incorporating an invariant in a model means that any system made according to the model is faulty when the invariant is broken. How to react when an invariant is broken is explained in Section 4.6.2.

To indicate that the expression is intended to be an invariant, the context declaration is followed by the keyword inv, an optional name , and a colon , as shown in the following example:

  context  Customer  inv  myInvariant23: self.name = 'Edward' 

Using an OCL expression as invariant means that for all instances of the contextual type, the expression must evaluate to true. Thus, in our (contrived) example, all instances of class Customer would have to be named Edward.

An invariant may be named, which can be useful for reference in an accompanying text. The preceding invariant is named myInvariant23 .

6.2.2 Definitions of Attributes or Operations

Attributes or operations may be defined by an OCL expression. Defining an attribute or operation this way means that every instance of the contextual type holds an attribute or operation that conforms to the given definition.

The context of an attribute or operation definition is always the type that must hold the new element. To indicate that the expression is intended to be a definition, the context declaration is followed by the keyword def and a colon, as shown in the following examples. The second example, by the way, is a specification of the operation used in the use case example described in Section 3.8.1:

  context  Customer  def  : initial : String = name.substring(1,1)  context  CustomerCard  def  : getTotalPoints( d: Date ) : Integer =         transactions->select( date.isAfter(d) ).points->sum() 

In the case of an attribute definition, the name and type of the attribute must be given. The expression following the equal sign is also mandatory. This expression indicates how the value of the attribute must be calculated. It is a derivation rule (see Section 6.3.1).

All operations defined by an OCL expression are considered to be query operations. The name, parameters (including their types), and the return type (if any) of the operation must be given. The expression following the equal sign is also mandatory, and states the result of the operation (see Section 6.4.2).



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