DiaDes  0.1
DIAgnosis of Discrete-Event System
StateCreation.hh
Go to the documentation of this file.
1 
9 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__STATECREATION__HH
10 #define __DIADES__AUTOMATA__EXPERIMENTAL__STATECREATION__HH
11 
18 
19 namespace Diades {
20  namespace Automata {
21  namespace Experimental {
22  using Diades::Utils::Ptr;
23 
32  template<typename _StateMachine>
33  class StateCreation {
34  public:
35 
36  static string
38  return "Automata::StateCreation";
39  }
41  public:
42  using Fsm = _StateMachine;
45 
46  using State = typename Fsm::State;
47 
48  protected:
49  const Fsm & _source;
51  public:
52 
61  _source(source), _target(target) {
62 
63  }
64 
69  const Fsm &
70  source() const {
71  return _source;
72  }
73 
78  const Fsm &
79  target() const {
80  return _target;
81  }
82 
87  Fsm &
88  target() {
89  return _target;
90  }
91 
92 
93  };
94 
103  template<typename StateMachine>
104  class NewStateCreation : public StateCreation<StateMachine> {
105  public:
107 
108  public:
109 
118  SC(source, target) {
119  }
120 
128  virtual std::pair<typename SC::State, bool>
130  return SC::_target->newState();
131  }
132 
133 
134  };
135 
148  template<typename StateMachine>
149  class StateCopy : public StateCreation<StateMachine> {
150  public:
152 
153  public:
154 
163  SC(source, target) {
164  }
165 
174  virtual std::pair<typename SC::State, bool>
176  auto result = SC::target().newState(SC::source().getStatePropertyId(source));
177  if (SC::source().isInitial(source)) {
178  SC::target().setInitial(result.first);
179  }
180  return result;
181  }
182 
191  virtual std::pair<typename SC::State, bool>
192  newState(typename SC::State source, const typename SC::StatePropertyId & statePropertyId) {
193  auto result = SC::target().newState(statePropertyId);
194  if (SC::source().isInitial(source)) {
195  SC::target().setInitial(result.first);
196  }
197  return result;
198  }
199  };
200 
209  template<typename StateMachine, typename _StateProperty>
210  class ManagedStateCreation : public StateCreation<StateMachine> {
211  public:
213  using Fsm = typename SC::Fsm;
214  using StateProperty = _StateProperty;
217  private:
220 
221  public:
222 
227  const StManager &
229  return _sourceStateManager;
230  }
231 
236  StManager &
238  return _targetStateManager;
239  }
240  public:
241 
253  const StManager & sourceStateManager,
254  StManager & targetStateManager) :
255  SC(source, target),
256  _sourceStateManager(sourceStateManager),
257  _targetStateManager(targetStateManager) {
258  }
259 
260  };
261 
262  template<typename StateMachine, typename StateProperty>
263  class ManagedStateCopy : public ManagedStateCreation<StateMachine, StateProperty> {
264  public:
265 
266  public:
268  using Fsm = typename MSC::Fsm;
269  using StManager = typename MSC::StManager;
270  using State = typename MSC::State;
272 
274  Fsm & target,
275  const StManager & sourceStateManager,
276  StManager & targetStateManager) :
277  MSC(source, target, sourceStateManager, targetStateManager) {
278  }
279 
288  virtual std::pair<State, bool>
290  auto propId = MSC::source().getStatePropertyId(source);
291  std::pair<State, bool> result;
292  if (propId != MSC::source().nullStatePropertyId) {
293  if (MSC::sourceStateManager().hasStatePropertyId(propId)) {
294  result = MSC::target().newState(MSC::targetStateManager().statePropertyId(MSC::sourceStateManager().getStateProperty(propId)));
295  } else {
296  result = MSC::target().newState(propId);
297  }
298  } else {
299  result = MSC::target().newState(MSC::source().nullStatePropertyId);
300  }
301  if (MSC::source().isInitial(source)) {
302  MSC::target().setInitial(result.first);
303  }
304  return result;
305  }
306 
315  virtual std::pair<State, bool>
316  newState(State source, const StatePropertyId & statePropertyId) {
317  auto propId = MSC::source().getStatePropertyId(source);
318  std::pair<State, bool> result;
319  if (propId != MSC::source().nullStatePropertyId) {
320 
321  if (MSC::sourceStateManager().hasStatePropertyId(propId)) {
322  const auto & property = MSC::sourceStateManager().getStateProperty(propId);
323  if (!MSC::sourceStateManager().isNull(property)) {
324  // here the source state is associated with a non-null StateProperty
325  // let us check whether the target manager know about this StateProperty
326 
327  if (!MSC::targetStateManager().hasStateProperty(property)) {
328  // the target manager does not know about this StateProperty
329  if (MSC::targetStateManager().setStateProperty(property, statePropertyId)) {
330  // all is fine, new StateProperty set to a new StatePropertyId, ready to
331  // create a new State for this new StatePropertyId
332  result = MSC::target().newState(statePropertyId);
333  } else {
334  // aya ! The StatePropertyId that is given is
335  // already associated with a StateProperty in the manager and it is wrong one,
336  //I give up!
338  "newState (ManagedStateCopy): attempting to create a state associated with a StatePropertyId already assigned to a StateProperty that is not the StateProperty of the source state");
339  }
340  } else {
341  // the target manager knows the StateProperty
342  // so actually the StateProperty is already associated with a StatePropertyId
343  // in target, that identifies one and only one State in the result
344  result = MSC::target().newState(MSC::targetStateManager().statePropertyId(property));
345  }
346  } else {
347  // here, the StateManager associated with the source fsm
348  // knows the StatePropertyId but is associated with a null property
349  // so here I consider that in the target the StateManager
350  // does not know any StateProperty and the target state
351  // is the null StatePropertyId
352  result = std::pair(MSC::target().newState(), true);
353 
354  }
355  } else {
356  // here, the StateManager associated with the source fsm
357  // does not know the StatePropertyId while it is not null
358  // so here I consider that in the target the StateManager
359  // does not know any StateProperty and the target state
360  // is the null StatePropertyId
361  result = std::pair(MSC::target().newState(), true);
362  }
363  } else {
364  // the source state is associated to a null StatePropertyId, so should be
365  // the target state
366  result = std::pair(MSC::target().newState(), true);
367  }
368  if (MSC::source().isInitial(source)) {
369  MSC::target().setInitial(result.first);
370  }
371  return result;
372  }
373  };
374 
384  template<typename StateMachine>
386  public:
387 
389  using State = typename StateMachine::State;
391  public:
392 
393 
394 
404  virtual std::pair<State, bool>
405  newState(State source, const FsmBs & bs) = 0;
406  };
407 
416  template<typename StateMachine>
418  public:
419 
421  using State = typename StateMachine::State;
423  public:
424 
425 
426 
436  virtual std::pair<State, bool>
437  newState(const FsmBs & bs) = 0;
438  };
439 
450  template<typename StateMachine>
451  class OnlySourceStateCreator : public StateCreation<StateMachine>,
452  public BeliefStateWithSourceStateCreator<StateMachine> {
453  public:
456  using State = typename SC::State;
457  using FsmBs = typename BSSSC::FsmBs;
458  public:
459 
466  SC(source, target), BSSSC() {
467  }
468 
477  virtual std::pair<State, bool>
478  newState(State source, const FsmBs & bs) {
479  // why using StateCreation<StateMachine>::_target instead of _target? Does not compile
480  // see https://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html for explanation
481 
482  // here we do not take into account the belief state of source
483  return SC::_target.newState(SC::_source.getStatePropertyId(source));
484  }
485  };
486 
498  template<typename StateMachine, typename StatePropertyCombination>
500  public ManagedStateCreation< StateMachine, typename StatePropertyCombination::StateProperty>,
501  public BeliefStateWithSourceStateCreator<StateMachine> {
502  public:
505  using State = typename MSC::State;
506  using FsmBs = typename BSSSC::FsmBs;
509 
518  const StatePropertyManager<StateProperty, StatePropertyId> & sourceStateManager,
520  MSC(source, target, sourceStateManager, targetStateManager), BSSSC() {
521  }
522 
532  virtual std::pair<State, bool>
533  newState(State source, const FsmBs & bs) {
534 
535  // get the iterator of the StatePropertyId associated with the state *bs.begin() in _source
536  auto begin = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.begin()));
537  // get the iterator of the StatePropertyId associated with the-after-the-last-state bs.end() in _source
538  auto end = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.end()));
539  // creation of the new StateProperty compatible with the projection as specified in StatePropertyProjection
540  auto statePropertyId = MSC::targetStateManager().statePropertyId(
541  StatePropertyCombination()(MSC::sourceStateManager().getStateProperty(MSC::_source.getStatePropertyId(source)),
542  begin, end));
543  // create the state in target with the statePropertyId created in the _projectionStateManager
544  return MSC::_target.newState(statePropertyId);
545  }
546 
547  };
548 
560  template<typename StateMachine>
561  class OnlyStateCreator : public StateCreation<StateMachine>,
562  public BeliefStateBasedStateCreator<StateMachine> {
563  public:
566  using State = typename SC::State;
567  using FsmBs = typename BSBSC::FsmBs;
568  public:
569 
576  SC(source, target), BSBSC() {
577  }
578 
587  virtual std::pair<State, bool>
588  newState(const FsmBs & bs) {
589  // why using StateCreation<StateMachine>::_target instead of _target? Does not compile
590  // see https://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html for explanation
591 
592  // here we do not take into account the belief state of source
593  return std::make_pair(SC::_target.newState(), true);
594  }
595  };
596 
607  template<typename StateMachine, typename StatePropertyCombination>
609  public ManagedStateCreation<StateMachine, typename StatePropertyCombination::Property>,
610  public BeliefStateBasedStateCreator<StateMachine> {
611  public:
614  using State = typename MSC::State;
615  using FsmBs = typename BSBSC::FsmBs;
618 
619  public:
620 
629  const StatePropertyManager<StateProperty, StatePropertyId> & sourceStateManager,
631  MSC(source, target, sourceStateManager, targetStateManager), BSBSC() {
632  }
633 
643  virtual std::pair<State, bool>
644  newState(const FsmBs & bs) {
645  // get the iterator of the StatePropertyId associated with the state *bs.begin() in _source
646  auto begin = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.begin()));
647  // get the iterator of the StatePropertyId associated with the-after-the-last-state bs.end() in _source
648  auto end = MSC::sourceStateManager().infoIdRangeInfoIterator(MSC::_source.statePropertyIdRangeIterator(bs.end()));
649  // creation of the new StateProperty compatible with the projection as specified in StatePropertyProjection
650  auto statePropertyId = MSC::targetStateManager().statePropertyId(
651  StatePropertyCombination()(begin, end));
652  // create the state in target with the statePropertyId created in the _projectionStateManager
653  return MSC::_target.newState(statePropertyId);
654  }
655 
656  };
657 
658  template<typename T>
660 
661  template<typename T1, typename T2>
663  {
664 
665  static constexpr bool isManaged() {
666  return true;
667  }
668  };
669 
670  template<typename T1>
672  {
673 
674  static constexpr bool isManaged() {
675  return false;
676  }
677  };
678 
715  template<typename _StateMachine, typename _StateProperty, typename StatePropertyCombination>
717  public:
718 
723  using StateMachine = _StateMachine;
724 
729 
734  using StateProperty = _StateProperty;
735 
740 
745  using State = typename StateMachine::State;
746 
747  protected:
755 
756 
765 
766 
772 
779 
780 
786  typename Ptr<std::vector<State>>::ConstP _pstates;
787 
788  public:
789 
804  ManagedSynchronisedStateCreation(const std::vector<typename Ptr<StateMachine>::ConstP> & machines,
805  const std::vector<typename Ptr<SManager>::ConstP> & managers,
806  SManager & syncManager)
807  : _machines(Ptr<std::vector<typename Ptr<StateMachine>::ConstP>>::get(machines)),
808  _managers(Ptr<std::vector<typename Ptr<SManager>::ConstP>>::get(managers)),
809  _syncManager(Ptr<SManager>::get(syncManager)),
810  _target(nullptr),
811  _pstates(nullptr) {
812  }
813 
819  void
821  _target = Ptr<StateMachine>::get(target);
822  }
823 
824  private:
825 
836  public:
844  using Iterator = size_t;
845 
850 
857  const Data & operator()(const MapStructure & m, size_t index) const {
858  return m._managers->at(index)->getStateProperty(m._machines->at(index)->getStatePropertyId(m._pstates->at(index)));
859  }
860  };
861 
862  public:
863 
871  virtual std::pair<State, bool>
872  newState(const std::vector<State> & states) {
873  /*
874  * If no target, no state is created
875  */
876  if (_target == nullptr) {
877  return std::pair<State, bool>(State(), false);
878  }
879 
880  /*
881  * Storing the current state vector before using GetStateProperty
882  */
883  _pstates = Ptr<std::vector < State>>::get(states);
884 
885  /*
886  * The range of StateProperties will be accessed with the help
887  * of a RangeMapperIterator (from diades/utils/Iterator.hh)
888  * The Mapper for this RangeMapperIterator is a GetStateProperty functor
889  * The Iterator is simply the size_t index of the states vector
890  *
891  * The range is defined by [begin,end[
892  */
895  RangeMapperIterator<GetStateProperty> end(*this, states.size());
896 
897 
898 
899  /*
900  * Creation of the statePropertyId by that syncManager resulting from
901  * the created StateProperty. The created StateProperty is a combination
902  * of the local StateProperty's from the current local states
903  */
904  auto statePropertyId = _syncManager->statePropertyId(StatePropertyCombination()(begin, end));
905 
906  /*
907  * effective creation of the State associated with statePropertyId if needed.
908  */
909  return _target->newState(statePropertyId);
910  }
911  };
912 
913  }
914  }
915 }
916 
917 
918 #endif /* __DIADES__AUTOMATA__EXPERIMENTAL__STATECREATION__HH */
919 
StateCopy(const StateMachine &source, StateMachine &target)
ManagedBeliefStateWithSourceStateCreator(const StateMachine &source, StateMachine &target, const StatePropertyManager< StateProperty, StatePropertyId > &sourceStateManager, StatePropertyManager< StateProperty, StatePropertyId > &targetStateManager)
static ConstP get(const T &obj)
Definition: Pointer.hh:37
#define always_require(Exception, expr, message)
Definition: Assertion.hh:122
virtual std::pair< State, bool > newState(State source)
const T * ConstP
Definition: Pointer.hh:31
virtual std::pair< State, bool > newState(State source, const FsmBs &bs)
FaultyEventStateMachine< CandidateId, EventInfoId > Fsm
STL namespace.
virtual std::pair< State, bool > newState(const FsmBs &bs)
OnlyStateCreator(const StateMachine &source, StateMachine &target)
OnlySourceStateCreator(const StateMachine &source, StateMachine &target)
StIndexes states
Definition: Run.cc:266
ManagedSynchronisedStateCreation(const std::vector< typename Ptr< StateMachine >::ConstP > &machines, const std::vector< typename Ptr< SManager >::ConstP > &managers, SManager &syncManager)
virtual std::pair< State, bool > newState(const std::vector< State > &states)
ManagedBeliefStateBasedStateCreator(const StateMachine &source, StateMachine &target, const StatePropertyManager< StateProperty, StatePropertyId > &sourceStateManager, StatePropertyManager< StateProperty, StatePropertyId > &targetStateManager)
typename MSC::StatePropertyId StatePropertyId
AutFsm::State State
Definition: Run.cc:72
virtual std::pair< typename SC::State, bool > newState(typename SC::State source)
const Data & operator()(const MapStructure &m, size_t index) const
ManagedStateCreation(const Fsm &source, Fsm &target, const StManager &sourceStateManager, StManager &targetStateManager)
Namespace of the Diades project.
virtual std::pair< State, bool > newState(const FsmBs &bs)
NewStateCreation(const StateMachine &source, StateMachine &target)
virtual std::pair< State, bool > newState(State source, const StatePropertyId &statePropertyId)
virtual std::pair< typename SC::State, bool > newState(typename SC::State source)
ConstIterator on the Net.
Ptr< std::vector< typename Ptr< StateMachine >::ConstP > >::ConstP _machines
virtual std::pair< State, bool > newState(State source, const FsmBs &bs)
StateCreation(const Fsm &source, Fsm &target)
Ptr< std::vector< typename Ptr< SManager >::ConstP > >::ConstP _managers
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)