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="http://www.eclipse.org/emf/2002/Ecore">
    <details key="constraints" value="hasValidAge"/>
    </eAnnotations>
...

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) {
            diagnostics.add
                (createDiagnostic
                    (Diagnostic.ERROR,
                     DIAGNOSTIC_SOURCE,
                     0,
                     "_UI_GenericConstraint_diagnostic",
                     new Object[] { "hasValidAge", getObjectLabel(person, context) },
                     new Object[] { person },
                     context));
        }
        return false;
    }
    return true;
}

In Ecore Editor

You can also edit the constraint in the Ecore Editor:

Werbung



comments powered by Disqus

Copyright Dunkel & Iwer GbR | Datenschutzerklärung | Impressum