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

Another Generic State Machine

By , 12 May 2013
 

Introduction

I was looking for a generic implementation of the statemachine pattern.
Of course, there are many out there. But what I found was either too specific or part of some monster framework.

So I wrote my own.

Background

The State Machine pattern is discussed here.

Using the Code

To use the StateMachine, simply derive from the class, and setup your states and transition events:

public class MyStateMachine : StateMachine<StateData, int, string>
{
   public MyStateMachine ()
   {
       AddState(new State { data = new StateData { Identity = 0, Name = "Solid" }, 
                            IdSelector = x=> x.Identity });
       AddState(new State { data = new StateData { Identity = 1, Name = "Liquid" }, 
                            IdSelector = x => x.Identity });
       AddState(new State { data = new StateData { Identity = 2, Name = "Gas" }, 
                            IdSelector = x => x.Identity });
       AddEvent(new StateMachine<StateData, int, <span style="font-size: 9pt;">string</span><span style="font-size: 9pt;">>.Event { ID = "Cool", </span>
                   Transitions = new Dictionary<int, int> { { 0, 0 }, { 1, 0 }, { 2, 1 } } });
       AddEvent(new StateMachine<StateData, int, <span style="font-size: 9pt;">string</span><span style="font-size: 9pt;">>.Event { ID = "Heat", </span>
                   Transitions = new Dictionary<int, int> { { 0, 1 }, { 1, 2 }, { 2, 2 } } });
   } 
   public void Heat()
   {
       var ev = GetEventByKey("Heat");
       FireEvent(ev);
   }
   public void Cool()
   {
      var ev = GetEventByKey("Cool");
      FireEvent(ev);
   }

} 

An event in this StateMachine is the action that happens in the world, this then triggers transitions in the state machine from one state to another. You can setup public methods to match the actions set on your StateMachine.

In this simplified example, the state machine matches state of matter depending on its heat property. Cooling a gas will result in a liquid. Heating a solid will result in a liquid.

Using the state machine is easy:

var target = new MyStateMachine();
target.LeaveState += LeaveHandler; 
target.SetState(1); 
target.SetState(1); 

Points of Interest

I ran into interesting questions while writing unit tests for this class. See tip on "Mocking Event Handlers".

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

forester joe
Software Developer (Senior)
Israel Israel
Member
I've been developing in C# for over 7 years.
Always in some unique corner or another.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGreat design !memberPaul Van Bladel II11 May '13 - 20:22 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130513.1 | Last Updated 12 May 2013
Article Copyright 2013 by forester joe
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid