-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachineCollections.h
494 lines (403 loc) · 12.1 KB
/
StateMachineCollections.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
#pragma once
#include"RxConcurrent/CommonDefines.h"
#include"RxConcurrent/StateMachineMethods.h"
namespace BaseLib { namespace Concurrent
{
/**
* @brief The StateVertex class is a vertex with an adjacency list.
*
* Note StateType can be any type.
*/
template <typename StateType, typename T>
class StateVertex
{
public:
typedef Collection::ISet<StateType> AdjacencyList;
public:
StateVertex(StateType key, T value)
: vertex_(key)
, value_(value)
{}
virtual ~StateVertex()
{}
virtual bool AddNeighbor(StateType state)
{
if(state == vertex_) return false;
return neighbors_.insert(state).second;
}
virtual bool RemoveNeighbor(StateType state)
{
return neighbors_.erase(state) > 0;
}
unsigned int Degree() const
{
return neighbors_.size();
}
AdjacencyList& Neighbors()
{
return neighbors_;
}
/**
* @brief Key returns the key of the vertex.
*/
const StateType& Key() const
{
return vertex_;
}
/**
* @brief Value returns the state associated to the vertex.
*/
const T& Value() const
{
return value_;
}
/**
* @brief Value returns the state associated to the vertex.
* @return
*/
T& Value()
{
return value_;
}
/**
* @brief operator ==
* @param that
* @return
*/
bool operator==(const StateVertex<StateType, T> &that)
{
return this->vertex_ == that.vertex_;
}
private:
StateType vertex_;
T value_;
AdjacencyList neighbors_;
};
/**
* @brief The StateTransition class represents a state transition (from, to).
*
* The individual StateType represents a vertex and the state transition (from, to) an edge.
*/
template <typename StateType>
class Transition
{
public:
Transition(StateType from, StateType to)
: from_(from)
, to_(to)
{}
virtual ~Transition()
{}
const StateType& From() const
{
return from_;
}
const StateType& To() const
{
return to_;
}
bool operator==(const Transition &that)
{
return this->from_ == that.from_ && this->to_ == that.to_;
}
bool operator!=(const Transition &that)
{
return !(*this == that);
}
bool operator<(const Transition &that)
{
return this->from_ < that.from_;
}
private:
StateType from_;
StateType to_;
};
/**
* TODO: Starting to require graph functionality.
*/
template <typename StateType>
class TransitionPath : public Collection::IMultiMap<StateType, Transition<StateType> >
{
public:
TransitionPath(StateType origin)
: origin_(origin)
{}
virtual ~TransitionPath()
{}
const StateType& Origin() const
{
return origin_;
}
void AddTransition(StateType from, StateType to)
{
this->put(from, Transition<StateType>(from, to));
}
void RemoveState(StateType state)
{
this->erase(state);
}
bool RemoveTransition(const Transition<StateType> &transition)
{
typename Collection::IMultiMap<StateType, Transition<StateType> >::iterator it = this->find(transition.From());
for( ; it != this->end() && it->first == transition.From(); it++)
{
if(transition == it->second)
{
return erase(it) == 1;
}
}
return false;
}
private:
StateType origin_;
};
/**
* @brief The StateCollection class is used by StateMachineCollection to store objects that are in a given StateType.
*
* Key = StateType
* Value = T
*/
template <typename StateType, typename T>
class StateCollection
: public Collection::IMap<InstanceHandle, std::shared_ptr<T>>
, public Runnable
, public Templates::NoOpMethod
, public ENABLE_SHARED_FROM_THIS_T2(StateCollection, StateType, T)
{
private:
typedef Collection::IMap<InstanceHandle, std::shared_ptr<T>> Map;
public:
StateCollection(StateType stateType)
: stateType_(stateType)
{}
virtual ~StateCollection()
{}
CLASS_TRAITS(StateCollection)
virtual void run()
{
futureTasks_.run();
}
/**
* @brief RunTasks Execute N futuretasks in FIFO order
*/
InstanceHandleSet RunTasks(unsigned int number)
{
InstanceHandleSet handles = futureTasks_.GetNHandlesFIFO(number);
futureTasks_.RunTasks(handles);
return handles;
}
void Merge(typename StateCollection<StateType, T>::Ptr fromCollection)
{
for(typename StateCollection<StateType, T>::iterator it = fromCollection->begin(), it_end = fromCollection->end(); it != it_end; ++it)
{
Templates::RunnableFuture::Ptr future = fromCollection->Actions().GetTaskBase(it->first);
ASSERT(future);
InstanceHandle key = futureTasks_.AddTask(future);
Map::put(key, it->second);
}
}
void Merge(typename StateCollection<StateType, T>::Ptr fromCollection, const InstanceHandleSet &handles)
{
for(InstanceHandleSet::const_iterator it = handles.begin(), it_end = handles.end(); it != it_end; ++it)
{
InstanceHandle handle = *it;
Templates::RunnableFuture::Ptr future = fromCollection->Actions().GetTaskBase(handle);
ASSERT(fromCollection->containsKey(handle));
ASSERT(future);
InstanceHandle key = futureTasks_.AddTask(future);
std::shared_ptr<T> t = fromCollection->at(handle);
Map::put(key, t);
}
}
/**
* @return number of objects in collection actually removed
*/
size_t Remove(const InstanceHandleSet &handles)
{
for(InstanceHandleSet::const_iterator it = handles.begin(), it_end = handles.end(); it != it_end; ++it)
{
InstanceHandle handle = *it;
bool isRemoved = futureTasks_.RemoveTask(handle);
ASSERT(isRemoved);
}
return Map::eraseKeys(handles);
}
void ClearAll()
{
Map::clear();
futureTasks_.Clear();
}
bool IsEmpty() const
{
return Map::empty();
}
size_t Size() const
{
return Map::size();
}
bool AddObject(std::shared_ptr<T> object)
{
InstanceHandle key = futureTasks_.AddTask<void, T>(object.get(), &Templates::NoOpMethod::NoOp);
return Map::put(key, object);
}
template <typename Return>
bool AddAction(std::shared_ptr<T> object, Return (T::*member)())
{
InstanceHandle key = futureTasks_.AddTask<Return, T>(object.get(), member);
return Map::put(key, object);
}
template <typename Return, typename Arg1>
bool AddAction(std::shared_ptr<T> object, Return (T::*member)(Arg1), Arg1 arg1)
{
InstanceHandle key = futureTasks_.AddTask<Return, T, Arg1>(object.get(), member, arg1);
return Map::put(key, object);
}
// bool RemoveObject(const T &object)
// {
// InstanceHandleSet handles = futureTasks_.GetHandlesFromClass<void, T>(&object, &Templates::NoOpMethod::NoOp);
// return removeFromCollection(handles);
// }
// template <typename Return>
// bool RemoveAction(const T &object, Return (T::*member)())
// {
// InstanceHandleSet handles = futureTasks_.GetHandlesFromClass<Return, T>(&object, member);
// return removeFromCollection(handles);
// }
// template <typename Return, typename Arg1>
// bool RemoveAction(const T &object, Return (T::*member)(Arg1))
// {
// InstanceHandleSet handles = futureTasks_.GetHandlesFromClass<Return, T, Arg1>(&object, member);
// return removeFromCollection(handles);
// }
StateType GetStateType() const
{
return stateType_;
}
FutureTasks& Actions()
{
return futureTasks_;
}
const FutureTasks& Actions() const
{
return futureTasks_;
}
bool operator==(const StateCollection &other) const
{
return stateType_ == other.stateType_;
}
bool operator!=(const StateCollection &other) const
{
return stateType_ != other.stateType_;
}
private:
bool removeFromCollection(const InstanceHandleSet &handles)
{
size_t numRemoved = futureTasks_.RemoveTasks(handles);
return Map::eraseKeys(handles) == numRemoved;
}
private:
FutureTasks futureTasks_;
StateType stateType_;
};
/**
* @brief The StateMachineCollections class is an implementation of the "Collections for states" software design pattern.
*
* A Key Value state machine where StateType = Key, T = Value.
*/
template <typename StateType, typename T>
class StateMachineCollections
: public Templates::Lockable<Mutex>
, public ENABLE_SHARED_FROM_THIS_T2(StateMachineCollections, StateType, T)
{
public:
StateMachineCollections()
{}
virtual ~StateMachineCollections()
{}
CLASS_TRAITS(StateMachineCollections)
typename StateMachineCollections::Ptr GetPtr()
{
return this->shared_from_this();
}
size_t ExecuteObjects(StateType state)
{
typename StateCollection<StateType,T>::Ptr fromCollection = getOrCreateCollection(state);
fromCollection->run();
return fromCollection->Size();
}
size_t ExecuteNObjects(int number, StateType state)
{
typename StateCollection<StateType,T>::Ptr fromCollection = getOrCreateCollection(state);
InstanceHandleSet handles = fromCollection->RunTasks(number);
return handles.size();
}
bool ExecuteObjectsAndMove(StateType from, StateType to)
{
// --------------------------------
// Execute actions in from state
// --------------------------------
typename StateCollection<StateType,T>::Ptr fromCollection = getOrCreateCollection(from);
fromCollection->run();
// --------------------------------
// merge from collection into to
// --------------------------------
{
typename StateCollection<StateType,T>::Ptr toCollection = getOrCreateCollection(to);
toCollection->Merge(fromCollection);
fromCollection->ClearAll();
}
return fromCollection->IsEmpty();
}
bool ExecuteNObjectsAndMove(unsigned int number, StateType from, StateType to)
{
// --------------------------------
// Execute N actions in from state
// --------------------------------
typename StateCollection<StateType,T>::Ptr fromCollection = getOrCreateCollection(from);
InstanceHandleSet handles = fromCollection->RunTasks(number);
// --------------------------------
// merge N objects from collection into to
// --------------------------------
{
typename StateCollection<StateType,T>::Ptr toCollection = getOrCreateCollection(to);
toCollection->Merge(fromCollection, handles);
fromCollection->Remove(handles);
}
return true;
}
bool ClearCollection(StateType type)
{
if(!states_.containsKey(type)) return false;
typename StateCollection<StateType,T>::Ptr collection = getOrCreateCollection(type);
collection->ClearAll();
return collection->IsEmpty();
}
typename StateCollection<StateType,T>::Ptr GetCollection(StateType type)
{
return getOrCreateCollection(type);
}
// bool CreateStateTransition(StateType from, StateType to)
// {
// createCollection(from);
// createCollection(to);
// return addTransition(from, to);
// }
private:
typename StateCollection<StateType,T>::Ptr getOrCreateCollection(StateType state)
{
if(!states_.containsKey(state))
{
typename StateCollection<StateType, T>::Ptr collection(new StateCollection<StateType, T>(state));
states_.put(state, collection);
}
return states_.at(state);
}
// bool addTransition(StateType from, StateType to)
// {
// return transitions_.put(from, to);
// }
private:
Collection::IMap<StateType, typename StateCollection<StateType, T>::Ptr > states_;
//Collection::IMap<StateType, StateType> transitions_;
};
}}