You can use the clear operator in an OCL action to empty a many-role association or mutable ViewModel list when you no longer want that list to contain its current elements.
Syntax
collection->clearcollection must be a collection that the action can modify. After clear runs, the collection is empty.
Clear a many-role association
A many-role association is an association end that can hold more than one object. Use clear when the current object should no longer have any objects in that role.
For example, in an action evaluated on a Customer ViewModel:
self.Orders->clearThis removes every Order from the current customer's Orders collection. The collection is empty after the action.
Clear a mutable ViewModel list
A mutable ViewModel list is a list in a ViewModel that an action can change. Apply clear directly to the list when you need to remove its current entries.
For example:
self.SelectedOrders->clearAfter this expression runs, SelectedOrders contains no selected orders.
Clear is not delete
clear empties the collection it is applied to. If you need to perform an action for every member, such as deleting each object, iterate with foreach instead.
For example, this expression invokes delete for each order:
self.Orders->foreach(o | o.delete)Use clear when the required result is an empty association or list. Use foreach when each member needs its own operation.
Check the result
You can test whether a collection is empty with isEmpty. For example:
self.Orders->clear
self.Orders->isEmptyAfter the clear expression, isEmpty evaluates to true.
