Header menu logo TDesu.FSharp

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
type State = | Idle | Running
Multiple items
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>
type Effect = | Log of string
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val b: obj
union case State.Idle: State
union case State.Running: State
union case Event.Start: Event
union case Event.Stop: Event
union case Effect.Log: string -> Effect
val machine: obj
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
val r: obj
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val msg: string
val failwith: message: string -> 'T

Types

Type Description

Builder<'TState, 'TEvent, 'TEffect>

Declarative builder for constructing state machine definitions.

Definition<'TState, 'TEvent, 'TEffect>

Compiled state machine definition.

TransitionResult<'TState, 'TEffect>

Result of a state machine transition.

Functions and values

Function or value Description

StateMachine.apply def state event

Full Usage: StateMachine.apply def state event

Parameters:
    def : 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.

def : Definition<'TState, 'TEvent, 'TEffect>
state : 'TState
event : 'TEvent
Returns: Result<TransitionResult<'TState, 'TEffect>, string>

StateMachine.fail msg

Full Usage: StateMachine.fail msg

Parameters:
    msg : string

Returns: Result<TransitionResult<'TState, 'TEffect>, string>
Modifiers: inline
Type parameters: 'TState, 'TEffect

Fail the transition with an error message. The error message.

msg : string
Returns: Result<TransitionResult<'TState, 'TEffect>, string>

StateMachine.goto newState effects

Full Usage: StateMachine.goto newState effects

Parameters:
    newState : '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.

newState : 'a
effects : 'b list
Returns: Result<TransitionResult<'a, 'b>, 'c>

StateMachine.stay state effects

Full Usage: StateMachine.stay state effects

Parameters:
    state : '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 [] for no effects). The current state to remain in. Side effects to produce.

state : 'a
effects : 'b list
Returns: Result<TransitionResult<'a, 'b>, 'c>

StateMachine.tryApply def state event

Full Usage: StateMachine.tryApply def state event

Parameters:
    def : 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.

def : Definition<'TState, 'TEvent, 'TEffect>
state : 'TState
event : 'TEvent
Returns: 'TState * Result<'TEffect list, string>

Type something to start searching.