# Statechart Notation

## Introduction

A statechart is simply a network of states and events, extending the concept of Finite State Machines (FSM).

State charts contain these elements

• states
• transition on events
• transitions with conditions
• entry point
• exit points
• hierarchy of statecharts, a state may have its own internal statechart.

## Transitions

If event E occurs in state S and condition C holds then make the transition to state T.

Actions to be carried out when event E occurs into state T are normally put into an event-action table rather than part of the diagram.  The actions could be lengthy and detailed.

## Hierarchical statecharts

FSM are flat.  They provide no facility to represent hierarchies.

Details sometimes can be hidden to portray higher level abstraction.

Because the arrows go inside node S, events A and B indicate that there is more detail in state S.

State S is hierarchical.  It has its own set of states, local to it.

Combinations of situations reflected in this statechart:

• When in state S it is also in either state P or Q.
• When in state S, move from state P to Q on event C
• When in state T move to state S and within S to P on event B.
• Regardless of states P or Q, on event E, move to T
• Move to T from Q and S on event A

## Clustering

Clustering is another form of hierarchy.

Since there's a transistion  from both P and Q on event A, we can represent that as a single arc instead of 2

The advantage is the reduction in the number of arrows.

Likely states P and Q are abstractly different from T.

## Three modes of a digital watch

The arcs represent the pressing of the 4 buttons found on the 4 corners of the watch.  Note the multi-use of some of the buttions, especially "a".

Can you describe what each button generally does? a? b? c? d?

Note there are 3 main states (modes) called "NormalDisplay", "ChimeAlarmSet" and "Update"

Within each state there are substates with transitions in each

## Start and Stop States

Start states are the simple large dots.  Sometimes omitted.

Stop states are the encircled dots.

A nested statechart should have a start point indicated and a stop state if it's not obvious.

Notice in this example we start at state S but within S we start at P.

We need to know which state is active at each level.

## History mechanism

When entering a state that has a nested statechart, you may want to resume where you left off when you left the enclosing state.  The encircled H as the entry point as below means to start in the state within that was exited.

The history is not hard to implement as you can just ensure the state variable is not reset from the last time in that parent state.  Below there would be a state variable tracking where in S and we just keep it around.

Each level can have its own history mechanism.  Each history variable would be initialized to the start state of the level.

The encircled H can have an asterisk attached to indicate that the history mechanism is to be used at all levels of the hierarchy.

## Concurrency

Concurrency refers to the ability to manage more than one state simultaneously.

If a simple FSM is used, the number of states needed is the product of the number of states that can be represented in each group separately.

Example: Consider the 3 style types -- bold, italics and underline.  These styles can be used in any combination. Bold is on or off, italics is on or off, underline is on or off.  The total number of states is 2*2*2 = 8 and the number of transitions is nearly as bad.

Instead, keep each style type as a separate statechart (bold on or off), (italics on or off) and (underline on or off). Each statechart is a simple pair of states.

Example: Consider the 4 paragraph positions -- left, center, right and justified.  These types are mutually exclusive but the number of transition arcs is 10 (=4*5/2).

Example: Consider the 3 paragraph types -- normal, bulleted and numbered.  Again these three are mutually exclusive, but can be combined with the 4 positions.  We need 3 states and 6 arcs.  If we tried to combine paragraph positions and types, then we have 12 states in a FSM.  Further, combining with the 8 states of font types, then 96 states would be needed.

There are some clarifications of state entry, history and exit in this form of the hierarchy.

## Delays and Timeouts

Delays are indicated by including a notation of delayTime < in the state. E.g., 10 sec <

Delays cause the state to hold against any action or event to occur until the delaytime has expired.

Timeouts are indicate by a similar notation < timeoutAmt in the state, e.g, < 5 min and then have an extra event arc labeled "timeout" exiting the state. That is, time out is an event.

In the case of events (and conditions) being ambiguous (i.e., more than one transition could be taken), the arc may have a priority encoded in brackets, e.g. [value]