fin-ger / declarative-state-machine Goto Github PK
View Code? Open in Web Editor NEWAn approach to create state machines in rust in a declarative way
License: GNU General Public License v3.0
An approach to create state machines in rust in a declarative way
License: GNU General Public License v3.0
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;
}
}
}
Please add comments on this proposal
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.