-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyfsm.go
282 lines (240 loc) · 5.66 KB
/
yfsm.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package yfsm
import (
"database/sql"
"errors"
"fmt"
"sync"
"github.com/gocraft/dbr/v2"
"github.com/gocraft/dbr/v2/dialect"
"github.com/lib/pq"
)
type History interface {
Save()
}
type Storage interface {
Get()
Save()
}
type Machine interface {
ID() int
Name() string
// Check if a type's object is in a proper state to handle event.
Can(id int, event Event) (bool, error)
ToState(fromState int, event Event) (int, error)
Fire(id int, event Event) error
}
type State interface {
ID() int
Machine() int
Name() string
}
// Event describes an event to happen.
type Event interface {
ID() int
Transition() int
Name() string
// Machine() int
// FromState() int
// ToState() int
Identify() bool
}
func EventFromName(name string) Event {
return event{name: name}
}
func EventFromTransition(id int) Event {
return event{transition: id}
}
func EventFromID(id int) Event {
return event{id: id}
}
type event struct {
id int
transition int
machine int
name string
fromState int
toState int
}
func (e event) ID() int {
return e.id
}
func (e event) Transition() int {
return e.transition
}
func (e event) Machine() int {
return e.machine
}
func (e event) Name() string {
return e.name
}
func (e event) FromState() int {
return e.fromState
}
func (e event) ToState() int {
return e.toState
}
// Identify checks if a provided data identifies transition.
// It must be one of:
// - the state_machine_transition_id
// - from_state, state_machine_event_id
func (e event) Identify() bool {
return e.transition > 0 || e.id > 0 && e.fromState > 0
}
type SqlType struct {
db *sql.DB
selectQ, updateQ string
}
func NewSqlType(db *sql.DB, table string, column string) *SqlType {
selectQ := fmt.Sprintf("SELECT %s FROM %s WHERE id=$1",
pq.QuoteIdentifier(column),
pq.QuoteIdentifier(table))
updateQ := fmt.Sprintf("UPDATE %s SET %s=$1 WHERE id=$2 AND %s=$3 RETURNING id",
pq.QuoteIdentifier(table), pq.QuoteIdentifier(column),
pq.QuoteIdentifier(column))
return &SqlType{db: db, selectQ: selectQ, updateQ: updateQ}
}
func (s *SqlType) Get(id int) (int, error) {
err := s.db.QueryRow(s.selectQ, id).Scan(&id)
if err == sql.ErrNoRows {
return 0, ErrCannotFindInstance
}
return id, err
}
type MapType struct {
data map[int]int
m sync.RWMutex
}
func NewMapType() *MapType {
return &MapType{
make(map[int]int), sync.RWMutex{},
}
}
func (m *MapType) Add(id, state int) error {
m.m.Lock()
defer m.m.Unlock()
if _, ok := m.data[id]; ok {
return ErrDuplicate
}
m.data[id] = state
return nil
}
func (m *MapType) Get(id int) (int, error) {
m.m.RLock()
defer m.m.RUnlock()
state, ok := m.data[id]
if !ok {
return 0, ErrCannotFindInstance
}
return state, nil
}
func (m *MapType) Transition(id, fromState, toState int) error {
m.m.Lock()
defer m.m.Unlock()
state, ok := m.data[id]
if !ok {
return ErrCannotFindInstance
}
if state != fromState {
return ErrCannotFindTransition
}
m.data[id] = toState
return nil
}
func (s *SqlType) Transition(id, fromState, toState int) error {
err := s.db.QueryRow(s.updateQ, toState, id, fromState).Scan(&id)
if err == sql.ErrNoRows {
return ErrCannotFindInstance
}
return err
}
type Type interface {
Get(id int) (int, error)
Transition(id, fromState, toState int) error
}
// Loads a given machine for a given type.
func LoadMachineForType(id int, t Type) Machine {
return nil
}
type machine struct {
id int
name string
t Type
db *dbr.Session
}
func (m machine) ID() int {
panic("implement me")
}
func (m machine) Name() string {
panic("implement me")
}
var (
ErrDuplicate = errors.New("duplicate object")
ErrCannotFindInstance = errors.New("cannot find object")
ErrCannotFindTransition = errors.New("cannot find transition")
ErrCannotIdentifyEvent = errors.New("cannot identify event")
)
// Can verifies that event can be handled. It queries database for a transition
// where state matches the resource state and state_machine_id matches
// the machine. Then:
// 1. if transition id is specified, we query for it.
// 2. if event id is specified, we check for event id.
// 3. if event name is specified, we look for transition under such name.
func (m machine) Can(id int, event Event) (bool, error) {
stateID, err := m.t.Get(id)
if err != nil {
return false, err
}
_, err = m.ToState(stateID, event)
if err == ErrCannotFindTransition {
return false, nil
}
return err == nil, err
}
func (m machine) ToState(fromState int, event Event) (int, error) {
q := m.db.Select("to_state_id").
From("state_machine_transition").
Where(dbr.Eq("from_state_id", fromState),
dbr.Eq("state_machine_id", m.id))
var hasCheck bool
if transition := event.Transition(); transition > 0 {
q.Where("id=?", transition)
hasCheck = true
}
if id := event.ID(); id > 0 {
q.Where("state_machine_event_id=?", event.ID())
hasCheck = true
}
if name := event.Name(); name != "" {
q.Where(dbr.Expr("state_machine_event_id IN (SELECT id FROM state_machine_event WHERE name=?)", name))
hasCheck = true
}
if !hasCheck {
return 0, ErrCannotIdentifyEvent
}
var endStateID int
err := q.LoadOne(&endStateID)
if err == dbr.ErrNotFound {
return 0, ErrCannotFindTransition
}
return endStateID, err
}
func (m machine) Fire(id int, event Event) error {
fromState, err := m.t.Get(id)
if err != nil {
return err
}
toState, err := m.ToState(fromState, event)
if err != nil {
return err
}
return m.t.Transition(id, fromState, toState)
}
func NewMachine(rawDB *sql.DB, t Type) Machine {
db := &dbr.Connection{
DB: rawDB,
Dialect: dialect.PostgreSQL,
EventReceiver: &dbr.NullEventReceiver{},
}
s := db.NewSession(nil)
return &machine{db: s, t: t}
}