Skip to content

Composite States

Normally, a statechart does not only consist of two states. The specification of even simple systems require tens and more complex systems hundreds of states and transitions. To cope with large number of states the base concept of composite states can be used.

Let’s enhance our light switch to explain this in more detail. The light switch now provides an additional motion sensing mode which automatically switches the light on and off depending on the input of a motion sensor. Initially, the light is off, but whenever the motion sensor senses a motion, it turns the light on. After 30 seconds without any motion the light is turned off again. As manually switching and dimming the light should still be supported the existing statechart must be extended.

First, an additional event toggleMode is defined. This is used to toggle between the already existing manual and the automated motion sensing mode. To model these modes two additional states are introduced.

  1. The composite state Manual contains the existing behavior. These are the On and Off states of previous statechart. Here only the dimming transitions were replaced by local state reactions.
  2. The composite state MotionSensing is completely new and contains the states MotionSensing.On and MotionSensing.Off. These handle the motionDetected event. The required timeout is specified using the time trigger after 30s. If the state does not change for 30 seconds then a time event is raised which will cause to switch the light off.

A composite state is a state that contains one or more other states (sub states). One of the advantages is the possibility to group states into logical compounds and thus make the statechart more comprehensible. Another advantage is the option to exit a composite state by transitions that have the composite state as their source states, not any substate. If such a transition is executed, the composite state is left regardless of which of its sub states are active at the moment. You could achieve the same without composite states, but it would be cumbersome since you would need a transition from each of the plain states in the group to the “outside”.

The transitions which toggles between MotionSensing and Manual state are defined for the composite states. This implies that the transitions will be taken independent of the concrete active sub state. So all sub states share the behavior of their parent states.

If you want to learn more about composite states, you can check out our Hierarchical tatecharts example or the chapter Composite states in our documentation.

The statechart interface is defined as:

	in event switch
	in event changeBrightness
	in event toggleMode
	in event motionDetected
	var brightness: integer