StateMachine Module
Helpers for building state machines as plain F# match expressions.
Define transitions as let apply state event = match state, event with ...
and use goto/stay/fail to build results.
Example
type State = Idle | Running
type Event = Start | Stop
type Effect = Log of string
let apply state event =
match state, event with
| Idle, Start -> StateMachine.goto Running [ Log "started" ]
| Running, Stop -> StateMachine.goto Idle [ Log "stopped" ]
| _ -> StateMachine.fail "invalid transition"
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 |
|
Result of a state machine transition. |
Functions and values
| Function or value |
Description
|
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 state result
Parameters:
'TState
result : Result<TransitionResult<'TState, 'TEffect>, string>
Returns: 'TState * Result<'TEffect list, string>
|
Apply a transition result, keeping state unchanged on error. Returns (newState, Ok effects) or (unchangedState, Error msg). The current state (returned unchanged on error). The transition result from your apply function.
|
TDesu.FSharp