GithubHelp home page GithubHelp logo

gfsm's Introduction

GFSM

A Generic Finite State Machine in C#

Implementation is easy:

// Entirely unnecessary, just here to implement a common DoWork()
public abstract class MyStateBase : IState {
  public virtual void Enter() {}
  public abstract void DoWork();
  public virtual void Leave() {}
}

// Define a state and it's transitions
[Transition("next", typeof(EndState))]
public class StartState : MyStateBase {
  public override void DoWork() => Console.WriteLine("In StartState");
  public override void Leave() => Console.WriteLine("\tLeaving StartState");
}

[Transition("next", null)]
public class EndState : MyStateBase {
  public override void Enter() => Console.WriteLine("\tEntered EndState");
  public override void DoWork() => Console.WriteLine("In EndState");
  public override void Leave() => Console.WriteLine("\tLeaving EndState");
}

internal class Program {
  private static void Main() {
    var fsm = new FiniteStateMachine<StartState>();
    
    fsm.Transitioning += transition => Console.WriteLine($"Beginning transition: {transition}");
    fsm.Transitioned += transition => {
      Console.WriteLine($"Done transitioning: {transition}");
      if (transition.To == null)
        Console.WriteLine("\nExited");
    };

    Console.WriteLine("Started\n");
    fsm.GetCurrentState<MyStateBase>().DoWork();
    fsm.DoTransition("next");
    fsm.GetCurrentState<MyStateBase>().DoWork();
    fsm.DoTransition("next");

    Console.ReadLine();
  }
}
/*
Will print:
Started

In StartState
Beginning transition: DemoApp.StartState + 'next' = DemoApp.EndState
    Leaving StartState
    Entered EndState
Done transitioning: DemoApp.StartState + 'next' = DemoApp.EndState
In EndState
Beginning transition: DemoApp.EndState + 'next' = null
    Leaving EndState
Done transitioning: DemoApp.EndState + 'next' = null

Exited
*/

gfsm's People

Contributors

sidneys1 avatar

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.