Code Monkey home page Code Monkey logo

fsm2's Introduction

FSM2 provides an implementation of the core design aspects of the UML2 state diagrams.

FMS2 supports:

  • Nested States
  • Concurrent Regions
  • Guard Conditions
  • sideEffects
  • onEnter/onExit
  • streams
  • static analysis tools
  • visualisation tools.

Overview

FSM2 uses a builder to delcare each state machine.

Your application may declare as many statemachines as necessary.

Example

import 'package:fsm2/fsm2.dart';

void main() {
  final machine = StateMachine.create((g) => g
    ..initialState(Solid())
    ..state<Solid>((b) => b
      ..on<OnMelted, Liquid>(sideEffect: (e) async => print("I'm melting"))
    ..state<Liquid>((b) {})
 ));

The above examples creates a Finite State Machine (machine) which declares its initial state as being Solid and then declares a single transition which occurs when the event OnMelted event is triggered causing a transition to a new state Liquid.

To trigger an event:

machine.applyEvent(OnMelted());

Documentation

Full documentation is available on gitbooks at:

https://fsm2.onepub.dev/

Example 2

A simple example showing the life cycle of H2O.

import 'package:fsm2/fsm2.dart';

void main() {
  final machine = StateMachine.create((g) => g
    ..initialState(Solid())
    ..state<Solid>((b) => b
      ..on<OnMelted, Liquid>(sideEffect: (e) => print('Melted'),
          )))
    ..state<Liquid>((b) => b
      ..onEnter((s, e) => print('Entering ${s.runtimeType} State'))
      ..onExit((s, e) => print('Exiting ${s.runtimeType} State'))
      ..on<OnFroze, Solid>(sideEffect: (e) => print('Frozen')))
      ..on<OnVaporized, Gas>(sideEffect: (e) => print('Vaporized'))))
    ..state<Gas>((b) => b
      ..on<OnCondensed, Liquid>(sideEffect: (e) => print('Condensed'))))
    ..onTransition((t) => print(
        'Received Event ${t.event.runtimeType} in State ${t.fromState.runtimeType} transitioning to State ${t.toState.runtimeType}')));

  await machine.complete;

  ///   machine.isInState<Hard>()

  print(machine.isInSt<Solid>); // TRUE

  machine.transition(OnMelted());
  print(machine.isInState<Liquid>); // TRUE

  machine.transition(OnFroze());
  print(machine.isInState<Solid>); // TRUE
}

Credits:

FMS2 is derived from the FSM library which in turn was inspired by Tinder StateMachine library.

fsm2's People

Contributors

bsutton avatar ookami-kb avatar vaetas 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.