fi-night
Finite State Machine
Finite state machine can be considered a transformation from 2D space of {S, E} to {S} where S and E are sets associated with States and Events respectively. Rust provides syntax that covers this pattern:
let next_state =
match ;
From a programmer's perspetive an FSM could be seen as a black box which reacts on Events from the outer, from the machine's perspective, world, depending on its actual State.
These "reactions" or transitions are what is expected to be programmed once the machine template is defined.
This crate presents procedural macros as a trial to provide some sugar to this pattern.
As there is no dynamic data manipulation involved except user defined state context, each transition is supposed to be as fast as two method calls.
Usage
To use it include this in your code:
extern crate fsm;
Introduction
The main macro that defines the machine's type looks like so:
fsm!;
which is equivalent to more compact chained notation:
fsm!;
From this declaration the macro will deduce the following:
I called this machine Useless for a reason: it receives scarse information from the outer world and is unable to produce coherent reactions because no user data is associated with its state.
The only way to obtain something useful from such a machine is by repeating matching its state.
Practical Usage
Here is how a machine definition can be expanded:
// user data associated with state
// this will count letters only, discriminating A & B from the rest
// the rest of the states is introduced just for presentation purpose
fsm!;
A struct LetterCounter with associated type Count is generated. Every individual signal can carry input typed with type in parentheses. (note the detail here: once Other(char) is declared, there is no need to repeat (char) in declaration - will panic on type clash).
Now, this is not going to compile as we lack the essence - transitions, so here the second part of our declaration comes - define a method for every arrow:
// this reads: do sth when Start signal received while in Idle state
transition_handler!;
// this reads: do sth when LetterA signal received no matter in which actual state
from_any_state_transition_handler!;
from_any_state_transition_handler!;
from_any_state_transition_handler!;
transition_handler!;
from_any_state_transition_handler!;
from_any_state_transition_handler!;
from_any_state_transition_handler!;
These macros hide two references: a mutable one to data associated with state exposed by 'data', and another immutable 'input' delivered with the signal.
As LetterCounter is a struct, you can expand its API if you want to:
LetterCounter::signal(...) method encapsulates the transition matcher.
from_any_state_transition_handler!
is alternative for equivalent notation:
transition_handler!
Now define the instance:
let mut counter = new;
And use it:
counter.start;
counter.signal;
counter.signal;
counter.signal;
counter.signal;
counter.signal;
counter.signal;
counter.signal;
let n = counter.data;
println!;
One can notice that in this example no code directly refers to current fsm's state (although it can be accessed via state() method).
I consider current state an implementation detail of machine's black box which does not provide sufficient information about past, namely it does not make much sense to know what current state is out of context of some transition.
Conditional Transitions
There is another way to break the black box though.
Consider that this silly counter needs to distinguish the situation of more than 5 B letters counted entering a specific state, call it MoreThan5B.
One could define it this way:
Started on LetterB => IncB_1 on LetterB => IncB_2 on LetterB... and so forth.
There is an alternative conditional pattern for this scenario :
fsm!;
This interrogation sign declares that the LetterCounter delegates the determination of resulting state to the transition handler itself.
And here is where this macro jumps out:
conditional_handler!;
Now, this won't compile, the machine doesn't know what IncB and MoreThan5B are, so let's add some silly transitions to our machine:
fsm!;
and their corresponding handlers:
transition_handler!;
transition_handler!;
No failure on no transition
This design defines a relaxed machine in sense that it won't change state on signal no transition for which exists for current state.
This means this scenario is not covered:
fsm!;
Contrary to the above General Failure or Reset scenarios are quite common:
fsm!;
meta_iter Feature
The crate defines a feature "meta_iter" which, when is on brings a machine's iterator into scope.
Note that there is no dynamic collection to iterate on, this iterator operates on metadata resulting from waste product of declarations of machine's enums.
This feature brings a bit of text to the code and is only useful for enumerating transitions while debugging, for instance.
The result could be then sent to a graph visualization software or just printed out.
It works like so:
let mut chains = chains;
while let Some = chains.next
The result of printout will be a list machine's transitions. Note that there is almost no chance that the list printout coincide with yours machine layout view in source code.
Caveats
Lifetime parsing for user data type is not covered, so this won't compile:
fsm!;