StateMachine Module
Generic finite state machine: state + event → new state + effects.
Example
type State = Idle | Running
type Event = Start | Stop
type Effect = Log of string
let b = StateMachine.Builder<State, Event, Effect>()
b.StateTag(fun s -> match s with Idle -> 0 | Running -> 1)
b.EventTag(fun e -> match e with Start -> 0 | Stop -> 1)
b.On(0, 0, fun _ _ -> StateMachine.goto Running [ Log "started" ])
b.On(1, 1, fun _ _ -> StateMachine.goto Idle [ Log "stopped" ])
let machine = b.Build()
match StateMachine.apply machine Idle Start with
| Ok r -> r.NewState, r.Effects
| Error msg -> failwith msg
module Event from Microsoft.FSharp.Control
--------------------
type Event = | Start | Stop
--------------------
type Event<'T> = new: unit -> Event<'T> member Trigger: arg: 'T -> unit member Publish: IEvent<'T> with get
--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate and reference type)> = new: unit -> Event<'Delegate,'Args> member Trigger: sender: objnull * args: 'Args -> unit member Publish: IEvent<'Delegate,'Args> with get
--------------------
new: unit -> Event<'T>
--------------------
new: unit -> Event<'Delegate,'Args>
val string: value: 'T -> string
--------------------
type string = System.String
Types
| Type | Description |
|
Declarative builder for constructing state machine definitions. |
|
|
Compiled state machine definition. |
|
|
Result of a state machine transition. |
Functions and values
| Function or value |
Description
|
Full Usage:
StateMachine.apply def state event
Parameters:
Definition<'TState, 'TEvent, 'TEffect>
state : 'TState
event : 'TEvent
Returns: Result<TransitionResult<'TState, 'TEffect>, string>
|
Apply a transition: look up handler by (stateTag, eventTag) and execute it. The state machine definition. The current state. The event to process.
|
Full Usage:
StateMachine.fail msg
Parameters:
string
Returns: Result<TransitionResult<'TState, 'TEffect>, string>
Modifiers: inline Type parameters: 'TState, 'TEffect |
Fail the transition with an error message. The error message.
|
Full Usage:
StateMachine.goto newState effects
Parameters:
'a
effects : 'b list
Returns: Result<TransitionResult<'a, 'b>, 'c>
Modifiers: inline Type parameters: 'a, 'b, 'c |
Create a successful transition to a new state with effects. The state to transition to. Side effects to produce.
|
Full Usage:
StateMachine.stay state effects
Parameters:
'a
effects : 'b list
Returns: Result<TransitionResult<'a, 'b>, 'c>
Modifiers: inline Type parameters: 'a, 'b, 'c |
Stay in the current state, producing effects (pass
|
Full Usage:
StateMachine.tryApply def state event
Parameters:
Definition<'TState, 'TEvent, 'TEffect>
state : 'TState
event : 'TEvent
Returns: 'TState * Result<'TEffect list, string>
|
Apply transition. On error, state is unchanged. Returns (newState, Ok effects) or (unchangedState, Error msg). The state machine definition. The current state. The event to process.
|
TDesu.FSharp