B.1. OCL BasicsThe Object Constraint Language is just that: a language. It obeys a syntax and has keywords. However, unlike other languages, it can't be used to express program logic or flow control. By design, OCL is a query-only language; it can't modify the model (or executing system) in any way. It can be used to express preconditions, postconditions, invariants (things that must always be TRue), guard conditions, and results of method calls. OCL can be used virtually anywhere in UML and is typically associated with a classifier using a note. When an OCL expression is evaluated, it is considered to be instantaneous, meaning the associated classifier can't change state during the evaluation of an expression. B.1.1. Basic TypesOCL has several built-in types that can be used in OCL expressions:
In addition to the built-in types, any classifier used in your UML model is recognized as a type by OCL. Because OCL is a strongly typed language, you can't compare values of one type directly with values of another type. B.1.2. CastingOCL does support casting objects from one type to another as long as they are related through a generalization relationship. To cast from one type to another use the operation oldType.oclAsType(newType). For example, to cast a Java List to an ArrayList to call the size( ) operation, use the expression: List.oclAsType(ArrayList).size( ) If the actual object isn't an instance of the new type, the expression is considered undefined. |