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
15 #include <stdair/basic/BasChronometer.hpp>
16 #include <stdair/basic/BasConst_General.hpp>
17 #include <stdair/basic/ProgressStatusSet.hpp>
18 #include <stdair/bom/BomRoot.hpp>
19 #include <stdair/bom/BookingRequestStruct.hpp>
20 #include <stdair/bom/AirlineStruct.hpp>
21 #include <stdair/bom/EventStruct.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 #include <stdair/factory/FacBomManager.hpp>
28 #include <sevmgr/SEVMGR_Service.hpp>
43 TRADEMGEN_Service::TRADEMGEN_Service() : _trademgenServiceContext (NULL) {
48 TRADEMGEN_Service::TRADEMGEN_Service (
const TRADEMGEN_Service& iService)
49 : _trademgenServiceContext (NULL) {
54 TRADEMGEN_Service::TRADEMGEN_Service (
const stdair::BasLogParams& iLogParams,
55 const stdair::RandomSeed_T& iRandomSeed)
56 : _trademgenServiceContext (NULL) {
59 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
60 initStdAirService (iLogParams);
63 initServiceContext (iRandomSeed);
67 const bool ownStdairService =
true;
68 addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
74 initTrademgenService();
78 TRADEMGEN_Service::TRADEMGEN_Service (
const stdair::BasLogParams& iLogParams,
79 const stdair::BasDBParams& iDBParams,
80 const stdair::RandomSeed_T& iRandomSeed)
81 : _trademgenServiceContext (NULL) {
84 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
85 initStdAirService (iLogParams, iDBParams);
88 initServiceContext (iRandomSeed);
92 const bool ownStdairService =
true;
93 addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
99 initTrademgenService();
104 TRADEMGEN_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
105 SEVMGR::SEVMGR_ServicePtr_T ioSEVMGR_Service_ptr,
106 const stdair::RandomSeed_T& iRandomSeed)
107 : _trademgenServiceContext (NULL) {
110 initServiceContext (iRandomSeed);
114 const bool doesNotOwnStdairService =
false;
115 addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
118 addSEVMGRService (ioSEVMGR_Service_ptr);
121 initTrademgenService();
131 void TRADEMGEN_Service::finalise() {
132 assert (_trademgenServiceContext != NULL);
134 _trademgenServiceContext->reset();
138 void TRADEMGEN_Service::
139 initServiceContext (
const stdair::RandomSeed_T& iRandomSeed) {
141 TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
143 _trademgenServiceContext = &lTRADEMGEN_ServiceContext;
147 void TRADEMGEN_Service::
148 addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
149 const bool iOwnStdairService) {
151 assert (_trademgenServiceContext != NULL);
152 TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
153 *_trademgenServiceContext;
156 lTRADEMGEN_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
161 void TRADEMGEN_Service::
162 addSEVMGRService (SEVMGR::SEVMGR_ServicePtr_T ioSEVMGR_Service_ptr) {
165 assert (_trademgenServiceContext != NULL);
166 TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
167 *_trademgenServiceContext;
170 lTRADEMGEN_ServiceContext.setSEVMGR_Service (ioSEVMGR_Service_ptr);
174 stdair::STDAIR_ServicePtr_T TRADEMGEN_Service::
175 initStdAirService (
const stdair::BasLogParams& iLogParams,
176 const stdair::BasDBParams& iDBParams) {
183 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
184 boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
185 assert (lSTDAIR_Service_ptr != NULL);
187 return lSTDAIR_Service_ptr;
191 stdair::STDAIR_ServicePtr_T TRADEMGEN_Service::
192 initStdAirService (
const stdair::BasLogParams& iLogParams) {
199 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
200 boost::make_shared<stdair::STDAIR_Service> (iLogParams);
201 assert (lSTDAIR_Service_ptr != NULL);
203 return lSTDAIR_Service_ptr;
207 void TRADEMGEN_Service::initSEVMGRService() {
210 assert (_trademgenServiceContext != NULL);
211 TRADEMGEN_ServiceContext& lTRADEMGEN_ServiceContext =
212 *_trademgenServiceContext;
215 stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
216 lTRADEMGEN_ServiceContext.getSTDAIR_ServicePtr();
225 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
226 boost::make_shared<SEVMGR::SEVMGR_Service> (lSTDAIR_Service_ptr);
229 lTRADEMGEN_ServiceContext.setSEVMGR_Service (lSEVMGR_Service_ptr);
234 void TRADEMGEN_Service::initTrademgenService() {
244 if (_trademgenServiceContext == NULL) {
245 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
246 "not been initialised");
248 assert (_trademgenServiceContext != NULL);
253 *_trademgenServiceContext;
254 const bool doesOwnStdairService =
255 lTRADEMGEN_ServiceContext.getOwnStdairServiceFlag();
258 stdair::STDAIR_Service& lSTDAIR_Service =
259 lTRADEMGEN_ServiceContext.getSTDAIR_Service();
262 stdair::BomRoot& lPersistentBomRoot =
263 lSTDAIR_Service.getPersistentBomRoot();
266 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
267 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
270 stdair::RandomGeneration& lSharedGenerator =
271 lTRADEMGEN_ServiceContext.getUniformGenerator();
275 lTRADEMGEN_ServiceContext.getPOSProbabilityMass();
280 stdair::BasChronometer lDemandGeneration; lDemandGeneration.start();
282 lSharedGenerator, lDefaultPOSProbabilityMass);
283 const double lGenerationMeasure = lDemandGeneration.elapsed();
297 STDAIR_LOG_DEBUG (
"Demand generation time: " << lGenerationMeasure);
303 if (doesOwnStdairService ==
true) {
313 if (_trademgenServiceContext == NULL) {
314 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
315 "not been initialised");
317 assert (_trademgenServiceContext != NULL);
322 *_trademgenServiceContext;
323 const bool doesOwnStdairService =
324 lTRADEMGEN_ServiceContext.getOwnStdairServiceFlag();
327 stdair::STDAIR_Service& lSTDAIR_Service =
328 lTRADEMGEN_ServiceContext.getSTDAIR_Service();
331 stdair::BomRoot& lPersistentBomRoot =
332 lSTDAIR_Service.getPersistentBomRoot();
338 if (doesOwnStdairService ==
true) {
340 lSTDAIR_Service.buildSampleBom();
354 stdair::RandomGeneration& lSharedGenerator =
355 lTRADEMGEN_ServiceContext.getUniformGenerator();
359 lTRADEMGEN_ServiceContext.getPOSProbabilityMass();
362 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
363 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
367 lDefaultPOSProbabilityMass);
375 if (doesOwnStdairService ==
true) {
385 if (_trademgenServiceContext == NULL) {
386 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
387 "not been initialised");
389 assert (_trademgenServiceContext != NULL);
394 *_trademgenServiceContext;
395 const bool doesOwnStdairService =
396 lTRADEMGEN_ServiceContext.getOwnStdairServiceFlag();
399 stdair::STDAIR_Service& lSTDAIR_Service =
400 lTRADEMGEN_ServiceContext.getSTDAIR_Service();
406 if (doesOwnStdairService ==
true) {
408 lSTDAIR_Service.clonePersistentBom ();
414 stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
428 if (_trademgenServiceContext == NULL) {
429 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
430 "not been initialised");
432 assert (_trademgenServiceContext != NULL);
435 *_trademgenServiceContext;
438 stdair::STDAIR_Service& lSTDAIR_Service =
439 lTRADEMGEN_ServiceContext.getSTDAIR_Service();
442 return lSTDAIR_Service.buildSampleBookingRequest (isForCRS);
450 if (_trademgenServiceContext == NULL) {
451 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
452 "not been initialised");
454 assert (_trademgenServiceContext != NULL);
457 *_trademgenServiceContext;
460 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
461 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
463 return lSEVMGR_Service_ptr->jsonHandler (iJSONString);
471 if (_trademgenServiceContext == NULL) {
472 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
473 "not been initialised");
475 assert (_trademgenServiceContext != NULL);
478 *_trademgenServiceContext;
481 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
482 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
493 if (_trademgenServiceContext == NULL) {
494 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
495 "not been initialised");
497 assert (_trademgenServiceContext != NULL);
500 *_trademgenServiceContext;
503 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
504 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
507 return lSEVMGR_Service_ptr->list ();
513 list(
const stdair::EventType::EN_EventType& iEventType)
const {
516 if (_trademgenServiceContext == NULL) {
517 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
518 "not been initialised");
520 assert (_trademgenServiceContext != NULL);
523 *_trademgenServiceContext;
526 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
527 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
530 return lSEVMGR_Service_ptr->list (iEventType);
538 if (_trademgenServiceContext == NULL) {
539 throw stdair::NonInitialisedServiceException (
"The TraDemGen service has "
540 "not been initialised");
542 assert (_trademgenServiceContext != NULL);
547 boost::posix_time::ptime lNowDateTime =
548 boost::posix_time::second_clock::local_time();
552 STDAIR_LOG_DEBUG (std::endl
553 <<
"==================================================="
558 stdair::BasChronometer lAirListChronometer;
559 lAirListChronometer.start();
562 stdair::DBSession_T& lDBSession =
563 stdair::DBSessionManager::instance().getDBSession();
566 stdair::AirlineStruct lAirline;
567 stdair::DBRequestStatement_T lSelectStatement (lDBSession);
568 stdair::DBManagerForAirlines::prepareSelectStatement (lDBSession,
573 bool hasStillData =
true;
574 unsigned int idx = 0;
575 while (hasStillData ==
true) {
577 stdair::DBManagerForAirlines::iterateOnStatement (lSelectStatement,
581 STDAIR_LOG_DEBUG (
"[" << idx <<
"]: " << lAirline);
587 const double lAirListMeasure = lAirListChronometer.elapsed();
590 STDAIR_LOG_DEBUG (
"Sample service for airline list retrieval: "
599 assert (_trademgenServiceContext != NULL);
601 *_trademgenServiceContext;
604 SEVMGR::SEVMGR_Service& lSEVMGR_Service =
605 lTRADEMGEN_ServiceContext.getSEVMGR_Service();
608 const stdair::Count_T& oExpectedTotalNumberOfRequestsToBeGenerated =
609 lSEVMGR_Service.getExpectedTotalNumberOfEventsToBeGenerated (stdair::EventType::BKG_REQ);
612 return oExpectedTotalNumberOfRequestsToBeGenerated;
620 assert (_trademgenServiceContext != NULL);
622 *_trademgenServiceContext;
625 SEVMGR::SEVMGR_Service& lSEVMGR_Service =
626 lTRADEMGEN_ServiceContext.getSEVMGR_Service();
629 const stdair::Count_T& oActualTotalNumberOfRequestsToBeGenerated =
630 lSEVMGR_Service.getActualTotalNumberOfEventsToBeGenerated (stdair::EventType::BKG_REQ);
633 return oActualTotalNumberOfRequestsToBeGenerated;
639 stdair::ProgressStatusSet& ioPSS,
640 const stdair::DemandGenerationMethod& iDemandGenerationMethod)
const {
643 assert (_trademgenServiceContext != NULL);
645 *_trademgenServiceContext;
648 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
649 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
652 const bool oStillHavingRequestsToBeGenerated =
655 iDemandGenerationMethod);
658 return oStillHavingRequestsToBeGenerated;
666 assert (_trademgenServiceContext != NULL);
668 *_trademgenServiceContext;
671 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
672 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
675 stdair::RandomGeneration& lGenerator =
676 lTRADEMGEN_ServiceContext.getUniformGenerator();
679 const stdair::Count_T& oActualTotalNbOfEvents =
681 iDemandGenerationMethod);
684 return oActualTotalNbOfEvents;
690 const stdair::DemandGenerationMethod& iDemandGenerationMethod)
const {
693 assert (_trademgenServiceContext != NULL);
695 *_trademgenServiceContext;
698 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
699 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
702 stdair::RandomGeneration& lGenerator =
703 lTRADEMGEN_ServiceContext.getUniformGenerator();
708 iDemandGenerationMethod);
713 popEvent (stdair::EventStruct& ioEventStruct)
const {
716 assert (_trademgenServiceContext != NULL);
718 *_trademgenServiceContext;
721 SEVMGR::SEVMGR_Service& lSEVMGR_Service =
722 lTRADEMGEN_ServiceContext.getSEVMGR_Service();
725 return lSEVMGR_Service.popEvent (ioEventStruct);
732 assert (_trademgenServiceContext != NULL);
734 *_trademgenServiceContext;
737 SEVMGR::SEVMGR_Service& lSEVMGR_Service =
738 lTRADEMGEN_ServiceContext.getSEVMGR_Service();
741 const bool isQueueDone = lSEVMGR_Service.isQueueDone();
750 const stdair::PartySize_T& iPartySize,
751 const stdair::DateTime_T& iRequestTime,
752 const stdair::Date_T& iDepartureDate)
const {
755 assert (_trademgenServiceContext != NULL);
757 *_trademgenServiceContext;
760 stdair::RandomGeneration& lGenerator =
761 lTRADEMGEN_ServiceContext.getUniformGenerator();
764 stdair::EventStruct lEventStruct;
765 stdair::EventStruct& lRefEventStruct = lEventStruct;
768 const bool hasCancellationBeenGenerated =
770 iPartySize, iRequestTime,
771 iDepartureDate, lRefEventStruct);
774 if (hasCancellationBeenGenerated ==
false) {
775 return hasCancellationBeenGenerated;
777 assert (hasCancellationBeenGenerated ==
true);
780 SEVMGR::SEVMGR_Service& lSEVMGR_Service =
781 lTRADEMGEN_ServiceContext.getSEVMGR_Service();
791 lSEVMGR_Service.addEvent (lRefEventStruct);
794 const bool hasProgressStatus =
795 lSEVMGR_Service.hasProgressStatus(stdair::EventType::CX);
796 if (hasProgressStatus ==
false) {
797 const stdair::Count_T lCancellationNumber = 1;
798 lSEVMGR_Service.addStatus (stdair::EventType::CX, lCancellationNumber);
800 stdair::Count_T lCurrentCancellationNumber =
801 lSEVMGR_Service.getActualTotalNumberOfEventsToBeGenerated (stdair::EventType::CX);
802 lCurrentCancellationNumber++;
803 lSEVMGR_Service.updateStatus (stdair::EventType::CX, lCurrentCancellationNumber);
806 return hasCancellationBeenGenerated;
814 assert (_trademgenServiceContext != NULL);
816 *_trademgenServiceContext;
819 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
820 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
823 stdair::RandomGeneration& lSharedGenerator =
824 lTRADEMGEN_ServiceContext.getUniformGenerator();
828 lSharedGenerator.getBaseGenerator());
835 assert (_trademgenServiceContext != NULL);
837 *_trademgenServiceContext;
840 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
841 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
844 return lSEVMGR_Service_ptr->getStatus();
853 assert (_trademgenServiceContext != NULL);
855 *_trademgenServiceContext;
858 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
859 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
862 return lSEVMGR_Service_ptr->getStatus(iEventType);
871 assert (_trademgenServiceContext != NULL);
873 *_trademgenServiceContext;
876 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
877 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
880 return lSEVMGR_Service_ptr->hasEventGenerator<
DemandStream,
881 stdair::DemandStreamKeyStr_T>(iDemandStreamKey);
888 assert (_trademgenServiceContext != NULL);
890 *_trademgenServiceContext;
893 SEVMGR::SEVMGR_ServicePtr_T lSEVMGR_Service_ptr =
894 lTRADEMGEN_ServiceContext.getSEVMGR_ServicePtr();
898 lSEVMGR_Service_ptr->getEventGeneratorList<
DemandStream>();
901 std::ostringstream oStream;
903 for (DemandStreamList_T::const_iterator itDemandStream =
904 lDemandStreamList.begin(); itDemandStream !=
905 lDemandStreamList.end(); itDemandStream++) {
907 assert (lDemandStream_ptr != NULL);
908 oStream << lDemandStream_ptr->
describeKey() << std::endl;
911 return oStream.str();
927 template<
class EventGenerator>
928 void SEVMGR_Service::addEventGenerator (EventGenerator& iEventGenerator)
const {
930 const stdair::STDAIR_Service& lSTDAIR_Service =
931 this->getSTDAIR_Service();
934 stdair::BomRoot& lPersistentBomRoot =
935 lSTDAIR_Service.getPersistentBomRoot();
938 stdair::FacBomManager::linkWithParent (lPersistentBomRoot, iEventGenerator);
941 stdair::FacBomManager::addToListAndMap (lPersistentBomRoot,
947 template<
class EventGenerator,
class Key>
948 EventGenerator& SEVMGR_Service::getEventGenerator(
const Key& iKey)
const {
951 const stdair::STDAIR_Service& lSTDAIR_Service =
952 this->getSTDAIR_Service();
955 stdair::BomRoot& lPersistentBomRoot =
956 lSTDAIR_Service.getPersistentBomRoot();
959 EventGenerator& lEventGenerator =
960 stdair::BomManager::getObject<EventGenerator> (lPersistentBomRoot,
963 return lEventGenerator;
968 template<
class EventGenerator,
class Key>
969 bool SEVMGR_Service::hasEventGenerator(
const Key& iKey)
const {
971 bool hasEventGenerator =
true;
974 const stdair::STDAIR_Service& lSTDAIR_Service =
975 this->getSTDAIR_Service();
978 stdair::BomRoot& lPersistentBomRoot =
979 lSTDAIR_Service.getPersistentBomRoot();
982 EventGenerator* lEventGenerator_ptr =
983 stdair::BomManager::getObjectPtr<EventGenerator> (lPersistentBomRoot,
985 if (lEventGenerator_ptr == NULL) {
986 hasEventGenerator =
false;
989 return hasEventGenerator;
994 template<
class EventGenerator>
995 const std::list<EventGenerator*> SEVMGR_Service::getEventGeneratorList()
const {
998 const stdair::STDAIR_Service& lSTDAIR_Service =
999 this->getSTDAIR_Service();
1002 stdair::BomRoot& lPersistentBomRoot =
1003 lSTDAIR_Service.getPersistentBomRoot();
1006 const std::list<EventGenerator*> lEventGeneratorList =
1007 stdair::BomManager::getList<EventGenerator> (lPersistentBomRoot);
1009 return lEventGeneratorList;
1013 template<
class EventGenerator>
1014 bool SEVMGR_Service::hasEventGeneratorList()
const {
1017 const stdair::STDAIR_Service& lSTDAIR_Service =
1018 this->getSTDAIR_Service();
1021 stdair::BomRoot& lPersistentBomRoot =
1022 lSTDAIR_Service.getPersistentBomRoot();
1024 const bool hasListEventGenerator =
1025 stdair::BomManager::hasList<EventGenerator> (lPersistentBomRoot);
1027 return hasListEventGenerator;
1035 template void SEVMGR_Service::
1039 getEventGenerator<TRADEMGEN::DemandStream, stdair::DemandStreamKeyStr_T> (
const stdair::DemandStreamKeyStr_T&)
const;
1041 template bool SEVMGR_Service::
1042 hasEventGenerator<TRADEMGEN::DemandStream, stdair::DemandStreamKeyStr_T> (
const stdair::DemandStreamKeyStr_T&)
const;
1045 getEventGeneratorList<TRADEMGEN::DemandStream> ()
const;
1047 template bool SEVMGR_Service::hasEventGeneratorList<TRADEMGEN::DemandStream>()
const;