fsm 0.0.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

A library for finite state machine realization in Dart. Inspired by Tinder StateMachine library.

Usage #

A simple usage example (using dfunc library for generating sealed classes):

import 'package:dfunc/dfunc.dart';
import 'package:fsm/fsm.dart';

part 'fsm_example.g.dart';

void main() {
  final machine = StateMachine<State, Event, SideEffect>.create((g) => g
    ..initialState(Solid())
    ..state<Solid>((b) => b
      ..on<OnMelted>(
          (Solid s, OnMelted e) => b.transitionTo(Liquid(), LogMelted())))
    ..state<Liquid>((b) => b
      ..onEnter((s) => print('Entering ${s.runtimeType} state'))
      ..onExit((s) => print('Exiting ${s.runtimeType} state'))
      ..on<OnFroze>(
          (Liquid s, OnFroze e) => b.transitionTo(Solid(), LogFrozen()))
      ..on<OnVaporized>(
          (Liquid s, OnVaporized e) => b.transitionTo(Gas(), LogVaporized())))
    ..state<Gas>((b) => b
      ..on<OnCondensed>(
          (Gas s, OnCondensed e) => b.transitionTo(Liquid(), LogCondensed())))
    ..onTransition((t) => t.match((v) => print(v.sideEffect), (_) {})));

  print(machine.currentState is Solid); // TRUE

  machine.transition(OnMelted());
  print(machine.currentState is Liquid); // TRUE

  machine.transition(OnFroze());
  print(machine.currentState is Solid); // TRUE
}

@sealed
abstract class State with _$State {}

class Solid extends State {}

class Liquid extends State {}

class Gas extends State {}

@sealed
abstract class Event with _$Event {}

class OnMelted extends Event {}

class OnFroze extends Event {}

class OnVaporized extends Event {}

class OnCondensed extends Event {}

@sealed
abstract class SideEffect with _$SideEffect {}

class LogMelted extends SideEffect {}

class LogFrozen extends SideEffect {}

class LogVaporized extends SideEffect {}

class LogCondensed extends SideEffect {}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.0.5 #

  • Introduced stronger typing in builder methods.
  • Added onEnter and onExits state listeners.

0.0.4 #

  • Restructured code.
  • Updated documentation.

0.0.3 #

  • Updated example.

0.0.2 #

  • Updated README.

0.0.1 #

  • Initial version.

example/lib/fsm_example.dart

import 'package:dfunc/dfunc.dart';
import 'package:fsm/fsm.dart';

part 'fsm_example.g.dart';

void main() {
  final machine = StateMachine<State, Event, SideEffect>.create((g) => g
    ..initialState(Solid())
    ..state<Solid>((b) => b
      ..on<OnMelted>(
          (Solid s, OnMelted e) => b.transitionTo(Liquid(), LogMelted())))
    ..state<Liquid>((b) => b
      ..onEnter((s) => print('Entering ${s.runtimeType} state'))
      ..onExit((s) => print('Exiting ${s.runtimeType} state'))
      ..on<OnFroze>(
          (Liquid s, OnFroze e) => b.transitionTo(Solid(), LogFrozen()))
      ..on<OnVaporized>(
          (Liquid s, OnVaporized e) => b.transitionTo(Gas(), LogVaporized())))
    ..state<Gas>((b) => b
      ..on<OnCondensed>(
          (Gas s, OnCondensed e) => b.transitionTo(Liquid(), LogCondensed())))
    ..onTransition((t) => t.match((v) => print(v.sideEffect), (_) {})));

  print(machine.currentState is Solid); // TRUE

  machine.transition(OnMelted());
  print(machine.currentState is Liquid); // TRUE

  machine.transition(OnFroze());
  print(machine.currentState is Solid); // TRUE
}

@sealed
abstract class State with _$State {}

class Solid extends State {}

class Liquid extends State {}

class Gas extends State {}

@sealed
abstract class Event with _$Event {}

class OnMelted extends Event {}

class OnFroze extends Event {}

class OnVaporized extends Event {}

class OnCondensed extends Event {}

@sealed
abstract class SideEffect with _$SideEffect {}

class LogMelted extends SideEffect {}

class LogFrozen extends SideEffect {}

class LogVaporized extends SideEffect {}

class LogCondensed extends SideEffect {}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  fsm: ^0.0.5

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:fsm/fsm.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
21
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

We analyzed this package on May 30, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
dfunc >=0.3.1 <0.4.0 0.3.5
Transitive dependencies
matcher 0.12.6
meta 1.1.8
path 1.7.0
quiver 2.1.3
stack_trace 1.9.3
Dev dependencies
build_runner ^1.7.4
mockito ^4.1.1
pedantic ^1.8.0
test ^1.6.0