Section A.5. Context


A.5. Context

Figure A-2 defined OCL expressions on the elements they constrain, while Figure A-3 defined an OCL expression on the containing class. You can write an OCL expression at different areas in your diagram. How you write the OCL expression depends on the context, or where you are in the diagram.

Figure A-4 shows how to check that baseCost of AccountFee is greater than or equal to 0 at different reference points in the diagram. The first diagram shows this constraint in the context of baseCost, the second shows this constraint at AccountFee, and the third shows this constraint at Author.

Figure A-4. The way you write a constraint depends on your reference point in the diagram


If your reference point is baseCost, e.g., by writing a constraint in curly braces after baseCost, then you write:

 baseCost >= 0.0 

If you're referring to the AccountFee class, e.g., by attaching a note to the AccountFee class, then you write:

 self.baseCost >= 0.0 

Finally, if you're referring to the Author class, e.g., by attaching a note to the Author class, then you write:

 self.fee.baseCost >= 0.0 

You can also write OCL constraints that aren't physically attached to model elements. For example, your UML tool may provide a text editor for entering constraints. If you do this, write the context explicitly. If the context is the AccountFee class, then you write:

 Context AccountFee inv: self.baseCost >= 0.0 

The inv keyword indicates that the constraint is an invariant, or a condition that must always be true. When specifying the context, you also specify the type of constraint it is. Constraint types are discussed in "Types of Constraints," next.




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