Giter Site home page Giter Site logo

microstatemachine's Introduction

MicroStateMachine

A tiny implementation of a generic statemachine.

MSM is the result of a small code dojo, that turned out something rather powerful, given the few lines of code actually written for it. MSM allows the modelling and use of generic state machines (hence the name!)

Usage:

##Example

enum Events
{
    Event1,
    Event2,
    Event3
}

void main()
{
    var S1 = new BasicState<Events>("S1");
    var S2 = new BasicState<Events>("S2");

    var transitions = new[]
    {
        new StateTransition<Events>{from = S1, to = S2, evtFunc = x => x == Events.Event1, guard = null },
        new StateTransition<Events>{from = S2, to = S1, evtFunc = x => x == Events.Event2, guard = null },
    };

    StateMachine<Events> sm = new StateMachine<Events>(transitions, S1);

    sm.Event(Events.Event1);

    Assert.AreEqual(sm.ActiveState, S2);
}

The class BasicState allows you to use a generic state thingy, that does not do anything special. If you need to actually do something within the states, you should implement IState as so:

class PrintingState: IState<char>
{
  public void OnEnter(char c)
  {
    // Called whenever the state is entered, with the value of the
    // event that caused the statetransition
  }

  public void OnLeave(char c)
  {
    // Called whenever the state is left, with the value of the
    // event that caused the statetransition
  }
}

Note, that in the previous example, we implemented IState for events of type char. You can use any type as an event as long as it is comparable.

For a more elaborate example, of what can be done with little code check SSMParseProtocol.cs

microstatemachine's People

Contributors

rincewound avatar

Watchers

 avatar

Forkers

bubdm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.