This tutorial is an EMF that shows a simple way to add Java validation rules to a model using the EMF validation Framework. To go fast, it takes the output of the GMF tutorial 7 as an input (in order not to have to build a new ecore model).
The EMF Validation Framework is rich and offers different ways to implement validation rules for a model : you can use annotations, OCL language, and so on. If you are a Java developer, Java language is maybe the best way to implement validation rules, and you maybe don't want to learn OCL, at least for now. If that is the case, you may follow this tutorial to see a simple way to implement your validation rules in Java.
This tutorial is based on the model built in the seventh GMF tutorial wich is availabale through :
Full code is available through :
This tutorial has been built with :
on save
validation for EMF resources.
As you may know, it is possible to add operations to an EMF model EClass. On important thing to notice is that if the operation has the following signature : public boolean validate(DiagnosticChain diagnostic, Map<Object, Object> context) then EMF considers that this is an invariant operation.
Once having added your invariant to your ecore model and regenerated your model classes, if you implement the invariant body, it will be evaluated during the default EMF validation process (right click in the tree editor, and Validation
).
In this tutorial, we will add two invariants.
The first invariant will check if the student has at least on friend. If the rule is not validated, a warning will be generated.
The second will check if a classroom has at least one student. If the rule is not validated, an error will be generated.
School/model/school.ecore
fileEOperation
to the Student
Eclass with the following properties :Name
: validate
EType
: EBoolean
EParameter
to the EOperation
you've just created with the following properties :Name
: diagnostic
EType
: EDiagnosticChain
EParameter
to the EOperation
you've just created with the following properties :Name
: context
EType
: EMap
EMap<?, ?>
node and for each child node : select the EJavaObject
value for the EClassifier
propertyvalidate
operation from the Student
to the School
EClass.School/model/school.genmodel
fileGenerate Model Code
)StudentImpl
class and go to the validate
method.NOT
after the @generated
string in the javadoc comments/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public boolean validate(DiagnosticChain diagnostic, Map<Object, Object> context) { boolean valid = true; if (diagnostic != null) { if (getFriends().size() == 0) { valid = false; diagnostic.add(new BasicDiagnostic(Diagnostic.WARNING, SchoolValidator.DIAGNOSTIC_SOURCE, SchoolValidator.STUDENT__VALIDATE, "The student '" + getName() + "' has no friend.", new Object[] { this })); } } return valid; }
ClassroomImpl
class and go to the validate
method.NOT
after the @generated
string in the javadoc comments/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated NOT */ public boolean validate(DiagnosticChain diagnostic, Map<Object, Object> context) { boolean valid = true; if (diagnostic != null) { if (getStudents().size() == 0) { valid = false; diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR, SchoolValidator.DIAGNOSTIC_SOURCE, SchoolValidator.CLASSROOM__VALIDATE, "The classroom '" + getName() + "' has no student.", new Object[] { this })); } } return valid; }
School
model with two classrooms, the first with no student, the second with one only student. If you right click on the root node of the EMF tree editor and select Validate
, you may get this result :
I hope that this material will be helpful for you. If you want to support it, your help is welcome :
Discussion
Hello,
I have been given a project to model a transactional language and allow the User to develop a process from the same in a Graphical Editor. I have done this, in GMF and the user can draw (make) the diagrams in the editor. The next task is to incorporate the semantic rules of the language (like structural congruence and reduction relations) and then based on these rules I am supposed to make a functionality that gives user an option to 'Transform' a particular diagram into a resulting diagram.
For example : if there are 2 processes in an editor diagram (made by the user), one of them Process Q is a SUCCESS and other is a normal process and both are in sequence i.e (Process P followed by another Process Q(which is a SUCCESS) ) then the result of this should be another editor diagram that shows only Process P. (as this is how the language rules defines such a sequence)
The doubts I have are :
1) How do I read the editor diagram in a file/code in order to generate the other editor diagram ? 2) Which files or codes (generated by GMF genmodel / EMF genmodel ) am I supposed to write all this code in ? 3) What structure should I follow ? 4) How do I add the functionality 'Transform' on the context Menu in the Eclipse instance (or the Editor window) ?
Also, I have a node T with 3 compartments, I want that when I click on the compartment, a new diagram editor should open up and allow me to add the same type of components. (i.e the meta-model, gmfgraph, gmfmap, gmftools will be the same ). In simpler terms, the compartments should contain a diagram. Also is it possible to add the diagram within that compartment rather than opening a new editor (Eclipse Instance)?
Thanks Kieara
How I think I have already answered this question in the fifth tutorial… Sorry for the delay, I think that my answer has come too late…
Dear All,
Just to improve the tutorial and automate the process, i.e., to avoid playing with the generated code, one could make use of annotations in the model and generate them. You could follow the following steps:
1. After the “Invariant declaration” step, right click the class and add a new annotation. 2. Enter the source as “http://www.eclipse.org/emf/2002/GenModel”. 3. Right click the annotation and add a new “Details Entry”. Set the key as “body” and in the value add the java code that you want to get generated.
More information at http://www.vogella.com/tutorials/EclipseEMF/article.html#methods
Thanks again for the great tutorial.
Cheers Arun