SEvMgr Logo  1.00.0
C++ Simulation-Oriented Discrete Event Management Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
EventQueueManager.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // Boost
7 #include <boost/make_shared.hpp>
8 // StdAir
9 #include <stdair/basic/ProgressStatusSet.hpp>
10 #include <stdair/basic/EventType.hpp>
11 #include <stdair/basic/BasConst_Event.hpp>
12 #include <stdair/bom/BomManager.hpp>
13 #include <stdair/bom/EventStruct.hpp>
14 #include <stdair/bom/BookingRequestStruct.hpp>
15 #include <stdair/bom/BreakPointStruct.hpp>
16 #include <stdair/service/Logger.hpp>
17 #include <stdair/STDAIR_Service.hpp>
18 // SEvMgr
21 
22 namespace SEVMGR {
23 
24  // //////////////////////////////////////////////////////////////////////
25  void EventQueueManager::
26  buildSampleQueue (stdair::STDAIR_ServicePtr_T lSTDAIR_ServicePtr,
27  EventQueue& ioEventQueue) {
28 
29  // Total number of booking requests into the queue
30  stdair::Count_T lNbOfBookingRequests (2);
31  addStatus(ioEventQueue, stdair::EventType::BKG_REQ, lNbOfBookingRequests);
32 
33  // Create a shared pointer on a first booking request
34  // Date of the request (15-MAY-2011)
35  const stdair::BookingRequestStruct& lBookingRequest =
36  buildSampleBookingRequest (lSTDAIR_ServicePtr);
37  const stdair::BookingRequestPtr_T lBookingRequest_ptr =
38  boost::make_shared<stdair::BookingRequestStruct> (lBookingRequest);
39 
40  // Create an event structure
41  stdair::EventStruct lEventStruct (stdair::EventType::BKG_REQ,
42  lBookingRequest_ptr);
43 
44  // Add the event into the queue
45  addEvent(ioEventQueue, lEventStruct);
46 
47  // Create a second shared pointer on a second booking request
48  // Date of the request (22-JAN-2010)
49  const bool isForCRS = true;
50  const stdair::BookingRequestStruct& lBookingRequestForCRS =
51  buildSampleBookingRequest (lSTDAIR_ServicePtr, isForCRS);
52  const stdair::BookingRequestPtr_T lBookingRequestForCRS_ptr =
53  boost::make_shared<stdair::BookingRequestStruct> (lBookingRequestForCRS);
54 
55  // Create an event structure
56  stdair::EventStruct lEventStructForCRS (stdair::EventType::BKG_REQ,
57  lBookingRequestForCRS_ptr);
58 
59  // Add the event into the queue
60  addEvent(ioEventQueue, lEventStructForCRS);
61 
62  // Total number of break points into the queue
63  stdair::Count_T lNbOfBreakPoints (2);
64  addStatus(ioEventQueue, stdair::EventType::BRK_PT, lNbOfBreakPoints);
65 
66  // Create a shared pointer on a second break point
67  // Date of the break point (21-JAN-2010)
68  const stdair::Date_T lBP1Date (2010, boost::gregorian::Jan, 21);
69  // Time of the break point (00:00)
70  const stdair::Duration_T lBP1Time (0, 0, 0);
71  // Date-time of the break point (made of the date and time above)
72  const stdair::DateTime_T lBP1DateTime (lBP1Date, lBP1Time);
73  const stdair::BreakPointPtr_T lBreakPoint1_ptr =
74  boost::make_shared<stdair::BreakPointStruct> (lBP1DateTime);
75 
76  // Create an event structure
77  stdair::EventStruct lEventBreakPoint1 (stdair::EventType::BRK_PT,
78  lBreakPoint1_ptr);
79 
80  // Add the event into the queue
81  addEvent(ioEventQueue, lEventBreakPoint1);
82 
83  // Create a shared pointer on a second break point
84  // Date of the break point (14-MAY-2011)
85  const stdair::Date_T lBP2Date (2011, boost::gregorian::May, 14);
86  // Time of the break point (00:00)
87  const stdair::Duration_T lBP2Time (0, 0, 0);
88  // Date-time of the break point (made of the date and time above)
89  const stdair::DateTime_T lBP2DateTime (lBP2Date, lBP2Time);
90 
91  // TODO: understand why the form above does not work.
92  const stdair::BreakPointPtr_T lBreakPoint2_ptr =
93  boost::make_shared<stdair::BreakPointStruct> (lBP2DateTime);
94 
95  // Create an event structure
96  stdair::EventStruct lEventBreakPoint2 (stdair::EventType::BRK_PT,
97  lBreakPoint2_ptr);
98 
99  // Add the event into the queue
100  addEvent(ioEventQueue, lEventBreakPoint2);
101 
102  }
103 
104  // //////////////////////////////////////////////////////////////////////
105  stdair::BookingRequestStruct EventQueueManager::
106  buildSampleBookingRequest(stdair::STDAIR_ServicePtr_T lSTDAIR_ServicePtr,
107  const bool isForCRS) {
108 
109  // Delegate the booking request building to the dedicated service
110  stdair::BookingRequestStruct oBookingRequest =
111  lSTDAIR_ServicePtr->buildSampleBookingRequest (isForCRS);
112 
113  return oBookingRequest;
114  }
115 
116 
117  // ////////////////////////////////////////////////////////////////////
118  void EventQueueManager::reset (EventQueue& ioEventQueue) {
119 
123  ioEventQueue.reset();
124  }
125 
126  // ////////////////////////////////////////////////////////////////////
127  bool EventQueueManager::
128  hasProgressStatus (const EventQueue& iEventQueue,
129  const stdair::EventType::EN_EventType& iEventType) {
130 
134  const bool hasProgressStatus = iEventQueue.hasProgressStatus(iEventType);
135 
136  //
137  return hasProgressStatus;
138  }
139 
140  // ////////////////////////////////////////////////////////////////////
141  void EventQueueManager::addEvent (EventQueue& ioEventQueue,
142  stdair::EventStruct& iEventStruct) {
143 
147  ioEventQueue.addEvent(iEventStruct);
148  }
149 
150  // //////////////////////////////////////////////////////////////////////
151  const std::string EventQueueManager::
152  list (const EventQueue& iEventQueue) {
153 
157  const std::string& lEventListStr = iEventQueue.list();
158 
159  //
160  return lEventListStr;
161  }
162 
163  // //////////////////////////////////////////////////////////////////////
164  const std::string EventQueueManager::
165  list (const EventQueue& iEventQueue,
166  const stdair::EventType::EN_EventType& iEventType) {
167 
171  const std::string& lEventListStr =
172  iEventQueue.list(iEventType);
173 
174  //
175  return lEventListStr;
176  }
177 
178  // //////////////////////////////////////////////////////////////////////
179  const std::string EventQueueManager::
180  describeKey (const EventQueue& iEventQueue) {
181 
185  const std::string& lEventQueueKeyStr = iEventQueue.describeKey();
186 
187  //
188  return lEventQueueKeyStr;
189  }
190 
191  // ////////////////////////////////////////////////////////////////////
192  stdair::ProgressStatusSet EventQueueManager::
193  popEvent (EventQueue& ioEventQueue,
194  stdair::EventStruct& iEventStruct) {
195 
196  try {
200  const stdair::ProgressStatusSet& lProgressStatusSet
201  = ioEventQueue.popEvent (iEventStruct);
202 
203  // DEBUG
204  std::ostringstream oEventStr;
205  oEventStr << "Poped event: '"
206  << iEventStruct.describe() << "'.";
207  STDAIR_LOG_DEBUG (oEventStr.str());
208 
209  //
210  return lProgressStatusSet;
211 
212  } catch (EventQueueException& lEventQueueException) {
213  // DEBUG
214  std::ostringstream oErrorMessage;
215  oErrorMessage << "The event queue is empty: no event can be popped out.";
216  std::cerr << oErrorMessage.str() << std::endl;
217  STDAIR_LOG_DEBUG(oErrorMessage.str());
218 
219  }
220 
221  //
222  return stdair::ProgressStatusSet(stdair::EventType::BKG_REQ);
223  }
224 
225  // ////////////////////////////////////////////////////////////////////
226  void EventQueueManager::run (EventQueue& ioEventQueue,
227  stdair::EventStruct& iEventStruct) {
228 
229  // Default event type
230  stdair::EventType::EN_EventType lEventType = stdair::EventType::BKG_REQ;
231 
232  // While no break point has been encountered, keep on extracting events
233  while (ioEventQueue.isQueueDone() == false
234  && lEventType != stdair::EventType::BRK_PT) {
238  ioEventQueue.popEvent (iEventStruct);
239  lEventType = iEventStruct.getEventType();
240 
241  }
242 
243  }
244 
245  // ////////////////////////////////////////////////////////////////////
246  bool EventQueueManager::select (EventQueue& ioEventQueue,
247  stdair::EventStruct& iEventStruct,
248  const stdair::DateTime_T& iDateTime) {
249 
250  // Search if an event has the given key
251  const bool hasResearchBeenSuccessful =
252  ioEventQueue.hasEventDateTime (iDateTime);
253 
254  // If no event has the given hey, return
255  if (hasResearchBeenSuccessful == false) {
256  return hasResearchBeenSuccessful;
257  }
258  assert (hasResearchBeenSuccessful == true);
259 
260  // Default date time
261  stdair::DateTime_T lDateTime = stdair::DEFAULT_EVENT_OLDEST_DATETIME;
262 
263  // While the event with the given key has not been retrieved, keep on
264  // extracting events
265  while (ioEventQueue.isQueueDone() == false
266  && lDateTime != iDateTime) {
267  ioEventQueue.popEvent (iEventStruct);
268  lDateTime = iEventStruct.getEventTime ();
269 
270  }
271 
272  assert (lDateTime == iDateTime);
273  return hasResearchBeenSuccessful;
274 
275  }
276 
277  // ////////////////////////////////////////////////////////////////////
278  void EventQueueManager::
279  updateStatus (EventQueue& ioEventQueue,
280  const stdair::EventType::EN_EventType& iEventType,
281  const stdair::Count_T& iEventCount) {
282 
286  ioEventQueue.updateStatus (iEventType, iEventCount);
287  }
288 
289  // ////////////////////////////////////////////////////////////////////
290  void EventQueueManager::
291  addStatus (EventQueue& ioEventQueue,
292  const stdair::EventType::EN_EventType& iEventType,
293  const stdair::Count_T& iEventCount) {
294 
299  ioEventQueue.addStatus (iEventType, iEventCount);
300  }
301 
302  // ////////////////////////////////////////////////////////////////////
303  bool EventQueueManager::
304  isQueueDone (const EventQueue& iEventQueue) {
305 
309  const bool isQueueDone = iEventQueue.isQueueDone();
310 
311  //
312  return isQueueDone;
313  }
314 
315  // ////////////////////////////////////////////////////////////////////
316  const stdair::Count_T& EventQueueManager::
317  getQueueSize (const EventQueue& iEventQueue) {
318 
322  const stdair::Count_T& lQueueSize = iEventQueue.getQueueSize();
323 
324  //
325  return lQueueSize;
326  }
327 
328  // ////////////////////////////////////////////////////////////////////
329  const stdair::Count_T& EventQueueManager::
330  getExpectedTotalNumberOfEventsToBeGenerated (const EventQueue& ioEventQueue) {
331 
335  const stdair::Count_T& lExpectedTotalNumberOfEvents =
336  ioEventQueue.getExpectedTotalNbOfEvents ();
337 
338  //
339  return lExpectedTotalNumberOfEvents;
340  }
341 
342  // ////////////////////////////////////////////////////////////////////
343  const stdair::Count_T& EventQueueManager::
344  getExpectedTotalNumberOfEventsToBeGenerated (const EventQueue& ioEventQueue,
345  const stdair::EventType::EN_EventType& iEventType) {
346 
350  const stdair::Count_T& lExpectedTotalNumberOfEvents =
351  ioEventQueue.getExpectedTotalNbOfEvents (iEventType);
352 
353  //
354  return lExpectedTotalNumberOfEvents;
355  }
356 
357  // ////////////////////////////////////////////////////////////////////
358  const stdair::Count_T& EventQueueManager::
359  getActualTotalNumberOfEventsToBeGenerated (const EventQueue& ioEventQueue) {
360 
364  const stdair::Count_T& lActualTotalNumberOfEvents =
365  ioEventQueue.getActualTotalNbOfEvents ();
366 
367  //
368  return lActualTotalNumberOfEvents;
369 
370  }
371 
372  // ////////////////////////////////////////////////////////////////////
373  const stdair::Count_T& EventQueueManager::
374  getActualTotalNumberOfEventsToBeGenerated (const EventQueue& ioEventQueue,
375  const stdair::EventType::EN_EventType& iEventType) {
376 
380  const stdair::Count_T& lActualTotalNumberOfEvents =
381  ioEventQueue.getActualTotalNbOfEvents (iEventType);
382 
383  //
384  return lActualTotalNumberOfEvents;
385 
386  }
387 
389  const stdair::ProgressStatus& EventQueueManager::
390  getStatus (const EventQueue& iEventQueue,
391  const stdair::EventType::EN_EventType& iEventType) {
392 
396  return iEventQueue.getStatus(iEventType);
397 
398  }
399 
401  const stdair::ProgressStatus& EventQueueManager::
402  getStatus (const EventQueue& iEventQueue) {
403 
407  return iEventQueue.getStatus();
408 
409  }
410 
411 }