DiaDes  0.1
DIAgnosis of Discrete-Event System
SynchronisationRules.hh
Go to the documentation of this file.
1 
9 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__SYNCHRONISATIONRULES_HH
10 #define __DIADES__AUTOMATA__EXPERIMENTAL__SYNCHRONISATIONRULES_HH
11 
12 #include<list>
13 #include<numeric>
18 
19 namespace Diades
20 {
21  namespace Automata
22  {
23  namespace Experimental
24  {
25 
39  template<typename StateMachine>
41  {
42  public:
43 
44  static string
46  {
47  return "Automata::Experimental::SynchronisationRules";
48  }
52  using Event = typename SyncEvent::Event;
55  using Size = typename SyncEvent::Size;
56  using SynchronisationList = std::list<SyncEvent>;
57  using SynchronisationIterator = typename SynchronisationList::iterator;
58  using ConstSynchronisationIterator = typename SynchronisationList::const_iterator;
59 
60  protected:
62  std::list<SyncEvent> _synchros;
63 
64  public:
65 
69  SynchronisationRules() = default;
70 
71 
76  SynchronisationRules(SynchronisationRules const& other) = default;
82  SynchronisationRules& operator=(SynchronisationRules const& other) = default;
87  SynchronisationRules(SynchronisationRules && other) = default;
97  virtual ~SynchronisationRules() = default;
98 
110  SynchronisationRules(const ComponentVector & components) : _components(), _synchros()
111  {
112  setComponentVector(components);
113  }
114 
127  bool
129  {
130  using CompPtr = const Component *;
131  using CompId = typename Component::Id;
132  _components.clear();
133  _synchros.clear();
134  std::map<CompId, CompPtr> dictionary;
135  std::for_each(components.begin(), components.end(),
136  [&](CompPtr pComp)
137  {
138  if(pComp != nullptr)
139  {
140  dictionary[pComp->id()] = pComp;
141  }
142  }
143  );
144  bool ok = (dictionary.size() == components.size());
145  if(ok)
146  {
147  _components.reserve(components.size());
148  std::for_each(dictionary.begin(), dictionary.end(),
149  [&](const std::pair<CompId, CompPtr> & p)
150  {
151  _components.push_back(p.second);
152  });
153  }
154  else
155  {
156  clear();
157  }
158  return ok;
159  }
160 
164  virtual void
166  {
167  clearRules();
168  _components.clear();
169 
170  }
171 
175  virtual void
177  {
178  _synchros.clear();
179  }
180 
186  {
187  return _synchros.begin();
188  }
189 
195  {
196  return _synchros.end();
197  }
198 
204  {
205  return _synchros.begin();
206  }
207 
213  {
214  return _synchros.end();
215  }
216 
221  Size
223  {
224  return _synchros.size();
225  }
226 
230  Size
232  {
233  return _components.size();
234  }
235 
239  Size
240  size() const
241  {
242  return _synchros.size();
243  }
244 
250  {
251  return ComponentIterator(&_components, nullptr, 0);
252  }
253 
259  {
260  return ComponentIterator(&_components, nullptr, _components.size());
261  }
262 
270  const Component &
271  getComponent(Size index) const
272  {
274  Diades::Utils::Msg("getComponent: out-of-range index '%1% > %2%'")
275  % index % numberOfComponents());
276  return *_components[index];
277  }
278 
285  findComponent(const string & name) const
286  {
287  return std::find_if(beginOfComponents(), endOfComponents(),
288  [&](const Component & comp) -> bool
289  {
290  return(name == comp.name());
291  });
292  }
293 
300  findComponent(const Component & component) const
301  {
302  const Component * ptr = &component;
303  return std::find_if(beginOfComponents(), endOfComponents(),
304  [&](const Component & comp) -> bool
305  {
306  return(ptr == &comp);
307  });
308  }
309 
313  bool operator==(const SynchronisationRules & rules) const
314  {
315  return(_components == rules._components) && (_synchros == rules._synchros);
316  }
317 
327  void
328  getSynchronisationEvents(const Component & component, Event event,
329  std::list< typename Ptr<SyncEvent>::ConstP > & events) const
330  {
331  require(Exception, component.isValid(), "getSynchronisationEvents: component is invalid");
332  require(Exception, component.containsEvent(event), "getSynchronisationEvents: the event does not belong to the component");
334  it != endOfSynchronisedEvents();
335  ++it)
336  {
337  ComponentIterator compIt = it->findComponent(component);
338  if(compIt != it->endOfComponents())
339  {
340  if(it->getAssociatedEvent(compIt) == event)
341  {
342  events.push_back(Ptr<SyncEvent>::get(*it));
343  ensure(Exception, Ptr<SyncEvent>::get(*it)->isValid(), "getSynchronisationEvents returns an invalid SynchronisationEvent, BUG");
344  }
345  }
346  }
347 
348  }
349 
353  bool
354  isValid() const
355  {
357  return !_components.empty() && std::accumulate(_synchros.begin(), _synchros.end(), true, AccValidity());
358 
359  // if(_components.empty())
360  // {std::cout << "HELOW\n";
361  // return false;
362  // }
363  // bool valid = true;
364  // auto it = _synchros.begin();
365  // while(valid && it != _synchros.end())
366  // {
367  // std::cout << "HELO\n";
368  // valid = it->isValid();
369  // ++it;
370  // }
371  // return valid;
372 
373  }
374 
375 
376 
377  };
378 
379  template<typename SyncRules, typename SynchronisationEventEncoder>
381  {
382  private:
383  SyncRules _rules;
384  public:
385 
386  SynchronisationRulesEncoder(SyncRules & rules) : _rules(rules)
387  {
388  SynchronisationEventEncoder encoder(rules.numberOfSynchronisedEvents());
389  std::for_each(rules.beginOfSynchronisedEvents(), rules.endOfSynchronisedEvents(),
390  [&](typename SyncRules::SyncEvent & evt)
391  {
392  evt.setSynchronisedEvent(encoder.newEvent(evt));
393  });
394 
395  }
396  };
397 
398 
399  }
400  }
401 }
402 
403 #endif /* __DIADES__AUTOMATA__EXPERIMENTAL__SYNCHRONISATIONRULES_HH */
404 
ComponentIterator findComponent(const Component &component) const
ConstSynchronisationIterator endOfSynchronisedEvents() const
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
bool containsEvent(const EventPropertyId &e) const
typename SyncEvent::ComponentIterator ComponentIterator
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
typename SynchronisationList::const_iterator ConstSynchronisationIterator
bool operator==(const SynchronisationRules &rules) const
ConstSynchronisationIterator beginOfSynchronisedEvents() const
bool setComponentVector(const ComponentVector &components)
void getSynchronisationEvents(const Component &component, Event event, std::list< typename Ptr< SyncEvent >::ConstP > &events) const
boost::format Msg
Definition: Verbose.hh:42
ComponentIterator findComponent(const string &name) const
SynchronisationRules & operator=(SynchronisationRules const &other)=default
typename SynchronisationList::iterator SynchronisationIterator