Wednesday, February 26, 2014

Stateless State Machine

The other day I went to grab a snack in the vending machine at work. I swiped my RFID badge on the reader, waited a few seconds, it showed my balance, and while entering the number of the yummy snack I was ready to consume, the balance went back to zero, which then had the effect of me hitting enter and it displaying “not enough funds, please enter money”.

Knowing full well that a few seconds ago, it showed my balance as being more then plenty, I repeated this process, this time with vigilance in preparing the number ahead of time, so that the second it showed my balance, I could hit enter. Suffice to say, I enjoyed my snack. But this brought to my attention that this state machine didn’t have a path for stopping its pricing update when it had a balance. In other words, a broken state machine.


A state machine is defined as any device that stores the status of something at a given time and can operate on input to change the status and/or an action or output to take place for any given change. That is a great “what is” definition, but what is it really, and how can I make these awesome prone to buggy snippets of code?

Lets start with a basic state machine:
    public class BasicStateMachine
    {
        public string State { get; private set; }

        public void Transition(string state)
        {
            switch (state)
            {
                case "on":
                    State = "ON";
                    OnSwitchedOn();
                    break;
                default:
                    State = "OFF";
                    OnSwitchedOff();
                    break;
            }
        }

        private void OnSwitchedOn()
        {
            // Do something clever
        }

        private void OnSwitchedOff()
        {
            // Stop doing something clever
        }
    }


The above code is pretty straightforward. You have the ability to Transition to states, when the state is set to on, it sets the state to “ON” and fires the OnSwitchedOn method. Anything else attempting to transition will just switch it off.

Now that the “what is a state machine” is taken care of, lets check out a fluent API called Stateless by to do the same thing above, but “better”.

Stateless by Nicholas Blumhardt is a base for exploration of generic and functional programming to drive workflow in .Net. It allows you to create lightweight state machine based workflows directly in code.

On NuGet you will find two packages Stateless and Stateless-4.0. The latter is just a .Net 4.0 complication.

PM> Install-Package stateless-4.0

With Stateless our state machine looks something like this.


    public class StatelessStateMachine
    {
        private readonly StateMachine _stateMachine;

        public enum Trigger
        {
            TurnOn,
            TurnOff
        }

        public enum State
        {
            On,
            Off
        }

        public State Current { get { return _stateMachine.State; } }

        public StatelessStateMachine()
        {
            _stateMachine = new StateMachine(State.Off);
            _stateMachine.Configure(State.Off)
                .Permit(Trigger.TurnOn, State.On)
                .OnEntry(OnSwitchedOff);

            _stateMachine.Configure(State.On)
                .Permit(Trigger.TurnOff, State.Off)
                .OnEntry(OnSwitchedOn);
        }

        public bool Transition(Trigger trigger)
        {
            if (!_stateMachine.CanFire(trigger))
                return false;

            _stateMachine.Fire(trigger);

            return true;
        }
        private void OnSwitchedOn()
        {
            // Do something clever
        }

        private void OnSwitchedOff()
        {
            // Stop doing something clever
        }

    }


Now.. that is a ton more code it looks like, but this is a dynamic system that can grow with need. The State Machine object requires two objects, one for the “State” and one for the “Trigger” or as I like to reference it, the “Transition”.

Going over the Configure section, this is simply setting up that in the “Off” state we are allowed to transition to the “On” state with the “TurnOn” trigger, and finally when the state is entered, call the OnSwitchedOff. There is a point of confusion that can occur here, as with most Fluent API’s. The OnEntry is for the Configure, not the Permit. It might even be more useful later to move the OnEntry above the Permit just to clarify for other people using the code.

Stateless is a great bit of code, the source includes a ton of unit tests (nUnit) as well as a decent amount of samples. It is also really lightweight being 3 total classes (spread out over 13 files) and no external dependencies outside of the BCL. So simple in fact, on one recent project I worked with, I just included the source code, rather than referencing the library to keep down on my dependencies.

I highly recommend heading over to the Google Code Project and giving it a shot!

You can find the code used in this Blog Post on my GitHub Repository.

28 comments:

ragini ragini said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site. Dotnet developer
Authorized Dot Net training in chennai

ragini ragini said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, xamarin training in Chennai

cynthia williams said...

Very good blog, thanks for sharing such a wonderful blog with us. Keep sharing such worthy information to my vision.
Blockchain Training in Chennai
Blockchain course in Chennai
AWS Training in Chennai
AWS Certification in Chennai
Python Training in Chennai
Python Classes in Chennai

Anbarasan14 said...

Thanks for sharing a worthy information. This is really helpful. Keep doing more.

French Class in Mulund
French Coaching in Mulund
French Classes in Mulund East
French Language Classes in Mulund
French Training in Mulund
French Coaching Classes in Mulund
French Classes in Mulund West

Sadhana Rathore said...

Thanks for sharing this information admin, it helps me to learn new things. Continue sharing more like this.
Appium Training in Chennai
Best Appium Training institute in Chennai
Appium Certification in Chennai
Mobile Appium Training in Chennai
Mobile Appium course in Chennai

Aruna Ram said...

Thank you for sharing such a great concept. I got more knowledge from this blog. Your site was amazing. Keep update on some more details...
Blue Prism Training Bangalore
Blue Prism Classes in Bangalore
Blue Prism Training Centers in Bangalore
Blue Prism Institute in Bangalore
Blue Prism Training in Perambur
Blue Prism Training in Nolambur

LindaJasmine said...

Amazing Post . Thanks for sharing. Your style of writing is very unique. Pls keep on updating.
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
Best Spoken English Class in Chennai
English Coaching Classes in Chennai

sathyaramesh said...

Selenium Training in Chennai
Selenium Course in Chennai
iOS Course in Chennai
Digital Marketing Training in Chennai
J2EE Training in Chennai
german language classes
german teaching institutes in chennai
german language coaching centres in chennai

Vicky Ram said...

Really great information!!! Thanks for your blog.

ejobsalert
Guest posting sites

punitha said...

Nice article I was really impressed by seeing this blog, it was very interesting and it is very useful for me.
Javascript Training in Bangalore
Java script Training in Bangalore
Javascript Training Institutes in Bangalore
Javascript Course in Bangalore

punitha said...

Nice article I was really impressed by seeing this blog, it was very interesting and it is very useful for me.
Javascript Training in Bangalore
Java script Training in Bangalore
Javascript Training Institutes in Bangalore
Javascript Course in Bangalore

Gautam krish said...

The blog which you have shared is more informative. Thanks for your information.
Best Institute for JAVA
Best JAVA Training
JAVA Programming Certification Course
Best JAVA Certification
Best JAVA Training

pavithra dass said...

I wanted to thank you for this great blog! I really enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
Angularjs course in Chennai
Angularjs Training institute in Chennai
Angular 2 Training in Chennai
DevOps Training
DevOps course in Chennai
DevOps course

Aruna Ram said...

Good think! I appreciate you sharing this post. Your posts is very useful for me. Really thank you!
Data Science Training in Chennai Adyar
Data Science Course in Annanagar
Data Science Training in Adyar
Data Science Training in Velachery
Data Science Training in Chennai Velachery
Data Science Training in Tnagar

Riya Raj said...

Wonderful blog!!! Thanks for your information… Waiting for your upcoming data.
Ethical Hacking Course in Coimbatore
Hacking Course in Coimbatore
Ethical Hacking Training in Coimbatore
Ethical Hacking Training Institute in Coimbatore
Ethical Hacking Training
Ethical Hacking Course

mercyroy said...

Brilliant ideas that you have share with us.It is really help me lot and i hope it will help others also.update more different ideas with us.
Selenium Training in Navalur
Selenium Training in Karapakkam
Selenium Training in Guindy
Selenium Training in Saidapet

Kayal m said...

Thank you for provided this content. I learn a lot of information. Please keep it....
Automation Courses in Bangalore
RPA Courses in Bangalore
Robotics Classes in Bangalore
Robotics Training in Bangalore
RPA Training in Bangalore
Robotics Courses in Bangalore

Aruna Ram said...

Thank you for your nice post. I glad to read your blog. I want to more updates….
SEO Course in Aminjikarai
SEO Training in Vadapalani
SEO Course in Chennai
SEO Course in Karappakkam
SEO Training in Padur
SEO Classes near me

sathyaramesh said...

Nice article. I liked very much. All the informations given by you are really helpful for my research. keep on posting your views.
Salesforce Administrator 201 Training in Chennai
Salesforce Administrator 211 Training in Chennai
Salesforce Developer 401 Training in Chennai
Cloud computing Training in Chennai
Cloud computing Training
Cloud computing Training near me

Gautam krish said...

The blog is well written and Thanks for your information. Java is one of the widely accepted language. The reason is it's features and it is platform independent.
Advanced JAVA Training
JAVA Training Classes
Core JAVA Certification
JAVA Language Course
Core JAVA Course

Riya Raj said...

Outstanding information!!! Thanks for sharing your blog with us.
Spoken English Class in Coimbatore
Best Spoken English Classes in Coimbatore
Spoken English in Coimbatore
Spoken English Classes
English Speaking Course

Kayal m said...

Good work, really very great information and good creativities. Thank you for your wonderful post!
Digital Marketing Course in Tnagar
Digital Marketing Training in Tnagar
Digital Marketing Classes near me
Digital Marketing Training in Navalur
Digital Marketing Training in Omr
Digital Marketing Course in Omr

sathyaramesh said...

I am really enjoying reading your well-written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
SEO Training in Chennai
Digital Marketing Chennai
Digital Marketing Courses in Chennai
Digital marketing courses
SEO Institutes in Chennai
SEO Course Chennai

jefrin adams said...

Very impressive to read
Best Blockchain training in chennai

harish sharma said...

whatsapp group links

abhi pranesh said...

Thanks for posting this article it has really a good content.
Spoken English Classes in Chennai
Spoken English in Chennai
Japanese Classes in Chennai
French Classes in Chennai
pearson vue
German Classes in Chennai
Spoken English Classes in Adyar
Spoken English Classes in Velachery

venu bharath said...

You are an amazing writer. The content is extra-ordinary. Looking for such a masterpiece. Thanks for sharing.
IoT Training in Chennai
IoT courses in Chennai
IoT Courses
IoT Training in Porur
IoT Training in Adyar

nikitha josh said...

Excellent post gained so much of information, Keep posting like this.
Aviation Courses in Chennai
Air hostess training in Chennai
Airline Courses in Chennai
airport ground staff training courses in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai