-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (190 loc) · 4.74 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
)
// StateMachine represents the structure of the XState JSON format
type StateMachine struct {
Initial string `json:"initial"`
States map[string]StateConfig `json:"states"`
Guards map[string]GuardImplementation
}
// StateConfig represents the configuration for each state
type StateConfig struct {
On map[string]Transition `json:"on"`
Entry []Action `json:"entry,omitempty"`
Exit []Action `json:"exit,omitempty"`
}
// State represents the current state
type State struct {
Value string `json:"value"`
}
// Event represents an event that can trigger a state transition
type Event struct {
Type string `json:"type"`
}
// Action represents an action to be executed
type Action struct {
Type string `json:"type"`
Params map[string]interface{} `json:"params,omitempty"`
}
// Transition represents a transition between states
type Transition struct {
Target string `json:"target"`
Guard *Guard `json:"guard,omitempty"`
Actions []Action `json:"actions,omitempty"`
}
// Guard represents a condition for a transition
type Guard struct {
Type string `json:"type"`
Params map[string]interface{} `json:"params,omitempty"`
}
// GuardImplementation is a function type for guard implementations
type GuardImplementation func(params map[string]interface{}) bool
// CreateMachine creates a new StateMachine with the given JSON and guard implementations
func CreateMachine(machineJSON string, guards map[string]GuardImplementation) (*StateMachine, error) {
var machine StateMachine
err := json.Unmarshal([]byte(machineJSON), &machine)
if err != nil {
return nil, err
}
machine.Guards = guards
return &machine, nil
}
// TransitionStateMachine takes a state machine definition, current state, and event,
// and returns the next state and actions to execute
func (machine *StateMachine) TransitionStateMachine(currentState State, event Event) (State, []Action) {
stateConfig, exists := machine.States[currentState.Value]
if !exists {
return currentState, []Action{}
}
transition, exists := stateConfig.On[event.Type]
if !exists {
return currentState, []Action{}
}
if transition.Guard != nil {
guardImpl, exists := machine.Guards[transition.Guard.Type]
if !exists || !guardImpl(transition.Guard.Params) {
return currentState, []Action{}
}
}
nextState := State{Value: transition.Target}
nextStateConfig, exists := machine.States[transition.Target]
if !exists {
return nextState, []Action{}
}
actions := []Action{}
if stateConfig.Exit != nil {
actions = append(actions, stateConfig.Exit...)
}
if transition.Actions != nil {
actions = append(actions, transition.Actions...)
}
if nextStateConfig.Entry != nil {
actions = append(actions, nextStateConfig.Entry...)
}
return nextState, actions
}
func main() {
// Define guard implementations
guards := map[string]GuardImplementation{
"isValid": func(params map[string]interface{}) bool {
id, ok := params["someParam"].(string)
return ok && id == "accepted"
},
}
// Example usage
machineJSON := `{
"initial": "green",
"states": {
"green": {
"on": {
"timer": {
"target": "yellow",
"guard": {
"type": "isValid",
"params": {
"someParam": "accepted"
}
},
"actions": [
{
"type": "logTransition",
"params": {
"message": "Transitioning from green to yellow"
}
}
]
}
},
"entry": [
{
"type": "turnOnLight",
"params": {
"color": "green"
}
}
],
"exit": [
{
"type": "turnOffLight",
"params": {
"color": "green"
}
}
]
},
"yellow": {
"on": {
"timer": {
"target": "red"
}
},
"entry": [
{
"type": "startTimer",
"params": {
"duration": 5000
}
},
{
"type": "turnOnLight",
"params": {
"color": "yellow"
}
}
]
},
"red": {
"on": {
"timer": {
"target": "green"
}
},
"entry": [
{
"type": "turnOnLight",
"params": {
"color": "red"
}
}
]
}
}
}`
machine, err := CreateMachine(machineJSON, guards)
if err != nil {
fmt.Println("Error creating state machine:", err)
return
}
currentState := State{Value: "green"}
event := Event{Type: "timer"}
nextState, actions := machine.TransitionStateMachine(currentState, event)
fmt.Printf("Current state: %s\n", currentState.Value)
fmt.Printf("Event: %s\n", event.Type)
fmt.Printf("Next state: %s\n", nextState.Value)
fmt.Println("Actions to execute:")
for _, action := range actions {
fmt.Printf(" - Type: %s, Params: %v\n", action.Type, action.Params)
}
}