Various types, attributes, and methods can be defined in the ecore model in detail, but there are no restrictions such as "at least two products in each category. To solve this problem, EMF provides a verification framework (validator framework) that identifies specific methods in the ecore file as verification methods and generatesCode.
Taking the shop model as an example, assuming that "there are at least two products in each category" is required, we need to add a verification method named "validateproductscount" in shop. ecore, as shown in 1. The return type of the verification method must have two parameters: the first is the ediagnosticchain type, and the second is the emap type. The parameter names are not required, but the order of the two parameters cannot be exchanged.
Figure 1 new verification method
Next, generate the code again through shop. genmodel (if there is no shop. genmodel file, create one through "new-> EMF model"). Note that there is no need to reload this genmodel file. By comparing the code before and after the verification method is added, we can find that EMF generates a file named shopvalidator. Java in the util package. At the same time, the Code in the validateproductscount () method of categoryimpl is as follows:
/**
* <! -- Begin-user-doc -->
* <! -- End-user-doc -->
* @ Generated
*/
Public Boolean Validateproductscount (diagnosticchain diagnostics, map contex ){
// Todo: implement this method
// -> Specify the condition that violates the invariant
// -> Verify the details of the diagnostic, including severity and message
// Ensure that you remove @ generated or mark it @ generated not
If ( False ){
If (Diagnostics ! = Null ){
Diagnostics. Add
( New Basicdiagnostic
(Diagnostic. error,
Shopvalidator. diagnostic_source,
Shopvalidator. categoryincluvalidate_products_count,
Ecoreplugin. instance. getstring ( " _ Ui_genericinvariant_diagnostic " , New Object [] { " Validateproductscount " , Eobjectvalidator. getobjectlabel ( This , Contex )}),
New Object [] { This }));
}
Return False ;
}
Return True ;
}
Different from the implementation of common methods (simply throwing an unsupportedoperationexception exception ). Because this code is "If (false ){...} "surrounded, so if not customized, the content will never be executed. The function of the surrounded code is to record verification errors for unified reporting. Now, we need to modify this condition to tell EMF when to run the code, as shown below:
/**
* <! -- Begin-user-doc -->
* <! -- End-user-doc -->
* @ Generated not
*/
Public Boolean Validateproductscount (diagnosticchain diagnostics, map contex ){
// We modified the verification conditions.
If (Getproducts (). Size () < 2 ){
If (Diagnostics ! = Null ){
Diagnostics. Add
( New Basicdiagnostic
(Diagnostic. error,
Shopvalidator. diagnostic_source,
Shopvalidator. categoryincluvalidate_products_count,
Ecoreplugin. instance. getstring ( " _ Ui_genericinvariant_diagnostic " , New Object [] { " Validateproductscount " , Eobjectvalidator. getobjectlabel ( This , Contex )}),
New Object [] { This }));
}
Return False ;
}
Return True ;
}
Because the verification conditions we want to implement are very simple, so there are few changes to the Code. Do not forget to modify the @ generated mark in the comment. Now we can run our shop editor. Right-click a category that contains only one product and select the "Validate" command in the pop-up menu, the prompt information shown in 2 is displayed.
Figure 2 Model Verification Failed
It is intuitive to express the constraints of the model by modifying the Code. However, when there are many and uncertain conditions, it may be easier to directly express these conditions in the ecore file, in addition, Java code does not even need to be re-generated and compiled. This article on the eclipse websiteArticle"Implementing Model integrity in EMF with emft OCL" has implemented this function through a custom jet template. If you are interested, try it.
Download this project
Add another method for EMF to generate the validate code: Create an eannotation under eclass that needs to be verified in the ecore model, and set its source attribute to "Invalid entry, the value attribute is specified as the expected constraint name. To define multiple constraint names, separate them by spaces (for the format, see ecoreutil # setconstraints (), as shown in 3. In this way, EMF generates xxxvalidator In the util package. java files and corresponding verification methods. The Code of these methods is similar to that of the first statement above. You also need to modify the conditions in the IF Statement by yourself.
Figure 3 add eannotation to the ecore model to generate Verification Code