Section B.1. OCL Basics


B.1. OCL Basics

The 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 Types

OCL has several built-in types that can be used in OCL expressions:


Boolean

Must be either true or false. Supports the logical operators and, or, xor, not, implies, and if-then-else.


Integer

Any integer value (e.g., 100, -12345, 5211976, etc.). Supports the operators *, +, -, /, and abs( ).


Real

Any decimal value (e.g., 2.222003, -67.76, etc.). Supports the operators *, +, -, /, and floor( ).


String

A series of letters, numbers, or symbols interpreted as a string (e.g., "All writing and no play make Dan..."). Supports the operators concat( ), size( ), and substring( ).

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. Casting

OCL 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.




UML 2.0 in a Nutshell
UML 2.0 in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596007957
EAN: 2147483647
Year: 2005
Pages: 132

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