Introduction
Finite State Machines (FSMs) are mainly considered as tools to design parsers but there is another area where they can be very useful and that is in 'Software design'. This FSM uses a XML-file to control the flow of execution in a program. It implements a vending machine that sells any number of items, in this example, a 'Juicy Fruit' for 20 cents and a 'Coke' for 50 cents.
="1.0"
<fsm>
<init item="JuicyFruit" price="20"/>
<init item="Coke" price="50"/>
<state name="Start"/>
<state name="WantMoney">
<transition next="WantMoney"/>
</state>
<state name="DeliverItem"/>
<state name="GiveChange"/>
<state name="Terminate">
<transition next="Start"/>
</state>
</fsm>
- The program first reads all items using
XMLStateMachine.GetItems()
.
- The FSM starts the vending machine program at the 'Start' state.
- The state 'Start' triggers a member-function of the class
VendingMachineStates
called Start
.
- The state 'Start' doesn't have a
<transition>
, it 'falls through' to the next state.
VendingMachineStates.WantMoney
is executed.
- The transition rule returns the execution to '
WantMoney
' until VendingMachineStates.isHappy()
is set.
VendingMachineStates.DeliverItem
is executed.
VendingMachineStates.GiveChange
is executed.
VendingMachineStates.Terminate
is executed.
- The transition rule returns the execution to 'Start'.
Every state has a corresponding function in the class VendingMachineStates
. The function is executed in Fsm.NextState()
using Reflection.
Software design
But why would you or anyone design a program like this? Why not write it 'the good ol' way'? Like this:
If item="JuicyFruit" Then
OneMoreJuicyFruit:
Do
WantMoney()
If input="nickel" Then
money+=5
ElseIf input="dime" Then
money+=10
ElseIf input="quarter" Then
money+=25
End If
Loop While money<20
DeliverItem()
GiveChange()
Reset()
Goto OneMoreJuicyFruit
ElseIf item="Coke" Then
OneMoreCoke:
Do
WantMoney()
.
.
.
Loop While money<50
.
.
Goto OneMoreCoke
End If
Well, in a small program like this, the logic is still manageable but as it grows, it quickly becomes very complicated with all the If
/Else
/ ElseIf
/Case
/Switch
or whatever the syntax your favorite language has to offer. And this vending machine only sells 'Juicy fruit' and 'Coke'! Add some more soft drinks and candy, and you will quickly find yourself in a logical hell! I know I've been there! If you want to sell more, just add an 'item' like <init item="Pepsi" price="45"/>
in 'vending.xml'. No new code is needed. Another good reason to design software using a State machine-file like 'vending.xml' is that it simplifies maintenance, especially if you are not the one that's going to do it. The XML-version is much easier for a new guy to understand than the code above with all its If
s and ElseIf
s.
Background
Please read the original article, 'XML Finite State Machine in C#' by Kevin Stewart, to get the background.
Libero is a code generator that generates a FSM from a text file.
Using the code
The module FSM_XML
is a 'black box' containing the classes Fsm
and XMLStateMachine
. The module VendingMachine
includes the Main
, and the file 'VendingMachineStates.vb' contains the class VendingMachineStates
with its members that map the states in 'vending.xml'.
The method VendingMachineStates.External()
is always run before any other method. This is where you can handle external events.
The 'init
' tag can have any number of attributes. For instance, if you want to add sugar content:
<init item="Coke" size="small" price="50" sugar="10%"/>
All attributes are stored in a Hashtable
. The 'item
' is the key, so you cannot do like this:
<init item="Coke" size="small" price="50" sugar="10%"/>
<init item="Coke" size="big" price="75" sugar="10%"/>
You must do like this:
<init item="CokeSmall" price="50" sugar="10%"/>
<init item="CokeBig" price="75" sugar="10%"/>
History
- 2005-12-04 - Version 1.0.