DiaDes  0.1
DIAgnosis of Discrete-Event System
FiniteAutomaton.hh
Go to the documentation of this file.
1 
8 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__FINITEAUTOMATON__HH__
9 #define __DIADES__AUTOMATA__EXPERIMENTAL__FINITEAUTOMATON__HH__
10 
11 #include<unordered_set>
15 
16 namespace Diades
17 {
18  namespace Automata
19  {
20  namespace Experimental
21  {
22  using Diades::Utils::Msg;
23 
44  template<typename _StatePropertyId,
45  typename _InputSymbolId,
46  typename _NullStatePropertyId = NullValue<_StatePropertyId>,
47  typename _NullInputSymbolId = NullValue< _InputSymbolId>>
48  class FiniteAutomaton : public StateMachine<_StatePropertyId, _InputSymbolId, _NullStatePropertyId, _NullInputSymbolId>
49  {
50  public:
52  using InputSymbolId = _InputSymbolId;
53  using StatePropertyId = _StatePropertyId;
54  using NullStatePropertyId = _NullStatePropertyId;
55  using NullInputSymbolId = _NullInputSymbolId;
56  using State = typename SM::State;
57  using Transition = typename SM::Transition;
58  using AcceptingStates = std::unordered_set<State>;
59  using ConstAcceptingStateIterator = typename AcceptingStates::const_iterator;
60  using AcceptingStateIterator = typename AcceptingStates::iterator;
61 
66 
67  private:
69 
70  public:
71 
76  FiniteAutomaton() : SM(), _accepting()
77  {
78  }
79 
85  FiniteAutomaton(const FiniteAutomaton & automaton) : SM(automaton)
86  {
87  // each copied state has the id of the state in the given automaton
88  std::for_each(automaton.acceptingStateBegin(),
89  automaton.acceptingStateEnd(),
90  [&](State s)
91  {
92  setAcceptingState(SM::behaviour().getNode(s.id()));
93  });
94  }
95 
102  {
103  if(this != &automaton)
104  {
105  clear();
106  SM::operator=(automaton);
107  // each copied state has the id of the state in the given automaton
108  std::for_each(automaton.acceptingStateBegin(),
109  automaton.acceptingStateEnd(),
110  [&](State s)
111  {
112  setAcceptingState(SM::behaviour().getNode(s.id()));
113  });
114  }
115  return *this;
116  }
117 
125  {
126  return SM::eventBegin();
127  }
128 
136  {
137  return SM::eventEnd();
138  }
139 
145  const vector<InputSymbolId> &
147  {
148  return SM::events();
149  }
150 
155  State
156  initialState() const
157  {
159  {
160  return State();
161  }
162  if(SM::numberOfInitialStates() > 1)
163  {
164  return State();
165  }
166  return *SM::initialStateBegin();
167  }
168 
174  virtual
175  bool
176  isValid() const
177  {
178  return SM::isValid() && initialState().valid();
179  }
180 
184  decltype(auto)
186  {
187  return SM::numberOfEvents();
188  }
189 
196  {
197  return _accepting.begin();
198  }
199 
206  {
207  return _accepting.end();
208  }
209 
214  const AcceptingStates &
216  {
217  return _accepting;
218  }
219 
228  {
229  if(isAcceptingState(state))
230  {
231  unsetAcceptingState(state);
232  }
233  else
234  {
235  setAcceptingState(state);
236  }
237  }
238 
242  virtual
243  void
245  {
247  SM::clear();
248  }
249 
258  virtual void
260  {
261  unsetAcceptingState(state);
262  SM::deleteState(state);
263  }
264 
271  std::pair<AcceptingStateIterator, bool>
273  {
274  return _accepting.insert(state);
275  }
276 
285  {
286  auto it = _accepting.find(state);
287  if(it != _accepting.end())
288  {
289  return _accepting.erase(_accepting.find(state));
290  }
291  return it;
292  }
293 
297  void
299  {
300  _accepting.clear();
301  }
302 
307  decltype(auto)
309  {
310  return _accepting.size();
311  }
312 
318  void
320  {
322  }
323 
329  bool
331  {
332  return _accepting.find(s) != _accepting.end();
333  }
334 
335  };
336 
337  template<typename FA, typename BS>
338  struct
340  {
341  using State = typename FA::State;
342 
343  MakeAcceptingState(const FA & source, FA & target, const BS & bs, State s)
344  {
345  auto it = std::find_if(bs.begin(), bs.end(),
346  [&](State s) -> bool
347  {
348  return source.isAcceptingState(s);
349  });
350  if(it != bs.end())
351  {
352  target.setAcceptingState(s);
353  }
354  };
355  };
356 
367  template<typename S, typename I, typename NS, typename NI>
369  public StateCreation<FiniteAutomaton<S, I, NS, NI>>,
370  public BeliefStateWithSourceStateCreator<FiniteAutomaton<S, I, NS, NI>>
371  {
372  public:
376  using State = typename SC::State;
377  using FsmBs = typename BSSSC::FsmBs;
378  public:
379 
385  OnlySourceStateCreator(const FA & source, FA & target) :
386  SC(source, target), BSSSC()
387  {
388  }
389 
398  virtual std::pair<State, bool>
399  newState(State source, const FsmBs & bs)
400  {
401  // why using StateCreation<StateMachine>::_target instead of _target? Does not compile
402  // see https://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html for explanation
403 
404  // here we do not take into account the belief state of source
405  auto result = SC::_target.newState(SC::_source.getStatePropertyId(source));
406  MakeAcceptingState<FA, FsmBs>(SC::_source, SC::_target, bs, std::get<0>(result));
407  return result;
408  }
409  };
410 
422  template<typename S, typename I, typename NS, typename NI, typename StatePropertyCombination>
423  class ManagedBeliefStateWithSourceStateCreator<FiniteAutomaton<S, I, NS, NI>, StatePropertyCombination> :
424  public ManagedStateCreation< FiniteAutomaton<S, I, NS, NI>, typename StatePropertyCombination::StateProperty>,
425  public BeliefStateWithSourceStateCreator<FiniteAutomaton<S, I, NS, NI>>
426  {
427  public:
428 
429 
433  using State = typename MSC::State;
434  using FsmBs = typename BSSSC::FsmBs;
437 
446  const StatePropertyManager<StateProperty, StatePropertyId> & sourceStateManager,
448  MSC(source, target, sourceStateManager, targetStateManager), BSSSC()
449  {
450  }
451 
461  virtual std::pair<State, bool>
462  newState(State source, const FsmBs & bs)
463  {
464 
465  // get the iterator of the StatePropertyId associated with the state *bs.begin() in _source
466  auto begin = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.begin()));
467  // get the iterator of the StatePropertyId associated with the-after-the-last-state bs.end() in _source
468  auto end = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.end()));
469  // creation of the new StateProperty compatible with the projection as specified in StatePropertyProjection
470  auto statePropertyId = MSC::targetStateManager().statePropertyId(
471  StatePropertyCombination()(MSC::sourceStateManager().getStateProperty(MSC::_source.getStatePropertyId(source)),
472  begin, end));
473  // create the state in target with the statePropertyId created in the _projectionStateManager
474  auto result = MSC::_target.newState(statePropertyId);
475  MakeAcceptingState<FA, FsmBs>(MSC::_source, MSC::_target, bs, std::get<0>(result));
476  return result;
477  }
478 
479  };
480 
492  template<typename S, typename I, typename NS, typename NI>
493  class OnlyStateCreator< FiniteAutomaton<S, I, NS, NI> > : public StateCreation<FiniteAutomaton<S, I, NS, NI>>,
494  public BeliefStateBasedStateCreator<FiniteAutomaton<S, I, NS, NI>>
495  {
496  public:
500  using State = typename SC::State;
501  using FsmBs = typename BSBSC::FsmBs;
502  public:
503 
509  OnlyStateCreator(const FA & source, FA & target) :
510  SC(source, target), BSBSC()
511  {
512  }
513 
522  virtual std::pair<State, bool>
523  newState(const FsmBs & bs)
524  {
525  // why using StateCreation<StateMachine>::_target instead of _target? Does not compile
526  // see https://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html for explanation
527 
528  // here we do not take into account the belief state of source
529  auto result = std::make_pair(SC::_target.newState(), true);
530  MakeAcceptingState<FA, FsmBs>(SC::_source, SC::_target, bs, std::get<0>(result));
531  return result;
532  }
533 
534 
535 
536 
537 
538  };
539 
550  template<typename S, typename I, typename NS, typename NI, typename StatePropertyCombination>
551  class ManagedBeliefStateBasedStateCreator <FiniteAutomaton<S, I, NS, NI>, StatePropertyCombination> :
552  public ManagedStateCreation<FiniteAutomaton<S, I, NS, NI>, typename StatePropertyCombination::Property>,
553  public BeliefStateBasedStateCreator<FiniteAutomaton<S, I, NS, NI>>
554  {
555  public:
559  using State = typename MSC::State;
560  using FsmBs = typename BSBSC::FsmBs;
563 
564  public:
565 
573  ManagedBeliefStateBasedStateCreator(const FA & source, FA & target,
574  const StatePropertyManager<StateProperty, StatePropertyId> & sourceStateManager,
576  MSC(source, target, sourceStateManager, targetStateManager), BSBSC()
577  {
578  }
579 
589  virtual std::pair<State, bool>
590  newState(const FsmBs & bs)
591  {
592  // get the iterator of the StatePropertyId associated with the state *bs.begin() in _source
593  auto begin = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.begin()));
594  // get the iterator of the StatePropertyId associated with the-after-the-last-state bs.end() in _source
595  auto end = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.end()));
596  // creation of the new StateProperty compatible with the projection as specified in StatePropertyProjection
597  auto statePropertyId = MSC::targetStateManager().statePropertyId(
598  StatePropertyCombination()(begin, end));
599  // create the state in target with the statePropertyId created in the _projectionStateManager
600  auto result = MSC::_target.newState(statePropertyId);
601  MakeAcceptingState<FA, FsmBs>(MSC::_source, MSC::_target, bs, std::get<0>(result));
602  return result;
603  }
604 
605 
606 
607  };
608 
645  template <typename S, typename I, typename NS, typename NI,
646  typename _StateProperty, typename StatePropertyCombination>
648  public ManagedSynchronisedStateCreation<FiniteAutomaton<S, I, NS, NI>, _StateProperty, StatePropertyCombination>
649  {
650  public:
651  using MSSC = ManagedSynchronisedStateCreation<FiniteAutomaton<S, I, NS, NI>, _StateProperty, StatePropertyCombination>;
652 
653  public:
654 
670  const std::vector<typename Ptr<typename MSSC::SManager>::ConstP> & managers,
671  typename MSSC::SManager & syncManager)
672  : MSSC(machines, managers, syncManager)
673  {
674  }
675 
676 
677  private:
678 
685  void
686  setAcceptingState(const std::vector<typename MSSC::State> & states,
687  typename MSSC::State newState)
688  {
689  bool isNotAcceptor = false;
690  size_t index = 0;
691  while(!isNotAcceptor && (index != states.size()))
692  {
693  isNotAcceptor = !MSSC::_machines->at(index)->isAcceptingState(states[index]);
694  ++index;
695  }
696  if(!isNotAcceptor)
697  {
698  MSSC::_target->setAcceptingState(newState);
699  }
700  }
701 
702  public:
703 
711  virtual std::pair<typename MSSC::State, bool>
712  newState(const std::vector<typename MSSC::State> & states)
713  {
714 
715  auto result = MSSC::newState(states);
716  if(std::get<1>(result))
717  {
718  setAcceptingState(states, std::get<0>(result));
719  }
720  return result;
721  }
722  };
723 
724 
725 
740  template <typename S, typename I, typename NS, typename NI>
741  class StateCopy<FiniteAutomaton<S, I, NS, NI>> : public StateCreation<FiniteAutomaton<S, I, NS, NI>>
742  {
743  public:
746 
747  public:
748 
756  StateCopy(const FA & source, FA & target) :
757  SC(source, target)
758  {
759  }
760 
769  virtual std::pair<typename SC::State, bool>
770  newState(typename SC::State source)
771  {
772 
773 
774  auto result = SC::target().newState(SC::source().getStatePropertyId(source));
775  if(SC::source().isInitial(source))
776  {
777  SC::target().setInitial(result.first);
778  }
779  if(SC::source().isAcceptingState(source))
780  {
781  SC::target().setAcceptingState(result.first);
782  }
783  return result;
784  }
785 
794  virtual std::pair<typename SC::State, bool>
795  newState(typename SC::State source, const typename SC::StatePropertyId & statePropertyId)
796  {
797  auto result = SC::target().newState(statePropertyId);
798  if(SC::source().isInitial(source))
799  {
800  SC::target().setInitial(result.first);
801  }
802  if(SC::source().isAcceptingState(source))
803  {
804  SC::target().setAcceptingState(result.first);
805  }
806  return result;
807  }
808  };
809 
828  template<typename S, typename I, typename NS, typename NI, typename StateProperty>
830  public ManagedStateCreation<FiniteAutomaton<S, I, NS, NI>, StateProperty>
831  {
832  public:
834  using Fsm = typename MSC::Fsm;
835  using StManager = typename MSC::StManager;
836  using State = typename MSC::State;
838 
839  ManagedStateCopy(const Fsm & source,
840  Fsm & target,
841  const StManager & sourceStateManager,
842  StManager & targetStateManager) :
843  MSC(source, target, sourceStateManager, targetStateManager)
844  {
845  }
846 
860  virtual std::pair<State, bool>
861  newState(State source)
862  {
863  auto propId = MSC::source().getStatePropertyId(source);
864  std::pair<State, bool> result;
865  if(propId != MSC::source().nullStatePropertyId)
866  {
867  if(MSC::sourceStateManager().hasStatePropertyId(propId))
868  {
869  result = MSC::target().newState(MSC::targetStateManager().statePropertyId(MSC::sourceStateManager().getStateProperty(propId)));
870  }
871  else
872  {
873  result = MSC::target().newState(propId);
874  }
875  }
876  else
877  {
878  result = MSC::target().newState(MSC::source().nullStatePropertyId);
879  }
880  if(MSC::source().isInitial(source))
881  {
882  MSC::target().setInitial(result.first);
883  }
884  if(MSC::source().isAcceptingState(source))
885  {
886  MSC::target().setAcceptingState(result.first);
887  }
888  return result;
889  }
890 
905  virtual std::pair<State, bool>
906  newState(State source, const StatePropertyId & statePropertyId)
907  {
908  auto result = MSC::target().newState(statePropertyId);
909  if(result.second)
910  {
911  auto propId = MSC::source().getStatePropertyId(source);
912  if(propId != MSC::source().nullStatePropertyId)
913  {
914  if(MSC::sourceStateManager().hasStatePropertyId(propId))
915  {
916  MSC::targetStateManager().setStateProperty(MSC::sourceStateManager().getStateProperty(propId),
917  statePropertyId);
918  }
919  }
920  }
921 
922  if(MSC::source().isInitial(source))
923  {
924  MSC::target().setInitial(result.first);
925  }
926  if(MSC::source().isAcceptingState(source))
927  {
928  MSC::target().setAcceptingState(result.first);
929  }
930  return result;
931  }
932  };
933 
934 
935 
936  };
937  };
938 };
939 
940 
941 #endif /* __DIADES__AUTOMATA__EXPERIMENTAL__FINITEAUTOMATON__HH__ */
942 
EventPropertyIdIterator eventEnd() const
typename AcceptingStates::const_iterator ConstAcceptingStateIterator
virtual std::pair< State, bool > newState(State source, const FsmBs &bs)
InputSymbolIdIterator inputSymbolIdEnd() const
typename AcceptingStates::iterator AcceptingStateIterator
vector< EventPropertyId >::const_iterator EventPropertyIdIterator
StateMachine & operator=(const StateMachine &machine)
ConstAcceptingStateIterator acceptingStateBegin() const
const T * ConstP
Definition: Pointer.hh:31
StatePropertyIdRangeIterator< StateIterator > statePropertyIdRangeIterator(StateIterator iterator) const
FaultyEventStateMachine< CandidateId, EventInfoId > Fsm
const AcceptingStates & acceptingStates() const
ConstAcceptingStateIterator acceptingStateEnd() const
MakeAcceptingState(const FA &source, FA &target, const BS &bs, State s)
InitialStateIterator initialStateEnd() const
void setAcceptingState(const std::vector< typename MSSC::State > &states, typename MSSC::State newState)
StIndexes states
Definition: Run.cc:266
FiniteAutomaton & operator=(const FiniteAutomaton &automaton)
const vector< InputSymbolId > & inputSymbolIds() const
virtual void insertEventPropertyId(const EventPropertyId &event)
AutFsm::State State
Definition: Run.cc:72
const vector< EventPropertyId > & events() const
std::pair< AcceptingStateIterator, bool > setAcceptingState(State state)
virtual void deleteState(State state)
delete state
Namespace of the Diades project.
virtual std::pair< typename MSSC::State, bool > newState(const std::vector< typename MSSC::State > &states)
virtual std::pair< typename SC::State, bool > newState(typename SC::State source)
EventPropertyIdIterator eventBegin() const
InitialStateIterator initialStateBegin() const
virtual std::pair< State, bool > newState(State source, const StatePropertyId &statePropertyId)
FiniteAutomaton(const FiniteAutomaton &automaton)
InputSymbolIdIterator inputSymbolIdBegin() const
typename SM::EventPropertyIdIterator InputSymbolIdIterator
boost::format Msg
Definition: Verbose.hh:42
ManagedBeliefStateBasedStateCreator(const FA &source, FA &target, const StatePropertyManager< StateProperty, StatePropertyId > &sourceStateManager, StatePropertyManager< StateProperty, StatePropertyId > &targetStateManager)
virtual std::pair< typename SC::State, bool > newState(typename SC::State source, const typename SC::StatePropertyId &statePropertyId)
ManagedStateCopy(const Fsm &source, Fsm &target, const StManager &sourceStateManager, StManager &targetStateManager)
ManagedBeliefStateWithSourceStateCreator(const FA &source, FA &target, const StatePropertyManager< StateProperty, StatePropertyId > &sourceStateManager, StatePropertyManager< StateProperty, StatePropertyId > &targetStateManager)
AcceptingStateIterator unsetAcceptingState(State state)
ManagedSynchronisedAcceptingStateCreation(const std::vector< typename Ptr< typename MSSC::StateMachine >::ConstP > &machines, const std::vector< typename Ptr< typename MSSC::SManager >::ConstP > &managers, typename MSSC::SManager &syncManager)
void swapAcceptingState(State state)
swap the status of the state as accepting/non-accepting