2 #include <boost/python.hpp>
10 #include <boost/accumulators/accumulators.hpp>
11 #include <boost/accumulators/statistics.hpp>
13 #include <stdair/stdair_basic_types.hpp>
14 #include <stdair/basic/ProgressStatusSet.hpp>
15 #include <stdair/basic/DemandGenerationMethod.hpp>
16 #include <stdair/bom/EventStruct.hpp>
17 #include <stdair/bom/BookingRequestStruct.hpp>
19 #include <stdair/service/Logger.hpp>
25 namespace ba = boost::accumulators;
33 typedef ba::accumulator_set<double,
34 ba::stats<ba::tag::min, ba::tag::max,
35 ba::tag::mean (ba::immediate),
47 std::ios::fmtflags oldFlags = oStream.flags();
50 oStream.setf (std::ios::fixed);
53 oStream <<
"Statistics for the demand generation runs: " << std::endl;
54 oStream <<
" minimum = " << ba::min (iStatAcc) << std::endl;
55 oStream <<
" mean = " << ba::mean (iStatAcc) << std::endl;
56 oStream <<
" maximum = " << ba::max (iStatAcc) << std::endl;
57 oStream <<
" count = " << ba::count (iStatAcc) << std::endl;
58 oStream <<
" variance = " << ba::variance (iStatAcc) << std::endl;
61 oStream.flags (oldFlags);
74 const std::string& iDemandGenerationMethodString) {
75 std::ostringstream oStream;
78 const stdair::DemandGenerationMethod
79 iDemandGenerationMethod (iDemandGenerationMethodString);
82 if (_logOutputStream == NULL) {
83 oStream <<
"The log filepath is not valid." << std::endl;
86 assert (_logOutputStream != NULL);
91 *_logOutputStream <<
"Demand generation for " << iNbOfRuns <<
" runs, "
92 <<
"with the following method: "
93 << iDemandGenerationMethod << std::endl;
95 if (_trademgenService == NULL) {
96 oStream <<
"The TraDemGen service has not been initialised, "
97 <<
"i.e., the init() method has not been called "
98 <<
"correctly on the Trademgener object. Please "
99 <<
"check that all the parameters are not empty and "
100 <<
"point to actual files.";
101 *_logOutputStream << oStream.str();
102 return oStream.str();
104 assert (_trademgenService != NULL);
111 const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
115 boost::progress_display
116 lProgressDisplay (lExpectedNbOfEventsToBeGenerated * iNbOfRuns);
118 for (
NbOfRuns_T runIdx = 1; runIdx <= iNbOfRuns; ++runIdx) {
120 *_logOutputStream <<
"Run number: " << runIdx << std::endl;
126 const stdair::Count_T& lActualNbOfEventsToBeGenerated =
130 *_logOutputStream <<
"[" << runIdx <<
"] Expected: "
131 << lExpectedNbOfEventsToBeGenerated <<
", actual: "
132 << lActualNbOfEventsToBeGenerated << std::endl;
141 while (_trademgenService->
isQueueDone() ==
false) {
144 stdair::EventStruct lEventStruct;
145 stdair::ProgressStatusSet lProgressStatusSet =
146 _trademgenService->
popEvent (lEventStruct);
153 const stdair::BookingRequestStruct& lPoppedRequest =
154 lEventStruct.getBookingRequest();
157 *_logOutputStream <<
"[" << runIdx <<
"] Poped booking request: '"
158 << lPoppedRequest.describe() <<
"'." << std::endl;
164 const stdair::DemandGeneratorKey_T& lDemandStreamKey =
165 lPoppedRequest.getDemandGeneratorKey();
169 const bool stillHavingRequestsToBeGenerated = _trademgenService->
170 stillHavingRequestsToBeGenerated (lDemandStreamKey,
172 iDemandGenerationMethod);
175 *_logOutputStream << lProgressStatusSet.describe() << std::endl;
176 *_logOutputStream <<
"=> [" << lDemandStreamKey
177 <<
"] is now processed. Still generate events "
178 <<
"for that demand stream? "
179 << stillHavingRequestsToBeGenerated << std::endl;
183 if (stillHavingRequestsToBeGenerated ==
true) {
185 stdair::BookingRequestPtr_T lNextRequest_ptr =
187 iDemandGenerationMethod);
189 assert (lNextRequest_ptr != NULL);
192 const stdair::Duration_T lDuration =
193 lNextRequest_ptr->getRequestDateTime()
194 - lPoppedRequest.getRequestDateTime();
195 if (lDuration.total_milliseconds() < 0) {
196 *_logOutputStream <<
"[" << lDemandStreamKey
197 <<
"] The date-time of the generated event ("
198 << lNextRequest_ptr->getRequestDateTime()
199 <<
") is lower than the date-time "
200 <<
"of the current event ("
201 << lPoppedRequest.getRequestDateTime()
207 *_logOutputStream <<
"[" << lDemandStreamKey
208 <<
"] Added request: '"
209 << lNextRequest_ptr->describe()
210 <<
"'. Is queue done? "
215 *_logOutputStream << std::endl;
222 lStatAccumulator (lActualNbOfEventsToBeGenerated);
225 _trademgenService->
reset();
229 *_logOutputStream <<
"End of the demand generation. Following are some "
230 <<
"statistics for the " << iNbOfRuns <<
" runs."
232 std::ostringstream oStatStr;
234 *_logOutputStream << oStatStr.str() << std::endl;
237 const std::string& lBOMStr = _trademgenService->
csvDisplay();
238 *_logOutputStream << lBOMStr << std::endl;
241 *_logOutputStream <<
"TraDemGen output: "
242 << oStream.str() << std::endl;
244 }
catch (
const stdair::RootException& eTrademgenError) {
245 oStream <<
"TraDemGen error: " << eTrademgenError.what() << std::endl;
247 }
catch (
const std::exception& eStdError) {
248 oStream <<
"Error: " << eStdError.what() << std::endl;
251 oStream <<
"Unknown error" << std::endl;
255 oStream <<
"TraDemGen has completed the generation of the booking "
256 <<
"requests. See the log file for more details." << std::endl;
258 return oStream.str();
263 Trademgener() : _trademgenService (NULL), _logOutputStream (NULL) {
268 : _trademgenService (iTrademgener._trademgenService),
269 _logOutputStream (iTrademgener._logOutputStream) {
274 _trademgenService = NULL;
275 _logOutputStream = NULL;
281 bool init (
const std::string& iLogFilepath,
282 const stdair::RandomSeed_T& iRandomSeed,
const bool isBuiltin,
283 const stdair::Filename_T& iDemandInputFilename) {
284 bool isEverythingOK =
true;
289 const bool isWriteable = (iLogFilepath.empty() ==
false);
291 if (isWriteable ==
false) {
292 isEverythingOK =
false;
293 return isEverythingOK;
297 _logOutputStream =
new std::ofstream;
298 assert (_logOutputStream != NULL);
301 _logOutputStream->open (iLogFilepath.c_str());
302 _logOutputStream->clear();
305 *_logOutputStream <<
"Python wrapper initialisation" << std::endl;
306 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG,
311 assert (_trademgenService != NULL);
314 if (isBuiltin ==
true) {
327 *_logOutputStream <<
"Python wrapper initialised" << std::endl;
329 }
catch (
const stdair::RootException& eTrademgenError) {
330 *_logOutputStream <<
"Trademgen error: " << eTrademgenError.what()
333 }
catch (
const std::exception& eStdError) {
334 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
337 *_logOutputStream <<
"Unknown error" << std::endl;
340 return isEverythingOK;
346 std::ofstream* _logOutputStream;
353 boost::python::class_<TRADEMGEN::Trademgener> (
"Trademgener")