Section A.1. Building OCL Expressions


A.1. Building OCL Expressions

Figure A-2 shows a class diagram with a few OCL expressions, including:


Simple number comparison

baseCost >= 0.0


More complicated number comparison

totalCost = baseCost * (1+getTaxRate( ))


String comparison

status <> 'Unpaid'

Figure A-2. Example OCL constraints of varying complexity


Unlike many languages, such as Java, in OCL the = operator is used to check whether two items are equal, not to assign a value.


OCL expressions consist of model elements, constants, and operators. Model elements include class attributes, operations, and members though association. The OCL expressions in Figure A-2 use the model elements baseCost, totalCost, and getTaxRate( ). (Later sections contain OCL expressions with members through association.)

Constants are unchanging values of one of the predefined OCL types. In Figure A-2, constants include 0.0 of type Real and 'Unpaid' of type String. Operators combine model elements and constants to form an expression. In Figure A-2, operators include <>, +, and =.

The following sections discuss the basics of OCL types and operators and then show how to combine these into expressions you can use in your UML models.




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