Click here to Skip to main content
Licence MIT
First Posted 31 Mar 2002
Views 111,563
Bookmarked 72 times

XML Finite State Machine in C#

By | 31 Mar 2002 | Article
An article on implementing a table-driven finite state machine using XML and C#

Introduction

This article demonstrates using the .NET Framework to implement a finite state machine (FSM). This project was inspired by the MSDN article Design Patterns: Solidify Your C# Application Architecture with Design Patterns. Like the article's author, I believe that using State objects is a more powerful way of implementing this particular design pattern. However, for those that belive in XP's principle of "doing the simplest thing that could possibly work", table-driven FSMs are quite simple to implement.

To make the implementation of the FSM more interesting, I chose to define the table using XML. The format is very simple and defines state and transition elements. Using the example from the MSDN article, here is the state table in XML.

<?xml version="1.0" ?>

<fsm name="Vending Machine">
    <states>
        <state name="start">
            <transition input="nickel" next="five" />
            <transition input="dime" next="ten" />
            <transition input="quarter" next="start" action="dispense" />
        </state>
        <state name="five">
            <transition input="nickel" next="ten" />
            <transition input="dime" next="fifteen" />
            <transition input="quarter" next="start" action="dispense" />
        </state>
        <state name="ten">
            <transition input="nickel" next="fifteen" />
            <transition input="dime" next="twenty" />
            <transition input="quarter" next="start" action="dispense" />
        </state>
        <state name="fifteen">
            <transition input="nickel" next="twenty" />
            <transition input="dime" next="start" action="dispense" />
            <transition input="quarter" next="start" action="dispense" />
        </state>
        <state name="twenty">
            <transition input="nickel" next="start" action="dispense" />
            <transition input="dime" next="start" action="dispense" />
            <transition input="quarter" next="start" action="dispense" />
        </state>
    </states>
</fsm>

Pretty simple. The class XMLStateMachine has three properties (Action, CurrentState, and StateTable) and one method, Next. To use it, simply create an instance of XMLStateMachine, set the StateTable property to the XML file you wish to use and set the CurrentState property to the initial state.

<br>
XMLStateMachine fsm = new XMLStateMachine();<br>
fsm.StateTable = "vending.xml";<br>
fsm.CurrentState ="start";<br>

Then, call the Next method with an appropriate input argument. Next returns the new state as well as modifies the CurrentState property.

NAnt is used to build the sample project. It is based on the Apache Ant project, which is used to build many Java-based projects. If you cannot afford Visual Studio .NET, or prefer to use your own editor and the command line compilers I highly recommend NAnt.

The code presented here could be expanded in many ways. The Action property could be replaced with an event. The System.Xml.XPath.XPathNavigator class could be used instead of System.Xml.XmlTextReader. But, that would defeat the purpose of implementing "the simplest thing that could possibly work"!

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Kevin Stewart

Web Developer

United States United States

Member

Kevin is a software development manager for a small consumer-oriented company in SoHo, NY. He is technology and platform agnostic having worked on PCs, Macintosh and various forms of Unix and Linux. His programming knowledge includes several languages, including C/C++, Java and C#. In the rare moments when his head is not buried in the latest tech book purchase from Amazon, Kevin enjoys spending time with his wife Donna and their dog Kirby.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionFall-through PinmemberArnoldo Ellerkamp6:53 12 Jan '12  
GeneralLimitations and Hierarchy Pinmemberdakash2:57 20 May '09  
QuestionHow to expand the action property with an event... Pinmemberkimfinkis6:55 20 Jun '08  
Generalsteed,net Pinmembermarc.thom5:03 8 Jun '06  
GeneralCorrected link to MSDN article... Pinmemberkemetokara15:30 28 Mar '05  
Generalabstract DoAction() makes forgetting the action impossible PinmemberBryan White13:39 28 Sep '03  
GeneralRe: abstract DoAction() makes forgetting the action impossible PinmemberKevin Stewart3:26 29 Sep '03  
GeneralURL Problems PinmemberJames T. Johnson18:10 1 Apr '02  
GeneralRe: URL Problems PinmemberKevin Stewart4:45 2 Apr '02  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 1 Apr 2002
Article Copyright 2002 by Kevin Stewart
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid