1+ #include " maxplus/base/fsm/fsm.h"
12#include " graph/smpls.h"
23#include " algebra/mptype.h"
4+ #include " maxplus/base/fsm/iofsm.h"
5+ #include " maxplus/graph/mpautomaton.h"
36#include < memory>
47
58using namespace FSM ;
69
710using ELSEdge = ::FSM::Labeled::Edge<CId, CString>;
11+ using ELSEdgeRef = ::FSM::Labeled::EdgeRef<CId, CString>;
812using ELSState = ::FSM::Labeled::State<CId, CString>;
13+ using ELSStateRef = ::FSM::Labeled::StateRef<CId, CString>;
914using ELSSetOfStates = ::FSM::Labeled::SetOfStates<CId, CString>;
1015using ELSSetOfEdges = ::FSM::Abstract::SetOfEdges;
1116using ELSSetOfEdgeRefs = ::FSM::Abstract::SetOfEdgeRefs;
@@ -36,13 +41,13 @@ namespace MaxPlus::SMPLS {
3641 for (const auto & it: elsEdges)
3742 {
3843
39- auto & e = dynamic_cast <ELSEdge&>( *it.second );
40- const auto & s = dynamic_cast <const ELSState& >(e. getDestination ());
41- const auto & oEdges = dynamic_cast <const ELSSetOfEdges&>(s. getOutgoingEdges ());
44+ auto e = dynamic_cast <ELSEdgeRef>(&( *it.second ) );
45+ const auto & s = dynamic_cast <ELSStateRef >(e-> getDestination ());
46+ const auto & oEdges = dynamic_cast <const ELSSetOfEdges&>(s-> getOutgoingEdges ());
4247 if (oEdges.empty ())
4348 {
44- edgesToBeRemoved.insert (& e);
45- statesToBeRemoved.insert (& s);
49+ edgesToBeRemoved.insert (e);
50+ statesToBeRemoved.insert (s);
4651 }
4752 }
4853
@@ -57,16 +62,13 @@ namespace MaxPlus::SMPLS {
5762
5863 // remove edges ending in dangling states
5964 // remove edges ending in dangling states from the outgoing edges of their source states
60- for (auto *erIt: edgesToBeRemoved) {
65+ for (const auto *erIt: edgesToBeRemoved) {
6166 const auto & e = dynamic_cast <const ELSEdge&>(*erIt);
6267 this ->removeEdge (e);
63- const auto & s = dynamic_cast <const ELSState& >(e.getSource ());
68+ const auto * const s = dynamic_cast <ELSStateRef >(e.getSource ());
6469
65-
66- // TODO (Marc Geilen) redesign for opportunities for subclasses to modify their FSM structure while external users get const access only. Perhaps through protected access.
67- // Maybe request modifiable access to a given const state/edge?
68- auto & ms = const_cast <ELSState&>(s);
69- ms.removeOutgoingEdge (e);
70+ const auto *ms = dynamic_cast <ELSStateRef>(s);
71+ this ->removeEdge (e);
7072 }
7173
7274 // empty the temporary sets
@@ -81,13 +83,13 @@ namespace MaxPlus::SMPLS {
8183 for (const auto & it: elsEdges)
8284 {
8385
84- auto & e = dynamic_cast <ELSEdge&>( *(it.second ));
85- const auto & s = dynamic_cast <const ELSState& >(e. getDestination ());
86- const auto & oEdges = dynamic_cast <const ELSSetOfEdges&>(s. getOutgoingEdges ());
86+ auto e = dynamic_cast <ELSEdgeRef>(&( *(it.second ) ));
87+ const auto s = dynamic_cast <ELSStateRef >(e-> getDestination ());
88+ const auto & oEdges = dynamic_cast <const ELSSetOfEdges&>(s-> getOutgoingEdges ());
8789 if (oEdges.empty ())
8890 {
89- edgesToBeRemoved.insert (& e);
90- statesToBeRemoved.insert (& s);
91+ edgesToBeRemoved.insert (e);
92+ statesToBeRemoved.insert (s);
9193 }
9294 }
9395 }
@@ -165,8 +167,8 @@ namespace MaxPlus::SMPLS {
165167 // create the FSM states for every pair of a states of the FSM
166168 // and an initial token
167169
168- const auto & I = dynamic_cast <const ELSSetOfStates &>(this ->elsFSM .getInitialStates ());
169- const auto & F = dynamic_cast <const ELSSetOfStates &>(this ->elsFSM .getFinalStates ());
170+ const auto & I = dynamic_cast <const ELSSetOfStateRefs &>(this ->elsFSM .getInitialStates ());
171+ const auto & F = dynamic_cast <const ELSSetOfStateRefs &>(this ->elsFSM .getFinalStates ());
170172 const auto & Q = dynamic_cast <const ELSSetOfStates&>(this ->elsFSM .getStates ());
171173 for (const auto & q: Q)
172174 {
@@ -175,7 +177,7 @@ namespace MaxPlus::SMPLS {
175177 unsigned int nrTokens = 0 ;
176178 if (! e.empty ())
177179 {
178- CString label = (dynamic_cast <ELSEdge* >(*e.begin ()))->getLabel ();
180+ CString label = (dynamic_cast <ELSEdgeRef >(*e.begin ()))->getLabel ();
179181
180182 for (const auto & smIt: this ->sm )
181183 {
@@ -191,13 +193,13 @@ namespace MaxPlus::SMPLS {
191193 nrTokens = (*this ->sm .begin ()).second ->getCols ();
192194 }
193195 // create a state for (q, k)
194- CId qId = (dynamic_cast <ELSState&>(qq)).getLabel ();
196+ const CId qId = (dynamic_cast <ELSState&>(qq)).getLabel ();
195197 bool isInitial = false ;
196198 bool isFinal = false ;
197199 // if els state is initial
198200 for (const auto & i: I)
199201 {
200- CId iId = (dynamic_cast <ELSState&>(*(i. second ) )).getLabel ();
202+ CId iId = (dynamic_cast <const ELSState&>(*i )).getLabel ();
201203 if (iId == qId)
202204 {
203205 isInitial = true ;
@@ -207,7 +209,7 @@ namespace MaxPlus::SMPLS {
207209 // if els state is final
208210 for (const auto & f: F)
209211 {
210- CId fId = (dynamic_cast <ELSState&>(*(f. second ) )).getLabel ();
212+ CId fId = (dynamic_cast <const ELSState&>(*f )).getLabel ();
211213 if (fId == qId)
212214 {
213215 isFinal = true ;
@@ -241,11 +243,11 @@ namespace MaxPlus::SMPLS {
241243
242244 // for every outgoing edge of the state
243245 const auto & t = dynamic_cast <const ELSSetOfEdgeRefs&>(q1.getOutgoingEdges ());
244- for (auto *e: t)
246+ for (const auto *e: t)
245247 {
246- auto & tr = dynamic_cast <ELSEdge&>(* e);
247- CId q2Id = dynamic_cast <const ELSState& >(tr. getDestination ()). getLabel ();
248- CString sc = tr. getLabel ();
248+ const auto * tr = dynamic_cast <ELSEdgeRef>( e);
249+ CId q2Id = dynamic_cast <ELSStateRef >(tr-> getDestination ())-> getLabel ();
250+ CString sc = tr-> getLabel ();
249251 std::shared_ptr<Matrix> Ms = this ->sm .at (sc);
250252 size_t r = Ms->getRows ();
251253 size_t c = Ms->getCols ();
@@ -255,14 +257,14 @@ namespace MaxPlus::SMPLS {
255257 {
256258 for (size_t col = 0 ; col < c; col++)
257259 {
258- MPDelay d = Ms->get (static_cast <unsigned int >(row), static_cast <unsigned int >(col));
260+ const MPDelay d = Ms->get (static_cast <unsigned int >(row), static_cast <unsigned int >(col));
259261
260262 if (!MP_IS_MINUS_INFINITY (d))
261263 {
262- MPAState& src = mpa->getStateLabeled (makeMPAStateLabel (q1Id, static_cast <unsigned int >(col)));
264+ MPAStateRef src = mpa->getStateLabeled (makeMPAStateLabel (q1Id, static_cast <unsigned int >(col)));
263265 MPAState& dst = mpa->getStateLabeled (makeMPAStateLabel (q2Id, static_cast <unsigned int >(row)));
264266 MPAEdgeLabel el = makeMPAEdgeLabel (d, sc);
265- el.mode = CString (tr. getLabel ());
267+ el.mode = CString (tr-> getLabel ());
266268 mpa->addEdge (src, el, dst);
267269 }
268270 }
@@ -434,27 +436,27 @@ namespace MaxPlus::SMPLS {
434436 void SMPLSwithEvents::prepareMatrices (const IOAState& s, std::multiset<Event>& eventList, IOASetOfEdgeRefs& visitedEdges)
435437 {
436438 const auto & adj = dynamic_cast <const IOASetOfEdgeRefs&>(s.getOutgoingEdges ());
437- for (auto *i: adj)
439+ for (const auto *i: adj)
438440 {
439- auto & e = dynamic_cast <IOAEdge&>(* i);
440- if (visitedEdges.count (& e) > 0 ) {
441+ const auto * e = dynamic_cast <IOAEdgeRef>( i);
442+ if (visitedEdges.count (e) > 0 ) {
441443 continue ;
442444 }
443445
444446 // create a unique label for the new edge. this name will also be the mode name for sm
445447 CString modeName = CString (s.getId ());
446448 modeName += " ," ;
447- modeName += e. getLabel ().first + " ," + e. getLabel ().second ;
449+ modeName += e-> getLabel ().first + " ," + e-> getLabel ().second ;
448450
449451 // add the edge with unique name between the corresponding states
450- this ->elsFSM .addEdge (this ->elsFSM .getStateLabeled (s.getLabel ()), modeName, this ->elsFSM .getStateLabeled ((dynamic_cast <const IOAState&>(e. getDestination ())).getLabel ()));
452+ this ->elsFSM .addEdge (this ->elsFSM .getStateLabeled (s.getLabel ()), modeName, this ->elsFSM .getStateLabeled ((dynamic_cast <const IOAState&>(e-> getDestination ())).getLabel ()));
451453
452454 // make a copy so that child node can not modify the parent nodes list of events
453455 // only adds and removes and passes it to it's children
454456 std::multiset<Event> eList (eventList);
455457
456- OutputAction output = e. getLabel ().second ;
457- InputAction input = e. getLabel ().first ;
458+ OutputAction output = e-> getLabel ().second ;
459+ InputAction input = e-> getLabel ().first ;
458460
459461 auto disSm = std::make_shared<DissectedModeMatrix>();
460462 std::shared_ptr<Matrix> sMatrix ; // matrix to be generated and assigned to this edge
@@ -579,12 +581,12 @@ namespace MaxPlus::SMPLS {
579581 }
580582
581583 this ->sm [modeName] = sMatrix ;
582- visitedEdges.insert (& e);
584+ visitedEdges.insert (e);
583585
584586 // we need this later to make all matrices square
585587 biggestMatrixSize = std::max (biggestMatrixSize, std::max (sMatrix ->getCols (), sMatrix ->getRows ()));
586588
587- prepareMatrices (dynamic_cast <const IOAState&>(e. getDestination ()), eList, visitedEdges);
589+ prepareMatrices (dynamic_cast <const IOAState&>(e-> getDestination ()), eList, visitedEdges);
588590 }
589591 }
590592
@@ -629,21 +631,20 @@ namespace MaxPlus::SMPLS {
629631 else // If current state is not final
630632 {
631633 const auto & adj = dynamic_cast <const IOASetOfEdgeRefs&>(s.getOutgoingEdges ());
632- for (auto *i: adj)
634+ for (const auto *i: adj)
633635 {
634636 // make a copy so that child node can not modify the parent nodes list
635637 // only adds and removes and passes it to its children
636638 EventList eList (eventList);
637639
638- auto & e = dynamic_cast <IOAEdge&>(* i);
639- OutputAction output = e. getLabel ().second ;
640- InputAction input = e. getLabel ().first ;
640+ const auto * e = dynamic_cast <IOAEdgeRef>( i);
641+ const OutputAction output = e-> getLabel ().second ;
642+ const InputAction input = e-> getLabel ().first ;
641643 if (!input.empty ()) {
642644 bool eventFound = false ;
643645 EventList::iterator eventIter;
644646 for (eventIter = eList.begin (); eventIter != eList.end (); eventIter++)
645647 {
646- std::list<EventOutcomePair>::iterator gammaIterator;
647648 for (const auto & gammaIterator: this ->gamma )
648649 {
649650 auto p = (EventOutcomePair (gammaIterator));
@@ -682,7 +683,7 @@ namespace MaxPlus::SMPLS {
682683 }
683684 }
684685 }
685- const auto & s2 = dynamic_cast <const IOAState&>(e. getDestination ());
686+ const auto & s2 = dynamic_cast <const IOAState&>(e-> getDestination ());
686687
687688 isConsistentUtil (s2, eList, finalStates, errMsg, visited);
688689 }
@@ -722,7 +723,7 @@ namespace MaxPlus::SMPLS {
722723 // we go through all the rest of the edges and remove them from the automaton
723724 for (; i != outEdge.end (); ++i)
724725 {
725- e = dynamic_cast <IOAEdge* >(*i);
726+ e = dynamic_cast <IOAEdgeRef >(*i);
726727 ioa->removeEdge (*e);
727728 // since we only start with one initial state and remove the others, then the destination state of this edge will be unreachable
728729 // ioa->removeState(dynamic_cast<IOAState*>(e->getDestination()));
0 commit comments