-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
71 lines (59 loc) · 1.7 KB
/
builder.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
package fsm
type StateMachineBuilder interface {
States([]State) StateMachineBuilder
Actions([]Action) StateMachineBuilder
Start(State) StateMachineBuilder
Accepts([]State) StateMachineBuilder
Transitions([]Transition) StateMachineBuilder
Build() stateMachineComprehension
}
type stateMachineBuilder struct {
states []State
actions []Action
start State
accepts []State
transitions []Transition
}
func Builder() *stateMachineBuilder {
return &stateMachineBuilder{}
}
type stateMachineComprehension struct {
states []State
actions []Action
start State
accepts []State
transitions map[StateActionTuple]State
}
func (builder *stateMachineBuilder) States(states []State) StateMachineBuilder {
builder.states = states
return builder
}
func (builder *stateMachineBuilder) Actions(actions []Action) StateMachineBuilder {
builder.actions = actions
return builder
}
func (builder *stateMachineBuilder) Start(start State) StateMachineBuilder {
builder.start = start
return builder
}
func (builder *stateMachineBuilder) Accepts(accepts []State) StateMachineBuilder {
builder.accepts = accepts
return builder
}
func (builder *stateMachineBuilder) Transitions(transitions []Transition) StateMachineBuilder {
builder.transitions = transitions
return builder
}
func (builder *stateMachineBuilder) Build() stateMachineComprehension {
transitions := map[StateActionTuple]State{}
for _, transition := range builder.transitions {
transitions[transition.Tuple] = transition.State
}
return stateMachineComprehension{
states: builder.states,
actions: builder.actions,
start: builder.start,
accepts: builder.accepts,
transitions: transitions,
}
}