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ât 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.
Creating an EA Script
To add scripting rules to Model Expert, you will need knowledge of the EA Scripting languages, 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.
Note: all code examples are written in JavaScript – Model Expert only supports JavaScript.
For details on creating an EA Script, see the EA help here.
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 guide of the diagram. - They must output an XML string of the form:
<results>
<violation note=’Serious problem’ severity=’7′ />
<violation note=’Not so serious problem’ severity=’3′</results>
</results>
1. 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. In VBScript, this is a single inverted comma: â
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 | dim e as EA.Element
set e = Repository.GetElementByID(ElementID.toString) |
We usually declare the output string at this point, to add violations to it as we progress through the script | dim outputHTML â a string which will contain the results from the script
outputHTML = â<results>â |
Now we can start to do some validation. This checks how many scenarios the use case has | âlook in more detail at the insides of the Use Case
if e.Scenarios.count = 0 then outputHTML = outputHTML & â<violation note=âMissing scenarios for use caseâ severity=â4â² />â end if |
and some more, to make sure we are not using Internal Requirements, as this is against our local standards | if e.Requirements.count > 0 then
outputHTML = outputHTML & â<violation note=âUse Case has Internal Requirementsâ severity=â3â² />â end if |
Output
Each violation instance that your script returns can have the following attributes:
Field | ||
Note | Required | |
Severity | Required | |
Note: EA Scripts seem to need to have the name of the script as the last item in the script e.g.
â¦
â¦
end function
Use_Case
We donât know why this has to be thereâ¦.
Notes for writing Scripted Validation
We suggest keeping the validation scripts simple. Remember, they will be run once for each instance of the meta-class. If this is 100s, then a script which does lots of look-ups into the EA model each time will be very slow.