-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachineController.h
546 lines (432 loc) · 16.1 KB
/
StateMachineController.h
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#pragma once
#include"RxFSM/CommonDefines.h"
#include"RxFSM/StateTransitions.h"
#include"RxFSM/State.h"
#include"RxFSM/StateAction.h"
#include"RxFSM/Export.h"
namespace Reactor
{
// ----------------------------------------------------
// StateMachineControllerBase
// ----------------------------------------------------
class DLL_STATE StateMachineControllerBase
: public Templates::ExecuteMethod0<bool>
, public Templates::ShutdownMethod
, public Templates::TimeoutMethod
, public Templates::BlockMethod
, public Templates::UnblockMethod
, public Templates::IsBlockedMethod
, public Templates::ResumeMethod
, public Templates::SuspendMethod
, public Templates::TriggerMethod
, public Templates::ResetMethod<bool>
, public Templates::TimeoutInMethod<int>
, public Templates::IsTimeoutMethod
, public Templates::IsExecutingMethod
, public Templates::IsSuspendedMethod
, public Templates::IsPolicyViolatedMethod
{
public:
virtual ~StateMachineControllerBase()
{ }
CLASS_TRAITS(StateMachineControllerBase)
};
// ----------------------------------------------------
// StateMachineControllerPolicy
// ----------------------------------------------------
class StateMachineControllerStatus
{
public:
StateMachineControllerStatus()
: stateMachineStatus_(Duration::FromMinutes(5))
{}
virtual ~StateMachineControllerStatus()
{ }
const Status::ExecutionStatus& executionStatus() const
{
return executionStatus_;
}
Status::ExecutionStatus& executionStatus()
{
return executionStatus_;
}
const Status::EventStatus& eventStatus() const
{
return eventStatus_;
}
Status::EventStatus& eventStatus()
{
return eventStatus_;
}
StateMachineStatus& machineStatus()
{
return stateMachineStatus_;
}
const StateMachineStatus& machineStatus() const
{
return stateMachineStatus_;
}
private:
Status::EventStatus eventStatus_;
Status::ExecutionStatus executionStatus_;
StateMachineStatus stateMachineStatus_;
};
// ----------------------------------------------------
// StateMachineControllerPolicy
// ----------------------------------------------------
class DLL_STATE StateMachineControllerPolicy
{
public:
StateMachineControllerPolicy(CommandControllerPolicy policy = CommandControllerPolicy::Default(),
FlowControllerPolicy flowControllerPolicy = FlowControllerPolicy::Default())
: policy_(policy)
, flowPolicy_(flowControllerPolicy)
{ }
virtual ~StateMachineControllerPolicy()
{ }
const CommandControllerPolicy& ControllerPolicy() const
{
return policy_;
}
const FlowControllerPolicy& FlowPolicy() const
{
return flowPolicy_;
}
private:
// Policy in scheduling and timing of scheduling of state actions after trigger.
CommandControllerPolicy policy_;
FlowControllerPolicy flowPolicy_;
};
// ----------------------------------------------------
// StateMachineControllerData
// ----------------------------------------------------
template <typename StateType, typename Context, typename Return, typename Input>
class StateMachineControllerData
{
public:
StateMachineControllerData(const Context& context, const StateTransitionTable<StateType, Return, Input>& table)
: context_(context)
, transitionTable_(table)
, iterator_(table)
{ }
virtual ~StateMachineControllerData()
{ }
typename FlowController<CommandBase::Ptr>::Ptr& Queue() const
{
return triggerQueue_;
}
const StateTransitionTable<StateType, Return, Input>& Table() const
{
return transitionTable_;
}
const Context& context() const
{
return context_;
}
Context& context()
{
return context_;
}
StateMachineDescription Description() const
{
return description_;
}
StateTransitionTableIterator<StateType, Return, Input> const& iterator() const
{
return iterator_;
}
StateTransitionTableIterator<StateType, Return, Input>& iterator()
{
return iterator_;
}
StateType currentState() const
{
return StateType();
}
private:
Context context_;
StateTransitionTable<StateType, Return, Input> transitionTable_;
StateTransitionTableIterator<StateType, Return, Input> iterator_;
typename FlowController<CommandBase::Ptr>::Ptr triggerQueue_;
StateMachineDescription description_;
// TODO: Current state if applicable
// if no current state is stored, then a transition has to contain information from - to
// if current state is stored, then a transition only requires information of state to
};
// ----------------------------------------------------
// StateMachineController
// ----------------------------------------------------
/*
One instance of state machine controller can represent a "Key" in my selector example.
I need a Next(Return value), alternative, Next(StateType startState, Return value)
See AsyncScheduler for initial implementation.
Identify Start state(s) and Final state(s). Final state(s) may be accepting, rejecting.
Alternative 1:
- User defines nextState function
- State machine uses nextState function (no transition rules)
Alternative 2:
- User defines transition rules
- State machine uses predifined nextState function that applies the transition rules
State machines depends on input:
- Define an input source for the entire state machine
- Define input sources for every action
- Use return values from every state
typename State, typename Input, typename Output
StateCommand<State, Output> cmd;
Action1<Output, Input> action;
State = StateType
Output = Return
Input = Event (Arg1)
State machines stores "current state".
Sequential: Use current to find next, execute next, set next as current.
Parallel:
TODO: What is logical for parallel? Execute from start-state?
Add StateMachineStatus
One execution may for example be [StartState -> FinalState] then write output to cache.
TODO: How to add and execute "input actions"?
TODO: Focus on supporting sequential. Parallel can come later and is perhaps solved otherwise.
TODO: Input == Event
TODO: FlowController for Input events. Input may be called events.
TODO: FlowController<Event>, see Observer implementation.
TODO: StateMachineController for input events can be similar to a subject
TODO: FlowController for Return. Are these events? They are used in transition condition to transition between states without input event.
TODO: What is logical for parallel? Execute from start-state?
Sequential machine execution:
- Execute any one state with input only in sequence (not in parallel)
- Execute individual states with input in sequence
Parallel machine execution (many current):
- Execute any one state with input only in sequence (not in parallel)
- Execute individual states with input in parallel
Embarrassingly parallel machine execution (many current, also duplicates of current):
- Execute any one state with input in parallel
- Execute individual states with input in parallel
*/
template <typename StateType, typename Return, typename Input, typename Context>
class StateMachineController
: public StateMachineControllerBase
, public Templates::Action0<bool>
//, public Templates::IsNextMethod1<bool, StateType>
, public RxObserverNew<Input>
, public Templates::ContextObjectShared<StateMachineControllerPolicy, StateMachineControllerData<StateType, Context, Return, Input>, StateMachineControllerStatus, StateType>
, public std::enable_shared_from_this<StateMachineController<StateType, Return, Input, Context>>
{
public:
typedef ISet<StateType> AdjacencyList;
typedef Reactor::State<StateType, Return, Input> StateHandler;
public:
StateMachineController(const Context& context, const StateTransitionTable<StateType, Return, Input>& table)
: Templates::ContextObjectShared<StateMachineControllerPolicy, StateMachineControllerData<StateType, Context, Return, Input>, StateMachineControllerStatus, StateType>
(
new StateMachineControllerPolicy(),
new StateMachineControllerData<StateType, Context, Return, Input>(context, table),
new StateMachineControllerStatus(),
new StateType()
)
{ }
virtual ~StateMachineController()
{ }
virtual bool Execute()
{
// ----------------------------------------------------
// Execute from start state
// - given only one start state and not executing
// ----------------------------------------------------
Reactor::State<StateType, Return, Input> state = this->data()->iterator().Next();
typename StateCommandController<Return>::Ptr controller = state.EntryActions();
controller->RxSubject().state().Subscribe(this->shared_from_this());
controller->Execute();
this->status()->executionStatus().Start();
return true;
}
// ---------------------------------------------------------
// Action0 interface
// TODO: Split subject base functionality and this action, which is basically a reactor
// ---------------------------------------------------------
virtual bool Invoke()
{
// if(this->IsBlocked()) return false;
//
// Events events = this->data()->Queue()->Pull();
// if(events.empty()) return false;
//
// StateType state = this->data()->currentState();
// AdjacencyList nextForCurr = this->data()->Table().TransitionsFor(state);
// return Submit(events);
return true;
}
virtual bool Cancel()
{
return true;
}
virtual bool IsDone() const
{
return true;
}
virtual bool IsSuccess() const
{
return true;
}
// ----------------------------------------------------
// Reactor functions (key,value) - input from sources
// ----------------------------------------------------
// Similar to Subject1::Next(Source source, Arg1 arg1)
// This is a dispatch function (play events to "observers", where observers are neighbors and only conditionally gets executed.
virtual IList<StateType> Next(StateType state, Input value)
{
AdjacencyList nextForCurr = this->data()->Table().TransitionsFor(state);
for(StateType neighbor : nextForCurr)
{
StateHandler handler = this->data()->Table().HandlerFor(neighbor);
handler.InputActions()->Condition()->UpdateTrigger(value);
if(handler.InputActions()->Condition()->IsTriggered())
{
handler.InputActions()->Execute();
}
}
return CollectionUtils::EmptyIList<StateType>();
}
// ---------------------------------------------------------
// Next event functions like subject1
// TODO: Inherit from Subject instead and copy Subject1 functions?
// ---------------------------------------------------------
// Similar to Subject1::Next(Source source, Arg1 arg1)
template <typename Source>
Concurrent::Event::Ptr Next(StateType state, Input value, Source* source)
{
typename Concurrent::Event1<Input>::Ptr event1(new Concurrent::Event1<Input>(state, value));
Concurrent::Event::Ptr event(new Concurrent::Event(event1, state, source));
this->data()->Queue()->Next(event);
return event;
}
// ----------------------------------------------------
// Reactor functions (value) - input from sources
// ----------------------------------------------------
virtual IList<StateType> Next(Input value)
{
// TODO: Use current state of state machine to find and execute next.
AdjacencyList nextForCurr = this->data()->Table().TransitionsFor(value);
//nextForCurr.Condition().IsTriggered(value);
return CollectionUtils::EmptyIList<StateType>();
}
virtual void Complete()
{
}
virtual void Error(BaseLib::Exception )
{
}
// ----------------------------------------------------
// Check status functions
// ----------------------------------------------------
virtual bool IsNext(StateType ) const
{
return false;
}
virtual bool Block()
{
return true;
}
virtual bool Unblock()
{
return true;
}
virtual bool IsBlocked() const
{
return true;
}
// -----------------------------------------------------------
// Handler monitoring
// -----------------------------------------------------------
// virtual bool OnEntry(StateMachineAction *handler)
// {
// // callback from handlers
// //handler->SetContext(machineContext);
// return true;
// }
//
// virtual bool OnExit(StateMachineAction *handler)
// {
// // callback from handlers
// //handler->UnsetContext();
// return true;
// }
// -----------------------------------------------------------
// Interface CommandObserver <T>
// -----------------------------------------------------------
// virtual bool OnComplete(Command<Return> *command)
// {
// return true;
// }
// virtual bool OnNext(Command<Return> *command, Return value)
// {
// // command represents the state, value represents input
// // StateTransition<state, Rules<Return, State>>
// // Use value to identify next state(s) to execute, or use context object?
// // identify next handler in HandlerChain
// // execute next handler with value as input
// // - create a command with handler and value as input
// // - feed to trigger queue of commands
// return true;
// }
// virtual bool OnError(Command<Return> *command, BaseLib::GeneralException exception)
// {
// return true;
// }
// -----------------------------------------------------------
// Interface RxObserver - Values from machine input source(s)
// -----------------------------------------------------------
virtual bool OnComplete()
{
return true;
}
virtual bool OnNext(Input value)
{
IINFO() << "Next value " << value;
// State machines depends on input:
// - Define an input source for the entire state machine
// - Define input sources for every action
// - Use return values from every state
// command represents the state, value represents input
// StateTransition<state, Rules<Return, State>>
// Use value to identify next state(s) to execute, or use context object?
// identify next handler in HandlerChain
// execute next handler with value as input
// - create a command with handler and value as input
// - feed to trigger queue of commands
return true;
}
virtual bool OnError(BaseLib::GeneralException )
{
return true;
}
// ---------------------------------------------------------
// Initialize methods
// ---------------------------------------------------------
void Initialize()
{
// bool isAttached = triggerQueue_->Set(this->shared_from_this());
// if(isAttached)
// {
// status_.Activate();
// }
}
bool IsInitialized() const
{
return false; //this->status()->IsActive();
}
private:
void executeManyStartStates()
{
for(auto startState : this->data()->Table().InitialStates())
{
// ----------------------------------------------------
// Execute start state input actions
// ----------------------------------------------------
// AdjacencyList nextForCurr = this->data()->Table().TransitionsFor(startState);
Reactor::State<StateType, Return, Input> state = this->data()->iterator().Next();
typename StateCommandController<Return>::Ptr controller = state.EntryActions();
controller->RxSubject().state().Subscribe(this->shared_from_this());
controller->Execute();
}
}
};
}