DiaDes  0.1
DIAgnosis of Discrete-Event System
DdSyncDescriptor.hh
Go to the documentation of this file.
1 
9 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__DDSYNCDESCRIPTOR__HH__
10 #define __DIADES__AUTOMATA__EXPERIMENTAL__DDSYNCDESCRIPTOR__HH__
11 
15 
16 namespace Diades
17 {
18  namespace Automata
19  {
20  namespace Experimental
21  {
22 
29  {
30  public:
31 
37  {
38  private:
39  using SynchroDictionary = std::map<std::string, DdAutEventId>;
41  using ConstIterator = SynchroDictionary::const_iterator;
42  using Iterator = SynchroDictionary::iterator;
43 
44  public:
48  Synchronisation() = default;
49 
50 
55  Synchronisation(Synchronisation const& other) = default;
61  Synchronisation& operator=(Synchronisation const& other) = default;
66  Synchronisation(Synchronisation&& other) = default;
72  Synchronisation& operator=(Synchronisation&& other) = default;
76  ~Synchronisation() = default;
77 
78 
90  std::pair<Iterator, bool> insertSynchronisation(const std::string & compName, DdAutEventId evt);
91 
92 
97  void eraseComponent(const std::string & compName);
98 
105  DdAutEventId getEvent(const std::string & compName) const;
106 
107 
113  bool containsComponent(const std::string & compName) const;
114 
115 
120  ConstIterator begin() const;
121 
126  ConstIterator end() const;
127 
128 
133  Iterator begin();
134 
139  Iterator end();
140 
141 
145  void clear();
146 
151  size_t
153  {
154  return _synchronisation.size();
155  }
156 
157  };
158  private:
159  using SynchronisationList = std::list<Synchronisation>;
161  using ConstIterator = SynchronisationList::const_iterator;
162  using Iterator = SynchronisationList::iterator;
163 
164  public:
168  DdSyncDescriptor() = default;
169 
170 
175  DdSyncDescriptor(DdSyncDescriptor const& other) = default;
181  DdSyncDescriptor& operator=(DdSyncDescriptor const& other) = default;
186  DdSyncDescriptor(DdSyncDescriptor&& other) = default;
192  DdSyncDescriptor& operator=(DdSyncDescriptor&& other) = default;
193 
197  ~DdSyncDescriptor() = default;
198 
199 
204  ConstIterator begin() const;
205 
210  ConstIterator end() const;
211 
212 
217  Iterator begin();
218 
223  Iterator end();
224 
228  void clear();
229 
235  Iterator insertSynchronisation(Synchronisation && synchronisation);
236 
237 
246  virtual bool readStream(std::istream& stream, std::string& error);
247 
248 
249 
255  virtual bool
256  toStream(std::ostream& stream);
257 
262  SynchronisationList::size_type
264  {
265  return _syncList.size();
266  }
267 
273  ostream &
274  toDot(ostream & os) const;
275 
276  private:
277 
282  enum class ParsingState
283  {
284  LeftP, RightP, Name, QuoteName, Comma, Evt, Out, Error, End
285  };
286 
287  using Size = std::string::size_type;
288  using QuoteIndexes = std::pair<Size, Size>;
289  using QuoteIndexesVector = std::vector< QuoteIndexes >;
290  static constexpr Size NPOS = std::string::npos;
291 
292 
305  bool analyseSynchronisationLine(const std::string & syncLine, std::string& error);
306 
307 
308 
317  bool
318  analyseQuoteLabels(const std::string & syncLine,
319  Size currentIndex,
320  QuoteIndexesVector & quoteIndexes,
321  std::string& error);
322 
323 
331  ParsingState analyseOut(const std::string & syncLine,
332  Size & currentIndex, std::string & error);
333 
334 
335 
336 
347  analyseLeftP(const std::string& syncLine,
348  const QuoteIndexesVector& quoteIndexes,
349  size_t currentQuoteIndex,
350  Size& currentIndex,
351  std::string& error);
352 
361  ParsingState analyseName(const std::string & syncLine,
362  Size & currentIndex,
363  std::string & label,
364  std::string & error);
365 
366 
377  ParsingState analyseQuoteName(const std::string & syncLine,
378  const QuoteIndexesVector & quoteIndexes,
379  size_t & currentQuoteIndex,
380  Size & currentIndex,
381  std::string & label,
382  std::string & error);
383 
391  ParsingState analyseComma(const std::string & syncLine,
392  Size & currentIndex,
393  std::string & error);
394 
403  ParsingState analyseEvt(const std::string & syncLine, Size & currentIndex, DdAutEventId & evt,
404  std::string & error);
405 
413  ParsingState analyseRightP(const std::string & syncLine, Size & currentIndex,
414  std::string & error);
415 
416  };
417 
424  template<typename DdAutFsmType>
425  bool
427  DdSyncDescriptor & desc)
428  {
429  bool success = rules.beginOfSynchronisedEvents() != rules.endOfSynchronisedEvents();
430  std::for_each(rules.beginOfSynchronisedEvents(), rules.endOfSynchronisedEvents(),
431  [&](const SynchronisationEvent<DdAutFsmType> & syncEvent)
432  {
434  auto it = syncEvent.beginOfSupport();
435  while(it != syncEvent.endOfSupport())
436  {
437  sync.insertSynchronisation(it->name(), syncEvent.getAssociatedEvent(it));
438  ++it;
439  }
440  desc.insertSynchronisation(std::move(sync));
441  });
442  return success;
443  }
444 
454  template<typename DdAutFsmType>
455  bool
457  ParametrizedSynchronisation<DdAutFsmType> & rules, std::string & errorMsg)
458  {
460  bool success = true;
461  rules.clearRules();
462  std::for_each(desc.begin(), desc.end(),
463  [&](const DdSyncDescriptor::Synchronisation & sync)
464  {
465  std::map<typename Ptr<typename Sync::Component>::ConstP, DdAutEventId> events;
466  std::for_each(sync.begin(), sync.end(),
467  [&](const std::pair<std::string, DdAutEventId> & entry)
468  {
469  if(success)
470  {
471  typename Sync::ComponentIterator compIt = rules.findComponent(entry.first);
472  success = compIt != rules.endOfComponents();
473  if(success)
474  {
475  success = compIt->containsEvent(entry.second);
476  if(success)
477  {
478  events[Ptr<typename Sync::Component>::get(*compIt)] = entry.second;
479  }
480  else
481  {
482  errorMsg += "descriptorToRules: the component " + entry.first
483  + " has no event with the id " + std::to_string(entry.second)+ +"Check that the rules are properly initialised with "
484  + "\n Please check that the rules are properly initialised with\n"
485  + "the correct set of components before reading this "
486  + "\n synchronisation rules'descriptor.\n";
487  }
488 
489  }
490  else
491  {
492  errorMsg += "descriptorToRules: the component " + entry.first
493  + " is unknown.\n "
494  + "Check that the rules are properly initialised with "
495  + "\n the correct set of components before reading this "
496  + "\n synchronisation rules'descriptor.\n";
497 
498  }
499  }
500 
501  });
502  if(success)
503  {
504  rules.synchronise(events.begin(), events.end());
505  }
506 
507  });
508  if(success)
509  {
510  rules.close();
511  }
512  else
513  {
514  rules.clear();
515  errorMsg += "descriptorToRules: error so the rules are cleared.\n";
516  }
517  return success;
518  }
519  }
520  }
521 }
522 
523 
524 
525 #endif /* DDSYNCDESCRIPTOR_HH */
526 
SynchronisationList::size_type numberOfSynchronisations() const
static ConstP get(const T &obj)
Definition: Pointer.hh:37
virtual bool readStream(std::istream &stream, std::string &error)
ConstSynchronisationIterator endOfSynchronisedEvents() const
ParsingState analyseRightP(const std::string &syncLine, Size &currentIndex, std::string &error)
bool descriptorToRules(const DdSyncDescriptor &desc, ParametrizedSynchronisation< DdAutFsmType > &rules, std::string &errorMsg)
ParsingState analyseQuoteName(const std::string &syncLine, const QuoteIndexesVector &quoteIndexes, size_t &currentQuoteIndex, Size &currentIndex, std::string &label, std::string &error)
SynchronisationList::const_iterator ConstIterator
bool analyseQuoteLabels(const std::string &syncLine, Size currentIndex, QuoteIndexesVector &quoteIndexes, std::string &error)
ParsingState analyseComma(const std::string &syncLine, Size &currentIndex, std::string &error)
bool analyseSynchronisationLine(const std::string &syncLine, std::string &error)
ParsingState analyseOut(const std::string &syncLine, Size &currentIndex, std::string &error)
Iterator insertSynchronisation(Synchronisation &&synchronisation)
ParsingState analyseLeftP(const std::string &syncLine, const QuoteIndexesVector &quoteIndexes, size_t currentQuoteIndex, Size &currentIndex, std::string &error)
Namespace of the Diades project.
ParsingState analyseName(const std::string &syncLine, Size &currentIndex, std::string &label, std::string &error)
ParsingState analyseEvt(const std::string &syncLine, Size &currentIndex, DdAutEventId &evt, std::string &error)
bool rulesToDescriptor(const SynchronisationRules< DdAutFsmType > &rules, DdSyncDescriptor &desc)
ConstSynchronisationIterator beginOfSynchronisedEvents() const
ConstIterator on the Net.
bool containsComponent(const std::string &compName) const
Synchronisation & operator=(Synchronisation const &other)=default
virtual bool toStream(std::ostream &stream)
ComponentIterator findComponent(const string &name) const
DdAutEventId getEvent(const std::string &compName) const
std::pair< Iterator, bool > insertSynchronisation(const std::string &compName, DdAutEventId evt)