Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

As an alternative to Java code that must be compiled, you can use an interpreted expression language like OCL for coding constraints. The advantage is a shorter development cycle, the downside is slightly more complex configuration. And of course, there are both advantages and disadvantages to expression languages. Note that the available expression languages depend on what plugins are installed in Eclipse. E.g. OCL support is not part of the standard modelling package, so must be installed , but luckily it is available in from the standard Eclipse update site, so it's easy to installinstallation site.

Adding and using an interpreted constraint is done in three steps (note that the OCLinEcore Editor provides a custom textual syntax for Ecore including constraints, see Editing Ecore models):

Step 1: Add an EAnnotation for using OCL for validation to the EPackage.

Support for using OCL for validation must be explicitly indicated with an EAnnotation on the EPackage. Right-click on the EClass in the editor and add an EAnnotation. Set the source attribute to http://www.eclipse.org/emf/2002/Ecore, and add a key/value pair, with validationDelegate as the key and a language-specific identifier as the value, e.g. http://www.eclipse.org/emf/2002/Ecore/OCL for the standard OCL support. Other identifiers are

 

If you have installed Acceleo (part of Sirius) and prefer the Acceleo Query Language you can use http://www.eclipse.org/acceleo/query/1.0.


Step 2: Add an EAnnotation to the appropriate EClass.

This step is the same as step 1 in the previous section: Add an EAnnotation with the source attribute set to http://www.eclipse.org/emf/2002/Ecore anda key/value pair, with constraints as the key and the logical names of the constraints separated by a space.

 

Step 3: Add an EAnnotation for the constraint expression.

This step links the validationDelegate from step 1 to the constraint in step 2: Add another EAnnotation to the EClass, where the source attribute is http://www.eclipse.org/emf/2002/Ecore/OCLthe same language identifier as above, i.e. the value for the validationDelegate key of step 1. Then add key/value pairs for each constraint, with the constraint name as the key and the OCL expression for the constraint as the value.

An example is shown in the figure to the right. The OCL expression is executed with an OrgUnit instance as self and computes the set of workers with the role of a manager and checks if its size is 1. It can be a bit tricky to formulate the correct expression, and many language installations provide a custom interpreter view for experimenting. E.g. OCL has a special OCL Console and Sirius has an Interpreter view that support evaluating both OCL and AQL expressions on the current selection in the active editor.