Skip to main content
blob: fb68c95499142f24fb982db17f1509966b89161d (plain) (blame)
<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><html xmlns=""><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body><h1 id="Constraint_validation">Constraint validation</h1><p>Currently, Papyrus supports two different ways to validate whether constraints are respected by application models. Both offer the option to refine constraint validation: by default,
a user will see which constraints are violated. While this is an important information, it is better to have a customized message what is wrong. It's also useful to specify the severity, in particular whether a constraint violation is an error or warning.</p><p>Do one of the following:</p><h2 id="Generate_constraints_directly_into_the_profile_definition">Generate constraints directly into the profile definition</h2><p>Constraints written in OCL within a UML profile can be generated into the definition of the profile. They are taken into account during the validation of models that apply the profile. This method is only applicable for OCL constraints.</p><p>How to embed the constraint definitions into a UML Profile:</p><ul><li>1. Save the profile</li><li>2. Papyrus asks: "Would you like to define it" (the profile), select Yes</li><li>3. Ensure "Save OCL constraints, if any, into the profile definition" is selected (this is checked by default). Please note that you should not embed constraints into the profile, if you plan to generate a plugin (see next section)<br/></li></ul><p><center>
<img border="0" src="PapyrusDSML-ConstraintDefinition.png"/><br/>
Save OCL Constraints in the Profile Definition
</center></p><h3 id="Refine_constraint_validation">Refine constraint validation</h3><p>The OCL pivot delegate supports a specific way to encode a customized message and severity in the OCL constraint: The constraint needs to be written in form of a tuple, as shown here for an example.</p><pre>
  message='\'' + + '\' is not active',
</pre><p>The original constraint expression is defined in the status field of the tuple, message and severity are further fields. Whereas only the status field is returned for evaluation, OCL evaluation with the Pivot delegate will also evaluate the custom message and severity.</p><p>In the moment, there is no specific support in Papyrus to facilitate entering OCL expressions in this way. Since the whole tuple is a "normal" OCL expression, syntax validation and completion is supported by the xtext based expression editor. But it is currently not clear whether Papyrus will offer a way to edit this tuple in a user friendly way, e.g. by synchronizing message and severity with information from the DSML stereotype and only showing the original OCL constraint to the user.</p><h3 id="Summary">Summary</h3><p>If you only deal with OCL constraints, this method is simple and straightforward. But it is not possible to select whether constraints defined in this way are included for validation or not (they are always included).</p><h2 id="Generate_a_plugin_that_embeds_the_constraints">Generate a plugin that embeds the constraints</h2><p>The user can generate a plugin from a profile that embeds the constraints, expressed either in OCL or Java. OCL constraints are embedded into the plugin.xml while Java constraints can directly be compiled into code. This is supported by the EMF validation framework.</p><p>CAVEAT: The validation of OCL rules within a plugin is a rather old mechanism. It does not take the user preference of an OCL validation delegate into account. Therefore, validation is done with the classical LPG mechanism whereas the constraint editor within Papyrus validates the constraint itself (not whether other parts of the model respect the constraint) using the Pivot OCL mechanism. In particular, the qualified name of UML meta classes in LPG must start with uml:: whereas Pivot requires UML:: (upper case). Papyrus will offer a way to assure that Pivot will be used for OCL contraints within the plugin as well, but this has not been implemented yet.</p><h3 id="How_to_embed_the_generate_constraints_into_a_plugin">How to embed the generate constraints into a plugin</h3><ul><li>1. Install the DSML validation support from the Papyrus extensions</li></ul><p>Help-&gt;Install New Software, select Papyrus update site, deselect "group items by category" and search for DSML.</p><ul><li>2. Select the UML Profile element in the Model Explorer</li><li>3. Right click UML Profile element</li><li>4. Select "create validation plugin for this DSML" from the context menu</li></ul><p><center>
<img border="0" src="PapyrusDSML-PluginValidationGeneration.png"/><br/>
Starting the validation plugin creation process
</center></p><ul><li>5. Choose whether you want to create a new plugin or generate into the plugin hosting the model. The latter is the default.</li></ul><p><center>
<img border="0" src="PapyrusDSML-GenPluginQuestion.png"/><br/>
Running the constraint validation creation wizard
</center></p><ul><li>6. <a href="Only">in case of new plugin</a>: Enter a Project name when prompted by the wizard, complete the wizard and select Finish</li></ul><p><center>
<img border="0" src="PapyrusDSML-PluginWizard.png"/><br/>
Running the constraint validation creation wizard
</center></p><ul><li>7. Install or deploy the plugin with associated profile</li></ul><h3 id="Refine_constraint_validation_2">Refine constraint validation</h3><p>Papyrus supports a UML profile that enables a developer to refine how constraints are violated. This profile is called Domain Specific Modeling Language (DSML), since it is often used in the context of profiles that adds domain specific concepts to UML.</p><p>The additional profile enables a specification of the following properties:</p><ul><li>Mode: Defines if the validation of the constraint is done in “batch” or “live” mode</li><li>Severity: Defines the severity of the constraint violation. It can be one of INFORMATION, WARNING or ERROR. The latter is the default severity (if none is specified). The CANCEL severity should be used with caution, as it causes the validation operation to be interrupted, possibly resulting in the loss of valuable diagnostic information from other constraints.</li><li>Message: Defines the message that will be displayed if the constraint is violated</li><li>Description: Provides a description of the constraint </li><li>Enabled by default: Defined if this constraint should be enabled by default or not</li></ul><p>The attributes of the profile correspond quite closely to the possibilities that the EMF validation framework offers when constraints are specified in the plugin.xml of a profile.</p><p>Advanced users can also define:</p><ul><li>Status code: The plug-in unique status code, useful for logging. </li><li>Target: The element to be validated (normally not required since generated context selectors take care of that, see section below)</li></ul><p>Please note that the additional constraint information via the profile is only taken into account, if you plan to generate a plugin embedded the constraints into the plugin.xml, as discussed above.</p><h3 id="How_to_apply_the_DSML_validation_profile">How to apply the DSML validation profile</h3><ul><li>1. Install the DSML profile from the Papyrus extensions</li></ul><p>Help-&gt;Install New Software, select Papyrus update site, deselect "group items by category" and search for DSML</p><ul><li>2. Select the UML Profile element in the Model Explorer</li></ul><ul><li>3. Select the Profile tab in the Properties View</li></ul><ul><li>4. Click on the "Apply registered profile" button</li></ul><ul><li>5. Select the "DSML Validation" profile</li></ul><p><center>
<img border="0" src="PapyrusDSML-profileApplication.png"/><br/>
Applying the DSML Validation Profile
</center></p><ul><li>5. Select the UML Constraint element in the Model Explorer or diagram</li></ul><ul><li>6. Select the Profile tab of the Properties View</li></ul><ul><li>7. Click on the "Apply stereotype" button</li></ul><ul><li>8. Select the ValidationRule stereotype</li></ul><p><center>
<img border="0" src="PapyrusDSML-stereotypeApplication.png"/><br/>
Applying the ValidationRule Stereotype
</center></p><ul><li>9. Edit the stereotype properties to define information about the behavior of the validation</li></ul><p><center>
<img border="0" src="PapyrusDSML-AnnotationEdition.png"/><br/>
Editing the DSML Stereotype Properties<br/>
</center></p><h3 id="Summary_2">Summary</h3><p>This method is a bit more complicated than the first, but also more powerful. It works for both OCL and Java. The constraints are grouped in a category that can be included in the validation or not. A message and severity specified via the DSML validation profile is taken into account, it is also possible to distinguish between Live and Batch constraints. Currently, it is of limited use in case of OCL constraints, since different OCL backends are used during constraint definition and validation.</p><h3 id="Information_about_generated_code">Information about generated code</h3><p>This section contains information about the code in the generated plugin. It is intended for readers that want to understand how the generated plugin works.<br/></p><p>The EMF validation framework supports constraints by means of a suitable definition in the plugin.xml file that embeds OCL or Java constraints, as shown here (example taken from the <a href="">OCL tutorial</a>).</p><p>First define a category</p><pre>
&lt;extension point="org.eclipse.emf.validation.constraintProviders"&gt;
         name="Your category" id="emf-validation-example/ocl"&gt;
      Category description
</pre><p>Then define constraints within the category</p><pre>
   &lt;constraint lang="OCL" severity="WARNING" mode="Batch" name="An example" id="example1" statusCode="101"&gt;
      &lt;description&gt;Describe it&lt;/description&gt;
      &lt;message&gt;Write the message&lt;/message&gt;
      &lt;target class="Writer"/&gt;
         self.books-&gt;collect(b : Book | b.category)-&gt;asSet()-&gt;size() &lt;= 2
</pre><p>Constraint validation needs to be done on the context element. Therefore, the plug-in definition needs to associate constraints with the elements that should be validated. This is called constraint binding. The following snipped shows an example of such a binding.</p><pre>
  &lt;extension point="org.eclipse.emf.validation.constraintBindings"&gt;
      &lt;clientContext id="MyContextID"&gt;
         &lt;selector class=myplugin.selectors.ConstraintSelector"/&gt;
      &lt;binding context="MyContextID"&gt;
         &lt;constraint ref="myplugin.example1"/&gt;
</pre><p>The generator creates a selector for each context element (typically a stereotype). The selector checks the element to be validated and selects those typed with the context element or a sub-types thereof. The constraint binding associates a list of constraints with the selector: those that share the same context element.</p></body></html>

Back to the top