$treeview $search $mathjax
TraDemGen Logo  1.00.0
$projectbrief
$projectbrief
$searchbox

TRADEMGEN_Service.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 // Boost
00008 #include <boost/make_shared.hpp>
00009 #if defined(SOCI_HEADERS_BURIED)
00010 #include <soci/core/soci.h>
00011 #else // SOCI_HEADERS_BURIED
00012 #include <soci/soci.h>
00013 #endif // SOCI_HEADERS_BURIED
00014 // StdAir
00015 #include <stdair/basic/BasChronometer.hpp>
00016 #include <stdair/basic/BasConst_General.hpp>
00017 #include <stdair/basic/ProgressStatusSet.hpp>
00018 #include <stdair/bom/BomRoot.hpp>
00019 #include <stdair/bom/BookingRequestStruct.hpp>
00020 #include <stdair/bom/AirlineStruct.hpp>
00021 #include <stdair/bom/EventStruct.hpp>
00022 #include <stdair/command/DBManagerForAirlines.hpp>
00023 #include <stdair/service/Logger.hpp>
00024 #include <stdair/service/DBSessionManager.hpp>
00025 #include <stdair/STDAIR_Service.hpp>
00026 #include <stdair/factory/FacBomManager.hpp>
00027 // SEvMgr
00028 #include <sevmgr/SEVMGR_Service.hpp>
00029 // TraDemGen
00030 #include <trademgen/basic/BasConst_TRADEMGEN_Service.hpp>
00031 #include <trademgen/bom/BomDisplay.hpp>
00032 #include <trademgen/bom/DemandStream.hpp>
00033 #include <trademgen/bom/DemandStreamTypes.hpp>
00034 #include <trademgen/factory/FacTRADEMGENServiceContext.hpp>
00035 #include <trademgen/command/DemandParser.hpp>
00036 #include <trademgen/command/DemandManager.hpp>
00037 #include <trademgen/service/TRADEMGEN_ServiceContext.hpp>
00038 #include <trademgen/TRADEMGEN_Service.hpp>
00039 
00040 namespace TRADEMGEN {
00041 
00042   // //////////////////////////////////////////////////////////////////////
00043   TRADEMGEN_Service::TRADEMGEN_Service() : _trademgenServiceContext (NULL) {
00044     assert (false);
00045   }
00046 
00047   // //////////////////////////////////////////////////////////////////////
00048   TRADEMGEN_Service::TRADEMGEN_Service (const TRADEMGEN_Service& iService) 
00049     : _trademgenServiceContext (NULL) {
00050     assert (false);
00051   }
00052 
00053   // //////////////////////////////////////////////////////////////////////
00054   TRADEMGEN_Service::TRADEMGEN_Service (const stdair::BasLogParams& iLogParams,
00055                                         const stdair::RandomSeed_T& iRandomSeed)
00056     : _trademgenServiceContext (NULL) {
00057     
00058     // Initialise the STDAIR service handler
00059     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00060       initStdAirService (iLogParams);
00061     
00062     // Initialise the service context
00063     initServiceContext (iRandomSeed);
00064 
00065     // Add the StdAir service context to the TRADEMGEN service context
00066     // \note TRADEMGEN owns the STDAIR service resources here.
00067     const bool ownStdairService = true;
00068     addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00069 
00070     // Initalise the SEvMgr service.
00071     initSEVMGRService();
00072 
00073     // Initialise the (remaining of the) context
00074     initTrademgenService();
00075   }
00076 
00077   // //////////////////////////////////////////////////////////////////////
00078   TRADEMGEN_Service::TRADEMGEN_Service (const stdair::BasLogParams& iLogParams,
00079                                         const stdair::BasDBParams& iDBParams,
00080                                         const stdair::RandomSeed_T& iRandomSeed)
00081     : _trademgenServiceContext (NULL) {
00082     
00083     // Initialise the STDAIR service handler
00084     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00085       initStdAirService (iLogParams, iDBParams);
00086     
00087     // Initialise the service context
00088     initServiceContext (iRandomSeed);
00089 
00090     // Add the StdAir service context to the TRADEMGEN service context
00091     // \note TRADEMGEN owns the STDAIR service resources here.
00092     const bool ownStdairService = true;
00093     addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00094 
00095     // Initalise the SEvMgr service.
00096     initSEVMGRService();
00097 
00098     // Initialise the (remaining of the) context
00099     initTrademgenService();
00100   }
00101 
00102   // ////////////////////////////////////////////////////////////////////
00103   TRADEMGEN_Service::
00104   TRADEMGEN_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
00105                      SEVMGR::SEVMGR_ServicePtr_T ioSEVMGR_Service_ptr,
00106                      const stdair::RandomSeed_T& iRandomSeed)
00107     : _trademgenServiceContext (NULL) {
00108 
00109     // Initialise the service context
00110     initServiceContext (iRandomSeed);   
00111 
00112     // Add the StdAir service context to the TRADEMGEN service context
00113     // \note TraDemGen does not own the STDAIR service resources here.
00114     const bool doesNotOwnStdairService = false;
00115     addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
00116 
00117     //Add the SEvMgr service to the TRADEMGEN service context.
00118     addSEVMGRService (ioSEVMGR_Service_ptr);
00119 
00120     // Initialise the context
00121     initTrademgenService();
00122   }
00123 
00124   // //////////////////////////////////////////////////////////////////////
00125   TRADEMGEN_Service::~TRADEMGEN_Service() {
00126     // Delete/Clean all the objects from memory
00127     finalise();
00128   }
00129 
00130   // ////////////////////////////////////////////////////////////////////
00131   void TRADEMGEN_Service::finalise() {
00132     assert (_trademgenServiceContext != NULL);
00133     // Reset the (Boost.)Smart pointer pointing on the STDAIR_Service object.
00134     _trademgenServiceContext->reset();
00135   }
00136 
00137   // //////////////////////////////////////////////////////////////////////
00138   void TRADEMGEN_Service::
00139   initServiceContext (const stdair::RandomSeed_T& iRandomSeed) {
00140     // Initialise the service context
00141     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext = 
00142       FacTRADEMGENServiceContext::instance().create (iRandomSeed);
00143     _trademgenServiceContext = &lTRADEMGEN_ServiceContext;
00144   }
00145 
00146   // ////////////////////////////////////////////////////////////////////
00147   void TRADEMGEN_Service::
00148   addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
00149                     const bool iOwnStdairService) {
00150     // Retrieve the TraDemGen service context
00151     assert (_trademgenServiceContext != NULL);
00152     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00153       *_trademgenServiceContext;
00154 
00155     // Store the STDAIR service object within the (TRADEMGEN) service context
00156     lTRADEMGEN_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
00157                                                  iOwnStdairService);
00158   } 
00159 
00160   // ////////////////////////////////////////////////////////////////////
00161   void TRADEMGEN_Service::
00162   addSEVMGRService (SEVMGR::SEVMGR_ServicePtr_T ioSEVMGR_Service_ptr) {
00163 
00164     // Retrieve the TraDemGen service context
00165     assert (_trademgenServiceContext != NULL);
00166     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00167       *_trademgenServiceContext;
00168 
00169     // Store the STDAIR service object within the (TRADEMGEN) service context
00170     lTRADEMGEN_ServiceContext.setSEVMGR_Service (ioSEVMGR_Service_ptr);
00171   }
00172   
00173   // //////////////////////////////////////////////////////////////////////
00174   stdair::STDAIR_ServicePtr_T TRADEMGEN_Service::
00175   initStdAirService (const stdair::BasLogParams& iLogParams,
00176                      const stdair::BasDBParams& iDBParams) {
00177 
00183     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr = 
00184       boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
00185     assert (lSTDAIR_Service_ptr != NULL);
00186 
00187     return lSTDAIR_Service_ptr;
00188   }
00189   
00190   // //////////////////////////////////////////////////////////////////////
00191   stdair::STDAIR_ServicePtr_T TRADEMGEN_Service::
00192   initStdAirService (const stdair::BasLogParams& iLogParams) {
00193 
00199     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr = 
00200       boost::make_shared<stdair::STDAIR_Service> (iLogParams);
00201     assert (lSTDAIR_Service_ptr != NULL);
00202 
00203     return lSTDAIR_Service_ptr;
00204   }
00205 
00206   // ////////////////////////////////////////////////////////////////////
00207   void TRADEMGEN_Service::initSEVMGRService() {
00208 
00209     // Retrieve the TraDemGen service context
00210     assert (_trademgenServiceContext != NULL);
00211     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00212       *_trademgenServiceContext;
00213 
00214     // Retrieve the StdAir service context
00215     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00216       lTRADEMGEN_ServiceContext.getSTDAIR_ServicePtr();
00217 
00225     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr = 
00226       boost::make_shared<SEVMGR::SEVMGR_Service> (lSTDAIR_Service_ptr);
00227     
00228     // Store the SEvMgr service object within the (TraDemGen) service context
00229     lTRADEMGEN_ServiceContext.setSEVMGR_Service (lSEVMGR_Service_ptr);
00230   }
00231   
00232   
00233   // //////////////////////////////////////////////////////////////////////
00234   void TRADEMGEN_Service::initTrademgenService() {
00235     // Do nothing at this stage. A sample BOM tree may be built by
00236     // calling the buildSampleBom() method
00237   }
00238 
00239   // //////////////////////////////////////////////////////////////////////
00240   void TRADEMGEN_Service::
00241   parseAndLoad (const DemandFilePath& iDemandFilePath) { 
00242 
00243     // Retrieve the TraDemGen service context
00244     if (_trademgenServiceContext == NULL) {
00245       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00246                                                     "not been initialised");
00247     }
00248     assert (_trademgenServiceContext != NULL);
00249 
00250     // Retrieve the TraDemGen service context and whether it owns the Stdair
00251     // service
00252     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00253       *_trademgenServiceContext;
00254     const bool doesOwnStdairService =
00255       lTRADEMGEN_ServiceContext.getOwnStdairServiceFlag();   
00256 
00257     // Retrieve the StdAir service object from the (TRADEMGEN) service context
00258     stdair::STDAIR_Service& lSTDAIR_Service =
00259       lTRADEMGEN_ServiceContext.getSTDAIR_Service();
00260 
00261     // Retrieve the persistent BOM root object.
00262     stdair::BomRoot& lPersistentBomRoot = 
00263       lSTDAIR_Service.getPersistentBomRoot();
00264 
00265     // Retrieve the pointer on the SEvMgr service handler.
00266     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00267       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00268     
00269     // Retrieve the shared generator
00270     stdair::RandomGeneration& lSharedGenerator =
00271       lTRADEMGEN_ServiceContext.getUniformGenerator();
00272 
00273     // Retrieve the default POS distribution
00274     const POSProbabilityMass_T& lDefaultPOSProbabilityMass =
00275       lTRADEMGEN_ServiceContext.getPOSProbabilityMass();
00276 
00280     stdair::BasChronometer lDemandGeneration; lDemandGeneration.start();
00281     DemandParser::generateDemand (iDemandFilePath, lSEVMGR_Service_ptr,
00282                                   lSharedGenerator, lDefaultPOSProbabilityMass);
00283     const double lGenerationMeasure = lDemandGeneration.elapsed();  
00284 
00294     buildComplementaryLinks (lPersistentBomRoot);
00295 
00296     // DEBUG
00297     STDAIR_LOG_DEBUG ("Demand generation time: " << lGenerationMeasure);
00298 
00303     if (doesOwnStdairService == true) {
00304       //
00305       clonePersistentBom ();
00306     }
00307   }
00308 
00309   // ////////////////////////////////////////////////////////////////////
00310   void TRADEMGEN_Service::buildSampleBom() {
00311 
00312     // Retrieve the TraDemGen service context
00313     if (_trademgenServiceContext == NULL) {
00314       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00315                                                     "not been initialised");
00316     }
00317     assert (_trademgenServiceContext != NULL);
00318 
00319     // Retrieve the TraDemGen service context and whether it owns the Stdair
00320     // service
00321     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00322       *_trademgenServiceContext;
00323     const bool doesOwnStdairService =
00324       lTRADEMGEN_ServiceContext.getOwnStdairServiceFlag();
00325 
00326     // Retrieve the StdAir service object from the (TraDemGen) service context
00327     stdair::STDAIR_Service& lSTDAIR_Service =
00328       lTRADEMGEN_ServiceContext.getSTDAIR_Service();
00329 
00330     // Retrieve the persistent BOM root object.
00331     stdair::BomRoot& lPersistentBomRoot = 
00332       lSTDAIR_Service.getPersistentBomRoot();
00333 
00338     if (doesOwnStdairService == true) {
00339       //
00340       lSTDAIR_Service.buildSampleBom();
00341     }
00342 
00353     // Retrieve the shared generator
00354     stdair::RandomGeneration& lSharedGenerator =
00355       lTRADEMGEN_ServiceContext.getUniformGenerator();
00356 
00357     // Retrieve the default POS distribution
00358     const POSProbabilityMass_T& lDefaultPOSProbabilityMass =
00359       lTRADEMGEN_ServiceContext.getPOSProbabilityMass();
00360 
00361     // Retrieve the pointer on the SEvMgr service handler.
00362     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00363       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00364 
00365     // Delegate the BOM building to the dedicated service
00366     DemandManager::buildSampleBom (lSEVMGR_Service_ptr, lSharedGenerator,
00367                                    lDefaultPOSProbabilityMass);
00368     // Build the complementary links
00369     buildComplementaryLinks (lPersistentBomRoot);
00370 
00375     if (doesOwnStdairService == true) {
00376       //
00377       clonePersistentBom ();
00378     }
00379   }  
00380 
00381   // ////////////////////////////////////////////////////////////////////
00382   void TRADEMGEN_Service::clonePersistentBom () {   
00383 
00384     // Retrieve the TraDemGen service context
00385     if (_trademgenServiceContext == NULL) {
00386       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00387                                                     "not been initialised");
00388     }
00389     assert (_trademgenServiceContext != NULL);
00390 
00391     // Retrieve the TraDemGen service context and whether it owns the Stdair
00392     // service
00393     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00394       *_trademgenServiceContext;
00395     const bool doesOwnStdairService =
00396       lTRADEMGEN_ServiceContext.getOwnStdairServiceFlag();
00397 
00398     // Retrieve the StdAir service object from the (TraDemGen) service context
00399     stdair::STDAIR_Service& lSTDAIR_Service =
00400       lTRADEMGEN_ServiceContext.getSTDAIR_Service(); 
00401 
00406     if (doesOwnStdairService == true) {
00407       //
00408       lSTDAIR_Service.clonePersistentBom ();
00409     }  
00410  
00414     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();   
00415     buildComplementaryLinks (lBomRoot);
00416   } 
00417 
00418   // ////////////////////////////////////////////////////////////////////
00419   void TRADEMGEN_Service::buildComplementaryLinks (stdair::BomRoot& ioBomRoot) {
00420     // Currently, no more things to do by TRADEMGEN at that stage.
00421   }
00422 
00423   // //////////////////////////////////////////////////////////////////////
00424   stdair::BookingRequestStruct TRADEMGEN_Service::
00425   buildSampleBookingRequest (const bool isForCRS) {
00426 
00427     // Retrieve the TraDemGen service context
00428     if (_trademgenServiceContext == NULL) {
00429       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00430                                                     "not been initialised");
00431     }
00432     assert (_trademgenServiceContext != NULL);
00433 
00434     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00435       *_trademgenServiceContext;
00436 
00437     // Retrieve the STDAIR service object from the (TraDemGen) service context
00438     stdair::STDAIR_Service& lSTDAIR_Service =
00439       lTRADEMGEN_ServiceContext.getSTDAIR_Service();
00440 
00441     // Delegate the BOM building to the dedicated service
00442     return lSTDAIR_Service.buildSampleBookingRequest (isForCRS);
00443   }
00444 
00445   // ////////////////////////////////////////////////////////////////////
00446   std::string TRADEMGEN_Service::
00447   jsonHandler (const stdair::JSONString& iJSONString) const {
00448 
00449     // Retrieve the TraDemGen service context
00450     if (_trademgenServiceContext == NULL) {
00451       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00452                                                     "not been initialised");
00453     }
00454     assert (_trademgenServiceContext != NULL);
00455     
00456     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00457       *_trademgenServiceContext;
00458 
00459     // Retrieve the pointer on the SEvMgr service handler.
00460     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00461       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00462 
00463     return lSEVMGR_Service_ptr->jsonHandler (iJSONString);
00464     
00465   }
00466 
00467   // //////////////////////////////////////////////////////////////////////
00468   std::string TRADEMGEN_Service::csvDisplay() const {
00469 
00470     // Retrieve the TraDemGen service context
00471     if (_trademgenServiceContext == NULL) {
00472       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00473                                                     "not been initialised");
00474     }
00475     assert (_trademgenServiceContext != NULL);
00476 
00477     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00478       *_trademgenServiceContext;
00479 
00480     // Retrieve the pointer on the SEvMgr service handler.
00481     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00482       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00483     
00484     // Delegate the BOM building to the dedicated service
00485     return BomDisplay::csvDisplay (lSEVMGR_Service_ptr);
00486 
00487   }
00488 
00489   // //////////////////////////////////////////////////////////////////////
00490   std::string TRADEMGEN_Service::list() const {
00491 
00492     // Retrieve the TraDemGen service context
00493     if (_trademgenServiceContext == NULL) {
00494       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00495                                                     "not been initialised");
00496     }
00497     assert (_trademgenServiceContext != NULL);
00498 
00499     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00500       *_trademgenServiceContext;
00501 
00502     // Retrieve the pointer on the SEvMgr service handler.
00503     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00504       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00505     
00506     // Delegate the BOM building to the dedicated service
00507     return lSEVMGR_Service_ptr->list ();
00508 
00509   }  
00510 
00511   // //////////////////////////////////////////////////////////////////////
00512   std::string TRADEMGEN_Service::
00513   list(const stdair::EventType::EN_EventType& iEventType) const {
00514 
00515     // Retrieve the TraDemGen service context
00516     if (_trademgenServiceContext == NULL) {
00517       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00518                                                     "not been initialised");
00519     }
00520     assert (_trademgenServiceContext != NULL);
00521 
00522     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00523       *_trademgenServiceContext;
00524 
00525     // Retrieve the pointer on the SEvMgr service handler.
00526     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00527       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00528     
00529     // Delegate the BOM building to the dedicated service
00530     return lSEVMGR_Service_ptr->list (iEventType);
00531 
00532   }
00533 
00534   // //////////////////////////////////////////////////////////////////////
00535   void TRADEMGEN_Service::displayAirlineListFromDB() const {
00536 
00537     // Retrieve the TraDemGen service context
00538     if (_trademgenServiceContext == NULL) {
00539       throw stdair::NonInitialisedServiceException ("The TraDemGen service has "
00540                                                     "not been initialised");
00541     }
00542     assert (_trademgenServiceContext != NULL);
00543     // TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00544     //  *_trademgenServiceContext;
00545 
00546     // Get the date-time for the present time
00547     boost::posix_time::ptime lNowDateTime =
00548       boost::posix_time::second_clock::local_time();
00549     //boost::gregorian::date lNowDate = lNowDateTime.date();
00550 
00551     // DEBUG
00552     STDAIR_LOG_DEBUG (std::endl
00553                       << "==================================================="
00554                       << std::endl
00555                       << lNowDateTime);
00556 
00557     // Delegate the query execution to the dedicated command
00558     stdair::BasChronometer lAirListChronometer;
00559     lAirListChronometer.start();
00560 
00561     // Retrieve the database session handler
00562     stdair::DBSession_T& lDBSession =
00563       stdair::DBSessionManager::instance().getDBSession();
00564     
00565     // Prepare and execute the select statement
00566     stdair::AirlineStruct lAirline;
00567     stdair::DBRequestStatement_T lSelectStatement (lDBSession);
00568     stdair::DBManagerForAirlines::prepareSelectStatement (lDBSession,
00569                                                           lSelectStatement,
00570                                                           lAirline);
00571     
00572     // Prepare the SQL request corresponding to the select statement
00573     bool hasStillData = true;
00574     unsigned int idx = 0;
00575     while (hasStillData == true) {
00576       hasStillData =
00577         stdair::DBManagerForAirlines::iterateOnStatement (lSelectStatement,
00578                                                           lAirline);
00579       
00580       // DEBUG
00581       STDAIR_LOG_DEBUG ("[" << idx << "]: " << lAirline);
00582       
00583       // Iteration
00584       ++idx;
00585     }
00586     
00587     const double lAirListMeasure = lAirListChronometer.elapsed();
00588     
00589     // DEBUG
00590     STDAIR_LOG_DEBUG ("Sample service for airline list retrieval: "
00591                       << lAirListMeasure);
00592   }
00593 
00594   // ////////////////////////////////////////////////////////////////////
00595   const stdair::Count_T& TRADEMGEN_Service::
00596   getExpectedTotalNumberOfRequestsToBeGenerated() const {
00597     
00598     // Retrieve the TraDemGen service context
00599     assert (_trademgenServiceContext != NULL);
00600     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00601       *_trademgenServiceContext;
00602 
00603     // Retrieve the SEvMgr service context
00604     SEVMGR::SEVMGR_Service& lSEVMGR_Service =
00605       lTRADEMGEN_ServiceContext.getSEVMGR_Service();
00606     
00607     // Delegate the call to the dedicated command
00608     const stdair::Count_T& oExpectedTotalNumberOfRequestsToBeGenerated =
00609       lSEVMGR_Service.getExpectedTotalNumberOfEventsToBeGenerated (stdair::EventType::BKG_REQ);
00610 
00611     //
00612     return oExpectedTotalNumberOfRequestsToBeGenerated;
00613   }
00614 
00615   // ////////////////////////////////////////////////////////////////////
00616   const stdair::Count_T& TRADEMGEN_Service::
00617   getActualTotalNumberOfRequestsToBeGenerated() const {
00618     
00619     // Retrieve the TraDemGen service context
00620     assert (_trademgenServiceContext != NULL);
00621     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00622       *_trademgenServiceContext;
00623 
00624     // Retrieve the SEvMgr service context
00625     SEVMGR::SEVMGR_Service& lSEVMGR_Service =
00626       lTRADEMGEN_ServiceContext.getSEVMGR_Service();
00627     
00628     // Delegate the call to the dedicated command
00629     const stdair::Count_T& oActualTotalNumberOfRequestsToBeGenerated =
00630       lSEVMGR_Service.getActualTotalNumberOfEventsToBeGenerated (stdair::EventType::BKG_REQ);
00631 
00632     //
00633     return oActualTotalNumberOfRequestsToBeGenerated;
00634   }
00635 
00636   // ////////////////////////////////////////////////////////////////////
00637   const bool TRADEMGEN_Service::
00638   stillHavingRequestsToBeGenerated (const stdair::DemandStreamKeyStr_T& iKey,
00639                                     stdair::ProgressStatusSet& ioPSS,
00640                                     const stdair::DemandGenerationMethod& iDemandGenerationMethod) const {
00641     
00642     // Retrieve the TraDemGen service context
00643     assert (_trademgenServiceContext != NULL);
00644     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00645       *_trademgenServiceContext;
00646 
00647     // Retrieve the pointer on the SEvMgr service handler.
00648     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00649       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00650     
00651     // Delegate the call to the dedicated command
00652     const bool oStillHavingRequestsToBeGenerated =
00653       DemandManager::stillHavingRequestsToBeGenerated (lSEVMGR_Service_ptr,
00654                                                        iKey, ioPSS,
00655                                                        iDemandGenerationMethod);
00656 
00657     //
00658     return oStillHavingRequestsToBeGenerated;
00659   }
00660 
00661   // ////////////////////////////////////////////////////////////////////
00662   stdair::Count_T TRADEMGEN_Service::
00663   generateFirstRequests (const stdair::DemandGenerationMethod& iDemandGenerationMethod) const {
00664 
00665     // Retrieve the TraDemGen service context
00666     assert (_trademgenServiceContext != NULL);
00667     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00668       *_trademgenServiceContext;
00669 
00670     // Retrieve the pointer on the SEvMgr service handler.
00671     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00672       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00673 
00674     // Retrieve the random generator
00675     stdair::RandomGeneration& lGenerator =
00676       lTRADEMGEN_ServiceContext.getUniformGenerator();
00677 
00678     // Delegate the call to the dedicated command
00679     const stdair::Count_T& oActualTotalNbOfEvents =
00680       DemandManager::generateFirstRequests (lSEVMGR_Service_ptr, lGenerator,
00681                                             iDemandGenerationMethod);
00682 
00683     //
00684     return oActualTotalNbOfEvents;
00685   }
00686 
00687   // ////////////////////////////////////////////////////////////////////
00688   stdair::BookingRequestPtr_T TRADEMGEN_Service::
00689   generateNextRequest (const stdair::DemandStreamKeyStr_T& iKey,
00690                        const stdair::DemandGenerationMethod& iDemandGenerationMethod) const {
00691 
00692     // Retrieve the TraDemGen service context
00693     assert (_trademgenServiceContext != NULL);
00694     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00695       *_trademgenServiceContext;
00696 
00697     // Retrieve the pointer on the SEvMgr service handler.
00698     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00699       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00700     
00701     // Retrieve the random generator
00702     stdair::RandomGeneration& lGenerator =
00703       lTRADEMGEN_ServiceContext.getUniformGenerator();
00704 
00705     // Delegate the call to the dedicated command
00706     return DemandManager::generateNextRequest (lSEVMGR_Service_ptr,
00707                                                lGenerator, iKey,
00708                                                iDemandGenerationMethod);
00709   }
00710 
00711   // ////////////////////////////////////////////////////////////////////
00712   stdair::ProgressStatusSet TRADEMGEN_Service::
00713   popEvent (stdair::EventStruct& ioEventStruct) const {
00714 
00715     // Retrieve the TraDemGen service context
00716     assert (_trademgenServiceContext != NULL);
00717     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00718       *_trademgenServiceContext;
00719 
00720     // Retrieve the SEvMgr service context
00721     SEVMGR::SEVMGR_Service& lSEVMGR_Service =
00722       lTRADEMGEN_ServiceContext.getSEVMGR_Service();
00723     
00724     // Extract the next event from the queue
00725     return lSEVMGR_Service.popEvent (ioEventStruct);
00726   }
00727 
00728   // ////////////////////////////////////////////////////////////////////
00729   bool TRADEMGEN_Service::isQueueDone() const {
00730 
00731     // Retrieve the TraDemGen service context
00732     assert (_trademgenServiceContext != NULL);
00733     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00734       *_trademgenServiceContext;
00735 
00736     // Retrieve the SEvMgr service context
00737     SEVMGR::SEVMGR_Service& lSEVMGR_Service =
00738       lTRADEMGEN_ServiceContext.getSEVMGR_Service();
00739     
00740     // Calculates whether the event queue has been fully emptied
00741     const bool isQueueDone = lSEVMGR_Service.isQueueDone();
00742 
00743     //
00744     return isQueueDone;
00745   }
00746 
00747   // ////////////////////////////////////////////////////////////////////
00748   bool TRADEMGEN_Service::
00749   generateCancellation (const stdair::TravelSolutionStruct& iTravelSolution,
00750                         const stdair::PartySize_T& iPartySize,
00751                         const stdair::DateTime_T& iRequestTime,
00752                         const stdair::Date_T& iDepartureDate) const {
00753 
00754     // Retrieve the TraDemGen service context
00755     assert (_trademgenServiceContext != NULL);
00756     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00757       *_trademgenServiceContext;
00758     
00759     // Retrieve the random generator
00760     stdair::RandomGeneration& lGenerator =
00761       lTRADEMGEN_ServiceContext.getUniformGenerator();
00762 
00763     // Build an event structure with the default constructor.
00764     stdair::EventStruct lEventStruct;
00765     stdair::EventStruct& lRefEventStruct = lEventStruct;
00766 
00767     // Generate the cancellation event
00768     const bool hasCancellationBeenGenerated =
00769       DemandManager::generateCancellation (lGenerator, iTravelSolution,
00770                                            iPartySize, iRequestTime,
00771                                            iDepartureDate, lRefEventStruct);
00772 
00773     // If the cancellation has been not sucessfully gerenerated, return.
00774     if (hasCancellationBeenGenerated == false) {
00775       return  hasCancellationBeenGenerated;
00776     }
00777     assert (hasCancellationBeenGenerated == true);
00778 
00779     // Retrieve the SEvMgr service context
00780     SEVMGR::SEVMGR_Service& lSEVMGR_Service =
00781       lTRADEMGEN_ServiceContext.getSEVMGR_Service();
00782       
00790     // Add the gerenerated cancellation event into the queue
00791     lSEVMGR_Service.addEvent (lRefEventStruct);
00792 
00793     // Update the status of cancellation events within the event queue. 
00794     const bool hasProgressStatus = 
00795       lSEVMGR_Service.hasProgressStatus(stdair::EventType::CX); 
00796     if (hasProgressStatus == false) { 
00797       const stdair::Count_T lCancellationNumber = 1;
00798       lSEVMGR_Service.addStatus (stdair::EventType::CX, lCancellationNumber); 
00799     } else { 
00800       stdair::Count_T lCurrentCancellationNumber = 
00801         lSEVMGR_Service.getActualTotalNumberOfEventsToBeGenerated (stdair::EventType::CX);
00802       lCurrentCancellationNumber++;
00803       lSEVMGR_Service.updateStatus (stdair::EventType::CX, lCurrentCancellationNumber);
00804     }
00805 
00806     return hasCancellationBeenGenerated;
00807 
00808   }
00809 
00810   // ////////////////////////////////////////////////////////////////////
00811   void TRADEMGEN_Service::reset() const {
00812 
00813     // Retrieve the TraDemGen service context
00814     assert (_trademgenServiceContext != NULL);
00815     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00816       *_trademgenServiceContext;
00817 
00818     // Retrieve the pointer on the SEvMgr service handler.
00819     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00820       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00821 
00822     // Retrieve the shared generator
00823     stdair::RandomGeneration& lSharedGenerator =
00824       lTRADEMGEN_ServiceContext.getUniformGenerator();
00825     
00826     // Delegate the call to the dedicated command
00827     DemandManager::reset (lSEVMGR_Service_ptr,
00828                           lSharedGenerator.getBaseGenerator());
00829   }  
00830 
00832   const stdair::ProgressStatus& TRADEMGEN_Service::getProgressStatus() const {    
00833 
00834     // Retrieve the TraDemGen service context
00835     assert (_trademgenServiceContext != NULL);
00836     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00837       *_trademgenServiceContext;
00838 
00839     // Retrieve the pointer on the SEvMgr service handler.
00840     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00841       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00842 
00843     // Delegate the call to the dedicated service
00844     return lSEVMGR_Service_ptr->getStatus();
00845   
00846   }
00847 
00849   const stdair::ProgressStatus& TRADEMGEN_Service::
00850   getProgressStatus (const stdair::EventType::EN_EventType& iEventType) const {     
00851 
00852     // Retrieve the TraDemGen service context
00853     assert (_trademgenServiceContext != NULL);
00854     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00855       *_trademgenServiceContext;
00856 
00857     // Retrieve the pointer on the SEvMgr service handler.
00858     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00859       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00860 
00861     // Delegate the call to the dedicated service
00862     return lSEVMGR_Service_ptr->getStatus(iEventType);
00863   
00864   }
00865   
00867   bool TRADEMGEN_Service::
00868   hasDemandStream (const stdair::DemandStreamKeyStr_T& iDemandStreamKey) const {
00869 
00870     // Retrieve the TraDemGen service context
00871     assert (_trademgenServiceContext != NULL);
00872     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00873       *_trademgenServiceContext;
00874 
00875     // Retrieve the pointer on the SEvMgr service handler.
00876     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00877       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00878 
00879     // Delegate the call to the dedicated service
00880     return lSEVMGR_Service_ptr->hasEventGenerator<DemandStream,
00881                                                   stdair::DemandStreamKeyStr_T>(iDemandStreamKey);
00882   }
00883 
00885   std::string TRADEMGEN_Service::displayDemandStream () const {
00886 
00887     // Retrieve the TraDemGen service context
00888     assert (_trademgenServiceContext != NULL);
00889     TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
00890       *_trademgenServiceContext;
00891 
00892     // Retrieve the pointer on the SEvMgr service handler.
00893     SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
00894       lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
00895 
00896     // Delegate the call to the dedicated service
00897     const DemandStreamList_T lDemandStreamList =
00898       lSEVMGR_Service_ptr->getEventGeneratorList<DemandStream>();
00899 
00900     // Output stream to store the display of demand streams.
00901     std::ostringstream  oStream;
00902 
00903     for (DemandStreamList_T::const_iterator itDemandStream =
00904            lDemandStreamList.begin(); itDemandStream !=
00905            lDemandStreamList.end(); itDemandStream++) {
00906       DemandStream* lDemandStream_ptr = *itDemandStream;
00907       assert (lDemandStream_ptr != NULL);
00908       oStream << lDemandStream_ptr->describeKey() << std::endl; 
00909       
00910     }
00911     return oStream.str();
00912   }
00913 
00914 }
00915 
00916 namespace SEVMGR {
00917 
00926   // ////////////////////////////////////////////////////////////////////
00927   template<class EventGenerator>
00928   void SEVMGR_Service::addEventGenerator (EventGenerator& iEventGenerator) const {
00929     // Retrieve the StdAir service
00930     const stdair::STDAIR_Service& lSTDAIR_Service =
00931       this->getSTDAIR_Service();
00932     
00933     // Retrieve the BOM root object instance
00934     stdair::BomRoot& lPersistentBomRoot = 
00935       lSTDAIR_Service.getPersistentBomRoot();
00936 
00937     // Link the DemandStream to its parent (EventQueue)
00938     stdair::FacBomManager::linkWithParent (lPersistentBomRoot, iEventGenerator);
00939     
00940     // Add the DemandStream to the dedicated list and map
00941     stdair::FacBomManager::addToListAndMap (lPersistentBomRoot, 
00942                                             iEventGenerator);
00943     
00944   }
00945   
00946   // ////////////////////////////////////////////////////////////////////
00947   template<class EventGenerator, class Key>
00948   EventGenerator& SEVMGR_Service::getEventGenerator(const Key& iKey) const {
00949 
00950     // Retrieve the StdAir service
00951     const stdair::STDAIR_Service& lSTDAIR_Service =
00952       this->getSTDAIR_Service(); 
00953 
00954     // Retrieve the BOM root object instance
00955     stdair::BomRoot& lPersistentBomRoot = 
00956       lSTDAIR_Service.getPersistentBomRoot();
00957 
00958     // Retrieve the DemandStream which corresponds to the given key.
00959     EventGenerator& lEventGenerator = 
00960       stdair::BomManager::getObject<EventGenerator> (lPersistentBomRoot, 
00961                                                      iKey);
00962 
00963     return lEventGenerator;
00964     
00965   }
00966   
00967   // ////////////////////////////////////////////////////////////////////
00968   template<class EventGenerator, class Key>
00969   bool SEVMGR_Service::hasEventGenerator(const Key& iKey) const {
00970 
00971     bool hasEventGenerator = true;
00972 
00973     // Retrieve the StdAir service
00974     const stdair::STDAIR_Service& lSTDAIR_Service =
00975       this->getSTDAIR_Service(); 
00976 
00977     // Retrieve the BOM root object instance
00978     stdair::BomRoot& lPersistentBomRoot = 
00979       lSTDAIR_Service.getPersistentBomRoot();
00980 
00981     // Retrieve the DemandStream which corresponds to the given key.
00982     EventGenerator* lEventGenerator_ptr = 
00983       stdair::BomManager::getObjectPtr<EventGenerator> (lPersistentBomRoot, 
00984                                                         iKey);
00985     if (lEventGenerator_ptr == NULL) {
00986       hasEventGenerator = false;
00987     } 
00988 
00989     return hasEventGenerator;
00990     
00991   }
00992   
00993   // ////////////////////////////////////////////////////////////////////
00994   template<class EventGenerator>
00995   const std::list<EventGenerator*> SEVMGR_Service::getEventGeneratorList() const {
00996 
00997     // Retrieve the StdAir service
00998     const stdair::STDAIR_Service& lSTDAIR_Service =
00999       this->getSTDAIR_Service();   
01000 
01001     // Retrieve the BOM root object instance
01002     stdair::BomRoot& lPersistentBomRoot = 
01003       lSTDAIR_Service.getPersistentBomRoot();
01004     
01005     // Retrieve the DemandStream list
01006     const std::list<EventGenerator*> lEventGeneratorList =
01007       stdair::BomManager::getList<EventGenerator> (lPersistentBomRoot);
01008 
01009     return lEventGeneratorList;
01010   }
01011 
01012   // ////////////////////////////////////////////////////////////////////
01013   template<class EventGenerator>
01014   bool SEVMGR_Service::hasEventGeneratorList() const {
01015 
01016     // Retrieve the StdAir service
01017     const stdair::STDAIR_Service& lSTDAIR_Service =
01018       this->getSTDAIR_Service();
01019 
01020     // Retrieve the BOM root object instance
01021     stdair::BomRoot& lPersistentBomRoot = 
01022       lSTDAIR_Service.getPersistentBomRoot();
01023 
01024     const bool hasListEventGenerator =
01025       stdair::BomManager::hasList<EventGenerator> (lPersistentBomRoot);
01026 
01027     return hasListEventGenerator;
01028   }
01029 
01030   // ////////////////////////////////////////////////////////////////////  
01035   template void SEVMGR_Service::
01036   addEventGenerator<TRADEMGEN::DemandStream> (TRADEMGEN::DemandStream&) const;
01037 
01038   template TRADEMGEN::DemandStream& SEVMGR_Service::
01039   getEventGenerator<TRADEMGEN::DemandStream, stdair::DemandStreamKeyStr_T> (const stdair::DemandStreamKeyStr_T&) const;
01040 
01041   template bool SEVMGR_Service::
01042   hasEventGenerator<TRADEMGEN::DemandStream, stdair::DemandStreamKeyStr_T> (const stdair::DemandStreamKeyStr_T&) const;
01043 
01044   template const TRADEMGEN::DemandStreamList_T SEVMGR_Service::
01045   getEventGeneratorList<TRADEMGEN::DemandStream> () const;
01046 
01047   template bool SEVMGR_Service::hasEventGeneratorList<TRADEMGEN::DemandStream>() const;
01048   // ////////////////////////////////////////////////////////////////////
01049 
01050 }