Documentation
Not logged in

Coding the Washing Machine

The code is a relatively straighforward encoding of the state model

We create a state model, wmachine, using the model command (line 9). The ::oomoore model command is a meta-class. You create a state model by giving it a specification. In the specification, each state of the model is declared with the state command. The state command invocations correspond to the boxes on the state model graphic. The state command has an interface like the core proc command. Any parameters would be listed in the second argument. The body of the state command is the Tcl code that will be run when the state is entered upon a transition. When the state code is run, it is actually run as a method so all TclOO facilities are available.

For this example, the components of the washing machine, such as the pump and motor, are simulated by simple puts commands. In a real program these would be replaced by procedure invocations or event generation to other state machines. Also note that there are several places where we simulate the behavior of some of these components by using delayed events to ourself.

The transition command defines which state is entered when a given event is delivered (i.e. line 31). The transition command invocations correspond to the arcs on the state model graphic.

Several things are worth noting in the specification of a state model.

The state model definition completes at line 110. The remainder of the code shows how to create and run an instance of a state machine.

At line 113, we draw the as-implemented state model. The oomoore package can run dot(1) if it is available. The result shows the as-implemented model in a graphical representation.

At line 115, we define a class named wm_model_100. This class has wmachine as a superclass and thus inherits the state behavior of the wmachine state model. The constructor (line 117) simply initializes an the state machine and defines some variable values that are used for timing the washing machine cycle.

At line 121, the times used in the machine are configured. The times are in milliseconds.

Line 129 configures state model tracing on. It is necessary that the log level for the object to be at least info.

Finally at line 131, we start the washing machine running by signalling a Run event to it.

At line 133, the program enters the Tcl event loop. Recall that the state action of the Idle state writes the the ::wmachine array and this is used as the synchronization mechanism between the state model and the test code.

Finally at line 135, we clean up the state machine by destroying it.

Previous Next