Giter Club home page Giter Club logo

declarative-state-machine's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

bengsparks

declarative-state-machine's Issues

state-machine syntax

Currently transitions cannot be controlled by additional event parameters or by the content of the previous state. As my goal is to build a Mealy (like) Machine, this needs to be changed. The following is a syntax proposal that lets the transitions block define an extra predicate for a transition. This predicate evaluates to a boolean and decides whether this transition can be taken or not. Additionally events and handlers are now decoupled which makes it possible to specify one handler for each transition even when the transitions are controlled by the same event.

state_machine! {
    machine bottle_filler {
        event fill(volume: f32);
        event full();
        event fuel();
        event dump();

        handler fill_bottle(_from: Option<&State>, to: &mut State, volume: f32) {
            if let State::Filling(ref mut filling_volume) = to {
                *filling_volume = volume;
            }
        }

        handler fuel_tank(_from: Option<&State>, to: &mut State) {
            if let State::Idle { ref mut remaining } = to {
                *remaining = 42;
            }
        }

        handler bottle_full(from: Option<&State>, to: &mut State) {
            if let State::Filling(ref volume) = from {
                if let State::Idle { ref mut remaining } = to {
                    *remaining -= volume;
                }
            }
        }

        states {
            Idle {
                remaining: f32,
            },
            Filling(f32),
            Empty,
        }

        transitions {
            // when volume that should be filled by bottle filler is smaller or equal
            // to the remaining capacity of the bottle filler, call the fill_bottle
            // handler.
            Idle { remaining } => Filling : fill(volume) {
                volume <= remaining
            } -> fill_bottle;

            // when volume that should be filled by bottle filler is greater than the
            // remaining capacity of the bottle filler, just go to empty state without
            // calling a handler.
            Idle { remaining } => Empty : fill(volume) {
                volume > remaining
            };

            // when dump event is fired from idle state, go to empty without calling a handler.
            Idle { .. } => Empty : dump();

            // when fuel event is fired from idle state, always call the fuel_tank handler.
            Idle { .. } => Idle : fuel() -> fuel_tank;

            // when full event is fired from filling state, always call bottle_full handler.
            Filling(..) => Idle : full() -> bottle_full;

            // when fuel event is fired from empty state, always call the fuel_tank handler.
            Empty => Idle : fuel() -> fuel_tank;
        }
    }
}

Cons

  • very complex transitions syntax
  • ambiguity for transition selection created by the predicates

Pros

  • not every event needs a handler
  • transitions can be controlled by event parameters
  • transitions can be controlled by data hold in states

Please add comments on this proposal

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.