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 VBScript, but you may use any scripting language which is supported by EA.
For details on creating an EA Script, see the EA help here.
Model Expert Validation scripts must be created in the ‘Normal’ group.
Writing a Model Expert Validation Script
Validation scripts have just three requirements:
- They must declare what kind of element type and stereotype they should be used for
- Some code which does the checking.
Your script will be passed a reference to the current Repository, and the ElementID of the instance of the EA element which your code will examine. - They must output an XML string of the form:
<results>
<violation note=(sometext) severity=(a positive integer) />
…
</results>
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. |
‘<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
Finally, complete the XML which is output, and assign it to the name of the script. This is how values are returned from EA Scripts. Our script was called “Use_Case”:
outputHTML = outputHTML & “</results>”
‘put the HTML string into the return
Use_Case = outputHTML
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.