Versions Compared

Key

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

...

Ecore supports two ways of implementing constraints, Java code in generated validation methods, and interpreted expressions written in one of many supported languages.

Constraints implemented in Java code
Anchor
java
java

The Java code variant relies on genmodel's ability to generate a validator subclass with methods for each constraint. Adding and using such a constraint is done in four steps:

Step 1: Add an EAnnotation to the appropriate EClass.

Right-click on the EClass in the editor and add an EAnnotation. Set the source attribute to http://www.eclipse.org/emf/2002/Ecore. Then add a key/value pair, with constraints as the key and the logical name of the constraint (a valid Java name) as the value. If you need to add several constraints, separate their names by a space.

Step 2: Generate code and locate the validation method stub(s)

Genmodel will generate an EValidator subclass in the util package with one method for each constraints (if there are any) named validator_<constraint name>. The method body contains template code for adding a diagnostic for a violated constraint with ERROR severity. The condition is initially false and needs to be replaced by the real constraint logic. An example of the generated stub is shown right.

 

Step 3: Implement the constraint logic in Java

The false condition has been replaced by a call to the hasOnlyNameCharacters helper method. You may also lower the severity if the constraint is more of a suggestion. Notice how NOT is added after @generated, to indicate that we've change generated code. This will prevent the code from being overwritten if the class is regenerated.

Code Block
	/**
	 * Validates the nameCharacters constraint of '<em>Person</em>'.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */

	public boolean validatePerson_nameCharacters(Person person, ...) {
		if (! hasOnlyNameCharacters(person.getName())) {
			if (diagnostics != null) {
				...
			}
			return false;
		}
		return true;
	}

	private boolean hasOnlyNameCharacters(String name) {
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (! (Character.isLetter(c) || c == ' ' || c == '-')) {
				return false;
			}
		}
		return true;
	}

Step 4: Make the implementation available for the editor

Since the constraint logic is implemented in Java code that must be compiled, you need to either launch another Eclipse or install the EMF project in the same Eclipse, to make the constraint available in an editor.

 

Interpreted constraints

Anchor
interpreted
interpreted

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 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 from the standard Eclipse installation site.

...