Documentation ¶
Index ¶
- Constants
- func GenerateUML(w io.Writer, syntaxType SyntaxType, parameters Parameters, ...) error
- func NewFSMHandler(parameters Parameters) (statemachine.StateHandler, error)
- func VerifyEventParameters(state StateType, stateKeyField StateKeyField, events []EventBuilder) error
- func VerifyStateParameters(parameters Parameters) error
- type ActionFunc
- type Context
- type Environment
- type ErrSkipHandler
- type EventBuilder
- type EventName
- type EventNameMap
- type EventProcessor
- type Events
- type Group
- type Notifier
- type Parameters
- type StateEntryFunc
- type StateEntryFuncs
- type StateKey
- type StateKeyField
- type StateNameMap
- type StateType
- type StoredState
- type SyntaxType
- type TransitionMap
- type TransitionToBuilder
Constants ¶
const NotificationQueueSize = 128
Variables ¶
This section is empty.
Functions ¶
func GenerateUML ¶
func GenerateUML(w io.Writer, syntaxType SyntaxType, parameters Parameters, stateNameMap StateNameMap, eventNameMap EventNameMap, startStates []StateKey, includeFromAny bool, stateCmp func(a, b StateKey) bool) error
GenerateUML genderates a UML state diagram (in Mermaid/PlantUML syntax) for a given FSM
func NewFSMHandler ¶
func NewFSMHandler(parameters Parameters) (statemachine.StateHandler, error)
NewFSMHandler defines an StateHandler for go-statemachine that implements a traditional Finite State Machine model -- transitions, start states, end states, and callbacks
func VerifyEventParameters ¶
func VerifyEventParameters(state StateType, stateKeyField StateKeyField, events []EventBuilder) error
func VerifyStateParameters ¶
func VerifyStateParameters(parameters Parameters) error
VerifyStateParameters verifies if the Parameters for an FSM specification are sound
Types ¶
type ActionFunc ¶
type ActionFunc interface{}
ActionFunc modifies the state further in addition to modifying the state key. It the signature func action<StateType, T extends any[]>(s stateType, args ...T) and then an event can be dispatched on context or group with the form .Event(Name, args ...T)
type Context ¶
type Context interface { // Context returns the golang context for this context Context() context.Context // Trigger initiates a state transition with the named event. // // The call takes a variable number of arguments that will be passed to the // callback, if defined. // // It will return nil if the event is one of these errors: // // - event X does not exist // // - arguments don't match expected transition Trigger(event EventName, args ...interface{}) error }
Context provides access to the statemachine inside of a state handler
type Environment ¶
type Environment interface{}
Environment are externals dependencies will be needed by this particular state machine
type ErrSkipHandler ¶
type ErrSkipHandler struct{}
ErrSkipHandler is a sentinel type that indicates not an error but that we should skip any state handlers present
func (ErrSkipHandler) Error ¶
func (e ErrSkipHandler) Error() string
type EventBuilder ¶
type EventBuilder interface { // From begins describing a transition from a specific state From(s StateKey) TransitionToBuilder // FromAny begins describing a transition from any state FromAny() TransitionToBuilder // FromMany begins describing a transition from many states FromMany(sources ...StateKey) TransitionToBuilder // Action describes actions taken on the state for this event Action(action ActionFunc) EventBuilder }
EventBuilder is an interface for describing events in an fsm and their associated transitions
type EventNameMap ¶
type EventNameMap interface{}
EventNameMap maps an event name to a human readable string
type EventProcessor ¶
type EventProcessor interface { // Event generates an event that can be dispatched to go-statemachine from the given event name and context args Generate(ctx context.Context, event EventName, returnChannel chan error, args ...interface{}) (interface{}, error) // Apply applies the given event from go-statemachine to the given state, based on transition rules Apply(evt statemachine.Event, user interface{}) (EventName, error) // ClearEvents clears out events that are synchronous with the given error message ClearEvents(evts []statemachine.Event, err error) }
EventProcessor creates and applies events for go-statemachine based on the given event list
func NewEventProcessor ¶
func NewEventProcessor(state StateType, stateKeyField StateKeyField, events []EventBuilder) (EventProcessor, error)
NewEventProcessor returns a new event machine for the given state and event list
type Events ¶
type Events []EventBuilder
Events is a list of the different events that can happen in a state machine, described by EventBuilders
type Group ¶
type Group interface { // Begin initiates tracking with a specific value for a given identifier Begin(id interface{}, userState interface{}) error // Send sends the given event name and parameters to the state specified by id // it will error if there are underlying state store errors or if the parameters // do not match what is expected for the event name Send(id interface{}, name EventName, args ...interface{}) (err error) // SendSync will block until the given event is actually processed, and // will return an error if the transition was not possible given the current // state SendSync(ctx context.Context, id interface{}, name EventName, args ...interface{}) (err error) // Get gets state for a single state machine Get(id interface{}) StoredState // GetSync will make sure all events present at the time of the call are processed before // returning a value, which is read into out GetSync(ctx context.Context, id interface{}, value cbg.CBORUnmarshaler) error // Has indicates whether there is data for the given state machine Has(id interface{}) (bool, error) // List outputs states of all state machines in this group // out: *[]StateT List(out interface{}) error // IsTerminated returns true if a StateType is in a FinalityState IsTerminated(out StateType) bool // Stop stops all state machines in this group Stop(ctx context.Context) error }
Group is a manager of a group of states that follows finite state machine logic
func New ¶
func New(ds datastore.Datastore, parameters Parameters) (Group, error)
New generates a new state group that operates like a finite state machine, based on the given parameters ds: data store where state comes from parameters: finite state machine parameters
type Notifier ¶
Notifier should be a function that takes two parameters, a native event type and a statetype -- nil means no notification it is called after every successful state transition with the even that triggered it
type Parameters ¶
type Parameters struct { // Environment is the environment in which the state handlers operate -- // used to connect to outside dependencies Environment Environment // StateType is the type of state being tracked. Should be a zero value of the state struct, in // non-pointer form StateType StateType // StateKeyField is the field in the state struct that will be used to uniquely identify the current state StateKeyField StateKeyField // Events is the list of events that that can be dispatched to the state machine to initiate transitions. // See EventDesc for event properties Events []EventBuilder // StateEntryFuncs - functions that will get called each time the machine enters a particular // state. this is a map of state key -> handler. StateEntryFuncs StateEntryFuncs // Notifier is a function that gets called on every successful event processing // with the event name and the new state Notifier Notifier // FinalityStates are states in which the statemachine will shut down, // stop calling handlers and stop processing events FinalityStates []StateKey }
Parameters are the parameters that define a finite state machine
type StateEntryFunc ¶
type StateEntryFunc interface{}
StateEntryFunc is called upon entering a state after all events are processed. It should have the signature func stateEntryFunc<StateType, Environment>(ctx Context, environment Environment, state StateType) error
type StateEntryFuncs ¶
type StateEntryFuncs map[StateKey]StateEntryFunc
StateEntryFuncs is a map between states and their handlers
type StateKey ¶
type StateKey interface{}
StateKey is a value for the field in the state that represents its key that uniquely identifies the state in practice it must have the same type as the field in the state struct that is designated the state key and must be comparable
type StateKeyField ¶
type StateKeyField string
StateKeyField is the name of a field in a state struct that serves as the key by which the current state is identified
type StateNameMap ¶
type StateNameMap interface{}
StateNameMap maps a state type to a human readable string
type StateType ¶
type StateType interface{}
StateType is a type for a state, represented by an empty concrete value for a state
type StoredState ¶
type StoredState interface { End() error Get(out cbg.CBORUnmarshaler) error Mutate(mutator interface{}) error }
StoredState is an abstraction for the stored state returned by the statestore
type SyntaxType ¶
type SyntaxType uint64
SyntaxType specifies what kind of UML syntax we're generating
const ( PlantUML SyntaxType = iota MermaidUML )
type TransitionMap ¶
TransitionMap is a map from src state to destination state
type TransitionToBuilder ¶
type TransitionToBuilder interface { // To means the transition ends in the given state To(StateKey) EventBuilder // ToNoChange means a transition ends in the same state it started in (just retriggers state cb) ToNoChange() EventBuilder // ToJustRecord means a transition ends in the same state it started in (and DOES NOT retrigger state cb) ToJustRecord() EventBuilder }
TransitionToBuilder sets the destination of a transition