-
Notifications
You must be signed in to change notification settings - Fork 19
/
event_builder.go
69 lines (56 loc) · 1.9 KB
/
event_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
package statemachine
import (
"time"
)
// EventBuildable implementation is able to consume the result of building
// features from EventBuilder. EventBuildable is oblivious to Event or
// it's implementation.
type EventBuildable interface {
SetEventDef(event string, def *EventDef)
}
// EventBuilder provides the ability to define an event along with its
// transitions and their guards. EventBuilder is oblivious to Event or it's
// implementation.
type EventBuilder interface {
TimedEvery(duration time.Duration) EventBuilder
// TODO: Assert that Choice(...) and Transition() are not used together.
Choice(condition ChoiceCondition) ChoiceBuilder
// Transition begins the transition builder, accepting states and guards.
Transition() TransitionBuilder
// Build plugs the collected feature definitions into any object
// that understands them (implements dsl.EventBuildable). Use this method
// if you're not using MachineBuilder.Event() to define the event.
Build(event EventBuildable)
}
// NewEventBuilder returns a zero-valued instance of eventBuilder, which
// implements EventBuilder.
func NewEventBuilder(name string) EventBuilder {
return &eventBuilder{
name: name,
def: &EventDef{},
}
}
// eventBuilder implements EventBuilder.
type eventBuilder struct {
name string
def *EventDef
}
var _ EventBuilder = (*eventBuilder)(nil)
func (e *eventBuilder) TimedEvery(duration time.Duration) EventBuilder {
e.def.SetEvery(duration)
return e
}
func (e *eventBuilder) Choice(condition ChoiceCondition) ChoiceBuilder {
choiceDef := &ChoiceDef{}
choiceDef.SetCondition(condition)
e.def.SetChoice(choiceDef)
return newChoiceBuilder(choiceDef)
}
func (e *eventBuilder) Transition() TransitionBuilder {
transitionDef := &TransitionDef{}
e.def.AddTransition(transitionDef)
return newTransitionBuilder(transitionDef)
}
func (e *eventBuilder) Build(event EventBuildable) {
event.SetEventDef(e.name, e.def)
}