OCL (OMG document ad/02-05-09, 2002) is an expression language in which you can write expressions over models, for instance, derivation rules for attributes, the body of query operations, invariants, and pre- and postconditions. OCL can be used for both MOF and UML models. Using OCL extends the expressive power of UML/MOF, and allows the modeler to create more precise and more extensive models.
Any OCL expression evaluates to a value. OCL is a specification language and an OCL expression always describes what the value is, but never dictates how the expression should be calculated. Of course, OCL expressions can be translated to programming languages (for example, Java) to specify how the expression is executed. OCL expressions are "pure" expressions in the sense that they never have side effects.
11.5.1 Using OCL with UML
Traditionally, OCL has been used in UML to specify constraints. The most common types of constraints are invariants, preconditions, and postconditions. More recently the use of OCL for other types of expressions has become popular. This includes the use of OCL for the following purposes:
A UML model of a system becomes more precise and more complete by applying OCL. In the context of MDA, this means that the source model of a transformation becomes much richer, which makes it possible to generate a much more complete target model. The ability to specify a precise and complete source model allows an MDA transformation to generate more of the PSM or code. The value of the MDA approach to the developers is enhanced considerably. OCL is used for specifying expressions in UML models by any developer that uses UML. As such, all developers can benefit from the added precision and expressiveness .
11.5.2 Using OCL with the MOF
As explained above, OCL can be used at the MOF layer as well, allowing one to write expressions over metamodels. The first usage of OCL with the MOF has been in the definition of the UML metamodel. Several hundreds of invariants, or so-called "well- formedness rules," were written in OCL to complete the diagrams that depict the metamodel graphically. The same approach is taken in the definition of other OMG standards, such as CWM and the MOF itself.
This results in the same advantages as the use of OCL gives to UML models. The metamodels become more precise and constitute a better specification with less ambiguity. For the definition of standards this is even more important than for other models.
11.5.3 The Role of OCL in MDA
In addition to bringing more precision to source models and to language definitions, OCL can be used very effectively in the definition of transformations, as the transformation definition language described in section 9.2 shows. A transformation maps one or more elements in a source model to one or more elements in a target model. One OCL query specifies the elements in the source model of a transformation, while a second OCL expression specifies the elements in the target model.
Many transformations can only be applied under certain conditions. These conditions can be specified in OCL too, one OCL condition on the source elements to be mapped, a second OCL condition on the target elements. All OCL expressions used in a transformation definition are specified on the metamodel of the source and target languages.