DiaDes  0.1
DIAgnosis of Discrete-Event System
ParametrizedSynchronisation.hh
Go to the documentation of this file.
1 
9 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__PARAMETRIZEDSYNCHRONISATION__HH__
10 #define __DIADES__AUTOMATA__EXPERIMENTAL__PARAMETRIZEDSYNCHRONISATION__HH__
11 
13 
14 namespace Diades
15 {
16  namespace Automata
17  {
18  namespace Experimental
19  {
20 
32  template<typename StateMachine>
34  {
35  public:
36 
37  static string
39  {
40  return "Automata::Experimental::ParametrizedSynchronisation";
41  }
44  using Component = typename SR::Component;
45  using SyncEvent = typename SR::SyncEvent;
46  using Event = typename SR::Event;
49  using Size = typename SR::Size;
52 
53  private:
54  bool _locked;
55  std::unordered_map<typename Ptr<Component>::ConstP, std::set<Event> > _isSynchronized;
57  const Event _nullEvent = NullEvent().nullValue();
58  public:
59 
60 
64  ParametrizedSynchronisation() = default;
65 
66 
92  virtual ~ParametrizedSynchronisation() = default;
93 
100  SR(components), _isSynchronized()
101  {
102  _locked = false;
103  }
104 
111  bool
112  init(const ComponentVector & components)
113  {
114  clear();
115  return SR::setComponentVector(components);
116  }
117 
124  void
126  {
127  require(Exception, SR::isValid(), "synchronise (Parametrized): the rules are not valid");
128  if(!isLocked())
129  {
130  if(e != _nullEvent)
131  {
132 
133  list<ComponentIterator> support;
135  it != SR::endOfComponents();
136  ++it)
137  {
138  if(it->containsEvent(e))
139  {
140  support.push_back(it);
141  }
142  }
143  require(Exception, !support.empty(), "synchronise (Parametrized): the event has no support");
145  std::for_each(support.begin(), support.end(),
146  [&](ComponentIterator it)
147  {
148  SR::_synchros.back().addEvent(*it, e);
149  _isSynchronized[Ptr<Component>::get(*it)].insert(e);
150  });
151 
152  }
153  }
154  ensure(Exception, SR::isValid(), "synchronise (Parametrized): the rules are not valid");
155  }
156 
163  template<typename ComponentEventIterator>
164  void
165  synchronise(ComponentEventIterator begin, ComponentEventIterator end)
166  {
167  synchronise(begin, end, false);
168  }
169 
183  template<typename ComponentEventIterator>
184  void
185  synchronise(ComponentEventIterator begin, ComponentEventIterator end, bool relaxation)
186  {
187  require(Exception, SR::isValid(), "synchronise (Parametrized + relaxation): the rules are not valid");
188  if(!isLocked())
189  {
191  auto & evt = SR::_synchros.back();
192  std::for_each(begin, end,
193  [&](const std::pair<typename Ptr<Component>::ConstP, Event> & entry)
194  {
195  evt.addEvent(*entry.first, entry.second);
196  _isSynchronized[entry.first].insert(entry.second);
197  if(relaxation)
198  {
200  SR::_synchros.back().addEvent(*entry.first, entry.second);
201 
202  }
203  }
204  );
205  }
206  ensure(Exception, SR::isValid(), "synchronise (Parametrized + relaxation): the rules are not valid");
207  }
208 
216  void
218  {
219  require(Exception, SR::isValid(), "close: the rules are not valid");
220  if(!isLocked())
221  {
222 
224  it != SR::endOfComponents();
225  ++it)
226  {
227  for(auto eventIt = it->eventBegin();
228  eventIt != it->eventEnd();
229  ++eventIt)
230  {
231  auto & syncSet = _isSynchronized[Ptr<Component>::get(*it)];
232  if(syncSet.find(*eventIt) == syncSet.end())
233  {
234 
236  SR::_synchros.back().addEvent(*it, *eventIt);
237  syncSet.insert(*eventIt);
238  }
239  }
240  }
241  // SR::encodeEvents();
242  _locked = true;
243  }
244  ensure(Exception, SR::isValid(), "close: the rules are not valid");
245  }
246 
250  void
252  {
253  _locked = false;
254  }
255 
259  bool
260  isLocked() const
261  {
262  return _locked;
263  }
264 
268  virtual void
270  {
271  _isSynchronized.clear();
272  _locked = false;
273  SR::clear();
274  }
275 
279  virtual void
281  {
282  _isSynchronized.clear();
283  _locked = false;
284  SR::clearRules();
285  }
286  };
287 
288  }
289  }
290 }
291 
292 #endif /* __DIADES__AUTOMATA__EXPERIMENTAL__PARAMETRIZEDSYNCHRONIZATION__HH__ */
293 
static ConstP get(const T &obj)
Definition: Pointer.hh:37
ParametrizedSynchronisation & operator=(ParametrizedSynchronisation const &other)=default
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
void synchronise(ComponentEventIterator begin, ComponentEventIterator end)
void synchronise(ComponentEventIterator begin, ComponentEventIterator end, bool relaxation)
typename SyncEvent::ComponentIterator ComponentIterator
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
bool setComponentVector(const ComponentVector &components)
std::unordered_map< typename Ptr< Component >::ConstP, std::set< Event > > _isSynchronized
typename SynchronisationList::iterator SynchronisationIterator