You can define enumerations in a Modlr model so that attributes, generated code, OCL expressions, and ViewModel validation use the same named set of values.
An enumeration is a model type with a fixed set of named values. For example, an ImportColumn enumeration can contain a value named JournalName. Once the model defines that type, you can use #JournalName in OCL rather than treating the value as an unrestricted string.
Define an enumeration
Define the enumeration in the model before assigning it as the type of an attribute.
- In the model content window, right-click any model element.
- Choose the command to edit enumerations. This opens the enumeration editing window.
- Add the enumeration name.
- Add each enumeration value.
- Enter code comments for the enumeration values when you need those comments in the model definition.
- Optionally assign an integer value to an enumeration value.
- Select the CodeGen checkbox when the enumeration must be added to generated code.
- Save the model.
After you define the type, select that enumeration as the type of the relevant attribute. For example, an attribute that represents an import column can use ImportColumn rather than String.
What the model retains
The enumeration is a class in the model type system. This means the model knows the allowed values, not only the name of a type that must later be implemented elsewhere in code.
That information is available when you write constraints and when you create ViewModel validations. The CodeGen setting controls whether code generation adds the enumeration to generated code; defining it in the model also makes its values available to model expressions.
| Model element | Purpose |
|---|---|
| Enumeration name | Defines the named type, such as ImportColumn.
|
| Enumeration value | Defines an allowed value, such as JournalName.
|
| Optional integer value | Stores an integer value for an enumeration value when required. |
| Code comment | Documents the enumeration value in the model definition. |
| CodeGen | Includes the enumeration in generated code. |
Use enumeration values in OCL
Use the # prefix when referring to a named enumeration value in OCL. In the following examples, ImportColumn is the enumeration type and JournalName is one of its values.
List values
Return all enumeration values:
ImportColumn.allInstances
This returns a collection of ImportColumn tuples. To create a collection of strings, for example for a list on a screen, use:
ImportColumn.allInstances.asString
Select a value
Select JournalName from the list and return its string representation:
ImportColumn.allInstances->select(ic|ic = #JournalName).asString
To get the value of one enumeration tuple, use toString:
(#JournalName).toString
Do not use asString on an individual enumeration tuple. Use it on the collection when you need a list of strings, or use toString for an individual value.
Test whether an enumeration collection contains a value
Do not use includes on a collection of enumeration values. Select the target value and test whether the selection has a result.
This works:
SysSingleton.SO.ActiveUser.UserModes->select(x|x = #Consultant)->notEmpty
This does not work:
SysSingleton.SO.ActiveUser.UserModes->includes(#Consultant)
Get the ordinal index
Use Indexof on all instances of the enumeration to get the ordinal index of an attribute value:
MyEnum.allinstances.Indexof(someObject.SomeEnumProp)
Example
Suppose an import process must identify a journal-name column.
- Define an enumeration named
ImportColumn. - Add
JournalNameas an enumeration value. - Set the type of the import-column attribute to
ImportColumn. - In an OCL expression, compare the attribute with
#JournalName.
This makes the allowed values visible in the model and lets expressions use the named value directly.
Model file considerations
A Modlr file is the zipped MDriven Designer model format and is used for uploading and downloading models with MDriven Server. For a multi-developer team, use the Ecomdl split-file format with source control because a zipped Modlr file does not diff well in Git or SVN.
