You can create a collection of text values in an OCL or EAL expression when you need a list to loop over, provide as a UI value source, or format as one display string.
A collection of strings is a collection whose elements are values of the String data type. It is not the same as one String: a String is its own data type, even though it consists of characters. For example, Sequence{'Red', 'Green', 'Blue'} is a collection containing three String values.
Choose a collection type
Use a collection literal with curly braces: CollectionType{value1, value2}.
| Collection type | Use it when | Example |
|---|---|---|
Sequence
|
You need an ordered list. | Sequence{'Low', 'Medium', 'High'}
|
Set
|
Each value must be unique. | Set{'Email', 'Phone'}
|
Bag
|
Duplicate values are allowed. | Bag{'Email', 'Email', 'Phone'}
|
For an ordered list of fixed choices, use Sequence. For example, a string-backed combobox can use a Sequence as its value source; see Create Comboboxes with Strings.
Create a fixed collection
Create an ordered collection by placing quoted String literals inside Sequence{}:
Sequence{'Materials', 'Colors', 'Finish'}You can process every value with collect. This example applies an expression to each string:
Sequence{'String', 'EmailAddress'}->collect(s| DoSomthingWithAString(s))Build values from expressions
Each item in a collection literal can be an expression rather than a fixed literal. This lets you build candidate labels from the state of the current object.
Sequence{
self.ValidMaterials->notEmpty.caseTrueFalse('Materials', String.nullValue),
self.ValidColors->notEmpty.caseTrueFalse('Colors', String.nullValue),
self.ValidFinish->notEmpty.caseTrueFalse('Finish', String.nullValue)
}In this example, each caseTrueFalse expression returns its label when the corresponding collection is not empty, otherwise it returns String.nullValue. The Sequence contains the three expression results. If you intend to turn these results into display text, verify how String.nullValue is handled in the expression context before relying on it to suppress a label.
Format a collection for display
Use asCommaList to convert a collection of strings into one comma-separated String:
Sequence{
self.ValidMaterials->notEmpty.caseTrueFalse('Materials', String.nullValue),
self.ValidColors->notEmpty.caseTrueFalse('Colors', String.nullValue),
self.ValidFinish->notEmpty.caseTrueFalse('Finish', String.nullValue)
}->asCommaListUse asSeparatedList when you need another separator. For example, the following produces one String with underscore-separated values:
Set{'Materials', 'Colors', 'Finish'}->asSeparatedList('_')Work with existing text
Do not treat a String as a collection of string values. To split comma-separated text into individual strings, use split and trim each result:
<string>.split(',')->collect(s|s.trim)To work with the individual characters in a String, use ToCharArray.
