Home / Model Expert Homepage / Model Expert Help / Validation rules / Writing scripted rules
Import an MDG to create a Reference Model
Importing and exporting Reference Models
Editing RM Connector type properties
Stereotypes inheriting from other Element Types
Customizing Reference Model Properties
Customizing Reference Model Element Properties
Writing scripted rules
Model Expert provides lots of validation rules, but it can not implement every kind of rule you might ever need.
What validations are possible?
You can create scripts which can run for either
- all meta-model types (not recommended),
- all instances of a specific element type, or
- instances of a specific meta-model type and stereotype.
- or for whole diagrams: see Validating Diagrams
These can check, for example:
- for specific values in a variable,
- whether users have obeyed a naming standard,
- whether some kinds of connector exist.
- any combination of the above.
- how many elements in a diagram
- whether the notes for a diagram are suitable
- â¦and anything else you can write code for.
Notes for writing Model Expert validation scripts
We suggest keeping the validation scripts simple. It’s quite possible to accidentally replicate lots of the built-in functions of Model Expert using scripts, so be sure to check that what you are coding can’t be done using a Reference Model.
Also, writing scripts is quite difficult: it has taken us quite a lot of time to write and debug our example scripts, so think about this before you start writing lots of scripts.
Creating an EA Script
To add scripting rules to Model Expert, you will need knowledge of the JavaScript – the only scripting language which we support- as well as some knowledge of the EA programming model. We have provided some example scripts, so you may choose to copy and modify these.
For details on creating an EA Script, see the EA help here.
Also see Testing your scripts.
Writing a Model Expert Validation Script
Validation scripts have just four requirements:
- They must declare themselves to be a Model Expert validation script, by having a special comment at the start of the script. This lets Model Expert quickly find all the Model Expert validation scripts.
- They must declare what kind of element type and stereotype they should be used for, or whether they are for validating a diagram.
- Some code which does the checking.
Your script will be passed a reference to the current Repository, and the ElementGuid of the instance of the EA element which your code will examine, or the guid of the diagram. - They must output an XML string of the form:
<ElementViolation name='Serious problem' severity='7' /> <ElementViolation name='Not so serious problem' severity='3'Â subject='This really needs fixing'Â Â violatorID='12345'
Script type declaration
At the start of the script, you must have a comment like:
//<Validation>Element</Validation>
– this says the script is for validating individual elements
or
//<Validation>Diagram</Validation>
– this script is for validating a whole diagram, optionally with all its contents
Type / Stereotype Declaration
This must be in the form of a code comment.Â
Example | Explanation |
---|---|
//<Type>All</Type>
|
This script will be run for All examples of all meta-types. Use this for checks which are needed for everything in your model, for example, you might decide that all elements must have a name, or some notes or alias. Please use this option with care – it will make all validations much slower. |
//<Type>UseCase</Type>
|
This script will run for all examples of this meta-type, regardless of their stereotype |
//<Type>UseCase</Type> //<Stereotype>Special</Stereotype> |
This script will only run for <<Special>>Use Cases |
Validation Code
The code to do the validation can be quite simple, because the script has been passed the EA Object_ID of the element being validated, as well as a reference to the current EA Repository.
Steps | Example Code |
---|---|
So the first task for a script is usually to get the EA.Element which corresponds to the Object_ID passed |
 let e = Repository.GetElementByGuid(Guid.toString()); |
We usually declare the output string at this point, to add violations to it as we progress through the script |
let outputHTML = ""; |
Now we can start to do some validation. This checks if the use case only has a ‘Basic Path’ scenario. | âlook in more detail at the insides of the Use Case
if (e.Scenarios.Count === 1) { let aScenario = e.Scenarios.GetAt(0); if (aScenario.Type === "Basic Path") { outputHTML += "<ElementViolation name='Only basic path scenarios for use case' severity='3' violatorID = '" + e.ElementID + "' subject='" + e.Name + "'/>"; } } |
and some more, to make sure we are not using Internal Requirements, as this is against our local standards |
 if (e.Requirements.Count > 0) { outputHTML += "<ElementViolation name='Use Case has Internal Requirements' severity='3' violatorID = '" + e.ElementID + "' subject='" + e.Name + "'/>"; } |
Notes
You will notice that there are lots of formatting marks in this output – lots of (‘) and (“) and <>Â characters. This is just what the script needs to have in order to work.
Output
Each violation instance that your script returns can have the following attributes:
Field | Required or optional | Explanation |
name | Required | This will become the title of the Violation, and will appear in bold on the annotated diagram |
severity | Required | Tells the user how bad the violation is. Higher = more serious |
violatorID | Optional | The ID of the element, diagram or package which created the violation |
subject | Optional | Usually the name of the element, diagram or package which caused the violation, or any other details which will help the user to fix the problem. |