Click here to Skip to main content
Click here to Skip to main content
Go to top

XML Finite State Machine in C#

, 31 Mar 2002
Rate this:
Please Sign up or sign in to vote.
An article on implementing a table-driven finite state machine using XML and C#
<!-- Add the rest of your HTML here -->

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.


XMLStateMachine fsm = new XMLStateMachine();

fsm.StateTable = "vending.xml";

fsm.CurrentState ="start";


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

Share

About the Author

Kevin Stewart
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

 
QuestionFall-through PinmemberArnoldo Ellerkamp12-Jan-12 6:53 
GeneralLimitations and Hierarchy Pinmemberdakash20-May-09 2:57 
QuestionHow to expand the action property with an event... Pinmemberkimfinkis20-Jun-08 6:55 
Generalsteed,net Pinmembermarc.thom8-Jun-06 5:03 
GeneralCorrected link to MSDN article... Pinmemberkemetokara28-Mar-05 15:30 
Generalabstract DoAction() makes forgetting the action impossible PinmemberBryan White28-Sep-03 13:39 
GeneralRe: abstract DoAction() makes forgetting the action impossible PinmemberKevin Stewart29-Sep-03 3:26 
GeneralURL Problems PinmemberJames T. Johnson1-Apr-02 18:10 
GeneralRe: URL Problems PinmemberKevin Stewart2-Apr-02 4:45 

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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 1 Apr 2002
Article Copyright 2002 by Kevin Stewart
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid