| <div id="midcolumn"> |
| <h2>How to add custom validation rules</h2> |
| <p>With the built in validation in emf it is possible to model the constraints of your model in the ecore file. While generating model code, validation code is also generated. The only aspects you have to adapt in the code are determining the condition when the validation shall fail, update attribute string, defining a meaningful message which explains the reason for the failing and insert the graphical object in the returning object so that also the live validation can be triggered on this attribute.</p> |
| <p>Step by Step description:</p><p>In the following step by step description the bowling model is used as an example.</p> |
| <p>1. Validation constraints are modelled by EOperations. In order that the validation code is generated automatically the signature of the EOperation is important. For instance, if we want each player (in the bowling model) to have a name we define following EOperation:</p> |
| <p> - meaningful name</p> |
| <p> -return value of type EBoolean</p> |
| <p> -2 parameters of type EDiagnosticChain and EMap<?,?></p> |
| <p><img height="473" src="images/image00.png" width="426"></p> |
| <p>2. Reload ecore model in the genmodel and generate model code. Following method will be generated in class PlayerImpl:</p> |
| <p><img height="245" src="images/image02.png" width="800"></p> |
| <p>3. To make this method work we have to adapt the aspects which are mentioned above:</p> |
| <p> 1. Determine condition when validation fails</p> |
| <p> 2. Update path of validation attribute</p> |
| <p> 3. Update meaningful error message</p> |
| <p> 4. Insert graphical object in return object</p> |
| <p><img height="382" src="images/image03.png" width="800"></p><p class="c0"></p> |
| <p>4. This is how the live validation looks like:</p><p><img height="359" src="images/image01.png" width="566"></p> |
| <p>5. To trigger validation manually you have to right-click on one element of the project and click “validate”. The validation results are shown in the “Validation View”:</p><p><img height="358" src="images/image04.png" width="661"> </p></body></html> |
| </div> |