Skip to content

Variables

While the classic theory of input/output automata is only aware of events, Harel statecharts and as such CREATE Statecharts have many more features, including variables. Variables allow to use quantitative values in statecharts and are very comparable to variables in programming languages.

To explain the use of variables we extend the light switch by adding dimming functionality. The light switch supports ten different dimming levels. It still defines the states Off and On with mutual transitions which are triggered by switch events.

To handle the dimming functionality the light switch statechart defines two additional elements:

  • The variable brightness holds the dimming level of the light. Changing this variable’s value corresponds to changing the lamp’s brightness.
  • The additional event changeBrightness increases changes the dimming level incrementally.

To handle the brightness level correctly the statechart must maintain its value depending on the active state and the occurring events. As such it defines the following rules:

  • Whenever Off is active brightness must be 0. This is enforced by the Off state’s entry action which is defined as entry / brightness = 0. This action assigns 0 and is executed whenever state Off is entered.
  • When the light switches from Off to On then the brightness must be set to the maximum value of 10.
  • If On is active and changeBrightness is requested and brightness is larger 1 then the brightness value is decremented. This rule is specified by an additional transition which reacts on the changeBrightness event and uses a guard condition which must become true for taking the transition. The proper variable assignment is specified as a transition action. The target of the transition is the state itself as the switch should remain in the On state. Such transitions are called self transition.
  • If On is active and changeBrightness is requested and brightness equals 1 then the value is reset to 10. Here again an additional transition with guard and action is defined to cover the desired condition and effect.

This example shows how variables can be used in conjunction with more complex conditions. Variables can be assigned and can also be used in conditions and calculation expressions. Actions can be specified as part of a transition as well as part of states.

The statechart interface is defined as:

interface:
	in event switch
	in event changeBrightness
	var brightness: integer


brightness: