GithubHelp home page GithubHelp logo

simple_finite_state_machine's Introduction

A Simple Finite State Machine

Features

  • well, its simple to use

Getting started

# add the dependencies
simple_finite_state_machine:
  git: https://github.com/raj457036/simple_finite_state_machine.git

Usage

Example of state if water

credit: https://github.com/Tinder/StateMachine

Below is a simple example based on above diagram

// states
class Solid extends MachineState<Solid> {
  @override
  get transitions => [Melt];
}

class Liquid extends MachineState<Liquid> {
  @override
  get transitions => [Vaporize, Freeze];
}

class Gas extends MachineState<Gas> {
  @override
  get transitions => [Condense];
}

// transitions
class Melt extends StateTransition<Solid> {
  @override
  MachineState? target(current) {
    return Liquid();
  }
}

class Vaporize extends StateTransition<Liquid> {
  @override
  MachineState? target(current) {
    return Gas();
  }
}

class Condense extends StateTransition<Gas> {
  @override
  MachineState? target(current) {
    return Liquid();
  }
}

class Freeze extends StateTransition<Liquid> {
  @override
  MachineState? target(current) {
    return Solid();
  }
}

// main program
void main(List<String> args) {
  final machine = Machine(Solid(), logging: true);

  machine.execute(Melt());
  machine.execute(Vaporize());
  machine.execute(Condense());
  machine.execute(Freeze());
  // This one will fail
  machine.execute(Condense());
}

Additional information

The example above is from Tinder State Machine Example

simple_finite_state_machine's People

Contributors

raj457036 avatar

Watchers

James Cloos avatar  avatar  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.