You can define named enumeration values in your model and use them safely in attributes, OCL expressions, constraints, and ViewModel validations.
What is an enumeration?
An enumeration is a model-defined type with a fixed set of named values. MDriven represents the defined enumeration as a class in the model type system. Each named value is an enumeration tuple, not a string.
For example, an enumeration named ImportColumn can contain values such as JournalName and other import-column names. In OCL, refer to a value with # followed by its name:
#JournalNameUse enumeration values when an attribute must be one of a known set of choices. This keeps the enumeration definition in the model, so its values can be used in constraints and ViewModel validations.
Define and edit an enumeration
In MDriven Designer, you can open enumeration editing from the Model content window.
- In Model content, right-click a model element.
- Choose the command to add model elements and edit enumerations.
- In the enumeration dialog, declare the enumeration name.
- Add the named values that the enumeration must contain.
- Optionally add code comments and an integer value for each enumeration value.
- Select the CodeGen checkbox when the enumeration must be added to generated code.
For example, define ImportColumn and add JournalName. You can then compare an ImportColumn value with #JournalName in OCL.
Work with enumeration values in OCL
Get all values
Use allInstances to get the collection of values defined for an enumeration.
ImportColumn.allInstancesThis expression returns a collection of ImportColumn tuples.
Create display strings for a list
Use asString on the collection to produce a list of strings, for example when populating a list on screen.
ImportColumn.allInstances.asStringThis converts each value in the ImportColumn collection to its string representation.
Find one value and return its string
Filter the enumeration collection when you need the string representation of a particular value.
ImportColumn.allInstances->select(ic|ic = #JournalName).asStringThe expression selects the ImportColumn value equal to #JournalName, then converts the resulting collection to strings.
Get the string for one enumeration tuple
Use toString when you have one enumeration tuple.
(#JournalName).toStringDo not use asString directly on an enumeration tuple. asString is used on the collection of enumeration values; toString is used for one value.
| Situation | Expression | Result |
|---|---|---|
| All values as enumeration tuples | ImportColumn.allInstances
|
A collection of ImportColumn values
|
| All values as display strings | ImportColumn.allInstances.asString
|
A collection of strings |
| One value as a string | (#JournalName).toString
|
The string representation of #JournalName
|
Test whether an enumeration collection has a value
Do not use includes on a collection of enumeration values. Instead, select matching values and test whether the result is non-empty.
For example, test whether the active user's UserModes collection contains #Consultant:
SysSingleton.SO.ActiveUser.UserModes->select(x|x = #Consultant)->notEmptyThe expression returns true when at least one selected value equals #Consultant.
The following expression does not work for a collection of enumeration values:
SysSingleton.SO.ActiveUser.UserModes->includes(#Consultant)Get an enumeration value's ordinal position
Use Indexof on allInstances to get the ordinal position of an enumeration value.
MyEnum.allinstances.Indexof(someObject.SomeEnumProp)In this example, someObject.SomeEnumProp holds a MyEnum value. The expression returns that value's position in MyEnum.allinstances.
