-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.go
43 lines (38 loc) · 1.39 KB
/
fsm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package fsm
import (
"golang.org/x/exp/constraints"
)
type Diagram interface {
// Visualize outputs a visualization of a Fsm in the desired format.
// If the type is not given it defaults to Graphviz
Visualize(t VisualizeType) (string, error)
}
type IFsm[E constraints.Ordered, S constraints.Ordered] interface {
// Clone the Fsm.
Clone() IFsm[E, S]
// CloneNewState clone the Fsm with new state.
CloneNewState(newState S) IFsm[E, S]
// Current returns the current state.
Current() S
// Is returns true if state match the current state.
Is(state S) bool
// SetCurrent allows the user to move to the given state from current state.
SetCurrent(state S)
// Trigger call a state transition with the named event and src state if success will change the current state.
// It will return nil if src state change to dst state success or one of these errors:
//
// - ErrInappropriateEvent: event inappropriate in the src state.
// - ErrNonExistEvent: event does not exist
Trigger(event E) error
// MatchOccur returns true if event can occur in the current state.
MatchCurrentOccur(event E) bool
// MatchAllOccur returns true if all the events can occur in current state.
MatchCurrentAllOccur(event ...E) bool
// AvailEvents returns a list of available transform event in current state.
CurrentAvailEvents() []E
ITransition[E, S]
Diagram
}
type ErrorTranslator interface {
Translate(err error) error
}