Eclipse: Add a constraint to an Ecore model

Adding a constraint to the EMF Ecore model in the Sample Ecore Model Editor is not very obvious.

You have to add this via an annotation to your Ecore XML file:

<eClassifiers xsi:type="ecore:EClass" name="Person">
    <eAnnotations source="">
    <details key="constraints" value="hasValidAge"/>

This will generate a constraint hasValidAge method in the EMF validator class:

 * Validates the hasValidAge constraint of '<em>Person</em>'.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
public boolean validatePerson_hasValidAge(Person person, DiagnosticChain diagnostics, Map<Object, Object> context) {
    // TODO implement the constraint
    // -> specify the condition that violates the constraint
    // -> verify the diagnostic details, including severity, code, and message
    // Ensure that you remove @generated or mark it @generated NOT
    if (false) {
        if (diagnostics != null) {
                     new Object[] { "hasValidAge", getObjectLabel(person, context) },
                     new Object[] { person },
        return false;
    return true;

In Ecore Editor

You can also edit the constraint in the Ecore Editor:


comments powered by Disqus

Copyright Dunkel & Iwer GbR | Datenschutzerklärung | Impressum