You can convert a state-machine state value to text in an OCL or OCL-PS expression when you need the state name as a string, for example for display or logging.
Convert a state value to a string
Use asString on a state literal:
(#state-name).asStringFor example, this expression returns the text New:
(#New).asString#New is a state value. Calling asString converts that value to its string representation for the result of the expression; it does not change the state machine or the object's state.
Use a state name in an expression
Write the state name after #, then call asString:
- Identify the state literal you need, such as
#New. - Add
.asString. - Use the resulting string where the expression requires text.
Example:
(#New).asStringResult:
New
Do not write 'New' when you need a state value. 'New' is already a string, while #New identifies the state value and (#New).asString converts it to text.
Choose the right state-machine operation
| Need | Use | Example |
|---|---|---|
| Get the text representation of a known state value | asString
|
(#New).asString
|
| Test whether an object is currently in a state | oclIsInState | a.oclIsInState(#Manufacturing)
|
| Get the names of states defined for a class | allStates | Returns a collection of state-name strings |
| Record state transitions | OnStateChange | Use the oldstate and newstate string parameters
|
Important
Converting a state value to a string is read-only. It does not move an object to another state and does not bypass state-machine rules. If you are repairing incorrect persisted state data in a debugger-like scenario, see stateMachineForceMode. That operator is intended only for exceptional correction scenarios.
