forked from ryanfaerman/fsm
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfsm.go
More file actions
132 lines (107 loc) · 3.08 KB
/
fsm.go
File metadata and controls
132 lines (107 loc) · 3.08 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package fsm
import (
"errors"
"fmt"
)
// Guard provides protection against transitioning to the goal State.
// Returning an error if the transition is not permitted
type Guard func(start *State, goal *State) error
const (
errTransitionFormat = "Cannot transition from %s to %s"
errNoRulesFormat = "No rules found for %s to %s"
errGuardFailedFormat = "Guard failed from %s to %s: %s"
)
var (
// ErrInvalidTransition describes the errors when doing an invalid transition
ErrInvalidTransition = errors.New("invalid transition")
)
// Transition is the change between States
type Transition interface {
Origin() ID
Exit() ID
}
// T implements the Transition interface; it provides a default
// implementation of a Transition. It should only be instantiated
// with NewTransition.
type T struct {
O, E ID
}
// Origin returns the starting state
func (t T) Origin() ID { return t.O }
// Exit returns the ending state
func (t T) Exit() ID { return t.E }
// NewTransition let's you create a new transition and apply some rules
func NewTransition(i1 IDer, i2 IDer) T {
return T{
O: i1.ID(),
E: i2.ID(),
}
}
// Ruleset stores the rules for the state machine.
type Ruleset map[ID][]Guard
// AddRule adds Guards for the given Transition
func (r Ruleset) AddRule(t Transition, guards ...Guard) {
for _, guard := range guards {
r[t] = append(r[t], guard)
}
}
// AddTransition adds a transition with a default rule
func (r Ruleset) AddTransition(t Transition) {
r.AddRule(t, func(start *State, goal *State) error {
if start.ID() != t.Origin() {
return fmt.Errorf(errTransitionFormat, start.ID(), goal.ID())
}
return nil
})
}
// CreateRuleset will establish a ruleset with the provided transitions.
// This eases initialization when storing within another structure.
func CreateRuleset(transitions ...Transition) Ruleset {
r := Ruleset{}
for _, t := range transitions {
r.AddTransition(t)
}
return r
}
// Permitted determines if a transition is allowed.
// This occurs in parallel.
// NOTE: Guards are not halted if they are short-circuited for some
// transition. They may continue running *after* the outcome is determined.
func (r Ruleset) Permitted(start *State, goal *State) error {
attempt := T{start.ID(), goal.ID()}
if guards, ok := r[attempt]; ok {
for _, guard := range guards {
err := guard(start, goal)
if err != nil {
return fmt.Errorf(errGuardFailedFormat, start.ID(), goal.ID(), err.Error())
}
start.id = start.ID()
goal.id = goal.ID()
}
return nil
}
return fmt.Errorf(errNoRulesFormat, start.ID(), goal.ID())
}
// Machine is a pairing of Rules and a State.
// The state or rules may be changed at any time within
// the machine's lifecycle.
type Machine struct {
Rules *Ruleset
State State
}
// Transition attempts to move the Subject to the Goal state.
func (m *Machine) Transition(goal State) (err error) {
if err = m.Rules.Permitted(&m.State, &goal); err == nil {
m.State = goal
return nil
}
return err
}
// New initializes a machine
func New(opts ...func(*Machine)) Machine {
var m Machine
for _, opt := range opts {
opt(&m)
}
return m
}