-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.go
More file actions
79 lines (68 loc) · 2.07 KB
/
Copy pathtransform.go
File metadata and controls
79 lines (68 loc) · 2.07 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
package msm
import (
"fmt"
"reflect"
)
func eventType(event Event) reflect.Type {
t := reflect.TypeOf(event)
if t.Kind() == reflect.Ptr {
return t
} else {
return reflect.PointerTo(t)
}
}
func (f *FSM) newTransition(state State, event Event, newState State, action Action, guard Guard) Transition {
eventType := eventType(event)
rule := Transition{
TransitionKey: TransitionKey{
State: state,
Event: eventType,
},
TransitionValue: TransitionValue{
NewState: newState,
Action: action,
Guard: guard,
},
}
// Validate Action
aType := reflect.FuncOf([]reflect.Type{
reflect.TypeOf(new(FSMInstance)),
f.storageType,
reflect.TypeOf(*new(State)),
reflect.TypeOf(*new(string)),
eventType,
}, []reflect.Type{},
false,
)
if reflect.ValueOf(action).Pointer() != reflect.ValueOf(NoAction).Pointer() && aType != reflect.TypeOf(action) {
panic(fmt.Sprintf("Rule: %+v\nExpected action:\n'%v', Got:\n'%v'\n", rule, aType, reflect.TypeOf(action)))
}
// Validate Guard
gType := reflect.FuncOf([]reflect.Type{
reflect.TypeOf(new(FSMInstance)),
f.storageType,
reflect.TypeOf(*new(State)),
reflect.TypeOf(*new(string)),
eventType,
}, []reflect.Type{
reflect.TypeOf(*new(bool)),
},
false,
)
if reflect.ValueOf(guard).Pointer() != reflect.ValueOf(NoGuard).Pointer() && gType != reflect.TypeOf(guard) {
panic(fmt.Sprintf("Rule: %+v\nExpected guard:\n'%v', Got:\n'%v'\n", rule, gType, reflect.TypeOf(guard)))
}
return rule
}
func (f *FSM) RowAG(state State, event Event, newState State, action Action, guard Guard) Transition {
return f.newTransition(state, event, newState, action, guard)
}
func (f *FSM) RowA(state State, event Event, newState State, action Action) Transition {
return f.newTransition(state, event, newState, action, NoGuard)
}
func (f *FSM) RowG(state State, event Event, newState State, guard Guard) Transition {
return f.newTransition(state, event, newState, NoAction, guard)
}
func (f *FSM) Row(state State, event Event, newState State) Transition {
return f.newTransition(state, event, newState, NoAction, NoGuard)
}