41
41
#ifndef MAXPLUS_BASE_FSM_FSM_H
42
42
#define MAXPLUS_BASE_FSM_FSM_H
43
43
44
- #include " maxplus/base/exception/exception.h"
45
44
#include " maxplus/base/basic_types.h"
45
+ #include " maxplus/base/exception/exception.h"
46
46
#include " maxplus/base/string/cstring.h"
47
47
#include < list>
48
48
#include < map>
@@ -71,7 +71,7 @@ class WithUniqueID {
71
71
72
72
// forward declaration of FSM state class
73
73
class State ;
74
- using StateRef = const State*;
74
+ using StateRef = const State *;
75
75
76
76
// Edge of an FSM
77
77
class Edge : public WithUniqueID {
@@ -112,7 +112,7 @@ class SetOfEdges : public std::map<CId, std::shared_ptr<Edge>> {
112
112
void remove (const Edge &e) { this ->erase (e.getId ()); }
113
113
};
114
114
115
- using EdgeRef = const Edge*;
115
+ using EdgeRef = const Edge *;
116
116
117
117
struct EdgeRefCompareLessThan {
118
118
bool operator ()(EdgeRef lhs, EdgeRef rhs) const { return lhs->lessThan (*rhs); };
@@ -136,7 +136,7 @@ class State : public WithUniqueID {
136
136
137
137
State (const State &) = default ;
138
138
State &operator =(const State &other) = delete ;
139
- State (State &&) noexcept = default ;
139
+ State (State &&) noexcept = default ;
140
140
State &operator =(State &&) = delete ;
141
141
142
142
// add an outgoing edge to the state
@@ -152,29 +152,21 @@ class State : public WithUniqueID {
152
152
153
153
void insertOutgoingEdge (Edge &e) { this ->outgoingEdges .insert (&e); }
154
154
155
- void removeOutgoingEdge (EdgeRef e) {
156
- this ->outgoingEdges .erase (e);
157
- }
155
+ void removeOutgoingEdge (EdgeRef e) { this ->outgoingEdges .erase (e); }
158
156
159
157
private:
160
158
SetOfEdgeRefs outgoingEdges;
161
-
162
159
};
163
160
164
-
165
161
// A set of states
166
162
// the set is assumed to have unique ownership of the states
167
163
class SetOfStates : public std ::map<CId, std::shared_ptr<State>> {
168
164
public:
169
165
void remove (const State &s) { this ->erase (s.getId ()); }
170
166
virtual ~SetOfStates () = default ;
171
- void addState (const std::shared_ptr<State> &s) {
172
- this ->insert (std::make_pair (s->getId (), s));
173
- }
167
+ void addState (const std::shared_ptr<State> &s) { this ->insert (std::make_pair (s->getId (), s)); }
174
168
175
- State& withId (const CId id) {
176
- return *this ->at (id);
177
- }
169
+ State &withId (const CId id) { return *this ->at (id); }
178
170
};
179
171
180
172
struct StateRefCompareLessThan {
@@ -207,7 +199,6 @@ class FiniteStateMachine {
207
199
[[nodiscard]] virtual StateRef getInitialState () const = 0;
208
200
[[nodiscard]] virtual const SetOfStateRefs &getInitialStates () const = 0;
209
201
[[nodiscard]] virtual const SetOfStateRefs &getFinalStates () const = 0;
210
-
211
202
};
212
203
213
204
//
@@ -347,16 +338,17 @@ template <typename StateLabelType, typename EdgeLabelType> class Edge : public A
347
338
EdgeLabelType label;
348
339
};
349
340
350
- template <typename StateLabelType, typename EdgeLabelType> using EdgeRef = const Edge<StateLabelType, EdgeLabelType>*;
341
+ template <typename StateLabelType, typename EdgeLabelType>
342
+ using EdgeRef = const Edge<StateLabelType, EdgeLabelType> *;
351
343
352
344
template <typename StateLabelType, typename EdgeLabelType>
353
345
class SetOfEdges : public Abstract ::SetOfEdges {
354
346
public:
355
347
using CIter = typename Abstract::SetOfEdges::const_iterator;
356
348
};
357
349
358
- template <typename StateLabelType, typename EdgeLabelType> using StateRef = const State<StateLabelType,EdgeLabelType>*;
359
-
350
+ template <typename StateLabelType, typename EdgeLabelType>
351
+ using StateRef = const State<StateLabelType, EdgeLabelType> *;
360
352
361
353
// template <typename StateLabelType, typename EdgeLabelType>
362
354
// class SetOfEdgeRefs : public Abstract::SetOfEdgeRefs {
@@ -369,13 +361,13 @@ class SetOfStates : public Abstract::SetOfStates {
369
361
private:
370
362
std::map<StateLabelType, std::shared_ptr<State<StateLabelType, EdgeLabelType>>> labelIndex;
371
363
372
- void addToStateIndex (StateLabelType l, std::shared_ptr<State<StateLabelType, EdgeLabelType>> s) {
364
+ void addToStateIndex (StateLabelType l,
365
+ std::shared_ptr<State<StateLabelType, EdgeLabelType>> s) {
373
366
this ->labelIndex [l] = s;
374
367
}
375
368
376
369
public:
377
-
378
- State<StateLabelType, EdgeLabelType>& withLabel (StateLabelType l) {
370
+ State<StateLabelType, EdgeLabelType> &withLabel (StateLabelType l) {
379
371
if (this ->labelIndex .find (l) != this ->labelIndex .end ()) {
380
372
return *this ->labelIndex [l];
381
373
}
@@ -386,11 +378,10 @@ class SetOfStates : public Abstract::SetOfStates {
386
378
return this ->labelIndex .find (l) != this ->labelIndex .end ();
387
379
}
388
380
389
- void addState (std::shared_ptr<State<StateLabelType, EdgeLabelType>>& s) {
381
+ void addState (std::shared_ptr<State<StateLabelType, EdgeLabelType>> & s) {
390
382
Abstract::SetOfStates::addState (std::dynamic_pointer_cast<Abstract::State>(s));
391
383
this ->addToStateIndex (s->getLabel (), s);
392
384
}
393
-
394
385
};
395
386
396
387
template <typename StateLabelType, typename EdgeLabelType>
@@ -447,17 +438,16 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
447
438
SetOfStateRefs<StateLabelType, EdgeLabelType> initialStates;
448
439
SetOfStateRefs<StateLabelType, EdgeLabelType> finalStates;
449
440
450
- State<StateLabelType, EdgeLabelType>& _getStateLabeled (const StateLabelType &s) {
441
+ State<StateLabelType, EdgeLabelType> & _getStateLabeled (const StateLabelType &s) {
451
442
return this ->states .withLabel (s);
452
443
};
453
444
454
- State<StateLabelType, EdgeLabelType>& _getState (const State<StateLabelType, EdgeLabelType> &s) {
455
- return dynamic_cast <State<StateLabelType, EdgeLabelType>&>(this ->states .withId (s.getId ()));
445
+ State<StateLabelType, EdgeLabelType> & _getState (const State<StateLabelType, EdgeLabelType> &s) {
446
+ return dynamic_cast <State<StateLabelType, EdgeLabelType> &>(this ->states .withId (s.getId ()));
456
447
};
457
448
458
-
459
449
public:
460
- FiniteStateMachine () : Abstract::FiniteStateMachine() {};
450
+ FiniteStateMachine () : Abstract::FiniteStateMachine(){};
461
451
462
452
~FiniteStateMachine () override = default ;
463
453
@@ -483,10 +473,10 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
483
473
EdgeLabelType lbl,
484
474
const State<StateLabelType, EdgeLabelType> &dst) {
485
475
// lookup state again to drop const qualifier
486
- auto &mySrc =
487
- dynamic_cast <State<StateLabelType, EdgeLabelType> &>( this ->states .withId (src.getId ()));
488
- auto &myDst =
489
- dynamic_cast <State<StateLabelType, EdgeLabelType> &>( this ->states .withId (dst.getId ()));
476
+ auto &mySrc = dynamic_cast <State<StateLabelType, EdgeLabelType> &>(
477
+ this ->states .withId (src.getId ()));
478
+ auto &myDst = dynamic_cast <State<StateLabelType, EdgeLabelType> &>(
479
+ this ->states .withId (dst.getId ()));
490
480
bool added = false ;
491
481
auto ep = std::make_shared<Edge<StateLabelType, EdgeLabelType>>(mySrc, lbl, myDst);
492
482
auto &e = *ep;
@@ -498,7 +488,7 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
498
488
void removeEdge (const Edge<StateLabelType, EdgeLabelType> &e) {
499
489
auto csrc = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(e.getSource ());
500
490
// get a non-const version of the state
501
- auto & src = this ->_getState (*csrc);
491
+ auto & src = this ->_getState (*csrc);
502
492
src.removeOutgoingEdge (&e);
503
493
this ->edges .remove (e);
504
494
}
@@ -541,7 +531,6 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
541
531
this ->finalStates .insert (&s);
542
532
};
543
533
544
-
545
534
[[nodiscard]] StateRef<StateLabelType, EdgeLabelType> getInitialState () const override {
546
535
if (this ->initialStates .empty ()) {
547
536
throw MaxPlus::MPException (" FSM has no initial state." );
@@ -584,7 +573,9 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
584
573
return false ;
585
574
};
586
575
587
- [[nodiscard]] const SetOfStates<StateLabelType, EdgeLabelType> &getStates () const { return this ->states ; };
576
+ [[nodiscard]] const SetOfStates<StateLabelType, EdgeLabelType> &getStates () const {
577
+ return this ->states ;
578
+ };
588
579
Abstract::SetOfStateRefs getStateRefs () {
589
580
Abstract::SetOfStateRefs result;
590
581
for (auto i : this ->states ) {
@@ -643,7 +634,8 @@ class FiniteStateMachine : public Abstract::FiniteStateMachine {
643
634
644
635
for (const auto &it : outgoingEdges) {
645
636
auto e = dynamic_cast <const Edge<StateLabelType, EdgeLabelType> *>(it);
646
- auto dstState = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(e->getDestination ());
637
+ auto dstState = dynamic_cast <StateRef<StateLabelType, EdgeLabelType>>(
638
+ e->getDestination ());
647
639
if (e->getLabel () == lbl && dstState->getLabel () == dst) {
648
640
return e;
649
641
}
@@ -957,7 +949,8 @@ namespace StateStringLabeled {
957
949
958
950
class State : public Labeled ::State<MaxPlus::MPString, char > {
959
951
public:
960
- explicit State (const MaxPlus::MPString &withLabel) : Labeled::State<MaxPlus::MPString, char>(withLabel) {}
952
+ explicit State (const MaxPlus::MPString &withLabel) :
953
+ Labeled::State<MaxPlus::MPString, char>(withLabel) {}
961
954
962
955
const Abstract::SetOfEdgeRefs &getOutgoingEdges () {
963
956
return dynamic_cast <const Abstract::SetOfEdgeRefs &>(
@@ -968,7 +961,8 @@ class State : public Labeled::State<MaxPlus::MPString, char> {
968
961
class FiniteStateMachine : public Labeled ::FiniteStateMachine<MaxPlus::MPString, char > {
969
962
public:
970
963
[[nodiscard]] Labeled::StateRef<MaxPlus::MPString, char > getInitialState () const override {
971
- return dynamic_cast <Labeled::StateRef<MaxPlus::MPString, char >>(Labeled::FiniteStateMachine<MaxPlus::MPString, char >::getInitialState ());
964
+ return dynamic_cast <Labeled::StateRef<MaxPlus::MPString, char >>(
965
+ Labeled::FiniteStateMachine<MaxPlus::MPString, char >::getInitialState ());
972
966
};
973
967
974
968
void setInitialStateLabeled (const MaxPlus::MPString &sl);
0 commit comments