Action

An action is the way a statechart can cause things to happen in the outside world, and indeed they are the main reason why the statecharts exist at all. The main point of introducing a statechart is for the actions to be invoked at the right times, depending on the sequence of events and the state of the guards.

Actions are most commonly triggered on entry or exit of a state, although it is possible to place them on a transition itself.

Actions should in general try to execute as quickly as possible, returning control to the statechart as quickly as possible, so that other actions may be executed. This means that anything beyond setting a few variables, they should typically be used to start and stop asynchronous tasks. Such asynchronous tasks that run while in a state are called activities.

Examples

An example of an action might be to enable a text field; such an action is of course synchronous, but it is also practically instantaneous.

Another example of an action would be to start and stop a HTTP request. The actual HTTP request would of course be an asynchronous process, and might result in guards functions responding differently, and trigger events that the statechart might be interested in.

Nested states

When a parent state and a substate are entered, then entry handlers of the parent state are executed before entry handlers of the substate. Conversely, when a parent state and substate both exit, the child exit handlers are executed before the parent state’s exit handlers.

Notation

entry and exit actions in a state are prefixed with entry / or exit / as follows:

Diagram depicting entry and exit handlers