![]() |
Languages »
C / C++ Language »
General
Intermediate
XML Finite State Machine in VB.NETBy Sten HjelmqvistThis article describes an enhancment of the techniques in the article 'XML Finite State Machine in C#' by Kevin Stewart. It encapsules the FSM in a 'black box' using reflection and interfaces. |
VB, XML, Windows, .NET 2.0VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
<?xml version="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>
XMLStateMachine.GetItems().
VendingMachineStates called Start.
<transition>, it 'falls through' to the next state.
VendingMachineStates.WantMoney is executed.
WantMoney' until VendingMachineStates.isHappy() is set.
VendingMachineStates.DeliverItem is executed.
VendingMachineStates.GiveChange is executed.
VendingMachineStates.Terminate is executed.
Every state has a corresponding function in the class VendingMachineStates. The function is executed in Fsm.NextState() using Reflection.
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 Ifs and ElseIfs.
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.
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%"/>
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Dec 2005 Editor: Smitha Vijayan |
Copyright 2005 by Sten Hjelmqvist Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |