Dresden OCL Toolkit

Using the Toolkit in Together

Prerequisites

Create a model

As a first step, start a new project in Together and create a class Person with two attributes age (integer) and isMarried (boolean).

Create OCL constraints

For creating some OCL constraints on this admittedly simplistic model process through the following steps:

  1. Select the newly created class Person on the diagram pane.

  2. Open the Inspector (press Ctrl+Alt+I) if not already open and choose the OCL Constraints pane.

  3. Press the Button for creating
a new constraint button and enter any legal constraint.

  4. Try to enter invalid OCL constraints or try to use wrong context declarations or wrong types in your expressions to see how syntax and type checker are working.

Screenshot of the Inspector tab

Constraints will be stored as Javadoc tags directly in the source code.

Instrument classes with constraint checking code

You can either instrument a single class or mark a number of classes for instrumentation first (using the 'for instrumentation' checkbox in the 'Properties' tab of the Inspector) and instrument several classes in one go. Here we will simply instrument the one class that we have created before.

  1. Select the newly created class Person on the diagram pane.

  2. Either choose 'Tools' --> 'Instrumentation' --> 'Instrument class(es)' from the main menu or right-click the class and choose 'Instrumentation' --> 'Instrument class(es)' from the popup menu.

  3. The instrumentation code will be inserted and because of Together's round trip engineering approach will blow up your diagram as well.

  4. Before you can actually compile the instrumented class, you will have to add the ocl-together.jar that resides in {TOGETHER-BASE-DIR}/modules/com/togethersoft/modules/dresden-ocl if you have followed the step-by-step guide at installation to your current project's class path ('Project' --> 'Project Properties' --> 'Search/Classpath' --> 'Add Path, Library or Archive').

  5. Choosing 'Tools' --> 'Instrumentation' --> 'Clean class(es)' when the instrumented class is selected will clean your class from all the instrumentation code.

Now that you know in principle how to instrument classes with OCL constraints go ahead and add a constructor for the Person class, create a main method and construct a couple of Person instances, some of them adhering to, some of them violating your OCL constraints. Then instrument the class, compile and run it. Watch the console output for notifications about constraint violations.

Note: Please be aware that the integration of the Dresden OCL Toolkit into Together is prototypical. A major shortcoming in the usage of the Injector from within Together is that the injection process still needs some information that it gathers via reflection instead of purely from the model in Together. In effect, when you use types in your constraints that stem from the model you have created the injection will fail as the reflection engine will normally fail as 'your' model will not be in Together's classpath.

As a workaround you can alter Together's start script manually and add your project's output directory to the classpath. Also make sure you compile your project before using injection.

SQL Code Generation

It is also possible, using the Toolkit and Together to generate SQL code from the UML/OCL model. This function is not explained in this example but mentioned, to ensure that you know the SQL code generation is possible.