- Computers & Software»
- Computer Science & Programming
The State Machine for Everyone
State Machine is a technical term commonly used in computer design and Computer Science . It is a programming and designing technique where a complex and repetitive task is broken down into smaller and definable pieces called states . By executing these states in precise step and timing, the complex task can be resolved in a satisfactory and exact fashion repeatedly without error.
The omnipotent computer can be considered to be built of state machines. We use computer to solve the same complex tasks precisely and repeatedly without error . The State Machine is the perfect tool and algorithm to accomplish it.
Outside of the computer design and Computer Science, similar state machine technique can also be observed in:
A gene 's open reading frame (ORF) is the series of codons (3 DNA bases) that specify the amino acid sequence of that gene's protein . The ORF begins with the initiation codon (usually ATG) and ends with a termination codon (TAA, TAG, TGA).
2) Washing machine
Drop clothes into tank, add water and detergent, stir, rinse, and spin.
In the cooling process, liquid Freon is pumped into a set of coils, turns to gas, and absorbs heat. In the dissipating process, gaseous Freon is compressed into a set of coils, turns to liquid, and gives off heat.
4) Internal-Combustion Engine
Crankshaft position 1: intake valve open, enters air/fuel mixture.
Crankshaft position 2: compressing air/fuel mixture.
Crankshaft position 3: firing the spark plug to ignite air/fuel mixture and forcing the Crankshaft down.
Crankshaft position 4: exhaust valve open, out goes the exhaust gases.
The state machine starts with an initial state (also called the idle state) where it is waiting for the occurrence of an input condition . Just like the washing machine, the washing cycle starts when the Control Dial on the panel is being set to the start of the cycle by the operator.
When the first input condition is active, the state machine goes to the 2nd state where a set of pre-defined functions are executed. It stays in this state till the occurrence of another input condition. Just like the washing machine, it starts to add water to the clothes in the tank.
When the 2nd input condition is active, the state machine goes to the 3rd state and so on till it reaches the last state to complete the pre-defined task. After that, the state machine returns to the initial state. Just like the washing machine, after the water reaches the desired level, it executes the stirring function. After a pre-set timer has expired, the washing machine goes to the rinse and spin cycles in similar fashion till the washing cycle is completed. It then returns to its initial state waiting for the next batch of clothes to be washed.
Working out the Bugs
Bringing up the very first washing machine, adjustments are needed to each cycle in order to achieve the cleanest clothes using the least amount of water and the minimum amount of time. After the state machine completes its preliminary design, for a simple and non-critical task, it can be immediately applied, checked, and adjusted for its accuracy on the job . For a critical or complex task, it is more practical to test the design in a simulated laboratory environment. The present-day microprocessor inside a computer can be considered as a complex state machine. It can take several hundred engineers and several years to design, debug , and build.
In the daily routine , we can apply the state machine technique to the cooking of a favorite dish. In order to cook a particular dish in the minimum amount of time and effort but with a consistent flavor and taste , the following general states need to be considered:
1) Prepare a list of the ingredients with the exact quantity and seasonings.
2) Define the exact time the heat that needs to be applied to the ingredients before adding seasonings.
3) Define how the ingredients are to be stirred and mixed.
4) Define how long to wait for the seasonings to be absorbed by the ingredients before the dish is ready to serve.
To work out the details, it takes time and practice. But, it is essential to document them. When the desired results are repeatedly consistent, this cooking state machine is completed. In the future, all you have to do to cook this particular dish is to execute this cooking state machine to the letter.