10 #include <boost/python.hpp>
12 #include <boost/accumulators/accumulators.hpp>
13 #include <boost/accumulators/statistics.hpp>
15 #include <stdair/stdair_basic_types.hpp>
16 #include <stdair/basic/BasConst_General.hpp>
17 #include <stdair/basic/ProgressStatusSet.hpp>
18 #include <stdair/basic/DemandGenerationMethod.hpp>
19 #include <stdair/bom/EventStruct.hpp>
20 #include <stdair/bom/BookingRequestStruct.hpp>
21 #include <stdair/bom/BomDisplay.hpp>
22 #include <stdair/service/Logger.hpp>
25 #include <trademgen/config/trademgen-paths.hpp>
28 namespace ba = boost::accumulators;
36 typedef ba::accumulator_set<double,
37 ba::stats<ba::tag::min, ba::tag::max,
38 ba::tag::mean (ba::immediate),
50 std::ios::fmtflags oldFlags = oStream.flags();
53 oStream.setf (std::ios::fixed);
56 oStream <<
"Statistics for the demand generation runs: " << std::endl;
57 oStream <<
" minimum = " << ba::min (iStatAcc) << std::endl;
58 oStream <<
" mean = " << ba::mean (iStatAcc) << std::endl;
59 oStream <<
" maximum = " << ba::max (iStatAcc) << std::endl;
60 oStream <<
" count = " << ba::count (iStatAcc) << std::endl;
61 oStream <<
" variance = " << ba::variance (iStatAcc) << std::endl;
64 oStream.flags (oldFlags);
77 const std::string& iDemandGenerationMethodString) {
78 std::ostringstream oStream;
81 const stdair::DemandGenerationMethod
82 iDemandGenerationMethod (iDemandGenerationMethodString);
85 if (_logOutputStream == NULL) {
86 oStream <<
"The log filepath is not valid." << std::endl;
89 assert (_logOutputStream != NULL);
94 *_logOutputStream <<
"Demand generation for " << iNbOfRuns <<
" runs, "
95 <<
"with the following method: "
96 << iDemandGenerationMethod << std::endl;
98 if (_trademgenService == NULL) {
99 oStream <<
"The TraDemGen service has not been initialised, "
100 <<
"i.e., the init() method has not been called "
101 <<
"correctly on the Trademgener object. Please "
102 <<
"check that all the parameters are not empty and "
103 <<
"point to actual files.";
104 *_logOutputStream << oStream.str();
105 return oStream.str();
107 assert (_trademgenService != NULL);
114 const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
118 boost::progress_display
119 lProgressDisplay (lExpectedNbOfEventsToBeGenerated * iNbOfRuns);
121 for (
NbOfRuns_T runIdx = 1; runIdx <= iNbOfRuns; ++runIdx) {
123 *_logOutputStream <<
"Run number: " << runIdx << std::endl;
129 const stdair::Count_T& lActualNbOfEventsToBeGenerated =
133 *_logOutputStream <<
"[" << runIdx <<
"] Expected: "
134 << lExpectedNbOfEventsToBeGenerated <<
", actual: "
135 << lActualNbOfEventsToBeGenerated << std::endl;
144 while (_trademgenService->
isQueueDone() ==
false) {
147 stdair::EventStruct lEventStruct;
148 stdair::ProgressStatusSet lProgressStatusSet =
149 _trademgenService->
popEvent (lEventStruct);
156 const stdair::BookingRequestStruct& lPoppedRequest =
157 lEventStruct.getBookingRequest();
160 *_logOutputStream <<
"[" << runIdx <<
"] Poped booking request: '"
161 << lPoppedRequest.describe() <<
"'." << std::endl;
167 const stdair::DemandGeneratorKey_T& lDemandStreamKey =
168 lPoppedRequest.getDemandGeneratorKey();
172 const bool stillHavingRequestsToBeGenerated = _trademgenService->
173 stillHavingRequestsToBeGenerated (lDemandStreamKey,
175 iDemandGenerationMethod);
178 *_logOutputStream << lProgressStatusSet.describe() << std::endl;
179 *_logOutputStream <<
"=> [" << lDemandStreamKey
180 <<
"] is now processed. Still generate events "
181 <<
"for that demand stream? "
182 << stillHavingRequestsToBeGenerated << std::endl;
186 if (stillHavingRequestsToBeGenerated ==
true) {
188 stdair::BookingRequestPtr_T lNextRequest_ptr =
190 iDemandGenerationMethod);
192 assert (lNextRequest_ptr != NULL);
195 const stdair::Duration_T lDuration =
196 lNextRequest_ptr->getRequestDateTime()
197 - lPoppedRequest.getRequestDateTime();
198 if (lDuration.total_milliseconds() < 0) {
199 *_logOutputStream <<
"[" << lDemandStreamKey
200 <<
"] The date-time of the generated event ("
201 << lNextRequest_ptr->getRequestDateTime()
202 <<
") is lower than the date-time "
203 <<
"of the current event ("
204 << lPoppedRequest.getRequestDateTime()
210 *_logOutputStream <<
"[" << lDemandStreamKey
211 <<
"] Added request: '"
212 << lNextRequest_ptr->describe()
213 <<
"'. Is queue done? "
218 *_logOutputStream << std::endl;
225 lStatAccumulator (lActualNbOfEventsToBeGenerated);
228 _trademgenService->
reset();
232 *_logOutputStream <<
"End of the demand generation. Following are some "
233 <<
"statistics for the " << iNbOfRuns <<
" runs."
235 std::ostringstream oStatStr;
237 *_logOutputStream << oStatStr.str() << std::endl;
240 const std::string& lBOMStr = _trademgenService->
csvDisplay();
241 *_logOutputStream << lBOMStr << std::endl;
244 *_logOutputStream <<
"TraDemGen output: "
245 << oStream.str() << std::endl;
247 }
catch (
const stdair::RootException& eTrademgenError) {
248 oStream <<
"TraDemGen error: " << eTrademgenError.what() << std::endl;
250 }
catch (
const std::exception& eStdError) {
251 oStream <<
"Error: " << eStdError.what() << std::endl;
254 oStream <<
"Unknown error" << std::endl;
258 oStream <<
"TraDemGen has completed the generation of the booking "
259 <<
"requests. See the log file for more details." << std::endl;
261 return oStream.str();
266 Trademgener() : _trademgenService (NULL), _logOutputStream (NULL) {
271 : _trademgenService (iTrademgener._trademgenService),
272 _logOutputStream (iTrademgener._logOutputStream) {
277 _trademgenService = NULL;
278 _logOutputStream = NULL;
284 bool init (
const std::string& iLogFilepath,
285 const stdair::RandomSeed_T& iRandomSeed,
const bool isBuiltin,
286 const stdair::Filename_T& iDemandInputFilename) {
287 bool isEverythingOK =
true;
292 const bool isWriteable = (iLogFilepath.empty() ==
false);
294 if (isWriteable ==
false) {
295 isEverythingOK =
false;
296 return isEverythingOK;
300 _logOutputStream =
new std::ofstream;
301 assert (_logOutputStream != NULL);
304 _logOutputStream->open (iLogFilepath.c_str());
305 _logOutputStream->clear();
308 *_logOutputStream <<
"Python wrapper initialisation" << std::endl;
309 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG,
314 assert (_trademgenService != NULL);
317 if (isBuiltin ==
true) {
330 *_logOutputStream <<
"Python wrapper initialised" << std::endl;
332 }
catch (
const stdair::RootException& eTrademgenError) {
333 *_logOutputStream <<
"Trademgen error: " << eTrademgenError.what()
336 }
catch (
const std::exception& eStdError) {
337 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
340 *_logOutputStream <<
"Unknown error" << std::endl;
343 return isEverythingOK;
349 std::ofstream* _logOutputStream;
356 boost::python::class_<TRADEMGEN::Trademgener> (
"Trademgener")