TraDemGen Logo  0.2.2
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
TRADEMGEN_Service.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // Boost
8 #include <boost/make_shared.hpp>
9 #if defined(SOCI_HEADERS_BURIED)
10 #include <soci/core/soci.h>
11 #else // SOCI_HEADERS_BURIED
12 #include <soci/soci.h>
13 #endif // SOCI_HEADERS_BURIED
14 // StdAir
15 #include <stdair/basic/BasChronometer.hpp>
16 #include <stdair/basic/BasConst_General.hpp>
17 #include <stdair/bom/BomRoot.hpp>
18 #include <stdair/bom/BookingRequestStruct.hpp>
19 #include <stdair/bom/AirlineStruct.hpp>
20 #include <stdair/bom/EventStruct.hpp>
21 #include <stdair/bom/EventQueue.hpp>
22 #include <stdair/command/DBManagerForAirlines.hpp>
23 #include <stdair/service/Logger.hpp>
24 #include <stdair/service/DBSessionManager.hpp>
25 #include <stdair/STDAIR_Service.hpp>
26 // TraDemGen
35 
36 namespace TRADEMGEN {
37 
38  // //////////////////////////////////////////////////////////////////////
39  TRADEMGEN_Service::TRADEMGEN_Service() : _trademgenServiceContext (NULL) {
40  assert (false);
41  }
42 
43  // //////////////////////////////////////////////////////////////////////
44  TRADEMGEN_Service::TRADEMGEN_Service (const TRADEMGEN_Service& iService)
45  : _trademgenServiceContext (NULL) {
46  assert (false);
47  }
48 
49  // //////////////////////////////////////////////////////////////////////
50  TRADEMGEN_Service::TRADEMGEN_Service (const stdair::BasLogParams& iLogParams,
51  const stdair::RandomSeed_T& iRandomSeed)
52  : _trademgenServiceContext (NULL) {
53 
54  // Initialise the STDAIR service handler
55  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
56  initStdAirService (iLogParams);
57 
58  // Initialise the service context
59  initServiceContext (iRandomSeed);
60 
61  // Add the StdAir service context to the TRADEMGEN service context
62  // \note TRADEMGEN owns the STDAIR service resources here.
63  const bool ownStdairService = true;
64  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
65 
66  // Initialise the (remaining of the) context
67  initTrademgenService();
68  }
69 
70  // //////////////////////////////////////////////////////////////////////
71  TRADEMGEN_Service::TRADEMGEN_Service (const stdair::BasLogParams& iLogParams,
72  const stdair::BasDBParams& iDBParams,
73  const stdair::RandomSeed_T& iRandomSeed)
74  : _trademgenServiceContext (NULL) {
75 
76  // Initialise the STDAIR service handler
77  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
78  initStdAirService (iLogParams, iDBParams);
79 
80  // Initialise the service context
81  initServiceContext (iRandomSeed);
82 
83  // Add the StdAir service context to the TRADEMGEN service context
84  // \note TRADEMGEN owns the STDAIR service resources here.
85  const bool ownStdairService = true;
86  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
87 
88  // Initialise the (remaining of the) context
89  initTrademgenService();
90  }
91 
92  // ////////////////////////////////////////////////////////////////////
93  TRADEMGEN_Service::
94  TRADEMGEN_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
95  const stdair::RandomSeed_T& iRandomSeed)
96  : _trademgenServiceContext (NULL) {
97 
98  // Initialise the service context
99  initServiceContext (iRandomSeed);
100 
101  // Add the StdAir service context to the TRADEMGEN service context
102  // \note TraDemGen does not own the STDAIR service resources here.
103  const bool doesNotOwnStdairService = false;
104  addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
105 
106  // Initialise the context
107  initTrademgenService();
108  }
109 
110  // //////////////////////////////////////////////////////////////////////
112  // Delete/Clean all the objects from memory
113  finalise();
114  }
115 
116  // ////////////////////////////////////////////////////////////////////
117  void TRADEMGEN_Service::finalise() {
118  assert (_trademgenServiceContext != NULL);
119  // Reset the (Boost.)Smart pointer pointing on the STDAIR_Service object.
120  _trademgenServiceContext->reset();
121  }
122 
123  // //////////////////////////////////////////////////////////////////////
124  void TRADEMGEN_Service::
125  initServiceContext (const stdair::RandomSeed_T& iRandomSeed) {
126  // Initialise the service context
127  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
129  _trademgenServiceContext = &lTRADEMGEN_ServiceContext;
130  }
131 
132  // ////////////////////////////////////////////////////////////////////
133  void TRADEMGEN_Service::
134  addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
135  const bool iOwnStdairService) {
136  // Retrieve the TraDemGen service context
137  assert (_trademgenServiceContext != NULL);
138  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
139  *_trademgenServiceContext;
140 
141  // Store the STDAIR service object within the (TRADEMGEN) service context
142  lTRADEMGEN_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
143  iOwnStdairService);
144  }
145 
146  // //////////////////////////////////////////////////////////////////////
147  stdair::STDAIR_ServicePtr_T TRADEMGEN_Service::
148  initStdAirService (const stdair::BasLogParams& iLogParams,
149  const stdair::BasDBParams& iDBParams) {
150 
156  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
157  boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
158  assert (lSTDAIR_Service_ptr != NULL);
159 
160  return lSTDAIR_Service_ptr;
161  }
162 
163  // //////////////////////////////////////////////////////////////////////
164  stdair::STDAIR_ServicePtr_T TRADEMGEN_Service::
165  initStdAirService (const stdair::BasLogParams& iLogParams) {
166 
172  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
173  boost::make_shared<stdair::STDAIR_Service> (iLogParams);
174  assert (lSTDAIR_Service_ptr != NULL);
175 
176  return lSTDAIR_Service_ptr;
177  }
178 
179  // //////////////////////////////////////////////////////////////////////
180  void TRADEMGEN_Service::initTrademgenService() {
181  // Do nothing at this stage. A sample BOM tree may be built by
182  // calling the buildSampleBom() method
183  }
184 
185  // //////////////////////////////////////////////////////////////////////
187  parseAndLoad (const stdair::Filename_T& iDemandInputFilename) {
188 
189  // Retrieve the TraDemGen service context
190  assert (_trademgenServiceContext != NULL);
191  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
192  *_trademgenServiceContext;
193 
194  // Retrieve the shared generator
195  stdair::RandomGeneration& lSharedGenerator =
196  lTRADEMGEN_ServiceContext.getUniformGenerator();
197 
198  // Retrieve the default POS distribution
199  const POSProbabilityMass_T& lDefaultPOSProbabilityMass =
200  lTRADEMGEN_ServiceContext.getPOSProbabilityMass();
201 
202  // Retrieve the StdAir service context
203  stdair::STDAIR_Service& lSTDAIR_Service =
204  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
205 
206  // Retrieve the event queue
207  stdair::EventQueue& lEventQueue = lSTDAIR_Service.getEventQueue();
208 
209  // Parse the input file and initialise the demand generators
210  stdair::BasChronometer lDemandGeneration; lDemandGeneration.start();
211  DemandParser::generateDemand (iDemandInputFilename, lEventQueue,
212  lSharedGenerator, lDefaultPOSProbabilityMass);
213  const double lGenerationMeasure = lDemandGeneration.elapsed();
214 
215  // DEBUG
216  STDAIR_LOG_DEBUG ("Demand generation time: " << lGenerationMeasure);
217  }
218 
219  // ////////////////////////////////////////////////////////////////////
221 
222  // Retrieve the TraDemGen service context
223  if (_trademgenServiceContext == NULL) {
224  throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
225  "not been initialised");
226  }
227  assert (_trademgenServiceContext != NULL);
228 
229  // Retrieve the TraDemGen service context and whether it owns the Stdair
230  // service
231  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
232  *_trademgenServiceContext;
233  const bool doesOwnStdairService =
234  lTRADEMGEN_ServiceContext.getOwnStdairServiceFlag();
235 
236  // Retrieve the StdAir service object from the (TraDemGen) service context
237  stdair::STDAIR_Service& lSTDAIR_Service =
238  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
239 
244  if (doesOwnStdairService == true) {
245  //
246  lSTDAIR_Service.buildSampleBom();
247  }
248 
261  // Retrieve the shared generator
262  stdair::RandomGeneration& lSharedGenerator =
263  lTRADEMGEN_ServiceContext.getUniformGenerator();
264 
265  // Retrieve the default POS distribution
266  const POSProbabilityMass_T& lDefaultPOSProbabilityMass =
267  lTRADEMGEN_ServiceContext.getPOSProbabilityMass();
268 
269  // Retrieve the event queue
270  stdair::EventQueue& lEventQueue = lSTDAIR_Service.getEventQueue();
271 
272  // Delegate the BOM building to the dedicated service
273  DemandManager::buildSampleBom (lEventQueue, lSharedGenerator,
274  lDefaultPOSProbabilityMass);
275  }
276 
277  // //////////////////////////////////////////////////////////////////////
278  stdair::BookingRequestStruct TRADEMGEN_Service::
279  buildSampleBookingRequest (const bool isForCRS) {
280 
281  // Retrieve the TraDemGen service context
282  if (_trademgenServiceContext == NULL) {
283  throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
284  "not been initialised");
285  }
286  assert (_trademgenServiceContext != NULL);
287 
288  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
289  *_trademgenServiceContext;
290 
291  // Retrieve the STDAIR service object from the (TraDemGen) service context
292  stdair::STDAIR_Service& lSTDAIR_Service =
293  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
294 
295  // Delegate the BOM building to the dedicated service
296  return lSTDAIR_Service.buildSampleBookingRequest (isForCRS);
297  }
298 
299  // //////////////////////////////////////////////////////////////////////
300  std::string TRADEMGEN_Service::csvDisplay() const {
301 
302  // Retrieve the TraDemGen service context
303  if (_trademgenServiceContext == NULL) {
304  throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
305  "not been initialised");
306  }
307  assert (_trademgenServiceContext != NULL);
308 
309  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
310  *_trademgenServiceContext;
311 
312  // Retrieve the STDAIR service object from the (TraDemGen) service context
313  stdair::STDAIR_Service& lSTDAIR_Service =
314  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
315 
316  // Retrieve the event queue
317  stdair::EventQueue& lEventQueue = lSTDAIR_Service.getEventQueue();
318 
319  // Delegate the BOM building to the dedicated service
320  return BomDisplay::csvDisplay (lEventQueue);
321  }
322 
323  // //////////////////////////////////////////////////////////////////////
325 
326  // Retrieve the TraDemGen service context
327  if (_trademgenServiceContext == NULL) {
328  throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
329  "not been initialised");
330  }
331  assert (_trademgenServiceContext != NULL);
332  // TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
333  // *_trademgenServiceContext;
334 
335  // Get the date-time for the present time
336  boost::posix_time::ptime lNowDateTime =
337  boost::posix_time::second_clock::local_time();
338  //boost::gregorian::date lNowDate = lNowDateTime.date();
339 
340  // DEBUG
341  STDAIR_LOG_DEBUG (std::endl
342  << "==================================================="
343  << std::endl
344  << lNowDateTime);
345 
346  // Delegate the query execution to the dedicated command
347  stdair::BasChronometer lAirListChronometer;
348  lAirListChronometer.start();
349 
350  // Retrieve the database session handler
351  stdair::DBSession_T& lDBSession =
352  stdair::DBSessionManager::instance().getDBSession();
353 
354  // Prepare and execute the select statement
355  stdair::AirlineStruct lAirline;
356  stdair::DBRequestStatement_T lSelectStatement (lDBSession);
357  stdair::DBManagerForAirlines::prepareSelectStatement (lDBSession,
358  lSelectStatement,
359  lAirline);
360 
361  // Prepare the SQL request corresponding to the select statement
362  bool hasStillData = true;
363  unsigned int idx = 0;
364  while (hasStillData == true) {
365  hasStillData =
366  stdair::DBManagerForAirlines::iterateOnStatement (lSelectStatement,
367  lAirline);
368 
369  // DEBUG
370  STDAIR_LOG_DEBUG ("[" << idx << "]: " << lAirline);
371 
372  // Iteration
373  ++idx;
374  }
375 
376  const double lAirListMeasure = lAirListChronometer.elapsed();
377 
378  // DEBUG
379  STDAIR_LOG_DEBUG ("Sample service for airline list retrieval: "
380  << lAirListMeasure);
381  }
382 
383  // ////////////////////////////////////////////////////////////////////
384  const stdair::Count_T& TRADEMGEN_Service::
386 
387  // Retrieve the TraDemGen service context
388  assert (_trademgenServiceContext != NULL);
389  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
390  *_trademgenServiceContext;
391 
392  // Retrieve the StdAir service context
393  stdair::STDAIR_Service& lSTDAIR_Service =
394  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
395 
396  // Retrieve the event queue object instance
397  const stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
398 
399  // Delegate the call to the dedicated command
400  const stdair::Count_T& oExpectedTotalNumberOfRequestsToBeGenerated =
401  lQueue.getExpectedTotalNbOfEvents (stdair::EventType::BKG_REQ);
402 
403  //
404  return oExpectedTotalNumberOfRequestsToBeGenerated;
405  }
406 
407  // ////////////////////////////////////////////////////////////////////
408  const stdair::Count_T& TRADEMGEN_Service::
410 
411  // Retrieve the TraDemGen service context
412  assert (_trademgenServiceContext != NULL);
413  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
414  *_trademgenServiceContext;
415 
416  // Retrieve the StdAir service context
417  stdair::STDAIR_Service& lSTDAIR_Service =
418  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
419 
420  // Retrieve the event queue object instance
421  const stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
422 
423  // Delegate the call to the dedicated command
424  const stdair::Count_T& oActualTotalNumberOfRequestsToBeGenerated =
425  lQueue.getActualTotalNbOfEvents (stdair::EventType::BKG_REQ);
426 
427  //
428  return oActualTotalNumberOfRequestsToBeGenerated;
429  }
430 
431  // ////////////////////////////////////////////////////////////////////
432  const bool TRADEMGEN_Service::
433  stillHavingRequestsToBeGenerated (const stdair::DemandStreamKeyStr_T& iKey,
434  stdair::ProgressStatusSet& ioPSS,
435  const stdair::DemandGenerationMethod& iDemandGenerationMethod) const {
436 
437  // Retrieve the TraDemGen service context
438  assert (_trademgenServiceContext != NULL);
439  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
440  *_trademgenServiceContext;
441 
442  // Retrieve the StdAir service context
443  stdair::STDAIR_Service& lSTDAIR_Service =
444  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
445 
446  // Retrieve the event queue object instance
447  const stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
448 
449  // Delegate the call to the dedicated command
450  const bool oStillHavingRequestsToBeGenerated =
452  iDemandGenerationMethod);
453 
454  //
455  return oStillHavingRequestsToBeGenerated;
456  }
457 
458  // ////////////////////////////////////////////////////////////////////
459  stdair::Count_T TRADEMGEN_Service::
460  generateFirstRequests (const stdair::DemandGenerationMethod& iDemandGenerationMethod) const {
461 
462  // Retrieve the TraDemGen service context
463  assert (_trademgenServiceContext != NULL);
464  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
465  *_trademgenServiceContext;
466 
467  // Retrieve the StdAir service context
468  stdair::STDAIR_Service& lSTDAIR_Service =
469  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
470 
471  // Retrieve the event queue object instance
472  stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
473 
474  // Retrieve the random generator
475  stdair::RandomGeneration& lGenerator =
476  lTRADEMGEN_ServiceContext.getUniformGenerator();
477 
478  // Delegate the call to the dedicated command
479  const stdair::Count_T& oActualTotalNbOfEvents =
480  DemandManager::generateFirstRequests (lQueue, lGenerator,
481  iDemandGenerationMethod);
482 
483  //
484  return oActualTotalNbOfEvents;
485  }
486 
487  // ////////////////////////////////////////////////////////////////////
488  stdair::BookingRequestPtr_T TRADEMGEN_Service::
489  generateNextRequest (const stdair::DemandStreamKeyStr_T& iKey,
490  const stdair::DemandGenerationMethod& iDemandGenerationMethod) const {
491 
492  // Retrieve the TraDemGen service context
493  assert (_trademgenServiceContext != NULL);
494  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
495  *_trademgenServiceContext;
496 
497  // Retrieve the StdAir service context
498  stdair::STDAIR_Service& lSTDAIR_Service =
499  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
500 
501  // Retrieve the event queue object instance
502  stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
503 
504  // Retrieve the random generator
505  stdair::RandomGeneration& lGenerator =
506  lTRADEMGEN_ServiceContext.getUniformGenerator();
507 
508  // Delegate the call to the dedicated command
509  return DemandManager::generateNextRequest (lQueue, lGenerator, iKey,
510  iDemandGenerationMethod);
511  }
512 
513  // ////////////////////////////////////////////////////////////////////
514  stdair::ProgressStatusSet TRADEMGEN_Service::
515  popEvent (stdair::EventStruct& ioEventStruct) const {
516 
517  // Retrieve the TraDemGen service context
518  assert (_trademgenServiceContext != NULL);
519  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
520  *_trademgenServiceContext;
521 
522  // Retrieve the StdAir service context
523  stdair::STDAIR_Service& lSTDAIR_Service =
524  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
525 
526  // Retrieve the event queue object instance
527  stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
528 
529  // Extract the next event from the queue
530  return lQueue.popEvent (ioEventStruct);
531  }
532 
533  // ////////////////////////////////////////////////////////////////////
535 
536  // Retrieve the TraDemGen service context
537  assert (_trademgenServiceContext != NULL);
538  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
539  *_trademgenServiceContext;
540 
541  // Retrieve the StdAir service context
542  stdair::STDAIR_Service& lSTDAIR_Service =
543  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
544 
545  // Retrieve the event queue object instance
546  const stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
547 
548  // Calculates whether the event queue has been fully emptied
549  const bool isQueueDone = lQueue.isQueueDone();
550 
551  //
552  return isQueueDone;
553  }
554 
555  // ////////////////////////////////////////////////////////////////////
557  generateCancellation (const stdair::TravelSolutionStruct& iTravelSolution,
558  const stdair::PartySize_T& iPartySize,
559  const stdair::DateTime_T& iRequestTime,
560  const stdair::Date_T& iDepartureDate) const {
561 
562  // Retrieve the TraDemGen service context
563  assert (_trademgenServiceContext != NULL);
564  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
565  *_trademgenServiceContext;
566 
567  // Retrieve the random generator
568  stdair::RandomGeneration& lGenerator =
569  lTRADEMGEN_ServiceContext.getUniformGenerator();
570 
571  // Retrieve the StdAir service context
572  stdair::STDAIR_Service& lSTDAIR_Service =
573  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
574 
575  // Retrieve the event queue object instance
576  stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
577 
578  return DemandManager::generateCancellation (lQueue, lGenerator,
579  iTravelSolution, iPartySize,
580  iRequestTime, iDepartureDate);
581  }
582 
583  // ////////////////////////////////////////////////////////////////////
585 
586  // Retrieve the TraDemGen service context
587  assert (_trademgenServiceContext != NULL);
588  TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
589  *_trademgenServiceContext;
590 
591  // Retrieve the StdAir service context
592  stdair::STDAIR_Service& lSTDAIR_Service =
593  lTRADEMGEN_ServiceContext.getSTDAIR_Service();
594  // Retrieve the event queue object instance
595  stdair::EventQueue& lQueue = lSTDAIR_Service.getEventQueue();
596 
597  // Retrieve the shared generator
598  stdair::RandomGeneration& lSharedGenerator =
599  lTRADEMGEN_ServiceContext.getUniformGenerator();
600 
601  // Delegate the call to the dedicated command
602  DemandManager::reset (lQueue, lSharedGenerator.getBaseGenerator());
603  }
604 }