-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFSM.java
347 lines (297 loc) · 9.45 KB
/
DFSM.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package in.kannangce.sm;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
*
* Class represents a Deterministic finite state machine. The below
* characteristics describes the state machine, <br/>
* <ol>
* <li>The machine is capable being in <i><b>a</b> state</i> at any given
* time.</li>
* <li>At any state, the machine is capable of accepting an <i>input</i></li>
* <li>If an input is acceptable in a state, the result is a new state.</li>
* <li>If the input is not valid in the context of the current state, exception
* will be thrown</li>
* <li>The machine has finite set of discreet states and actions.</li>
* <li>The machine will always be in a default state.</li>
* <li>Each states accepts only a discreet number of actions, which is subset of
* all the possible actions.</li>
* </ol>
*
* @author kannan.r
*
*/
public abstract class DFSM
{
private Map<String, State> indexedState = new HashMap<>();
private State defaultState;
private State currentState;
protected DFSM()
{
init();
}
/**
* Initializes the state machine with the state and actions defined from the
* argument.
*
* @param stateTransitionMap
*
* @param defaultState
*
*/
private void init()
{
Map<String, Map<String, String>> stateTransitionMap = getInitData();
String defaultState = getDefaultState();
for (Entry<String, Map<String, String>> entry : stateTransitionMap.entrySet())
{
new State(entry.getKey(), entry.getValue());
}
this.defaultState = indexedState.get(defaultState);
if (this.defaultState == null)
{
throw new IllegalArgumentException("Invalid default state specified");
}
this.setCurrentState(this.defaultState);
}
private void setCurrentState(State currentState)
{
this.currentState = currentState;
}
public String getCurrentState()
{
return this.currentState.getState();
}
/**
* Makes the transition for the given action on the current state of the
* machine. This results in state change.
*
* @param action
* The action to be applied on the current state of the machine.
* @return String representing the resulting state.
*/
public String transition(String action)
{
State nextState = currentState.next(action);
if (nextState == null)
{
throw new IllegalStateException(
MessageFormat.format("The action {0} is not available for the state {1}",
action, currentState.getState()));
}
currentState = indexedState.get(nextState.getState());
return nextState.getState();
}
/**
* Method that just checks what will be the next state. This itself doesn't
* do any change for the state machine
*
* @param currentState
* The current state.
* @param action
* The action to be done on the given state.
* @return The result state when the given action happens on the given
* state.
*/
public String whatNext(String currentState, String action)
{
State currStateInst = indexedState.get(currentState);
if (currStateInst == null)
{
throw new IllegalArgumentException(MessageFormat.format(
"The given state {0} is not available in the given state machine",
currentState));
}
State nextState = currStateInst.next(action);
return nextState != null ? nextState.getState() : null;
}
/**
* Resets the {@link DFSM} to the default state.
*
* @return The default state of the DFSM.
*/
public String reset()
{
currentState = defaultState;
return currentState.getState();
}
/**
* Expected to return the transition map required to initialize the state
* machine
*
* @return Map containing the state transitions, where the keys contains the
* state and the values contains another map. The inner map
* represents the state transition with the keys as action and
* values as result state.
*/
protected abstract Map<String, Map<String, String>> getInitData();
/**
* Expected to return the default state for the state machine.
*
* @return The default state of the state machine. Cannot be null. And
* should be one among the states as per getInitData.
*/
protected abstract String getDefaultState();
/**
* Class represents a state of the {@link StateMachine}
*
* @author kannan.r
*
*/
public class State
{
private String state;
private Map<Action, State> transition;
public State(String name)
{
state = name;
transition = new HashMap<>();
State primaryState = indexedState.get(name);
if (primaryState == null)
{
primaryState = this;
indexedState.put(name, primaryState);
}
}
/**
* Initializes a state along with the transition mapping. The
* <i>action</i> and the <i>result state</i> will be given as a string.
*
* @param name
* The name of the state to instantiated
* @param transitionMap
* The {@link Map} describing the action and the resultant
* state.
*/
public State(String name, Map<String, String> transitionMap)
{
this(name);
State primaryState = indexedState.get(name);
for (Entry<String, String> entry : transitionMap.entrySet())
{
State aResultState = indexedState.get(entry.getValue());
if (aResultState == null)
{
aResultState = new State(entry.getValue());
indexedState.put(entry.getValue(), aResultState);
}
primaryState.addTransition(entry.getKey(), entry.getValue());
}
}
/**
* Adds a transition to the given state
*
* @param action
* The action that triggers the transition
* @param resultState
* The result state
*/
protected void addTransition(String action, String resultState)
{
Action axn = new Action(action);
State aResult = new State(resultState);
State existingState = transition.get(axn);
if (existingState == null || existingState.equals(aResult))
{
transition.put(axn, aResult);
} else
{
throw new IllegalArgumentException(MessageFormat.format(
"The {0} in {1} is already mapped to {2}, now cannot be changed to {3}",
action, state, existingState, resultState));
}
}
/**
* Moves to state machine to the next state by accepting the given
* action.
*
* @param action
* The action to be applied on the state machine
* @return The result state
*/
protected State next(String action)
{
return transition.get(new Action(action));
}
@Override
public boolean equals(Object toCompare)
{
if (toCompare == null || !(toCompare instanceof State))
{
return false;
}
return state.equals(((State) toCompare).state);
}
@Override
public int hashCode()
{
return state.hashCode();
}
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
@Override
public String toString()
{
StringBuilder aStr = new StringBuilder();
aStr.append("{");
for (Entry<Action, State> entry : transition.entrySet())
{
aStr.append(entry.getKey().toString())
.append(':')
.append(entry.getValue().getState())
.append(',');
}
if (transition.size() > 0)
{
aStr.deleteCharAt(aStr.length() - 1);
}
aStr.append('}');
return aStr.toString();
}
}
/**
* Represents an action that is applicable on a given state.
*
* @author kannan.r
*
*/
public static class Action
{
private String action;
public Action(String action)
{
this.action = action;
}
public String getAction()
{
return action;
}
@Override
public boolean equals(Object toCompare)
{
if (toCompare == null || !(toCompare instanceof Action))
{
return false;
}
return action.equals(((Action) toCompare).action);
}
@Override
public int hashCode()
{
return action.hashCode();
}
@Override
public String toString()
{
return action;
}
}
}