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

AIRRAC_Service.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 // Boost
00007 #include <boost/make_shared.hpp>
00008 // StdAir
00009 #include <stdair/basic/BasChronometer.hpp>
00010 #include <stdair/bom/BomDisplay.hpp>
00011 #include <stdair/service/Logger.hpp>
00012 #include <stdair/STDAIR_Service.hpp>
00013 // Airrac
00014 #include <airrac/basic/BasConst_AIRRAC_Service.hpp>
00015 #include <airrac/factory/FacAirracServiceContext.hpp>
00016 #include <airrac/command/YieldParser.hpp>
00017 #include <airrac/command/YieldManager.hpp>
00018 #include <airrac/service/AIRRAC_ServiceContext.hpp>
00019 #include <airrac/AIRRAC_Service.hpp>
00020 
00021 namespace AIRRAC {
00022 
00023   // //////////////////////////////////////////////////////////////////////
00024   AIRRAC_Service::AIRRAC_Service() : _airracServiceContext (NULL) {
00025     assert (false);
00026   }
00027 
00028   // //////////////////////////////////////////////////////////////////////
00029   AIRRAC_Service::AIRRAC_Service (const AIRRAC_Service& iService) {
00030     assert (false);
00031   }
00032 
00033   // ////////////////////////////////////////////////////////////////////
00034   AIRRAC_Service::AIRRAC_Service (const stdair::BasLogParams& iLogParams)
00035     : _airracServiceContext (NULL) {
00036     
00037     // Initialise the STDAIR service handler
00038     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00039       initStdAirService (iLogParams);
00040     
00041     // Initialise the service context
00042     initServiceContext();
00043 
00044     // Add the StdAir service context to the AIRRAC service context
00045     // \note AIRRAC owns the STDAIR service resources here.
00046     const bool ownStdairService = true;
00047     addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00048     
00049     // Initialise the (remaining of the) context
00050     initAirracService();
00051   }
00052 
00053   // ////////////////////////////////////////////////////////////////////
00054   AIRRAC_Service::AIRRAC_Service (const stdair::BasLogParams& iLogParams,
00055                                   const stdair::BasDBParams& iDBParams)
00056     : _airracServiceContext (NULL) {
00057     
00058     // Initialise the STDAIR service handler
00059     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00060       initStdAirService (iLogParams, iDBParams);
00061     
00062     // Initialise the service context
00063     initServiceContext();
00064 
00065     // Add the StdAir service context to the AIRRAC service context
00066     // \note AIRRAC owns the STDAIR service resources here.
00067     const bool ownStdairService = true;
00068     addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00069     
00070     // Initialise the (remaining of the) context
00071     initAirracService();
00072   }
00073 
00074   // ////////////////////////////////////////////////////////////////////
00075   AIRRAC_Service::
00076   AIRRAC_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr)
00077     : _airracServiceContext (NULL) {
00078 
00079     // Initialise the service context
00080     initServiceContext();
00081 
00082     // Store the STDAIR service object within the (AIRRAC) service context
00083     // \note Airrac does not own the STDAIR service resources here.
00084     const bool doesNotOwnStdairService = false;
00085     addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
00086     
00087     // Initialise the context
00088     initAirracService();
00089   }
00090 
00091   // //////////////////////////////////////////////////////////////////////
00092   AIRRAC_Service::~AIRRAC_Service() {
00093     // Delete/Clean all the objects from memory
00094     finalise();
00095   }
00096   
00097   // //////////////////////////////////////////////////////////////////////
00098   void AIRRAC_Service::finalise() {
00099     assert (_airracServiceContext != NULL);
00100     // Reset the (Boost.)Smart pointer pointing on the STDAIR_Service object.
00101     _airracServiceContext->reset();
00102   }
00103 
00104   // //////////////////////////////////////////////////////////////////////
00105   void AIRRAC_Service::initServiceContext() {
00106     // Initialise the service context
00107     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = 
00108       FacAirracServiceContext::instance().create();
00109     _airracServiceContext = &lAIRRAC_ServiceContext;
00110   }
00111 
00112   // //////////////////////////////////////////////////////////////////////
00113   stdair::STDAIR_ServicePtr_T AIRRAC_Service::
00114   initStdAirService (const stdair::BasLogParams& iLogParams,
00115                      const stdair::BasDBParams& iDBParams) {
00116 
00124     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr = 
00125       boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
00126     
00127     return lSTDAIR_Service_ptr;
00128 
00129   }
00130   
00131   // //////////////////////////////////////////////////////////////////////
00132   stdair::STDAIR_ServicePtr_T AIRRAC_Service::
00133   initStdAirService (const stdair::BasLogParams& iLogParams) {
00134 
00142     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr = 
00143       boost::make_shared<stdair::STDAIR_Service> (iLogParams);
00144     
00145     return lSTDAIR_Service_ptr;
00146   }
00147   
00148   // ////////////////////////////////////////////////////////////////////
00149   void AIRRAC_Service::
00150   addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
00151                     const bool iOwnStdairService) {
00152 
00153     // Retrieve the Airrac service context
00154     assert (_airracServiceContext != NULL);
00155     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00156 
00157     // Store the STDAIR service object within the (AIRRAC) service context
00158     lAIRRAC_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
00159                                               iOwnStdairService);
00160   }
00161   
00162   // ////////////////////////////////////////////////////////////////////
00163   void AIRRAC_Service::initAirracService() {
00164     // Do nothing at this stage. A sample BOM tree may be built by
00165     // calling the buildSampleBom() method
00166   }
00167   
00168   // ////////////////////////////////////////////////////////////////////
00169   void AIRRAC_Service::
00170   parseAndLoad (const YieldFilePath& iYieldFilename) { 
00171 
00172     // Retrieve the AirRAC service context
00173     if (_airracServiceContext == NULL) {
00174       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00175                                                     " been initialised");
00176     }
00177     assert (_airracServiceContext != NULL);
00178 
00179     // Retrieve the AirRAC service context and whether it owns the Stdair
00180     // service
00181     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00182     const bool doesOwnStdairService =
00183       lAIRRAC_ServiceContext.getOwnStdairServiceFlag();
00184 
00185     // Retrieve the StdAir service object from the (AirRAC) service context
00186     stdair::STDAIR_Service& lSTDAIR_Service =
00187       lAIRRAC_ServiceContext.getSTDAIR_Service();
00188 
00189     // Retrieve the BOM root object.
00190     stdair::BomRoot& lPersistentBomRoot = 
00191       lSTDAIR_Service.getPersistentBomRoot();
00192     
00196     YieldParser::generateYieldStore (iYieldFilename, lPersistentBomRoot);    
00197 
00210     buildComplementaryLinks (lPersistentBomRoot);
00211 
00216     if (doesOwnStdairService == true) {
00217       //
00218       clonePersistentBom ();
00219     }
00220   }
00221 
00222   // ////////////////////////////////////////////////////////////////////
00223   void AIRRAC_Service::buildSampleBom() {
00224 
00225     // Retrieve the AirRAC service context
00226     if (_airracServiceContext == NULL) {
00227       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00228                                                     " been initialised");
00229     }
00230     assert (_airracServiceContext != NULL);
00231 
00232     // Retrieve the AirRAC service context and whether it owns the Stdair
00233     // service
00234     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00235     const bool doesOwnStdairService =
00236       lAIRRAC_ServiceContext.getOwnStdairServiceFlag();
00237 
00238     // Retrieve the StdAir service object from the (AirRAC) service context
00239     stdair::STDAIR_Service& lSTDAIR_Service =
00240       lAIRRAC_ServiceContext.getSTDAIR_Service(); 
00241 
00242     // Retrieve the persistent BOM root object.
00243     stdair::BomRoot& lPersistentBomRoot = 
00244       lSTDAIR_Service.getPersistentBomRoot();
00245 
00250     if (doesOwnStdairService == true) {
00251       //
00252       lSTDAIR_Service.buildSampleBom();
00253     }
00254 
00267     buildComplementaryLinks (lPersistentBomRoot);
00268 
00273     if (doesOwnStdairService == true) {
00274       //
00275       clonePersistentBom ();
00276     }
00277   } 
00278 
00279   // ////////////////////////////////////////////////////////////////////
00280   void AIRRAC_Service::clonePersistentBom () { 
00281 
00282     // Retrieve the AirRAC service context
00283     if (_airracServiceContext == NULL) {
00284       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00285                                                     " been initialised");
00286     }
00287     assert (_airracServiceContext != NULL);
00288 
00289     // Retrieve the AirRAC service context and whether it owns the Stdair
00290     // service
00291     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00292     const bool doesOwnStdairService =
00293       lAIRRAC_ServiceContext.getOwnStdairServiceFlag();
00294 
00295     // Retrieve the StdAir service object from the (AirRAC) service context
00296     stdair::STDAIR_Service& lSTDAIR_Service =
00297       lAIRRAC_ServiceContext.getSTDAIR_Service();
00298  
00303     if (doesOwnStdairService == true) {
00304  
00305       //
00306       lSTDAIR_Service.clonePersistentBom ();
00307     }  
00308 
00321     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();   
00322     buildComplementaryLinks (lBomRoot);
00323   } 
00324 
00325   // ////////////////////////////////////////////////////////////////////
00326   void AIRRAC_Service::buildComplementaryLinks (stdair::BomRoot& ioBomRoot) {
00327     // Currently, no more things to do by AirRAC at that stage.
00328   } 
00329 
00330   // //////////////////////////////////////////////////////////////////////
00331   void AIRRAC_Service::
00332   buildSampleTravelSolutions(stdair::TravelSolutionList_T& ioTravelSolutionList){
00333 
00334     // Retrieve the AIRRAC service context
00335     if (_airracServiceContext == NULL) {
00336       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00337                                                     " been initialised");
00338     }
00339     assert (_airracServiceContext != NULL);
00340 
00341     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00342   
00343     // Retrieve the STDAIR service object from the (AirRAC) service context
00344     stdair::STDAIR_Service& lSTDAIR_Service =
00345       lAIRRAC_ServiceContext.getSTDAIR_Service();
00346 
00347     // Delegate the BOM building to the dedicated service
00348     lSTDAIR_Service.buildSampleTravelSolutions (ioTravelSolutionList);
00349   }
00350 
00351   // //////////////////////////////////////////////////////////////////////
00352   std::string AIRRAC_Service::csvDisplay() const {
00353 
00354     // Retrieve the AIRRAC service context
00355     if (_airracServiceContext == NULL) {
00356       throw stdair::NonInitialisedServiceException ("The Airrac service "
00357                                                     "has not been initialised");
00358     }
00359     assert (_airracServiceContext != NULL);
00360 
00361     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00362   
00363     // Retrieve the STDAIR service object from the (Airrac) service context
00364     stdair::STDAIR_Service& lSTDAIR_Service =
00365       lAIRRAC_ServiceContext.getSTDAIR_Service();
00366 
00367     // Get the root of the BOM tree, on which all of the other BOM objects
00368     // are attached
00369     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00370 
00371     // Delegate the BOM display to the dedicated service
00372     std::ostringstream oCSVStr;
00373     stdair::BomDisplay::csvSimFQTAirRACDisplay (oCSVStr, lBomRoot);
00374     return oCSVStr.str();
00375     
00376   }
00377 
00378   // //////////////////////////////////////////////////////////////////////
00379   std::string AIRRAC_Service::
00380   csvDisplay (const stdair::TravelSolutionList_T& ioTravelSolutionList) const {
00381 
00382     // Retrieve the AirRAC service context
00383     if (_airracServiceContext == NULL) {
00384       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00385                                                     " been initialised");
00386     }
00387     assert (_airracServiceContext != NULL);
00388 
00389     // Retrieve the AirRAC service context
00390     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00391   
00392     // Retrieve the STDAIR service object from the (AirRAC) service context
00393     stdair::STDAIR_Service& lSTDAIR_Service =
00394       lAIRRAC_ServiceContext.getSTDAIR_Service();
00395 
00396     // Delegate the BOM building to the dedicated service
00397     return lSTDAIR_Service.csvDisplay (ioTravelSolutionList);
00398   }
00399 
00400   // ////////////////////////////////////////////////////////////////////
00401   void AIRRAC_Service::
00402   calculateYields (stdair::TravelSolutionList_T& ioTravelSolutionList) {
00403     
00404     // Retrieve the Airrac service context
00405     if (_airracServiceContext == NULL) {
00406       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00407                                                     " been initialised");
00408     }
00409     assert (_airracServiceContext != NULL);
00410     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00411 
00412     // Retrieve the StdAir service context
00413     stdair::STDAIR_Service& lSTDAIR_Service =
00414       lAIRRAC_ServiceContext.getSTDAIR_Service();
00415     
00416     // Get the root of the BOM tree, on which all of the other BOM objects
00417     // will be attached
00418     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00419 
00420     // Delegate the booking to the dedicated command: set the yields
00421     // for each travel solution of the given list
00422     stdair::BasChronometer lYieldChronometer;
00423     lYieldChronometer.start();
00424     YieldManager::calculateYield (ioTravelSolutionList, lBomRoot);
00425     const double lYieldMeasure = lYieldChronometer.elapsed();
00426 
00427     // DEBUG
00428     STDAIR_LOG_DEBUG ("Yield calculation: " << lYieldMeasure << " - "
00429                       << lAIRRAC_ServiceContext.display());
00430   }
00431 
00432   // ////////////////////////////////////////////////////////////////////
00433   void AIRRAC_Service::updateYields (stdair::BomRoot& ioBomRoot) {
00434     // Retrieve the AirRAC service context
00435     assert (_airracServiceContext != NULL);
00436     
00437     // Update the default yields to the booking classes.
00438     YieldManager::updateYields (ioBomRoot);
00439   }
00440 }