State machine types and instances

A statechart does not only contain the definition of behavior. It also includes structural properties like its declared variables and events. These define its public interface and this interface is represented by a type.

Lets take a look at the example statecharts to get an idea how the state machine types look like:

State machine types derived from statechart definitions.

The statechart TrafficLight.sct defines the type TrafficLight and the statechart TwoWayTrafficControl.sct defines TwoWayTrafficControl. The types include all members defined in the statecharts declaration section. The variables which are of type TrafficLight are references to instances of this type.

You should know the following facts about state machine types:

  1. Each statechart has its own type. This type has the same name as the statechart.
  2. The state machine type is a class type. All members defined in the statechart’s default interface are direct members of this type.
  3. These members can be variables, events, and operations.
  4. The type members have the identical name as defined in the statechart.
  5. State machine types used in variables, events and operations are always reference types.
  6. Named interfaces are owned by the state machine and are represented as a member with the interface name as name.
  7. These named interface members are complex types which include all members defined in the interfaces.
  8. All declarations of the internal scope are not exposed through the state machine type.
  9. The state machine type inherits from the abstract type IStateMachine. This type defines additional life cycle operations which are implemented by each state machine.
  10. Each statechart owns an additional enumeration type named <StatechartName>States. It defines an enumeration value for each state of the statechart.

If you know the state machine code which is generated from statecharts then you will discover that the definitions of the state machine types directly match the definitions of the generated code.

State machine types are always used as reference types. This implies that the state machine which defines a variable of a state machine type does not own the state machine instance. It will never be created by the state machine itself.

As a state machine type is a regular type you can use it like any other type. So you can assign state machine references to variables, use it as event payload, pass it as operation parameter, or use it as an operation’s return value.

Variables of state machine references can be reassigned. A valid value is null. null is the default value after initializing a state machine.