The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)

UML and OCL are not programming languages but specification languages, i.e., a UML/OCL model specifies how a system should be structured and what it should do, not how it should be implemented. The models are usually not directly compiled and executed. There is, however, a strong connection between the implementation of a system and the model that specifies it. Certainly within the context of MDA, the connection between the model and its implementation(s) is getting stronger and should be stated clearly.

In this chapter, we explain how to generate Java code from a combined UML/OCL model. Our focus, of course, is on how to build code from OCL expressions. However, as the UML class diagram must be built before writing OCL expressions, the first step in implementing the model is to define the implementation of the model elements defined in the class diagram(s).

In the next step, the OCL expressions can be translated. Their translation uses the implementation of the model elements. For instance, when an attribute has been implemented by a private class member with a get and a set operation for that member, the implementation of an OCL expression referencing this attribute must use the corresponding get operation.

Furthermore, the code fragments translated from the OCL expressions must be used according to their role in the model (invariant, attribute definition, derivation rule, and so on). The code fragments should be placed in the implementation in such a way that they fulfil their purpose.

The order of steps described in the implementation process is as follows :

  1. Define the implementation of the UML model elements.

  2. Define the implementation of the OCL standard library.

  3. Define the implementation of the OCL expressions.

  4. Place the code fragments implementing the OCL expressions correctly in the code for the model elements.

  5. For invariants, and pre- and postconditions, decide when to check them, and what to do when the check fails.

The second step needs to be done only once, and can easily be done outside a specific project context. The results will be re-usable over all projects.

Because OCL is a declarative language, it specifies what should be calculated, but not how to calculate the value of an expression. Therefore, the translation from OCL to code that we show in this chapter includes a number of implementation options. These are by no means the only ones or the best ones. In other circumstances, you might need to make other choices.

The rest of this chapter is structured according to the preceding steps.

Категории