TN0-AboutStateMachines
TN0-AboutStateMachines
Wagner
1. A state machine
Automata Theory distinguishes between combinatorial and sequential circuits. If inputs
determine outputs, we have a combinatorial circuit, otherwise a sequential circuit. A state
machine is a specific implementation of a sequential circuit. A state machine is known under
several names: state machine, finite state machine, automata, push down automata (Push
down state machines), Turing machine, deterministic state machine, statechart (Harel
Automata). Some of them are just the same. For instance, it is difficult to explain a difference
between a state machine and a finite state machine as the word “finite” is not
unambiguously defined. To justify the name finite state machine, shouldn’t we have also
infinite state machine? The word automaton (automata) is used as a synonym of a state
machine. In a lexical analysis a specific state machine called parser (also called recognizer or
acceptor) has been used.
In this paper a state machine is considered as a description of the system control behavior:
what to do in all imaginable situations. The base of a state machine is a state as a complete
information about the history of input changes. States represent all possible situations in
which a control system may ever be. A diagram in Figure 1 shows the dependencies involved
in a state machine functioning: both the State transition conditions, and the Action
conditions are functions of Inputs and a State.
The true sense of changing states is to perform some actions (except for parser where only
state changes count). The behavior of the control system is then described by a transition
1
table and/or a state transition diagram. The state transition diagram is a graphical
representation where we use two elements: circles for states and arcs for transitions. To get
the full information we use for each state a state transition table which contains: transitions
and their transition conditions, as well as all possible actions. Actions are performed on
inputs or state changes. Therefore, we distinguish:
- input actions performed if input changes
- entry actions on entering a state
- exit actions on leaving a state
- transition actions while state changes.
Effectively several actions are performed just in the same moment. When an input change
forces the state machine to change a state, all actions can be performed in the sequence:
In practice not all actions are used. For instance, we speak about:
Figure 2 shows a state transition diagram of a state machine that controls a pressure in a
vacuum chamber and Figure 3 shows a state transition table of the state Starting. In the
example three types of actions are used: input, entry and exit. We do not go into the details
(syntax) of the presentation as it depends on the tool used (the presented diagram and table
have been created using StateWORKS development system).
2
Several activities are initiated. Waiting for Pressure acknowledgements. Due to a Timer missing
acknowledgement leads to return to the Idle state. Too hot pump leads to the PumpError state.
Both erroneous situation generate corresponding alarms. Error by accessing the output function
returns the state machine also to the state Idle: it does not make sense to supervise the
pressure without having proper pressure limits (corresponding alarms are generated in Always
table).
RequiredPress_CHANGED Timer_ResetStart
Timer_OVER Al_PressureError
PumpError Pump_TooHot
Idle Timer_OVER |
Ofun_OwnerError |
Ofun_ParameterError
Regulating Press_OK
3
4. Why do state machines fail?
Theoretically state machines could be used in design and implementation of software control
systems. Everybody knows them as they are at least mentioned in some academic courses or
trainings but the idea that the entire software can be based on state machines is not
understood and considered as an unrealistic undertaking. State machines are used in
informal discussions or to solve some local control problems in a program. Several tools
based on a state machine have been developed (SDL, UML, ASML only to mentioned what
the author has tried) but they never found large acceptance. If applied, the tools are used in
the initial specification phase of the project and then forgotten.
The specification is never perfect. During implementation several changes are required, not
only cosmetic but essential ones. They are never done in the specification but directly in the
code. Over time the gap between specification and implementation is growing. Eventually we
can only say “in code we trust” as the initial specification has lost any credibility. Solving
control problems directly in the code is the major reason for software malfunctioning.
Understanding the software functioning by reading a code increases essentially the cost of
software maintenance and changes.
There are several issues that are responsible for the situation. In the following sections we
will discuss some of them which we regard as essential obstacle in broader application of
state machines in software practice. First, we show that the implementation model of a state
machine influences the specification.
Figure 4. Toggle 2
The motor will be switched on in the state _B_On and switched off in the state _B_Off. This
implementation will work if the execution system performs only one transition at a time.
Otherwise it will oscillate between states _B_On and _B_Off until the input signal changes to
LOW.
A second solution would be a state machine with 4 states shown on Figure 5. In that case the
motor will be switched on in the state OnBusy and switched off in the state OffBusy. In
addition, the states OnBusy and OffBusy “delay” the transition to the correspondingly stable
4
states On and Off until the input signal goes to LOW. In this solution the execution system
allows several transitions at a time.
Figure 5. Toggle 5
The example above shows that there are several specifications of control behavior. In other
words, each of this specification is correct but the programmer has to program it differently.
A similar toggle problem exists if the trigger signal is a multivalued one. For instance for a
simple temperature regulation problem (air conditioning or heating) the input signal has 3
values: Temperature_HIGH, Temperature_OK and Temperature_LOW. To specify the control
problem for air conditioning, we use two signals Temperature_HIGH and Temperature_LOW
as shown in Figure 6. The implementation does not restrict the functioning of the execution
system: one transition or several transition at a time will do. If the air conditioning is off the
Temperature_HIGH signal changes the state to On (air conditioning will be switched on). If
the air conditioning is is on the Temperature_LOW signal changes the state to Off (air
conditioning will be switched off). The coded implementation is in that case extremely
simple. Due to its sequence of changes: Temperature_HIGH - Temperature_OK -
Temperature_LOW … the input signal performs directly the control.
5
are Boolean. In contrary the state machines specified and implemented in the software
require logical conditions that are per se multivalued ones. Examples:
- Temperature can be at least Low, OK, High
- Commands may have several values: Init, Start, Stop, Break, Continue
- In a (hierarchical) system of state machines the Slaves state machines have many states
that are used in transition conditions of a Master state machine.
In addition, many input signals can be not known due for instance to a broken cable which
means that even a digital input signals (considered as classical Boolean values) are in fact 3
values signals: Low, High, Unknown. The Temperature example needs probably also the
value Unknown.
A Positive Logical Algebra solves this problem by creating a Virtual Environment which allows
specification of state machines for software using multivalued variables. The definition of the
Positive Logic Algebra can be found in Appendix 1.
6
Figure 7. Example of a hierarchical system of state machines
10. Conclusions
The paper has discussed the major problems that limit a true use of state machines in
software:
- Incomplete understanding of an implementation model of a state machine
- Restriction of input variables to Boolean values
- Misunderstanding of a system of state machines
- Programmers’ resistance to no coding solution.
We have shown that the use of Virtual Environment allows a full specification of the behavior
beyond the true Boolean values. Such a specification can be carried out in an execution
environment which eliminates the burden of coding the control flow. This solution works for
single state machine as well as a system of hierarchically organized state machines.
References
1. Wagner, F. &. (2006). Modeling Software with Finite State Machines. A practical
Approach. CRC Press Taylor & Francis Group.
2. https://en.wikipedia.org/wiki/Virtual_finite-state_machine
Logical expression
A logical expression is an OR-table of AND-sets (corresponds to disjunctive form of a boolean
expression).
For instance:
{𝐴1 𝐵3}
A1 & B3 | A1 & B2 & C4 | C2 = > [ {𝐴1 𝐵2 𝐶4}]
{𝐶2}
Logical expressions are used to express any logical function.
Virtual Environment
The Virtual Name and Virtual Output completed by State Names create a Virtual Environment VE where
the behavior is specified.