Documentation
¶
Index ¶
- type DiagramBuilder
- type Event
- type History
- type State
- func (s *State[E]) AddTransition(eventId int, target *State[E])
- func (s *State[E]) IsLeaf() bool
- func (s *State[E]) Name() string
- func (s *State[E]) State(name string) *StateBuilder[E]
- func (s *State[E]) String() string
- func (s *State[E]) Transition(eventId int, target *State[E]) *TransitionBuilder[E]
- type StateBuilder
- type StateMachine
- type StateMachineInstance
- type TransitionBuilder
- func (tb *TransitionBuilder[E]) Action(name string, f func(Event, E)) *TransitionBuilder[E]
- func (tb *TransitionBuilder[E]) Build()
- func (tb *TransitionBuilder[E]) Guard(name string, f func(Event, E) bool) *TransitionBuilder[E]
- func (tb *TransitionBuilder[E]) History(h History) *TransitionBuilder[E]
- func (tb *TransitionBuilder[E]) Internal() *TransitionBuilder[E]
- func (tb *TransitionBuilder[E]) Local(b bool) *TransitionBuilder[E]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DiagramBuilder ¶
type DiagramBuilder[E any] struct { // contains filtered or unexported fields }
DiagramBuilder allows minor customizations of PlantUML diagram layout before building the diagram. To create a builder, use StateMachine.DiagramBuilder().
func (*DiagramBuilder[E]) Arrow ¶
func (db *DiagramBuilder[E]) Arrow(src, dst *State[E], arrow string) *DiagramBuilder[E]
Arrow specifies the arrow style used for all transitions from src to dst state. See here for available arrow styles: https://crashedmind.github.io/PlantUMLHitchhikersGuide/layout/layout.html
func (*DiagramBuilder[E]) Build ¶
func (db *DiagramBuilder[E]) Build() string
Build creates and returns PlantUML diagram as a string.
func (*DiagramBuilder[E]) DefaultArrow ¶
func (db *DiagramBuilder[E]) DefaultArrow(arrow string) *DiagramBuilder[E]
DefaultArrow changes the arrow style used for transitions. The default is "-->".
type Event ¶
Event instance are delivered to state machine, causing it to run actions and change states. Id identifies type of the event, while Data is an optional arbitrary type containing auxiliary event data.
type State ¶
type State[E any] struct { // contains filtered or unexported fields }
State is a leaf or composite state in a state machine. To create a top-level state in a state machine, use hsm.StateMachine.State method. To create a sub-state of a composite state, use hsm.State.State method. State (and its containing StateMachine) are parameterized by E - the extended state type. E is usually a pointer to a struct containing various quantitative aspects of the object's state, as opposed to the qualitative aspects captured through the state machine's discrete states. If you don't need an extended state, use struct{} for E.
func (*State[E]) AddTransition ¶
AddTransition is a convenience method, equivalent to calling s.Transition(eventId, target).Build().
func (*State[E]) State ¶
func (s *State[E]) State(name string) *StateBuilder[E]
State creates and returns a builder for building a nested sub-state.
func (*State[E]) Transition ¶
func (s *State[E]) Transition(eventId int, target *State[E]) *TransitionBuilder[E]
Transition creates and returns a builder for the transition from the current state into a target state. The transition is triggered by event with the given id. The returned builder can be used to further customize the transition, such as providing action, guard condition, and transition type. To indicate state machine termination, provide nil for target state.
type StateBuilder ¶
type StateBuilder[E any] struct { // contains filtered or unexported fields }
StateBuilder provides Fluent API for building new State.
func (*StateBuilder[E]) Build ¶
func (sb *StateBuilder[E]) Build() *State[E]
Build builds and returns the new state.
func (*StateBuilder[E]) Entry ¶
func (sb *StateBuilder[E]) Entry(name string, f func(Event, E)) *StateBuilder[E]
Entry sets func f as the entry action for the state being built. May be called multiple times to assign multiple entry actions, to be executed in the order of assignment.
func (*StateBuilder[E]) Exit ¶
func (sb *StateBuilder[E]) Exit(name string, f func(Event, E)) *StateBuilder[E]
Exit sets func f as the exit action for the state being built. May be called multiple times to assign multiple exit actions, to be executed in the order of assignment.
func (*StateBuilder[E]) Initial ¶
func (sb *StateBuilder[E]) Initial() *StateBuilder[E]
Initial marks the state being built as initial sub-state of the parent state. In other words, Initial creates an automatic initial transition from the parent state into the new state being built.
type StateMachine ¶
type StateMachine[E any] struct { LocalDefault bool // default for whether transitions should be local // contains filtered or unexported fields }
StateMachine encapsulates the structure of the entire state machine, along with all its contained states, transitions, actions, guards, etc. StateMachine is only about the structure - to create an instance, deliver events to it and drive it through transitions, create StateMachineInstance tied to this StateMachine. Zero value of StateMachine is ready for use. Do not copy a non-zero StateMachine.
func (*StateMachine[E]) DiagramBuilder ¶
func (sm *StateMachine[E]) DiagramBuilder(evNameMapper func(int) string) *DiagramBuilder[E]
DiagramBuilder creates builder for customizing PlantUML diagram before building it. evNameMapper provides mapping of event ids to event names.
func (*StateMachine[E]) DiagramPUML ¶
func (sm *StateMachine[E]) DiagramPUML(evNameMapper func(int) string) string
DiagramPUML builds a PlantUML diagram of a finalized state machine. This method is a shorthand for sm.DiagramBuilder(evNameMapper).Build().
func (*StateMachine[E]) Finalize ¶
func (sm *StateMachine[E]) Finalize()
Finalize validates and finalizes the state machine structure. Finalize must be called before any state machine instances are initialized, and state machine structure must not be modified after this method is called.
func (*StateMachine[E]) State ¶
func (sm *StateMachine[E]) State(name string) *StateBuilder[E]
State starts a builder for a top-level state in a state machine. There must be at least one top-level state in a state machine, and exactly one of those must be marked as initial state.
type StateMachineInstance ¶
type StateMachineInstance[E any] struct { SM *StateMachine[E] Ext E // contains filtered or unexported fields }
StateMachineInstance is an instance of a particular StateMachine. StateMachineInstance receives events and goes through state transitions, executing actions along the way. Each StateMachineInstance should have its own, independent extended state, whose type is parameterized by E. Before using an instance, you must set the SM field to assign the instance to a finalized StateMachine. Prior to delivering any events to the instance, you must Initialize() it.
func (*StateMachineInstance[E]) Current ¶
func (smi *StateMachineInstance[E]) Current() *State[E]
Current returns current (leaf) state, or nil if state machine has terminated. This method should not be invoked while state machine is processing an event. In other words, the result is not well-defined if invoked from within state entry/exit actions, transition actions, or transition guards.
func (*StateMachineInstance[E]) Deliver ¶
func (smi *StateMachineInstance[E]) Deliver(e Event) (handled bool, src *State[E])
Deliver an event to the state machine, returning whether the event was handled, and in which state. Any applicable transitions and actions will be completed before the method returns. This method is not reentrant - do not invoke it from within transition actions, state entry/exit functions, or transition guard functions. If transition action needs to generate a new event, arrange for that event to be delivered to the instance only _after_ the current Deliver() method returns.
func (*StateMachineInstance[E]) Initialize ¶
func (smi *StateMachineInstance[E]) Initialize(e Event)
Initialize initializes this instance. Before this method returns, state machine will enter its initial leaf state, invoking any relevant entry actions. The event e is passed into the entry actions as the initial event, but is otherwise not delivered to state machine.
type TransitionBuilder ¶
type TransitionBuilder[E any] struct { // contains filtered or unexported fields }
TransitionBuilder provides Fluent API for building transition from one state to another. TransitionBuilder allows specifying a guard condition that must be true for the transition to take place, an action to take when making the transition, and a type of transition (external, internal, local).
func (*TransitionBuilder[E]) Action ¶
func (tb *TransitionBuilder[E]) Action(name string, f func(Event, E)) *TransitionBuilder[E]
Action specifies the transition action name and function. The transition action is invoked after any applicable state exit functions, and before any applicable state entry functions. Action name need not be unique, and is only used for state machine diagram generation. This method may be called multiple times to assign multiple actions to the same transition, to be executed in the order in which they were defined.
func (*TransitionBuilder[E]) Build ¶
func (tb *TransitionBuilder[E]) Build()
Build completes building the transition
func (*TransitionBuilder[E]) Guard ¶
func (tb *TransitionBuilder[E]) Guard(name string, f func(Event, E) bool) *TransitionBuilder[E]
Guard specifies the guard condition - a function that must return true for the transition to take place. Guard name need not be unique, and is only used for state machine diagram generation.
func (*TransitionBuilder[E]) History ¶
func (tb *TransitionBuilder[E]) History(h History) *TransitionBuilder[E]
History specifies that transition shall occur into the (shallow or deep) history of the target composite state. In case the system has not yet visited the composite state, the transition will proceed into the composite state's initial sub-state.
func (*TransitionBuilder[E]) Internal ¶
func (tb *TransitionBuilder[E]) Internal() *TransitionBuilder[E]
Internal specifies that transition should be treated as an internal transition, as opposed to the default external transition. This can only be specified for self-transitions - i.e. target state must be the same as the source state, or other the method will panic. Internal transitions differ from external transitions in that no entry or exit functions are invoked. Internal transitions specified in composite-states will be inherited by all the sub-states, unless explicitly overriden.
func (*TransitionBuilder[E]) Local ¶
func (tb *TransitionBuilder[E]) Local(b bool) *TransitionBuilder[E]
Local specifies whether the transition should be treated as local or external, overriding the default for the state machine. This can only be specified for transitions between composite state and one of its (direct or transitive) sub-states, because the concept of local transitions does not make sense otherwise. Local transitions differ from the external ones in that they do not feature exit and re-entry from the parent (composite) state.