Click here to Skip to main content
13,191,570 members (63,704 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 7 Dec 2005

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.


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" ?>

    <init item="JuicyFruit" price="20"/>
    <init item="Coke" price="50"/>

    <state name="Start"/>
    <state name="WantMoney">
        <transition next="WantMoney"/>

    <state name="DeliverItem"/>
    <state name="GiveChange"/>

    <state name="Terminate">
        <transition next="Start"/>
  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

        If input="nickel" Then
        ElseIf input="dime" Then
        ElseIf input="quarter" Then
        End If
    Loop While money<20

    Goto OneMoreJuicyFruit
ElseIf item="Coke" Then
    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.

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%"/>


  • 2005-12-04 - Version 1.0.


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralDownLoad Zip Files from Web server Pin
lakshmi patil26-Oct-06 23:13
memberlakshmi patil26-Oct-06 23:13 
GeneralSerialize and DeSerialize an AVI File Pin
Tony L17-Jul-06 4:41
memberTony L17-Jul-06 4:41 Pin
marc.thom12-Jun-06 5:26
membermarc.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.

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