In an earlier post, I identified ISO20022 as a model-driven success story. Here, I give some other examples of model-driven from e-Government and telecommunications. They are not all at the same level of maturity, but the reason why a model-driven approach makes sense is the same.
How to Add OCL Rules in MagicDraw
June 7th, 2012
There are lots of good reasons for adding rules to models, and lots of good reasons for writing the rules in OCL (see e.g. Jordi Cabot’s post on OCL and the Nomos post on Getting Benefit from OCL Rules with Nomos).
But what are the mechanics of adding rules into a modelling environment. Here’s how it’s done in MagicDraw. You’ll find similar mechanisms in other modelling environments.
This example uses MagicDraw Enterprise edition 17.0.1.
Let’s assume you have created the very simple model shown in the OCL Examples post in MagicDraw.
Add a Constraint Rule
To add an OCL rule to the Payment class, select the class in Magicdraw (this is the ‘context’). Right click and select ‘New->Element->Constraint’.
A new constraint rule appears in the tree under the selected class.
Type in a Name
Type in a name for the rule on the tree e.g. FrenchCurrency.
Write the rule in Natural Language
Always write the rule in english (or another spoken language) first. We always define constraints as Validation Rules, and use the ‘error message’ field to add a natural language description. The error message field is only available if you stereotype the constraint as a validationRule. But you could create a Comment under the constraint and add the description there. In fact, sometimes we add both an error message and a higher level business description in a Comment.
To make the constraint a validation rule, select the new constraint in the tree, right click, select Stereotype, and tick ‘Validation Rule’. You can type the first letter of the stereotype, v, to quickly find the right stereotype – saves scrolling through a long list of stereotypes.
If you are using the Cameo OCL Transformer, stereotype the constraint as OCLTransformerValidationRule.
Now double click the constraint in the tree. The constraint dialog opens.
To add the description, click on the ‘Error Message’ field. Type directly into the entry box, or click on the ‘…’ button and type the message into a dialog.
If the country is France, the currency must be euro
If you are using the Cameo OCL Transformer, you will also need to provide a name in ‘ruleset’ field.
Add the OCL Expression
OCL gets added to the Specification field in the Constraint dialog. When you click on the Specification entry field, a ‘…’ button appears. Click to open the Specification dialog.
For Language, select ‘OCL2.0’.
Now type the OCL into the dialog:
Country = ‘france’ implies Currency = ‘EUR’
That’s it. An OCL rule has been created.
To create a second rule
Creating a second rule is much easier. Just copy and paste the first rule. Then edit the rule name, the Error Message and the OCL. This saves on a few of the steps.
(Note: you can also manage your rules in a separate package in the model, rather than adding the rules to their ‘context’. Just add constraint rules to a package, and select the context in the Constraint dialog.)
Watch out for our post on how to view OCL in the MagicDraw generic table. Makes it easy to view related rules together.
If you want to turn OCL rules captured in MagicDraw into executable rulesets and use the rulesets in development and test environments, contact us about our Cameo OCL Transformer product.