|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionFinite 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>
Every state has a corresponding function in the class Software designBut 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 BackgroundPlease 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 codeThe module The method The ' <init item="Coke" size="small" price="50" sugar="10%"/>
All attributes are stored in a <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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||