tree_state_machine 3.0.0-dev.8 copy "tree_state_machine: ^3.0.0-dev.8" to clipboard
tree_state_machine: ^3.0.0-dev.8 copied to clipboard

Define and run hierarchical state machines in Dart and Flutter.

example/main.dart

import 'package:logging/logging.dart';
import 'package:tree_state_machine/tree_state_machine.dart';
import 'package:tree_state_machine/declarative_builders.dart';

// A simple stoplight that transitions between green/yellow/red when it is
//in the running state.
//
// See other examples at
// https://github.com/jlewicki/tree_state_machine/tree/master/example

class States {
  static final root = StateKey('root');

  static final running = StateKey('running');
  static final green = StateKey('green');
  static final yellow = StateKey('yellow');
  static final red = StateKey('red');
  static final stopped = StateKey('stopped');
}

enum Messages { timeout, stop, start }

DeclarativeStateTreeBuilder stoplightStateTree() {
  final greenTimeout = Duration(seconds: 5);
  final yellowTimeout = Duration(seconds: 2);
  final redTimeout = Duration(seconds: 5);

  var treeBuilder = DeclarativeStateTreeBuilder(initialChild: States.stopped);

  treeBuilder.state(States.running, (b) {
    b.onMessageValue(Messages.stop, (b) => b.goTo(States.stopped));
  }, initialChild: InitialChild(States.green));

  treeBuilder.state(States.green, (b) {
    b.onEnter(
        (b) => b.schedule(message: Messages.timeout, duration: greenTimeout));
    b.onMessageValue(Messages.timeout, (b) => b.goTo(States.yellow));
  }, parent: States.running);

  treeBuilder.state(States.yellow, (b) {
    b.onEnter(
        (b) => b.schedule(message: Messages.timeout, duration: yellowTimeout));
    b.onMessageValue(Messages.timeout, (b) => b.goTo(States.red));
  }, parent: States.running);

  treeBuilder.state(States.red, (b) {
    b.onEnter(
        (b) => b.schedule(message: Messages.timeout, duration: redTimeout));
    b.onMessageValue(Messages.timeout, (b) => b.goTo(States.green));
  }, parent: States.running);

  treeBuilder.state(States.stopped, (b) {
    b.onMessageValue(Messages.start, (b) => b.goTo(States.running));
  });

  return treeBuilder;
}

Future<void> main() async {
  initLogging();

  var declBuilder = stoplightStateTree();
  var stateMachine = TreeStateMachine(declBuilder);
  var currentState = await stateMachine.start();

  await currentState.post(Messages.start);
  assert(currentState.key == States.green);

  await Future<void>.delayed(Duration(seconds: 30));

  await currentState.post(Messages.stop);
  assert(currentState.key == States.stopped);

  var sb = StringBuffer();
  declBuilder.format(sb, DotFormatter());
  print(sb.toString());
}

void initLogging() {
  // Capture logging output generated by the state machine
  hierarchicalLoggingEnabled = true;
  Logger('tree_state_machine').level = Level.ALL;
  Logger.root.onRecord.listen((record) {
    print('${record.level.name}: ${record.time}: ${record.message}');
  });
}
4
likes
0
points
132
downloads

Publisher

unverified uploader

Weekly Downloads

Define and run hierarchical state machines in Dart and Flutter.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

async, collection, logging

More

Packages that depend on tree_state_machine