AirRAC Logo  0.2.3
C++ Simulated Revenue Accounting (RAC) System Library
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 BOM root object.
00173     assert (_airracServiceContext != NULL);
00174     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00175     stdair::STDAIR_Service& lSTDAIR_Service =
00176       lAIRRAC_ServiceContext.getSTDAIR_Service();
00177     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00178     
00179     // Initialise the airline inventories
00180     YieldParser::generateYieldStore (iYieldFilename, lBomRoot);
00181   }
00182 
00183   // ////////////////////////////////////////////////////////////////////
00184   void AIRRAC_Service::buildSampleBom() {
00185 
00186     // Retrieve the AirRAC service context
00187     if (_airracServiceContext == NULL) {
00188       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00189                                                     " been initialised");
00190     }
00191     assert (_airracServiceContext != NULL);
00192 
00193     // Retrieve the AirRAC service context and whether it owns the Stdair
00194     // service
00195     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00196     const bool doesOwnStdairService =
00197       lAIRRAC_ServiceContext.getOwnStdairServiceFlag();
00198 
00199     // Retrieve the StdAir service object from the (AirRAC) service context
00200     stdair::STDAIR_Service& lSTDAIR_Service =
00201       lAIRRAC_ServiceContext.getSTDAIR_Service();
00202 
00207     if (doesOwnStdairService == true) {
00208       //
00209       lSTDAIR_Service.buildSampleBom();
00210     }
00211 
00227   }
00228 
00229   // //////////////////////////////////////////////////////////////////////
00230   void AIRRAC_Service::
00231   buildSampleTravelSolutions(stdair::TravelSolutionList_T& ioTravelSolutionList){
00232 
00233     // Retrieve the AIRRAC service context
00234     if (_airracServiceContext == NULL) {
00235       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00236                                                     " been initialised");
00237     }
00238     assert (_airracServiceContext != NULL);
00239 
00240     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00241   
00242     // Retrieve the STDAIR service object from the (AirRAC) service context
00243     stdair::STDAIR_Service& lSTDAIR_Service =
00244       lAIRRAC_ServiceContext.getSTDAIR_Service();
00245 
00246     // Delegate the BOM building to the dedicated service
00247     lSTDAIR_Service.buildSampleTravelSolutions (ioTravelSolutionList);
00248   }
00249 
00250   // //////////////////////////////////////////////////////////////////////
00251   std::string AIRRAC_Service::csvDisplay() const {
00252 
00253     // Retrieve the AIRRAC service context
00254     if (_airracServiceContext == NULL) {
00255       throw stdair::NonInitialisedServiceException ("The Airrac service "
00256                                                     "has not been initialised");
00257     }
00258     assert (_airracServiceContext != NULL);
00259 
00260     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00261   
00262     // Retrieve the STDAIR service object from the (Airrac) service context
00263     stdair::STDAIR_Service& lSTDAIR_Service =
00264       lAIRRAC_ServiceContext.getSTDAIR_Service();
00265 
00266     // Get the root of the BOM tree, on which all of the other BOM objects
00267     // are attached
00268     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00269 
00270     // Delegate the BOM display to the dedicated service
00271     std::ostringstream oCSVStr;
00272     stdair::BomDisplay::csvSimFQTAirRACDisplay (oCSVStr, lBomRoot);
00273     return oCSVStr.str();
00274     
00275   }
00276 
00277   // //////////////////////////////////////////////////////////////////////
00278   std::string AIRRAC_Service::
00279   csvDisplay (const stdair::TravelSolutionList_T& ioTravelSolutionList) const {
00280 
00281     // Retrieve the AirRAC service context
00282     if (_airracServiceContext == NULL) {
00283       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00284                                                     " been initialised");
00285     }
00286     assert (_airracServiceContext != NULL);
00287 
00288     // Retrieve the AirRAC service context
00289     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00290   
00291     // Retrieve the STDAIR service object from the (AirRAC) service context
00292     stdair::STDAIR_Service& lSTDAIR_Service =
00293       lAIRRAC_ServiceContext.getSTDAIR_Service();
00294 
00295     // Delegate the BOM building to the dedicated service
00296     return lSTDAIR_Service.csvDisplay (ioTravelSolutionList);
00297   }
00298 
00299   // ////////////////////////////////////////////////////////////////////
00300   void AIRRAC_Service::
00301   calculateYields (stdair::TravelSolutionList_T& ioTravelSolutionList) {
00302     
00303     // Retrieve the Airrac service context
00304     if (_airracServiceContext == NULL) {
00305       throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
00306                                                     " been initialised");
00307     }
00308     assert (_airracServiceContext != NULL);
00309     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00310 
00311     // Retrieve the StdAir service context
00312     stdair::STDAIR_Service& lSTDAIR_Service =
00313       lAIRRAC_ServiceContext.getSTDAIR_Service();
00314     
00315     // Get the root of the BOM tree, on which all of the other BOM objects
00316     // will be attached
00317     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00318 
00319     // Delegate the booking to the dedicated command: set the yields
00320     // for each travel solution of the given list
00321     stdair::BasChronometer lYieldChronometer;
00322     lYieldChronometer.start();
00323     YieldManager::calculateYield (ioTravelSolutionList, lBomRoot);
00324     const double lYieldMeasure = lYieldChronometer.elapsed();
00325 
00326     // DEBUG
00327     STDAIR_LOG_DEBUG ("Yield calculation: " << lYieldMeasure << " - "
00328                       << lAIRRAC_ServiceContext.display());
00329   }
00330 
00331   // ////////////////////////////////////////////////////////////////////
00332   void AIRRAC_Service::updateYields () {
00333     // Retrieve the AirRAC service context
00334     assert (_airracServiceContext != NULL);
00335     AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
00336 
00337     // Retrieve the StdAir service context
00338     stdair::STDAIR_Service& lSTDAIR_Service =
00339       lAIRRAC_ServiceContext.getSTDAIR_Service();
00340     
00341     // Get the root of the BOM tree, on which all of the other BOM objects
00342     // will be attached
00343     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00344 
00345     // Update the default yields to the booking classes.
00346     YieldManager::updateYields (lBomRoot);
00347   }
00348 }
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines