oclGetStates lets you retrieve the names of the state-machine states that an object is currently in; use it when your OCL expression needs to inspect an object's current state or states.
Syntax
object.oclGetStatesReturn value
oclGetStates returns a collection of strings. Each string is the name of a state that the object currently occupies.
An object can return more than one state name because MDriven supports parallel state machines. A parallel state machine has regions that run at the same time, and an object can occupy one state in each region.
| State-machine configuration | Result from oclGetStates
|
|---|---|
| One active state-machine region | A collection containing the name of the current state. |
| Multiple parallel regions | A collection containing the current state name from each active region. |
Example
The following expression gets the current state names for the first existing Complaint object:
Complaint.allInstances->first.oclGetStatesIf that complaint is currently in a state named Registered, the returned collection contains Registered.
If its state machine has two parallel regions, and the complaint is in Registered in one region and AwaitingReply in another, the returned collection contains both state names: Registered and AwaitingReply.
The example uses allInstances to retrieve existing Complaint instances and first to select one object. Ensure that the collection contains an object before relying on first.
Use the returned state names
Because the result is a collection of strings, use collection operations to test whether a particular state name is present. For example, to test whether the first complaint has a current state named Registered:
Complaint.allInstances->first.oclGetStates->includes('Registered')This expression returns a Boolean value. Use it in a condition when your logic depends on whether the object is currently in that state.
Parallel-state-machine consideration
Do not assume that oclGetStates returns only one value. When a state machine has parallel regions, selecting one state name does not describe the complete state of the object. Test for the state name relevant to the condition, as in the includes example, and account for other active states where your model requires it.
