What does 'do' do in an UML Statechart diagram?

1.3k Views Asked by At

I am not 100% sure what 'do' does in an UML statechart diagram.

Is my thinking correct:

If a transition happens, 'entry' will be executed first and then 'do' will be executed. And if we are leaving the state only 'exit' will be executed.

Does this logic apply to reflexive transitions as well?

And what if a trigger happens, that does not set off a transition? Does this only execute 'do'?

2

There are 2 best solutions below

2
bruno On BEST ANSWER

The UML Specification tells us:

14.2.3.4.3 State entry, exit, and doActivity Behaviors

. . .

A State may also have an associated doActivity Behavior. This Behavior commences execution when the State is entered (but only after the State entry Behavior has completed) and executes concurrently with any other Behaviors that may be associated with the State, until:

  • it completes (in which case a completion event is generated) or
  • the State is exited, in which case execution of the doActivity Behavior is aborted.

The execution of a doActivity Behavior of a State is not affected by the firing of an internal transition of that State.

You :

Does this logic apply to reflexive transitions as well?

As said by the last sentence above that depends if the reflexive transition is internal or external.

0
Ister On

To add to bruno's answer something more specific.

As stated in the UML specification (and quoted by bruno in his answer) the do activity is performed while the system is in a certain state (after entry activities were completed). It is performed during the whole time the system is in the state. The do activities cease to be performed only in two situations:

  • The transition taking you out of the State is triggered; in such case, the do activities are interrupted, exit activities if any, are performed and then the transition occurs. The transition can be reflexive (i.e. once completed the system returns to the same state), in which case first entry activities will be carried out and once they are completed, the do activities will start again.
  • The do activities finish on its own; in such case, once the do activities finish it's time to leave the state. So the eventual exit activities are performed and the transition is performed. A valid system should have one or more transitions without a triggering event (if more than one then they should have conditions allowing to uniquely determine which one will be used) that would be followed once the exit activities (or do if there was no exit) completes.

Let's have a more tangible example. Consider a washing machine. There are two interesting states in it, one is filling with water, the other one is drum rolling.

For the filling with water state, one can expect the following activities:

  • entry: open water valve
  • do: monitor water level
  • exit: close water valve

Now, once the washing machine gets into the state, the valve is being open (entry) and immediately afterwards the machine starts the do: to monitor the level of water (as water now flows in freely). Once the set water level for the current program and step is reached, the event "water level reached" is sent, initiating next transition. As a result the do activity is stopped (there is no more point in monitoring the water level) and the exit is carried out (valve is being closed), followed by the transition to the next state (e.g. heating or drum rolling depending on the program stage).

As you can clearly see, in such case the do activity is interrupted once the transition trigger is captured.

For the drum rolling the situation is quite different though. For the sake of simplicity we will have just one *do( activity, with no entry or exit activities:

  • do: rotate the drum for n seconds with n speed

When the washing machine enters this state, it immediately starts rolling the drum and (unless interrupted by some exceptional event) keeps doing it for the predefined amount of time. Once that time elapses, the do activity interrupts immediately and the transition to the next state happens. In such case, it is the do activity that controls when the next state transition occurs, at least in the typical scenario.