Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Article

XML Finite State Machine in C#

Rate me:
Please Sign up or sign in to vote.
4.59/5 (15 votes)
31 Mar 2002MIT2 min read 146.1K   4.2K   74   11
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
<?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


Written By
Web Developer
United States United States
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.

Comments and Discussions

 
QuestionInner tableParser.Read() goes beyond a particular state tag Pin
jeffry copps13-Jun-16 2:05
jeffry copps13-Jun-16 2:05 
QuestionFall-through Pin
Arnoldo Ellerkamp12-Jan-12 6:53
Arnoldo Ellerkamp12-Jan-12 6:53 
AnswerRe: Fall-through Pin
jeffry copps13-Jun-16 2:24
jeffry copps13-Jun-16 2:24 
GeneralLimitations and Hierarchy Pin
dakash20-May-09 2:57
dakash20-May-09 2:57 
QuestionHow to expand the action property with an event... Pin
kimfinkis20-Jun-08 6:55
kimfinkis20-Jun-08 6:55 
Generalsteed,net Pin
marc.thom8-Jun-06 5:03
marc.thom8-Jun-06 5:03 
GeneralCorrected link to MSDN article... Pin
Charles T II28-Mar-05 15:30
Charles T II28-Mar-05 15:30 
Generalabstract DoAction() makes forgetting the action impossible Pin
Bryan White28-Sep-03 13:39
Bryan White28-Sep-03 13:39 
GeneralRe: abstract DoAction() makes forgetting the action impossible Pin
Kevin Stewart29-Sep-03 3:26
Kevin Stewart29-Sep-03 3:26 
GeneralURL Problems Pin
James T. Johnson1-Apr-02 18:10
James T. Johnson1-Apr-02 18:10 
GeneralRe: URL Problems Pin
Kevin Stewart2-Apr-02 4:45
Kevin Stewart2-Apr-02 4:45 
Hmmm, they were in the HREFs when I submitted the article. I will try and fix this. Thanks! Big Grin | :-D

Kevin

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.