You use ->notEmpty() in OCL to test whether a collection contains one or more elements.
Syntax
collection->notEmpty()The operation returns a Boolean value:
| Collection state | Result |
|---|---|
| Contains at least one element | true
|
| Contains no elements | false
|
Use notEmpty on a collection
Call ->notEmpty() on an expression that returns a collection. For example, if a Car has a collection of related objects named Wheels:
self.Wheels->notEmpty()The expression returns true when the car has one or more wheels, and false when Wheels is empty.
Use the result in a conditional expression
You can use ->notEmpty() to include a value only when a collection has content. The following expression returns 'Materials' when ValidMaterials contains at least one item; otherwise it returns an empty string value:
self.ValidMaterials->notEmpty.caseTrueFalse('Materials', String.nullValue)This pattern can be used when building a dynamic collection of labels. See Documentation:Collection of strings for a complete example that creates a comma-separated list.
Important: do not use it on a value type
Do not call ->notEmpty() on a value type such as a string, number, or Boolean. MDriven converts the value to a collection containing that value. That collection has one item, so ->notEmpty() evaluates to true.
For example, this does not test whether Name has text:
self.Name->notEmpty()Because Name is a value type, it is treated as a one-item collection and the expression evaluates to true. Use an operation appropriate for the value type instead. For string length, see Documentation:OCLOperators length.
Opposite operation
Use isEmpty when you need to test that a collection contains no elements.
