5 #include <boost/make_shared.hpp>
13 #include <stdair/stdair_demand_types.hpp>
14 #include <stdair/basic/ProgressStatusSet.hpp>
15 #include <stdair/basic/BasChronometer.hpp>
16 #include <stdair/bom/BookingRequestStruct.hpp>
17 #include <stdair/bom/SnapshotStruct.hpp>
18 #include <stdair/bom/CancellationStruct.hpp>
19 #include <stdair/bom/RMEventStruct.hpp>
20 #include <stdair/bom/BreakPointStruct.hpp>
21 #include <stdair/bom/EventStruct.hpp>
22 #include <stdair/bom/TravelSolutionStruct.hpp>
23 #include <stdair/service/Logger.hpp>
24 #include <stdair/STDAIR_Service.hpp>
26 #include <simcrs/SIMCRS_Service.hpp>
28 #include <sevmgr/SEVMGR_Service.hpp>
30 #include <trademgen/TRADEMGEN_Service.hpp>
32 #include <travelccm/TRAVELCCM_Service.hpp>
34 #include <airsched/AIRSCHED_Service.hpp>
45 simulate (SIMCRS::SIMCRS_Service& ioSIMCRS_Service,
46 TRADEMGEN::TRADEMGEN_Service& ioTRADEMGEN_Service,
47 TRAVELCCM::TRAVELCCM_Service& ioTRAVELCCM_Service,
48 stdair::STDAIR_Service& ioSTDAIR_Service,
49 SimulationStatus& ioSimulationStatus,
50 const stdair::DemandGenerationMethod& iDemandGenerationMethod) {
61 const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
62 ioTRADEMGEN_Service.getExpectedTotalNumberOfRequestsToBeGenerated();
63 const stdair::Count_T& lActualNbOfEventsToBeGenerated =
64 ioTRADEMGEN_Service.generateFirstRequests(iDemandGenerationMethod);
67 STDAIR_LOG_DEBUG (
"Expected number of events: "
68 << lExpectedNbOfEventsToBeGenerated <<
", actual: "
69 << lActualNbOfEventsToBeGenerated);
86 while (ioTRADEMGEN_Service.isQueueDone() ==
false) {
89 stdair::EventStruct lEventStruct;
90 stdair::ProgressStatusSet lPSS =
91 ioTRADEMGEN_Service.popEvent (lEventStruct);
94 const stdair::DateTime_T& lCurrentEventDateTime =
95 lEventStruct.getEventTime ();
96 const stdair::Date_T& lCurrentEventDate =
97 lCurrentEventDateTime.date();
98 ioSimulationStatus.setCurrentDate(lCurrentEventDate);
101 STDAIR_LOG_DEBUG (
"Poped event: '" << lEventStruct.describe() <<
"'.");
104 const stdair::EventType::EN_EventType& lEventType =
105 lEventStruct.getEventType();
107 stdair::BasChronometer lNextEventChronometer;
108 lNextEventChronometer.start();
110 switch (lEventType) {
112 case stdair::EventType::BKG_REQ:
113 playBookingRequest (ioSIMCRS_Service,
119 iDemandGenerationMethod);
122 case stdair::EventType::CX:
123 playCancellation (ioSIMCRS_Service,
127 case stdair::EventType::SNAPSHOT:
128 playSnapshotEvent (ioSIMCRS_Service,
132 case stdair::EventType::RM:
133 playRMEvent (ioSIMCRS_Service,
137 case stdair::EventType::BRK_PT:
140 updateStatus (ioTRADEMGEN_Service, lEventType, ioSimulationStatus);
144 case stdair::EventType::OPT_NOT_4_FD:
145 case stdair::EventType::OPT_NOT_4_NET:
146 case stdair::EventType::SKD_CHG:
148 default: assert (
false);
153 const double lNextEventMeasure = lNextEventChronometer.elapsed();
154 updateStatus (ioTRADEMGEN_Service, lEventType,
155 ioSimulationStatus, lNextEventMeasure);
168 updateStatus (
const TRADEMGEN::TRADEMGEN_Service& ioTRADEMGEN_Service,
169 const stdair::EventType::EN_EventType& iEN_EventType,
170 SimulationStatus& ioSimulationStatus,
171 const double& iEventMeasure) {
173 const stdair::ProgressStatus& lProgressStatus =
174 ioTRADEMGEN_Service.getProgressStatus ();
175 ioSimulationStatus.setCurrentProgressStatus (lProgressStatus);
178 const stdair::ProgressStatus& lProgressStatusByType =
179 ioTRADEMGEN_Service.getProgressStatus (iEN_EventType);
182 ioSimulationStatus.updateProgress(iEN_EventType,
183 lProgressStatusByType,
190 playBookingRequest (SIMCRS::SIMCRS_Service& ioSIMCRS_Service,
191 TRADEMGEN::TRADEMGEN_Service& ioTRADEMGEN_Service,
192 TRAVELCCM::TRAVELCCM_Service& ioTRAVELCCM_Service,
193 const stdair::EventStruct& iEventStruct,
194 stdair::ProgressStatusSet& ioPSS,
195 SimulationStatus& ioSimulationStatus,
196 const stdair::DemandGenerationMethod& iDemandGenerationMethod) {
199 const stdair::BookingRequestStruct& lPoppedRequest =
200 iEventStruct.getBookingRequest();
203 const stdair::Date_T& lDepDate = lPoppedRequest.getPreferedDepartureDate();
204 const stdair::Duration_T& lDepTime = lPoppedRequest.getPreferredDepartureTime();
205 const stdair::DateTime_T& lReqDateTime = lPoppedRequest.getRequestDateTime();
206 const stdair::Date_T& lReqDate = lReqDateTime.date();
207 const stdair::Duration_T& lReqTime = lReqDateTime.time_of_day();
208 const bool isRequestDateValid =
209 ((lDepDate > lReqDate) || (lDepDate == lReqDate && lDepTime > lReqTime));
210 assert (isRequestDateValid ==
true);
213 STDAIR_LOG_DEBUG (
"Poped booking request: '" << lPoppedRequest.describe()
217 const stdair::DemandGeneratorKey_T& lDemandStreamKey =
218 lPoppedRequest.getDemandGeneratorKey();
221 const bool stillHavingRequestsToBeGenerated =
222 ioTRADEMGEN_Service.stillHavingRequestsToBeGenerated (lDemandStreamKey,
224 iDemandGenerationMethod);
230 STDAIR_LOG_DEBUG (
"Progress status" << ioPSS.describe());
234 if (stillHavingRequestsToBeGenerated) {
235 stdair::BookingRequestPtr_T lNextRequest_ptr =
236 ioTRADEMGEN_Service.generateNextRequest (lDemandStreamKey,
237 iDemandGenerationMethod);
238 assert (lNextRequest_ptr != NULL);
241 const stdair::Duration_T lDuration =
242 lNextRequest_ptr->getRequestDateTime()
243 - lPoppedRequest.getRequestDateTime();
244 if (lDuration.total_milliseconds() < 0) {
245 STDAIR_LOG_NOTIFICATION (
"[" << lDemandStreamKey
246 <<
"] The date-time of the generated event ("
247 << lNextRequest_ptr->getRequestDateTime()
248 <<
") is lower than the date-time "
249 <<
"of the current event ("
250 << lPoppedRequest.getRequestDateTime()
264 stdair::TravelSolutionList_T lTravelSolutionList =
265 ioSIMCRS_Service.calculateSegmentPathList (lPoppedRequest);
267 if (lTravelSolutionList.empty() ==
false) {
270 ioSIMCRS_Service.fareQuote (lPoppedRequest, lTravelSolutionList);
273 ioSIMCRS_Service.calculateAvailability (lTravelSolutionList);
276 const stdair::TravelSolutionStruct* lChosenTS_ptr =
277 ioTRAVELCCM_Service.chooseTravelSolution (lTravelSolutionList,
279 if (lChosenTS_ptr != NULL) {
281 STDAIR_LOG_DEBUG (
"Chosen TS: " << lChosenTS_ptr->describe());
284 const stdair::NbOfSeats_T& lPartySizeDouble =
285 lPoppedRequest.getPartySize();
286 const stdair::PartySize_T lPartySize = std::floor (lPartySizeDouble);
289 bool saleSucceedful =
290 ioSIMCRS_Service.sell (*lChosenTS_ptr, lPartySize);
293 if (saleSucceedful ==
true) {
294 ioSimulationStatus.increaseGlobalNumberOfBookings(lPartySize);
295 ioTRADEMGEN_Service.generateCancellation (*lChosenTS_ptr,
296 lPartySize, lReqDateTime,
301 const stdair::DateTime_T lDepartureDateTime =
302 boost::posix_time::ptime (lDepDate, boost::posix_time::hours (0));
303 const stdair::Duration_T lDTDRequest = lReqDateTime - lDepartureDateTime;
304 const double lDTD = double(lDTDRequest.total_seconds()) / 86400.0;
305 std::ostringstream oStr;
306 const stdair::SegmentPath_T& lSegmentPath =
307 lChosenTS_ptr->getSegmentPath();
308 for (stdair::SegmentPath_T::const_iterator itSegPath =
309 lSegmentPath.begin();
310 itSegPath != lSegmentPath.end(); ++itSegPath) {
311 oStr << *itSegPath <<
";";
313 STDAIR_LOG_NOTIFICATION (oStr.str() << std::setprecision(10) << lDTD);
317 STDAIR_LOG_DEBUG (
"There is no chosen travel solution "
318 <<
"for this request: "<< lPoppedRequest.describe());
322 STDAIR_LOG_DEBUG (
"No travel solution has been found for: "
329 playCancellation (SIMCRS::SIMCRS_Service& ioSIMCRS_Service,
330 const stdair::EventStruct& iEventStruct) {
332 const stdair::CancellationStruct lCancellationStruct =
333 iEventStruct.getCancellation();
337 ioSIMCRS_Service.playCancellation (lCancellationStruct);
343 playSnapshotEvent (SIMCRS::SIMCRS_Service& ioSIMCRS_Service,
344 const stdair::EventStruct& iEventStruct) {
346 const stdair::SnapshotStruct lSnapshotStruct =
347 iEventStruct.getSnapshotStruct();
352 ioSIMCRS_Service.takeSnapshots (lSnapshotStruct);
357 playRMEvent (SIMCRS::SIMCRS_Service& ioSIMCRS_Service,
358 const stdair::EventStruct& iEventStruct) {
360 const stdair::RMEventStruct lRMEvent = iEventStruct.getRMEvent();
363 STDAIR_LOG_DEBUG (
"Running RM system: " << lRMEvent.describe());
365 ioSIMCRS_Service.optimise (lRMEvent);
369 const stdair::Count_T Simulator::
370 initialiseBreakPoint (
const TRADEMGEN::TRADEMGEN_Service& ioTRADEMGEN_Service,
371 SEVMGR::SEVMGR_Service& ioSEVMGR_Service,
372 const stdair::BreakPointList_T& iBreakPointList,
373 SimulationStatus& ioSimulationStatus) {
376 stdair::Count_T lBreakPointNumber = 0;
379 const stdair::Date_T& lStartDate = ioSimulationStatus.getStartDate();
380 const stdair::Date_T& lEndDate = ioSimulationStatus.getEndDate();
383 for (stdair::BreakPointList_T::const_iterator itBPEvent =
384 iBreakPointList.begin();
385 itBPEvent != iBreakPointList.end(); ++itBPEvent) {
386 const stdair::BreakPointStruct& lBPEvent = *itBPEvent;
387 const stdair::DateTime_T& lDateTimeBP =
388 lBPEvent.getBreakPointTime();
389 const stdair::Date_T lDateBP = lDateTimeBP.date();
392 if (lDateBP < lEndDate && lDateBP >= lStartDate) {
393 stdair::BreakPointPtr_T lBPEventPtr =
394 boost::make_shared<stdair::BreakPointStruct> (lBPEvent);
396 stdair::EventStruct lEventStruct (stdair::EventType::BRK_PT,
398 ioSEVMGR_Service.addEvent (lEventStruct);
399 lBreakPointNumber ++;
403 if (lBreakPointNumber > 0) {
405 const bool hasProgressStatus =
406 ioSEVMGR_Service.hasProgressStatus(stdair::EventType::BRK_PT);
407 if (hasProgressStatus ==
false) {
408 ioSEVMGR_Service.addStatus (stdair::EventType::BRK_PT,
411 stdair::Count_T lCurrentBPNumber = ioSEVMGR_Service.
412 getActualTotalNumberOfEventsToBeGenerated (stdair::EventType::BRK_PT);
413 lCurrentBPNumber += lBreakPointNumber;
414 ioSEVMGR_Service.updateStatus (stdair::EventType::BRK_PT,
418 updateStatus(ioTRADEMGEN_Service, stdair::EventType::BRK_PT,
421 return lBreakPointNumber;