SEvMgr Logo  1.00.0
C++ Simulation-Oriented Discrete Event Management Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
EventQueue.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // StdAir
7 #include <stdair/stdair_exceptions.hpp>
8 #include <stdair/basic/BasConst_Event.hpp>
9 #include <stdair/bom/EventStruct.hpp>
10 #include <stdair/service/Logger.hpp>
11 // SEvMgr
14 
15 namespace SEVMGR {
16 
17  // //////////////////////////////////////////////////////////////////////
18  EventQueue::EventQueue()
19  : _key (DEFAULT_EVENT_QUEUE_ID), _parent (NULL),
20  _progressStatus (stdair::DEFAULT_PROGRESS_STATUS,
21  stdair::DEFAULT_PROGRESS_STATUS) {
22  }
23 
24  // //////////////////////////////////////////////////////////////////////
25  EventQueue::EventQueue (const Key_T& iKey)
26  : _key (iKey), _parent (NULL),
27  _progressStatus (stdair::DEFAULT_PROGRESS_STATUS,
28  stdair::DEFAULT_PROGRESS_STATUS) {
29  }
30 
31  // //////////////////////////////////////////////////////////////////////
32  EventQueue::EventQueue (const EventQueue& iEventQueue)
33  : _key (DEFAULT_EVENT_QUEUE_ID), _parent (NULL),
34  _progressStatus (stdair::DEFAULT_PROGRESS_STATUS,
35  stdair::DEFAULT_PROGRESS_STATUS) {
36  assert (false);
37  }
38 
39  // //////////////////////////////////////////////////////////////////////
41  _eventList.clear();
42  }
43 
44  // //////////////////////////////////////////////////////////////////////
45  std::string EventQueue::toString() const {
46  std::ostringstream oStr;
47  oStr << "(" << _eventList.size() << ") "
48  << _progressStatus.getCurrentNb() << "/{"
49  << _progressStatus.getExpectedNb() << ","
50  << _progressStatus.getActualNb() << "}";
51  return oStr.str();
52  }
53 
54  // //////////////////////////////////////////////////////////////////////
55  std::string EventQueue::display() const {
56  std::ostringstream oStr;
57 
58  oStr << toString();
59 
60  return oStr.str();
61  }
62 
63  // //////////////////////////////////////////////////////////////////////
64  std::string EventQueue::list () const {
65  std::ostringstream oStr;
66  oStr << describeKey () << std::endl;
67  oStr << toString() << std::endl;
68 
69  // Browse the events
70  for (stdair::EventList_T::const_iterator itEvent = _eventList.begin();
71  itEvent != _eventList.end(); ++itEvent) {
72  const stdair::EventStruct& lEvent = itEvent->second;
73 
74  oStr << lEvent.describe();
75  }
76 
77  return oStr.str();
78  }
79 
80  // //////////////////////////////////////////////////////////////////////
81  std::string EventQueue::
82  list (const stdair::EventType::EN_EventType& iType) const {
83  std::ostringstream oStr;
84  oStr << describeKey () << std::endl;
85  oStr << toString() << std::endl;
86  oStr << "List " << stdair::EventType::getLabel(iType)
87  << " events:" << std::endl;
88 
89  // Browse the events
90  for (stdair::EventList_T::const_iterator itEvent = _eventList.begin();
91  itEvent != _eventList.end(); ++itEvent) {
92  const stdair::EventStruct& lEvent = itEvent->second;
93 
94  if (lEvent.getEventType() == iType) {
95  oStr << lEvent.describe();
96  }
97  }
98  return oStr.str();
99  }
100 
101  // //////////////////////////////////////////////////////////////////////
102  stdair::Count_T EventQueue::getQueueSize () const {
103  return _eventList.size();
104  }
105 
106  // //////////////////////////////////////////////////////////////////////
107  bool EventQueue::isQueueEmpty () const {
108  return _eventList.empty();
109  }
110 
111  // //////////////////////////////////////////////////////////////////////
112  bool EventQueue::isQueueDone () const {
113  const bool isQueueEmpty = _eventList.empty();
114  return isQueueEmpty;
115  }
116 
117  // //////////////////////////////////////////////////////////////////////
119  // Reset only the current number of events, not the expected one
120  _progressStatus.reset();
121 
122  // Empty the list of events
123  _eventList.clear();
124 
125  // Reset the progress statuses for all the event types
126  for (ProgressStatusMap_T::iterator itProgressStatus =
127  _progressStatusMap.begin();
128  itProgressStatus != _progressStatusMap.end(); ++itProgressStatus) {
129  stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
130  lProgressStatus.reset();
131  }
132  }
133 
134  // //////////////////////////////////////////////////////////////////////
135  bool EventQueue::
136  hasProgressStatus (const stdair::EventType::EN_EventType& iType) const {
137 
138  bool hasProgressStatus = true;
139 
140  // Retrieve the ProgressStatus structure corresponding to the
141  // given event type
142  ProgressStatusMap_T::const_iterator itProgressStatus =
143  _progressStatusMap.find (iType);
144  if (itProgressStatus == _progressStatusMap.end()) {
145  //
146  STDAIR_LOG_DEBUG ("No ProgressStatus structure can be retrieved in the "
147  << "EventQueue: " << display());
148 
149  hasProgressStatus = false;
150  }
151 
152  return hasProgressStatus;
153  }
154 
155  // //////////////////////////////////////////////////////////////////////
156  const stdair::Count_T& EventQueue::
157  getCurrentNbOfEvents (const stdair::EventType::EN_EventType& iType) const {
158 
159  // Retrieve the ProgressStatus structure corresponding to the
160  // given event type
161  ProgressStatusMap_T::const_iterator itProgressStatus =
162  _progressStatusMap.find (iType);
163  if (itProgressStatus == _progressStatusMap.end()) {
164  //
165  STDAIR_LOG_ERROR ("No ProgressStatus structure can be retrieved in the "
166  << "EventQueue: " << display());
167  assert (false);
168  }
169 
170  const stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
171  return lProgressStatus.getCurrentNb();
172  }
173 
174  // //////////////////////////////////////////////////////////////////////
175  const stdair::Count_T& EventQueue::
176  getExpectedTotalNbOfEvents (const stdair::EventType::EN_EventType& iType) const {
177 
178  // Retrieve the ProgressStatus structure corresponding to the
179  // given event type
180  ProgressStatusMap_T::const_iterator itProgressStatus =
181  _progressStatusMap.find (iType);
182  if (itProgressStatus == _progressStatusMap.end()) {
183  std::ostringstream oStr;
184  oStr << "No ProgressStatus structure can be retrieved in the EventQueue '"
185  << display() << "'. The EventQueue should be initialised, e.g., by "
186  << "calling a buildSampleBom() method.";
187  //
188  STDAIR_LOG_ERROR (oStr.str());
189  throw EventQueueException (oStr.str());
190  }
191 
192  const stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
193  return lProgressStatus.getExpectedNb();
194  }
195 
196  // //////////////////////////////////////////////////////////////////////
197  const stdair::Count_T& EventQueue::
198  getActualTotalNbOfEvents (const stdair::EventType::EN_EventType& iType) const {
199 
200  // Retrieve the ProgressStatus structure corresponding to the
201  // given event type
202  ProgressStatusMap_T::const_iterator itProgressStatus =
203  _progressStatusMap.find (iType);
204  if (itProgressStatus == _progressStatusMap.end()) {
205  //
206  STDAIR_LOG_ERROR ("No ProgressStatus structure can be retrieved in the "
207  << "EventQueue: " << display());
208  assert (false);
209  }
210 
211  const stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
212  return lProgressStatus.getActualNb();
213  }
214 
215  // //////////////////////////////////////////////////////////////////////
216  void EventQueue::updateStatus (const stdair::EventType::EN_EventType& iType,
217  const stdair::ProgressStatus& iProgressStatus) {
218 
219  // Retrieve, if existing, the ProgressStatus structure
220  // corresponding to the given event type
221  ProgressStatusMap_T::iterator itProgressStatus =
222  _progressStatusMap.find (iType);
223  if (itProgressStatus == _progressStatusMap.end()) {
224  const bool hasInsertBeenSuccessful =
226  value_type (iType, iProgressStatus)).second;
227 
228  if (hasInsertBeenSuccessful == false) {
229  STDAIR_LOG_ERROR ("No progress_status can be inserted "
230  << "for the following event type: "
231  << stdair::EventType::getLabel(iType)
232  << ". EventQueue: " << toString());
233  throw stdair::EventException ("No progress_status can be inserted for the "
234  "following event type: "
235  + stdair::EventType::getLabel(iType)
236  + ". EventQueue: " + toString());
237  }
238 
239  return;
240  }
241 
242  stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
243 
244  // Update the progress status
245  const stdair::Count_T& lCurrentNb = iProgressStatus.getCurrentNb();
246  lProgressStatus.setCurrentNb (lCurrentNb);
247 
248  const stdair::Count_T& lExpectedNb = iProgressStatus.getExpectedNb();
249  lProgressStatus.setExpectedNb(lProgressStatus.getExpectedNb() + lExpectedNb);
250 
251  const stdair::Count_T& lActualNb = iProgressStatus.getActualNb();
252  lProgressStatus.setActualNb (lProgressStatus.getActualNb() + lActualNb);
253  }
254 
255  // //////////////////////////////////////////////////////////////////////
256  void EventQueue::
257  addStatus (const stdair::EventType::EN_EventType& iType,
258  const stdair::NbOfEvents_T& iExpectedTotalNbOfEvents) {
259 
260  // Initialise the progress status object
261  const stdair::Count_T lExpectedTotalNbOfEventsInt =
262  static_cast<const stdair::Count_T> (std::floor (iExpectedTotalNbOfEvents));
263  const stdair::ProgressStatus lProgressStatus (lExpectedTotalNbOfEventsInt);
264 
265  // Update the progress status for the given event type
266  updateStatus (iType, lProgressStatus);
267 
268  // Update the overall progress status
269  const stdair::Count_T lExpectedNb =
270  static_cast<const stdair::Count_T> (_progressStatus.getExpectedNb()
271  + iExpectedTotalNbOfEvents);
272  _progressStatus.setExpectedNb (lExpectedNb);
273 
274  const stdair::Count_T lActualNb =
275  static_cast<const stdair::Count_T> (_progressStatus.getActualNb()
276  + iExpectedTotalNbOfEvents);
277  _progressStatus.setActualNb (lActualNb);
278 
279  }
280 
281  // //////////////////////////////////////////////////////////////////////
282  void EventQueue::updateStatus (const stdair::EventType::EN_EventType& iType,
283  const stdair::NbOfEvents_T& iActualNbOfEvents) {
284 
285  // Initialise the progress status object for the type key
286  stdair:: Count_T lActualNbOfEventsInt =
287  static_cast<const stdair::Count_T> (std::floor (iActualNbOfEvents));
288 
289  // Update the progress status for the corresponding content type key
290  ProgressStatusMap_T::iterator itProgressStatus =
291  _progressStatusMap.find (iType);
292  if (itProgressStatus != _progressStatusMap.end()) {
293 
294  //
295  stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
296 
297  // Update the overall progress status
298  const stdair::Count_T lActualEventTypeNb = lProgressStatus.getActualNb();
299  const stdair::Count_T lActualTotalNb = _progressStatus.getActualNb();
300  _progressStatus.setActualNb (lActualTotalNb + iActualNbOfEvents - lActualEventTypeNb);
301 
302  // Update the progress status for the corresponding type key
303  lProgressStatus.setActualNb (lActualNbOfEventsInt);
304  }
305  }
306 
307  // //////////////////////////////////////////////////////////////////////
308  void EventQueue::setStatus (const stdair::EventType::EN_EventType& iType,
309  const stdair::ProgressStatus& iProgressStatus) {
310 
311  // Retrieve the ProgressStatus structure corresponding to the
312  // given event type
313  ProgressStatusMap_T::iterator itProgressStatus =
314  _progressStatusMap.find (iType);
315  // assert (itProgressStatus != _progressStatusMap.end());
316  if (itProgressStatus != _progressStatusMap.end()) {
317  // Update the ProgressStatus structure
318  itProgressStatus->second = iProgressStatus;
319  }
320  }
321 
322  // //////////////////////////////////////////////////////////////////////
323  const stdair::ProgressStatus& EventQueue::
324  getStatus (const stdair::EventType::EN_EventType& iType) const {
325 
326  // Retrieve the ProgressStatus structure corresponding to the
327  // given event type
328  ProgressStatusMap_T::const_iterator itProgressStatus =
329  _progressStatusMap.find (iType);
330  if (itProgressStatus == _progressStatusMap.end()) {
331  std::ostringstream oStr;
332  oStr << "No ProgressStatus structure can be retrieved in the EventQueue '"
333  << display() << "' for the following event type: "
334  << stdair::EventType::getLabel(iType) << ".";
335  //
336  STDAIR_LOG_ERROR (oStr.str());
337  throw EventQueueException (oStr.str());
338  }
339  assert(itProgressStatus != _progressStatusMap.end());
340 
341  const stdair::ProgressStatus& oProgressStatus = itProgressStatus->second;
342  return oProgressStatus;
343  }
344 
345  // //////////////////////////////////////////////////////////////////////
346  stdair::ProgressPercentage_T EventQueue::
347  calculateProgress (const stdair::EventType::EN_EventType& iType) const {
348 
349  // Retrieve the ProgressStatus structure corresponding to the
350  // given event type
351  ProgressStatusMap_T::const_iterator itProgressStatus =
352  _progressStatusMap.find (iType);
353  if (itProgressStatus == _progressStatusMap.end()) {
354  //
355  STDAIR_LOG_ERROR ("No ProgressStatus structure can be retrieved in the "
356  << "EventQueue: " << display());
357  assert (false);
358  }
359 
360  const stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
361  return lProgressStatus.progress();
362  }
363 
364  // //////////////////////////////////////////////////////////////////////
365  stdair::ProgressStatusSet EventQueue::popEvent (stdair::EventStruct& ioEventStruct) {
366 
367  if (_eventList.empty() == true) {
368  std::ostringstream oStr;
369  oStr << "The event queue '" << describeKey() << "' is empty. "
370  << "No event can be popped.";
371  //
372  STDAIR_LOG_ERROR (oStr.str());
373  throw EventQueueException (oStr.str());
374  }
375 
379  // Get an iterator on the first event (sorted by date-time stamps)
380  stdair::EventList_T::iterator itEvent = _eventList.begin();
381 
388  ioEventStruct = itEvent->second;
389  // Retrieve the event type
390  const stdair::EventType::EN_EventType& lEventType = ioEventStruct.getEventType();
391  stdair::ProgressStatusSet oProgressStatusSet (lEventType);
392 
393  // Update the (current number part of the) overall progress status,
394  // to account for the event that is being popped out of the event
395  // queue.
396  ++_progressStatus;
397 
398  // Remove the event, which has just been retrieved
399  _eventList.erase (itEvent);
400 
401 
409  // Retrieve the progress status specific to that event type
410  stdair::ProgressStatus lEventTypeProgressStatus = getStatus (lEventType);
411 
412  // Increase the current number of events
413  ++lEventTypeProgressStatus;
414 
415  // Store back the progress status
416  setStatus (lEventType, lEventTypeProgressStatus);
417 
418  // Update the progress status of the progress status set, specific to
419  // the event type.
420  oProgressStatusSet.setTypeSpecificStatus (lEventTypeProgressStatus);
421 
425  // Update the overall progress status of the progress status set.
426  oProgressStatusSet.setOverallStatus (_progressStatus);
427 
428  //
429  return oProgressStatusSet;
430  }
431 
432  // //////////////////////////////////////////////////////////////////////
433  bool EventQueue::addEvent (stdair::EventStruct& ioEventStruct) {
434  bool insertionSucceeded =
435  _eventList.insert (stdair::EventListElement_T (ioEventStruct.getEventTimeStamp(),
436  ioEventStruct)).second;
437 
450  const unsigned int idx = 0;
451  while (insertionSucceeded == false && idx != 1e3) {
452  // Increment the date-time stamp (expressed in milliseconds)
453  ioEventStruct.incrementEventTimeStamp();
454 
455  // Retry to insert into the event queue
456  insertionSucceeded =
457  _eventList.insert (stdair::EventListElement_T (ioEventStruct.getEventTimeStamp(),
458  ioEventStruct)).second;
459  }
460  assert (idx != 1e3);
461 
462  return insertionSucceeded;
463  }
464 
465  // //////////////////////////////////////////////////////////////////////
466  bool EventQueue::hasEventDateTime (const stdair::DateTime_T& iDateTime) {
467 
468  bool hasSearchEventBeenSucessful = true;
469 
475  const stdair::Duration_T lDuration =
476  iDateTime - stdair::DEFAULT_EVENT_OLDEST_DATETIME;
477  const stdair::LongDuration_T lDateTimeStamp =
478  lDuration.total_milliseconds();
479 
480  // Searches the container for an element with iDateTime as key
481  stdair::EventList_T::iterator itEvent =
482  _eventList.find (lDateTimeStamp);
483 
484  // An iterator to map::end means the specified key has not found in the
485  // container.
486  if (itEvent == _eventList.end()) {
487  hasSearchEventBeenSucessful = false;
488  }
489 
490  return hasSearchEventBeenSucessful;
491 
492  }
493 
494 }