Moore Type State Machines in Tcl
Finite state machines are a powerful abstraction that can be used to structure the sequencing of programs. There are many examples of state machines out there and even a casual search of the Tcl Wiki shows many examples of how to implement simple state machines.
For more complicated programs, the simple examples of the wiki are not sufficient. There are several requirements that are needed to use state machines in a larger program context.
- We need to be able to specify a state model that can have multiple instances. There may be several instances of the same entity in a program and each instance must have the same behavior.
- We need to be able to both generate events to a state machine as well as generate a delayed event. A delayed event is one that is delivered at some time in the future. Events need to operate well with the Tcl event loop.
- For debugging, it is essential to be able to obtain a trace of the event dispatch and state transitions.
- For larger state models, a graphical depiction is very useful and so we need to be able to produce a graphic showing the as implemented state model.
- We need a way to associate some data with the state machine. This will allow us to connect the state machine processing back to the rest of the program.
Here we describe the moore package. This is a Tcl extension that facilitates designing state machines into your Tcl programs. The link references a package file that may be installed directly into your teacup.