-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpringStateMachine.java
153 lines (135 loc) · 4.41 KB
/
SpringStateMachine.java
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
// State pattern: spring framework implementation of telephone state machine
package behavioral.state.spring;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.state.State;
import org.springframework.statemachine.transition.Transition;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;
enum States
{
OFF_HOOK, // starting
ON_HOOK, // terminal
CONNECTING,
CONNECTED,
ON_HOLD
}
enum Events
{
CALL_DIALED,
HUNG_UP,
CALL_CONNECTED,
PLACED_ON_HOLD,
TAKEN_OFF_HOLD,
LEFT_MESSAGE,
STOP_USING_PHONE
}
class SpringStateDemo
{
public static StateMachine<States, Events> buildMachine()
throws Exception
{
// API uses builder pattern to build machine for us
StateMachineBuilder.Builder<States, Events> builder
= StateMachineBuilder.builder();
// Configure states
builder.configureStates()
.withStates()
.initial(States.OFF_HOOK)
.states(EnumSet.allOf(States.class));
builder.configureTransitions()
.withExternal()
.source(States.OFF_HOOK)
.event(Events.CALL_DIALED)
.target(States.CONNECTING)
.and()
.withExternal()
.source(States.OFF_HOOK)
.event(Events.STOP_USING_PHONE)
.target(States.ON_HOOK)
.and()
.withExternal()
.source(States.CONNECTING)
.event(Events.HUNG_UP)
.target(States.OFF_HOOK)
.and()
.withExternal()
.source(States.CONNECTING)
.event(Events.CALL_CONNECTED)
.target(States.CONNECTED)
.and()
.withExternal()
.source(States.CONNECTED)
.event(Events.LEFT_MESSAGE)
.target(States.OFF_HOOK)
.and()
.withExternal()
.source(States.CONNECTED)
.event(Events.HUNG_UP)
.target(States.OFF_HOOK)
.and()
.withExternal()
.source(States.CONNECTED)
.event(Events.PLACED_ON_HOLD)
.target(States.OFF_HOOK)
.and()
.withExternal()
.source(States.ON_HOLD)
.event(Events.TAKEN_OFF_HOLD)
.target(States.CONNECTED)
.and()
.withExternal()
.source(States.ON_HOLD)
.event(Events.HUNG_UP)
.target(States.OFF_HOOK);
return builder. build(); // build state machine
}
public static void main(String[] args) throws Exception
{
StateMachine<States, Events> machine = buildMachine();
machine.start(); // this fires up state machine
States exitState = States.ON_HOOK;
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in)
);
while (true)
{
State<States, Events> state = machine.getState(); // gives us a state object
System.out.println("The phone is currently " + state.getId());
System.out.println("Select a trigger:");
List<Transition<States, Events>> ts = machine.getTransitions()
.stream()
.filter(t -> t.getSource() == state)
.collect(Collectors.toList());
for (int i = 0; i < ts.size(); ++i)
{
System.out.println("" + i + ". " +
ts.get(i).getTrigger().getEvent());
}
boolean parseOK;
int choice = 0;
do
{
try
{
System.out.println("Please enter your choice:");
choice = Integer.parseInt(console.readLine());
parseOK = choice >= 0 && choice < ts.size();
}
catch (Exception e)
{
parseOK = false;
}
} while (!parseOK);
// perform the transition
machine.sendEvent(ts.get(choice).getTrigger().getEvent());
if (machine.getState().getId() == exitState)
break;
}
System.out.println("And we are done!");
}
}