Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Papyrus/UserGuide/Profile Constraints"

(Refine constraint validation)
(Constraint validation)
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[https://help.eclipse.org/2020-09/topic/org.eclipse.papyrus.dsml.validation.doc/target/generated-eclipse-help/dsml-validation.html?cp=73_1_3 Constraint validation (2020-09)]
  
= Refine constraint validation =
 
 
When constraints are validated, a user will by default see which constraints are violated. While this is an important information, a user typically wants to see a useful message what is wrong, e.g. a customized message or specific information about the severity.
 
 
There are different ways to specify this information, in the sequel, we talk about a specific profile available in an Papyrus add-on and a specific way to encode OCL constraints.
 
 
== Papyrus DSML profile ==
 
 
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.
 
 
The additional profile enables a more precise specification of the following properties:
 
* Mode: Defines if the validation of the constraint is done in “batch” or “live” mode
 
* 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.
 
* Message: Defines the message that will be displayed if the constraint is violated
 
* Description: Provides a description of the constraint
 
* Enabled by default: Defined if this constraint should be enabled by default or not
 
 
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.
 
 
Advanced users can also define:
 
* Status code: The plug-in unique status code, useful for logging.
 
* Target: The element to be validated
 
 
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 shown later.
 
 
=== How to apply the DSML validation profile ===
 
 
*1. Install the DSML profile from the Papyrus extensions
 
 
Help->Install New Software, select Papyrus update site, deselect "group items by category" and search for DSML
 
 
*2. Select the UML Profile element in the Model Explorer
 
 
*3. Select the Profile tab in the Properties View
 
 
*4. Click on the "Apply registered profile" button
 
 
*5. Select the "DSML Validation" profile
 
 
<center>
 
[[Image:PapyrusDSML-profileApplication.png]]<br>
 
Applying the DSML Validation Profile
 
</center>
 
 
*5. Select the UML Constraint element in the Model Explorer or diagram
 
 
*6. Select the Profile tab of the Properties View
 
 
*7. Click on the "Apply stereotype" button
 
 
*8. Select the ValidationRule stereotype
 
 
<center>
 
[[Image:PapyrusDSML-stereotypeApplication.png]]<br>
 
Applying the ValidationRule Stereotype
 
</center>
 
 
*9. Edit the stereotype properties to define information about the behavior of the validation
 
 
<center>
 
[[Image:PapyrusDSML-AnnotationEdition.png]]<br>
 
Editing the DSML Stereotype Properties<br/>
 
</center>
 
 
== OCL specific extensions ==
 
 
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.
 
 
<pre>
 
Tuple{
 
  status=base_Class.isActive,
 
  message='\'' + base_Class.name + '\' is not active',
 
  severity=-5
 
}.status
 
</pre>
 
 
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.
 
 
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.
 
 
= Generation from Constraints =
 
 
<center>
 
[[image:PapyrusDSML-UseCaseGeneration.png]]<br/>
 
</center>
 
 
A plugin can contain a set of validation rules that are expressed either in OCL or Java. EMF validation supports this by means of a suitable definition in the plugin.xml file that embeds OCL rules, as shown here (example taken from the [http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.emf.validation.doc%2Ftutorials%2FoclValidationTutorial.html OCL tutorial]).
 
 
First define a category
 
<pre>
 
<extension point="org.eclipse.emf.validation.constraintProviders">
 
  <category
 
        name="Your category" id="emf-validation-example/ocl">
 
      Category description
 
  </category>
 
</pre>
 
 
Then define constraints within the category
 
<pre>
 
  <constraint lang="OCL" severity="WARNING" mode="Batch" name="An example" id="example1" statusCode="101">
 
      <description>Describe it</description>
 
      <message>Write the message</message>
 
      <target class="Writer"/>
 
      <![CDATA[
 
        self.books->collect(b : Book | b.category)->asSet()->size() <= 2
 
      ]]>
 
  </constraint>
 
</pre>
 
 
Constraints 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.
 
 
<pre>
 
  <extension point="org.eclipse.emf.validation.constraintBindings">
 
      <clientContext id="MyContextID">
 
        <selector class=myplugin.selectors.ConstraintSelector"/>
 
      </clientContext>
 
      <binding context="MyContextID">
 
        <constraint ref="myplugin.example1"/>
 
      </binding>
 
</pre>
 
 
Papyrus supports the generation of a generated plug-in from a plugin, in particular for the case of stereotypes as context elements.
 
 
 
OCL constraints can be embedded into the profile definition and are validated without a dedicated plugin. Thus, if you only deal with OCL constraints, it is not necessary to create such a plugin. It may still be interesting, since the constraints defined in the generated plugin are grouped in a category.  In addition, a message and severity specified via the DSML validation profile is taken into account.
 
 
Do one of the following:
 
 
==Generate constraints directly into the profile definition==
 
 
Constraints written in OCL within a UML Profile can be generated into the definition of the profile and read during the validation of the model to which the profile is applied.
 
 
How to embed the constraint definitions into a UML Profile:
 
 
*1. Save the profile
 
*2. Papyrus asks to you if "Would you like to define it" (the profile), select Yes
 
*3. Ensure "Save OCL constraint in the definition" is selected (the box is checked by default in later versions of Papyrus)<br/>
 
<center>
 
[[image:PapyrusDSML-ConstraintDefinition.png]]<br>
 
Save OCL Constraints in the Profile Definition
 
</center>
 
 
Note that it is possible to control the message and severity using this method by writing the OCL constraint as a tuple, as shown here
 
 
<pre>
 
Tuple{
 
  status=base_Class.isActive,
 
  message='\'' + base_Class.name + '\' is not active',
 
  severity=-5
 
}.status
 
</pre>
 
 
It is currently not clear whether Papyrus should 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.
 
 
==Generate constraints as EMF validation plugins==
 
The user can generate a plugin that wraps constraints and can be used in the EMF plugin validation.<br>
 
The constraint can be generated in Java code, or directly from OCL.<br>
 
 
How to embed the generate constraints into a plugin:
 
 
*1. Select the UML Profile element in the Model Explorer
 
*2. Right click UML Profile element
 
*3. Select "create validation plugin for this DSML" from the context menu
 
*4. Enter a Project name when prompted by the wizard
 
*5. Complete the wizard and select Finish
 
*6. Install or deploy the plugin with associated profile
 
 
<center>
 
[[image:PapyrusDSML-PluginValidationGeneration.png]]<br/>
 
Starting the validation plugin creation process
 
</center>
 
 
<center>
 
[[image:PapyrusDSML-PluginWizard.png]]<br>
 
Running the constraint validation creation wizard
 
</center>
 
 
Please note that you should not embed constraints into the profile, if you plan to generate a plugin.
 
 
[[Category:Papyrus]]
 
[[Category:Papyrus]]

Latest revision as of 10:28, 26 November 2020

Constraint validation (2020-09)

Back to the top