00001
00002
00003
00004
00005 #include <cassert>
00006 #include <cmath>
00007
00008 #include <boost/make_shared.hpp>
00009
00010 #include <stdair/basic/BasChronometer.hpp>
00011 #include <stdair/basic/EventType.hpp>
00012 #include <stdair/bom/BomKeyManager.hpp>
00013 #include <stdair/bom/EventQueue.hpp>
00014 #include <stdair/bom/SnapshotStruct.hpp>
00015 #include <stdair/bom/RMEventStruct.hpp>
00016 #include <stdair/service/Logger.hpp>
00017 #include <stdair/STDAIR_Service.hpp>
00018
00019 #include <airinv/basic/BasConst_AIRINV_Service.hpp>
00020 #include <airinv/factory/FacAirinvMasterServiceContext.hpp>
00021 #include <airinv/command/InventoryParser.hpp>
00022 #include <airinv/command/InventoryManager.hpp>
00023 #include <airinv/service/AIRINV_Master_ServiceContext.hpp>
00024 #include <airinv/AIRINV_Service.hpp>
00025 #include <airinv/AIRINV_Master_Service.hpp>
00026
00027 namespace AIRINV {
00028
00029
00030 AIRINV_Master_Service::AIRINV_Master_Service()
00031 : _airinvMasterServiceContext (NULL) {
00032 assert (false);
00033 }
00034
00035
00036 AIRINV_Master_Service::
00037 AIRINV_Master_Service (const AIRINV_Master_Service& iService)
00038 : _airinvMasterServiceContext (NULL) {
00039 assert (false);
00040 }
00041
00042
00043 AIRINV_Master_Service::
00044 AIRINV_Master_Service (const stdair::BasLogParams& iLogParams,
00045 const stdair::BasDBParams& iDBParams)
00046 : _airinvMasterServiceContext (NULL) {
00047
00048
00049 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00050 initStdAirService (iLogParams, iDBParams);
00051
00052
00053 initServiceContext();
00054
00055
00056
00057 const bool ownStdairService = true;
00058 addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00059
00060
00061 initSlaveAirinvService();
00062 }
00063
00064
00065 AIRINV_Master_Service::
00066 AIRINV_Master_Service (const stdair::BasLogParams& iLogParams)
00067 : _airinvMasterServiceContext (NULL) {
00068
00069
00070 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00071 initStdAirService (iLogParams);
00072
00073
00074 initServiceContext();
00075
00076
00077
00078 const bool ownStdairService = true;
00079 addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00080
00081
00082 initSlaveAirinvService();
00083 }
00084
00085
00086 AIRINV_Master_Service::
00087 AIRINV_Master_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr)
00088 : _airinvMasterServiceContext (NULL) {
00089
00090
00091 initServiceContext();
00092
00093
00094
00095 const bool doesNotOwnStdairService = false;
00096 addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
00097
00098
00099 initSlaveAirinvService();
00100 }
00101
00102
00103 AIRINV_Master_Service::~AIRINV_Master_Service() {
00104
00105 finalise();
00106 }
00107
00108
00109 void AIRINV_Master_Service::finalise() {
00110 assert (_airinvMasterServiceContext != NULL);
00111
00112 _airinvMasterServiceContext->reset();
00113 }
00114
00115
00116 void AIRINV_Master_Service::initServiceContext() {
00117
00118 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00119 FacAirinvMasterServiceContext::instance().create();
00120 _airinvMasterServiceContext = &lAIRINV_Master_ServiceContext;
00121 }
00122
00123
00124 void AIRINV_Master_Service::
00125 addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
00126 const bool iOwnStdairService) {
00127
00128
00129 assert (_airinvMasterServiceContext != NULL);
00130 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00131 *_airinvMasterServiceContext;
00132
00133
00134 lAIRINV_Master_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
00135 iOwnStdairService);
00136 }
00137
00138
00139 stdair::STDAIR_ServicePtr_T AIRINV_Master_Service::
00140 initStdAirService (const stdair::BasLogParams& iLogParams,
00141 const stdair::BasDBParams& iDBParams) {
00142
00150 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00151 boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
00152
00153 return lSTDAIR_Service_ptr;
00154 }
00155
00156
00157 stdair::STDAIR_ServicePtr_T AIRINV_Master_Service::
00158 initStdAirService (const stdair::BasLogParams& iLogParams) {
00159
00167 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00168 boost::make_shared<stdair::STDAIR_Service> (iLogParams);
00169
00170 return lSTDAIR_Service_ptr;
00171 }
00172
00173
00174 void AIRINV_Master_Service::initSlaveAirinvService() {
00175
00176
00177 assert (_airinvMasterServiceContext != NULL);
00178 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00179 *_airinvMasterServiceContext;
00180
00181
00182 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00183 lAIRINV_Master_ServiceContext.getSTDAIR_ServicePtr();
00184 assert (lSTDAIR_Service_ptr != NULL);
00185
00195 AIRINV_ServicePtr_T lAIRINV_Service_ptr =
00196 boost::make_shared<AIRINV_Service> (lSTDAIR_Service_ptr);
00197
00198
00199 lAIRINV_Master_ServiceContext.setAIRINV_Service (lAIRINV_Service_ptr);
00200 }
00201
00202
00203 void AIRINV_Master_Service::
00204 parseAndLoad (const stdair::Filename_T& iInventoryInputFilename) {
00205
00206
00207 if (_airinvMasterServiceContext == NULL) {
00208 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00209 "has not been initialised");
00210 }
00211 assert (_airinvMasterServiceContext != NULL);
00212
00213 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00214 *_airinvMasterServiceContext;
00215
00216
00217
00218 AIRINV_Service& lAIRINV_Service =
00219 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00220
00221
00222 lAIRINV_Service.parseAndLoad (iInventoryInputFilename);
00223 }
00224
00225
00226 void AIRINV_Master_Service::
00227 parseAndLoad (const stdair::Filename_T& iScheduleInputFilename,
00228 const stdair::Filename_T& iODInputFilename,
00229 const AIRRAC::YieldFilePath& iYieldFilename) {
00230
00231
00232 if (_airinvMasterServiceContext == NULL) {
00233 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00234 "has not been initialised");
00235 }
00236 assert (_airinvMasterServiceContext != NULL);
00237
00238 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00239 *_airinvMasterServiceContext;
00240
00241
00242
00243 AIRINV_Service& lAIRINV_Service =
00244 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00245
00246
00247 lAIRINV_Service.parseAndLoad (iScheduleInputFilename, iODInputFilename,
00248 iYieldFilename);
00249 }
00250
00251
00252 void AIRINV_Master_Service::buildSampleBom() {
00253
00254
00255 if (_airinvMasterServiceContext == NULL) {
00256 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00257 "has not been initialised");
00258 }
00259 assert (_airinvMasterServiceContext != NULL);
00260
00261
00262
00263 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00264 *_airinvMasterServiceContext;
00265 const bool doesOwnStdairService =
00266 lAIRINV_Master_ServiceContext.getOwnStdairServiceFlag();
00267
00268
00269 stdair::STDAIR_Service& lSTDAIR_Service =
00270 lAIRINV_Master_ServiceContext.getSTDAIR_Service();
00271
00276 if (doesOwnStdairService == true) {
00277
00278 lSTDAIR_Service.buildSampleBom();
00279 }
00280
00289 AIRINV_Service& lAIRINV_Service =
00290 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00291 lAIRINV_Service.buildSampleBom();
00292
00300 }
00301
00302
00303 std::string AIRINV_Master_Service::
00304 jsonExport (const stdair::AirlineCode_T& iAirlineCode,
00305 const stdair::FlightNumber_T& iFlightNumber,
00306 const stdair::Date_T& iDepartureDate) const {
00307
00308
00309 if (_airinvMasterServiceContext == NULL) {
00310 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00311 "has not been initialised");
00312 }
00313 assert (_airinvMasterServiceContext != NULL);
00314
00315 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00316 *_airinvMasterServiceContext;
00317
00318
00319
00320 AIRINV_Service& lAIRINV_Service =
00321 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00322
00323
00324 return lAIRINV_Service.jsonExport (iAirlineCode, iFlightNumber,
00325 iDepartureDate);
00326 }
00327
00328
00329 std::string AIRINV_Master_Service::
00330 list (const stdair::AirlineCode_T& iAirlineCode,
00331 const stdair::FlightNumber_T& iFlightNumber) const {
00332 std::ostringstream oFlightListStr;
00333
00334
00335 if (_airinvMasterServiceContext == NULL) {
00336 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00337 "has not been initialised");
00338 }
00339 assert (_airinvMasterServiceContext != NULL);
00340
00341 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00342 *_airinvMasterServiceContext;
00343
00344
00345
00346 AIRINV_Service& lAIRINV_Service =
00347 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00348
00349
00350 return lAIRINV_Service.list (iAirlineCode, iFlightNumber);
00351 }
00352
00353
00354 bool AIRINV_Master_Service::
00355 check (const stdair::AirlineCode_T& iAirlineCode,
00356 const stdair::FlightNumber_T& iFlightNumber,
00357 const stdair::Date_T& iDepartureDate) const {
00358 std::ostringstream oFlightListStr;
00359
00360
00361 if (_airinvMasterServiceContext == NULL) {
00362 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00363 "has not been initialised");
00364 }
00365 assert (_airinvMasterServiceContext != NULL);
00366
00367 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00368 *_airinvMasterServiceContext;
00369
00370
00371
00372 AIRINV_Service& lAIRINV_Service =
00373 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00374
00375
00376 return lAIRINV_Service.check (iAirlineCode, iFlightNumber, iDepartureDate);
00377 }
00378
00379
00380 std::string AIRINV_Master_Service::csvDisplay() const {
00381
00382
00383 if (_airinvMasterServiceContext == NULL) {
00384 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00385 "has not been initialised");
00386 }
00387 assert (_airinvMasterServiceContext != NULL);
00388
00389 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00390 *_airinvMasterServiceContext;
00391
00392
00393
00394 AIRINV_Service& lAIRINV_Service =
00395 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00396
00397
00398 return lAIRINV_Service.csvDisplay();
00399 }
00400
00401
00402 std::string AIRINV_Master_Service::
00403 csvDisplay (const stdair::AirlineCode_T& iAirlineCode,
00404 const stdair::FlightNumber_T& iFlightNumber,
00405 const stdair::Date_T& iDepartureDate) const {
00406
00407
00408 if (_airinvMasterServiceContext == NULL) {
00409 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00410 "has not been initialised");
00411 }
00412 assert (_airinvMasterServiceContext != NULL);
00413
00414 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00415 *_airinvMasterServiceContext;
00416
00417
00418
00419 AIRINV_Service& lAIRINV_Service =
00420 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00421
00422
00423 return lAIRINV_Service.csvDisplay (iAirlineCode, iFlightNumber,
00424 iDepartureDate);
00425 }
00426
00427
00428 void AIRINV_Master_Service::
00429 initSnapshotAndRMEvents (const stdair::Date_T& iStartDate,
00430 const stdair::Date_T& iEndDate) {
00431
00432
00433 if (_airinvMasterServiceContext == NULL) {
00434 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00435 "has not been initialised");
00436 }
00437 assert (_airinvMasterServiceContext != NULL);
00438
00439 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00440 *_airinvMasterServiceContext;
00441
00442
00443 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00444 lAIRINV_Master_ServiceContext.getSTDAIR_ServicePtr();
00445 assert (lSTDAIR_Service_ptr != NULL);
00446
00447
00448 stdair::EventQueue& lQueue = lSTDAIR_Service_ptr->getEventQueue();
00449
00450
00451 InventoryManager::initSnapshotEvents (iStartDate, iEndDate, lQueue);
00452
00453
00454
00455
00456
00457
00458 AIRINV_Service& lAIRINV_Service =
00459 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00460 lQueue.addStatus (stdair::EventType::RM, 0);
00461 stdair::RMEventList_T lRMEventList =
00462 lAIRINV_Service.initRMEvents (iStartDate, iEndDate);
00463 InventoryManager::addRMEventsToEventQueue (lQueue, lRMEventList);
00464 }
00465
00466
00467 void AIRINV_Master_Service::
00468 calculateAvailability (stdair::TravelSolutionStruct& ioTravelSolution,
00469 const stdair::PartnershipTechnique& iPartnershipTechnique) {
00470
00471
00472 if (_airinvMasterServiceContext == NULL) {
00473 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00474 "has not been initialised");
00475 }
00476 assert (_airinvMasterServiceContext != NULL);
00477
00478 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00479 *_airinvMasterServiceContext;
00480
00481
00482
00483 AIRINV_Service& lAIRINV_Service =
00484 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00485
00486
00487 stdair::BasChronometer lAvlChronometer;
00488 lAvlChronometer.start();
00489
00490 lAIRINV_Service.calculateAvailability (ioTravelSolution, iPartnershipTechnique);
00491
00492
00493
00494
00495
00496 }
00497
00498
00499 bool AIRINV_Master_Service::sell (const std::string& iSegmentDateKey,
00500 const stdair::ClassCode_T& iClassCode,
00501 const stdair::PartySize_T& iPartySize) {
00502
00503
00504 if (_airinvMasterServiceContext == NULL) {
00505 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00506 "has not been initialised");
00507 }
00508 assert (_airinvMasterServiceContext != NULL);
00509
00510 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00511 *_airinvMasterServiceContext;
00512
00513
00514
00515
00516
00517
00518
00519 AIRINV_Service& lAIRINV_Service =
00520 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00521
00522
00523 stdair::BasChronometer lSellChronometer;
00524 lSellChronometer.start();
00525
00526
00527 const bool hasBeenSaleSuccessful =
00528 lAIRINV_Service.sell (iSegmentDateKey, iClassCode, iPartySize);
00529
00530
00531
00532
00533
00534
00535
00536
00537 return hasBeenSaleSuccessful;
00538 }
00539
00540
00541 bool AIRINV_Master_Service::cancel (const std::string& iSegmentDateKey,
00542 const stdair::ClassCode_T& iClassCode,
00543 const stdair::PartySize_T& iPartySize) {
00544
00545
00546 if (_airinvMasterServiceContext == NULL) {
00547 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00548 "has not been initialised");
00549 }
00550 assert (_airinvMasterServiceContext != NULL);
00551
00552 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00553 *_airinvMasterServiceContext;
00554
00555
00556
00557
00558
00559
00560
00561 AIRINV_Service& lAIRINV_Service =
00562 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00563
00564
00565 stdair::BasChronometer lCancelChronometer;
00566 lCancelChronometer.start();
00567
00568
00569 const bool hasBeenSaleSuccessful =
00570 lAIRINV_Service.cancel (iSegmentDateKey, iClassCode, iPartySize);
00571
00572
00573
00574
00575
00576
00577
00578
00579 return hasBeenSaleSuccessful;
00580 }
00581
00582
00583 void AIRINV_Master_Service::
00584 takeSnapshots (const stdair::SnapshotStruct& iSnapshot) {
00585
00586
00587 if (_airinvMasterServiceContext == NULL) {
00588 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00589 "has not been initialised");
00590 }
00591 assert (_airinvMasterServiceContext != NULL);
00592
00593 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00594 *_airinvMasterServiceContext;
00595
00596
00597
00598 AIRINV_Service& lAIRINV_Service =
00599 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00600
00601
00602 const stdair::DateTime_T& lSnapshotTime = iSnapshot.getSnapshotTime();
00603 const stdair::AirlineCode_T& lAirlineCode = iSnapshot.getAirlineCode();
00604
00605 lAIRINV_Service.takeSnapshots (lAirlineCode, lSnapshotTime);
00606 }
00607
00608
00609 void AIRINV_Master_Service::
00610 optimise (const stdair::RMEventStruct& iRMEvent,
00611 const stdair::ForecastingMethod& iForecastingMethod,
00612 const stdair::PartnershipTechnique& iPartnershipTechnique) {
00613
00614
00615 if (_airinvMasterServiceContext == NULL) {
00616 throw stdair::NonInitialisedServiceException ("The AirInvMaster service "
00617 "has not been initialised");
00618 }
00619 assert (_airinvMasterServiceContext != NULL);
00620
00621 AIRINV_Master_ServiceContext& lAIRINV_Master_ServiceContext =
00622 *_airinvMasterServiceContext;
00623
00624
00625
00626 AIRINV_Service& lAIRINV_Service =
00627 lAIRINV_Master_ServiceContext.getAIRINV_Service();
00628
00629
00630 const stdair::DateTime_T& lRMEventTime = iRMEvent.getRMEventTime();
00631 const stdair::AirlineCode_T& lAirlineCode = iRMEvent.getAirlineCode();
00632 const stdair::KeyDescription_T& lFDDescription =
00633 iRMEvent.getFlightDateDescription();
00634
00635 lAIRINV_Service.optimise (lAirlineCode, lFDDescription, lRMEventTime,
00636 iForecastingMethod, iPartnershipTechnique);
00637 }
00638 }