Click here to Skip to main content
Click here to Skip to main content

XML Finite State Machine in VB.NET

, 7 Dec 2005
Rate this:
Please Sign up or sign in to vote.
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 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:

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:

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

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

Share

About the Author

Sten Hjelmqvist
Software Developer (Senior) Affecto Sweden AB
Sweden Sweden
I'm a computer consultant at Affecto AB in Stockholm,Sweden and I have been designing software since the early 80's.

Comments and Discussions

 
GeneralDownLoad Zip Files from Web server Pinmemberlakshmi patil27-Oct-06 0:13 
GeneralSerialize and DeSerialize an AVI File PinmemberTony L17-Jul-06 5:41 
Generalsteed.net Pinmembermarc.thom12-Jun-06 6:26 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Dec 2005
Article Copyright 2005 by Sten Hjelmqvist
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid