StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EventQueue.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // StdAir
12 
13 namespace stdair {
14 
15  // //////////////////////////////////////////////////////////////////////
16  EventQueue::EventQueue()
17  : _key (DEFAULT_EVENT_QUEUE_ID), _parent (NULL),
18  _progressStatus (stdair::DEFAULT_PROGRESS_STATUS,
19  stdair::DEFAULT_PROGRESS_STATUS) {
20  }
21 
22  // //////////////////////////////////////////////////////////////////////
23  EventQueue::EventQueue (const Key_T& iKey)
24  : _key (iKey), _parent (NULL),
25  _progressStatus (stdair::DEFAULT_PROGRESS_STATUS,
26  stdair::DEFAULT_PROGRESS_STATUS) {
27  }
28 
29  // //////////////////////////////////////////////////////////////////////
30  EventQueue::EventQueue (const EventQueue& iEventQueue)
31  : _key (DEFAULT_EVENT_QUEUE_ID), _parent (NULL),
32  _progressStatus (stdair::DEFAULT_PROGRESS_STATUS,
33  stdair::DEFAULT_PROGRESS_STATUS) {
34  assert (false);
35  }
36 
37  // //////////////////////////////////////////////////////////////////////
39  _eventList.clear();
40  }
41 
42  // //////////////////////////////////////////////////////////////////////
43  std::string EventQueue::toString() const {
44  std::ostringstream oStr;
45  oStr << "(" << _eventList.size() << ") "
46  << _progressStatus.getCurrentNb() << "/{"
47  << _progressStatus.getExpectedNb() << ","
48  << _progressStatus.getActualNb() << "}";
49  return oStr.str();
50  }
51 
52  // //////////////////////////////////////////////////////////////////////
53  std::string EventQueue::display() const {
54  std::ostringstream oStr;
55 
56  oStr << toString();
57 
58  return oStr.str();
59  }
60 
61  // //////////////////////////////////////////////////////////////////////
63  return _eventList.size();
64  }
65 
66  // //////////////////////////////////////////////////////////////////////
67  bool EventQueue::isQueueEmpty() const {
68  return _eventList.empty();
69  }
70 
71  // //////////////////////////////////////////////////////////////////////
72  bool EventQueue::isQueueDone() const {
73  const bool isQueueEmpty = _eventList.empty();
74  return isQueueEmpty;
75  }
76 
77  // //////////////////////////////////////////////////////////////////////
79  // Reset only the current number of events, not the expected one
81 
82  // Empty the list of events
83  _eventList.clear();
84 
85  // Reset the progress statuses for all the event types
86  for (ProgressStatusMap_T::iterator itProgressStatus =
87  _progressStatusMap.begin();
88  itProgressStatus != _progressStatusMap.end(); ++itProgressStatus) {
89  ProgressStatus& lProgressStatus = itProgressStatus->second;
90  lProgressStatus.reset();
91  }
92 
93  }
94 
95  // //////////////////////////////////////////////////////////////////////
96  const Count_T& EventQueue::
98 
99  // Retrieve the ProgressStatus structure corresponding to the
100  // given event type
101  ProgressStatusMap_T::const_iterator itProgressStatus =
102  _progressStatusMap.find (iType);
103  if (itProgressStatus == _progressStatusMap.end()) {
104  //
105  STDAIR_LOG_ERROR ("No ProgressStatus structure can be retrieved in the "
106  << "EventQueue: " << display());
107  assert (false);
108  }
109 
110  const ProgressStatus& lProgressStatus = itProgressStatus->second;
111  return lProgressStatus.getCurrentNb();
112  }
113 
114  // //////////////////////////////////////////////////////////////////////
115  const Count_T& EventQueue::
117 
118  // Retrieve the ProgressStatus structure corresponding to the
119  // given event type
120  ProgressStatusMap_T::const_iterator itProgressStatus =
121  _progressStatusMap.find (iType);
122  if (itProgressStatus == _progressStatusMap.end()) {
123  std::ostringstream oStr;
124  oStr << "No ProgressStatus structure can be retrieved in the EventQueue '"
125  << display() << "'. The EventQueue should be initialised, e.g., by "
126  << "calling a buildSampleBom() method.";
127  //
128  STDAIR_LOG_ERROR (oStr.str());
129  throw EventQueueException (oStr.str());
130  }
131 
132  const ProgressStatus& lProgressStatus = itProgressStatus->second;
133  return lProgressStatus.getExpectedNb();
134  }
135 
136  // //////////////////////////////////////////////////////////////////////
137  const Count_T& EventQueue::
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_ERROR ("No ProgressStatus structure can be retrieved in the "
147  << "EventQueue: " << display());
148  assert (false);
149  }
150 
151  const ProgressStatus& lProgressStatus = itProgressStatus->second;
152  return lProgressStatus.getActualNb();
153  }
154 
155  // //////////////////////////////////////////////////////////////////////
157  const ProgressStatus& iProgressStatus) {
158 
159  // Retrieve, if existing, the ProgressStatus structure
160  // corresponding to the given event type
161  ProgressStatusMap_T::iterator itProgressStatus =
162  _progressStatusMap.find (iType);
163  if (itProgressStatus == _progressStatusMap.end()) {
164  const bool hasInsertBeenSuccessful =
166  value_type (iType, iProgressStatus)).second;
167 
168  if (hasInsertBeenSuccessful == false) {
169  STDAIR_LOG_ERROR ("No progress_status can be inserted "
170  << "for the following event type: "
171  << EventType::getLabel(iType)
172  << ". EventQueue: " << toString());
173  throw EventException ("No progress_status can be inserted for the "
174  "following event type: "
175  + EventType::getLabel(iType)
176  + ". EventQueue: " + toString());
177  }
178 
179  return;
180  }
181 
182  ProgressStatus& lProgressStatus = itProgressStatus->second;
183 
184  // Update the progress status
185  const Count_T& lCurrentNb = iProgressStatus.getCurrentNb();
186  lProgressStatus.setCurrentNb (lCurrentNb);
187 
188  const Count_T& lExpectedNb = iProgressStatus.getExpectedNb();
189  lProgressStatus.setExpectedNb(lProgressStatus.getExpectedNb() + lExpectedNb);
190 
191  const Count_T& lActualNb = iProgressStatus.getActualNb();
192  lProgressStatus.setActualNb (lProgressStatus.getActualNb() + lActualNb);
193  }
194 
195  // //////////////////////////////////////////////////////////////////////
196  void EventQueue::
198  const NbOfEvents_T& iExpectedTotalNbOfEvents) {
199 
200  // Initialise the progress status object
201  const Count_T lExpectedTotalNbOfEventsInt =
202  static_cast<const Count_T> (std::floor (iExpectedTotalNbOfEvents));
203  const ProgressStatus lProgressStatus (lExpectedTotalNbOfEventsInt);
204 
205  // Update the progress status for the given event type
206  updateStatus (iType, lProgressStatus);
207 
208  // Update the overall progress status
209  const Count_T lExpectedNb =
210  static_cast<const Count_T> (_progressStatus.getExpectedNb()
211  + iExpectedTotalNbOfEvents);
212  _progressStatus.setExpectedNb (lExpectedNb);
213 
214  const Count_T lActualNb =
215  static_cast<const Count_T> (_progressStatus.getActualNb()
216  + iExpectedTotalNbOfEvents);
217  _progressStatus.setActualNb (lActualNb);
218  }
219 
220  // //////////////////////////////////////////////////////////////////////
222  const NbOfEvents_T& iActualNbOfEvents) {
223 
224  // Initialise the progress status object for the type key
225  Count_T lActualNbOfEventsInt =
226  static_cast<const Count_T> (std::floor (iActualNbOfEvents));
227 
228  // Update the progress status for the corresponding content type key
229  ProgressStatusMap_T::iterator itProgressStatus =
230  _progressStatusMap.find (iType);
231  if (itProgressStatus != _progressStatusMap.end()) {
232  ProgressStatus& lProgressStatus = itProgressStatus->second;
233  //
234  lActualNbOfEventsInt += lProgressStatus.getActualNb();
235  //
236  lProgressStatus.setActualNb (lActualNbOfEventsInt);
237  }
238  }
239 
240  // //////////////////////////////////////////////////////////////////////
242  const ProgressStatus& iProgressStatus) {
243 
244  // Retrieve the ProgressStatus structure corresponding to the
245  // given event type
246  ProgressStatusMap_T::iterator itProgressStatus =
247  _progressStatusMap.find (iType);
248  // assert (itProgressStatus != _progressStatusMap.end());
249  if (itProgressStatus != _progressStatusMap.end()) {
250  // Update the ProgressStatus structure
251  itProgressStatus->second = iProgressStatus;
252  }
253  }
254 
255  // //////////////////////////////////////////////////////////////////////
257  getStatus (const EventType::EN_EventType& iType) const {
258 
259  // Retrieve the ProgressStatus structure corresponding to the
260  // given event type
261  ProgressStatusMap_T::const_iterator itProgressStatus =
262  _progressStatusMap.find (iType);
263  if (itProgressStatus != _progressStatusMap.end()) {
264  const ProgressStatus& oProgressStatus = itProgressStatus->second;
265  return oProgressStatus;
266  }
267 
268  return ProgressStatus();
269  }
270 
271  // //////////////////////////////////////////////////////////////////////
274 
275  // Retrieve the ProgressStatus structure corresponding to the
276  // given event type
277  ProgressStatusMap_T::const_iterator itProgressStatus =
278  _progressStatusMap.find (iType);
279  if (itProgressStatus == _progressStatusMap.end()) {
280  //
281  STDAIR_LOG_ERROR ("No ProgressStatus structure can be retrieved in the "
282  << "EventQueue: " << display());
283  assert (false);
284  }
285 
286  const ProgressStatus& lProgressStatus = itProgressStatus->second;
287  return lProgressStatus.progress();
288  }
289 
290  // //////////////////////////////////////////////////////////////////////
292  assert (_eventList.empty() == false);
293 
297  // Get an iterator on the first event (sorted by date-time stamps)
298  EventList_T::iterator itEvent = _eventList.begin();
299 
306  ioEventStruct = itEvent->second;
307  // Retrieve the event type
308  const EventType::EN_EventType& lEventType = ioEventStruct.getEventType();
309  ProgressStatusSet oProgressStatusSet (lEventType);
310 
311  // Update the (current number part of the) overall progress status,
312  // to account for the event that is being popped out of the event
313  // queue.
314  ++_progressStatus;
315 
316  // Remove the event, which has just been retrieved
317  _eventList.erase (itEvent);
318 
319 
327  // Retrieve the progress status specific to that event type
328  ProgressStatus lEventTypeProgressStatus = getStatus (lEventType);
329 
330  // Increase the current number of events
331  ++lEventTypeProgressStatus;
332 
333  // Store back the progress status
334  setStatus (lEventType, lEventTypeProgressStatus);
335 
336  // Update the progress status of the progress status set, specific to
337  // the event type.
338  oProgressStatusSet.setTypeSpecificStatus (lEventTypeProgressStatus);
339 
343  // Update the overall progress status of the progress status set.
344  oProgressStatusSet.setOverallStatus (_progressStatus);
345 
346  //
347  return oProgressStatusSet;
348  }
349 
350  // //////////////////////////////////////////////////////////////////////
351  bool EventQueue::addEvent (EventStruct& ioEventStruct) {
352  bool insertionSucceeded =
353  _eventList.insert (EventListElement_T (ioEventStruct._eventTimeStamp,
354  ioEventStruct)).second;
355 
368  const unsigned int idx = 0;
369  while (insertionSucceeded == false && idx != 1e3) {
370  // Retrieve the date-time stamp (expressed in milliseconds)
371  LongDuration_T& lEventTimeStamp (ioEventStruct._eventTimeStamp);
372  ++lEventTimeStamp;
373 
374  // Retry to insert into the event queue
375  insertionSucceeded =
376  _eventList.insert (EventListElement_T (ioEventStruct._eventTimeStamp,
377  ioEventStruct)).second;
378  }
379  assert (idx != 1e3);
380 
381  return insertionSucceeded;
382  }
383 
384 }