Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / XML
Article

XML Finite State Machine in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.27/5 (5 votes)
7 Dec 20052 min read 41.8K   457   22   3
This 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.

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.

XML
<?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>
  1. The program first reads all items using XMLStateMachine.GetItems().
  2. The FSM starts the vending machine program at the 'Start' state.
  3. The state 'Start' triggers a member-function of the class VendingMachineStates called Start.
  4. The state 'Start' doesn't have a <transition>, it 'falls through' to the next state.
  5. VendingMachineStates.WantMoney is executed.
  6. The transition rule returns the execution to 'WantMoney' until VendingMachineStates.isHappy() is set.
  7. VendingMachineStates.DeliverItem is executed.
  8. VendingMachineStates.GiveChange is executed.
  9. VendingMachineStates.Terminate is executed.
  10. 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:

VB
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.

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:

XML
<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:

XML
<init item="Coke" size="small" price="50" sugar="10%"/>
<init item="Coke" size="big" price="75" sugar="10%"/>

You must do like this:

XML
<init item="CokeSmall" price="50" sugar="10%"/>
<init item="CokeBig" price="75" sugar="10%"/>

History

  • 2005-12-04 - Version 1.0.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) SEB bank
Sweden Sweden
I work as a developer in the 'Risk control' department at SEB bank in Stockholm,Sweden and I have been designing software since the early 80's.

Comments and Discussions

 
GeneralDownLoad Zip Files from Web server Pin
lakshmi patil26-Oct-06 23:13
lakshmi patil26-Oct-06 23:13 
GeneralSerialize and DeSerialize an AVI File Pin
Tony L17-Jul-06 4:41
Tony L17-Jul-06 4:41 
I am developing a VB.Net application in which I need to serialize an avi file and store the data in an XML document along with some other data. Then at some other point in time I need to DeSerialize the data back into a useable AVI file with audio and video. I am able to serialize the data and save it to xml, but I cannot create an AVI file from the serialized data. Any help would be greatly appreciated.
Generalsteed.net Pin
marc.thom12-Jun-06 5:26
marc.thom12-Jun-06 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.