Section A.4. Pulling It Together


A.4. Pulling It Together

So far you've seen the building blocks of OCL expressions. Now let's combine them to build a sample OCL expression.

 totalCost = baseCost * (1+getTaxRate(  )) 

This OCL expression is taken from Figure A-2. It contains the following building blocks of an OCL expression:


Model elements

totalCost, baseCost, and getTaxRate( )


Constant

1


Operators

=, *, and +

The above expression actually consists of several OCL expressions, which are in turn combined by operators. For example, 1+getTaxRate( ) evaluates to a Real, which is then multiplied with baseCost. That resulting value is checked for equality with totalCost using the = operator. You can combine model elements, constants, and expressions according to their type, but the combined expression must be type Boolean. This is because we're focusing on using OCL to express constraints and guards, which must evaluate to true or false.

Another commonly used constraint is to specify that an object isn't null. To specify that an object isn't null, you have to use the OCL's notation for sets and operations on sets. Figure A-3 shows how to check that Author's member through association fee isn't null using the expression:

 self.fee->notEmpty(  ) 

Figure A-3. Constraining that a member isn't null


Notice the reference to self in the OCL expression in Figure A-3. Because it is attached to Author, self refers to objects of type Author. The self keyword is commonly used when you set a context in an OCL expression, as shown in the following section.




Learning UML 2.0
Learning UML 2.0
ISBN: 0596009828
EAN: 2147483647
Year: 2007
Pages: 175

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