Posts

Showing posts with the label state machine

Simple State Machine Updates

Introduction Some of you may remember my SimpleStateMachine project; I blogged about it here and here . It is available on GitHub and Nuget . I made a few changes to it: An alternative way to configure states Adding state to states (pun intended) Converting attributes to code state machines The possibility to clear state transitions (unsure about this one, but, it's here, at least for now) Let's see how these work. Building a State Machine We used to have two ways for building a simple state machine: From attributes From code I'm going to introduce a new one, but, first, here is quick recap for the existing methods. From Attributes As simple as adding attributes to enumeration fields: public enum TicketState { [InitialState] [Transitions<TicketState>(TicketState.Ready, TicketState.Closed)] Created, [Transitions<TicketState>(TicketState.InProgress, TicketState.Blocked, TicketState.Closed)] Ready, [Transitions<TicketState>(TicketSt...

A Simple State Machine in .NET - Adding Code-based Implementation

Introduction Updates: new post here . On a previous post , I introduced my SimpleStateMachine project. Essentially, it provided an easy way to define a state machine from enumeration values. The version presented used attributes on these enumeration values to build up the state machine. Because this can sometimes be intrusive, and we may not want to "pollute" our enumerations, I decided to implement another way to define a state machine from an enumeration. State Machines from Code So, I came up with this interface: public interface IStateMachine<T> where T : Enum { T GetInitialState(); IStateMachine<T> CanTransitionTo(T state, params IReadOnlyCollection<T> otherStates); IEnumerable<T> GetTransitions(T state); bool IsFinalState(T state); bool IsInitialState(T state); } It provides basically the same operations that were previously available: Get the initial state of an enumeration ( GetInitialState ) Check if an enumeration value i...

A Simple State Machine in .NET

Image
Introduction Update: new additions here  and here . This is another post that should be tagged lazy-weekend! Let's imagine a simple state machine for tracking the state of tickets. It consists of the following states: Created : the ticket has been created Ready : the ticket is ready to be picked up for implementation In Progress : the ticket is being implemented Blocked : the ticket is currently blocked In Review : the ticket was sent for review Closed : the ticket has been closed The following diagram illustrates these states and the possible transitions: A simple state machine Don't worry too much about the actual states, this is meant to be an example. An explanation for these transitions is in order: The first state for a ticket is Created From Created , the ticket can transition to Closed , or to Ready From Ready , the ticket can transition to In Progress , Blocked , or Closed   From In Progress , the ticket can transition to Blocked , In Review , Ready , or Closed From I...