TraDemGen Logo  0.2.2
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
DemandGenerationTestSuite.cpp
Go to the documentation of this file.
1 
5 // //////////////////////////////////////////////////////////////////////
6 // Import section
7 // //////////////////////////////////////////////////////////////////////
8 // STL
9 #include <sstream>
10 #include <fstream>
11 #include <map>
12 #include <cmath>
13 // Boost Unit Test Framework (UTF)
14 #define BOOST_TEST_DYN_LINK
15 #define BOOST_TEST_MAIN
16 #define BOOST_TEST_MODULE DemandGenerationTest
17 #include <boost/test/unit_test.hpp>
18 // StdAir
19 #include <stdair/stdair_basic_types.hpp>
20 #include <stdair/basic/BasConst_General.hpp>
21 #include <stdair/basic/BasLogParams.hpp>
22 #include <stdair/basic/BasDBParams.hpp>
23 #include <stdair/basic/BasFileMgr.hpp>
24 #include <stdair/basic/ProgressStatusSet.hpp>
25 #include <stdair/bom/EventStruct.hpp>
26 #include <stdair/bom/EventQueue.hpp>
27 #include <stdair/bom/BookingRequestStruct.hpp>
28 #include <stdair/service/Logger.hpp>
29 // TraDemGen
33 
34 namespace boost_utf = boost::unit_test;
35 
36 // (Boost) Unit Test XML Report
37 std::ofstream utfReportStream ("DemandGenerationTestSuite_utfresults.xml");
38 
42 struct UnitTestConfig {
44  UnitTestConfig() {
45  boost_utf::unit_test_log.set_stream (utfReportStream);
46  boost_utf::unit_test_log.set_format (boost_utf::XML);
47  boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
48  //boost_utf::unit_test_log.set_threshold_level (boost_utf::log_successful_tests);
49  }
50 
52  ~UnitTestConfig() {
53  }
54 };
55 
56 // Specific type definitions
57 typedef std::pair<stdair::Count_T, stdair::Count_T> NbOfEventsPair_T;
58 typedef std::map<const stdair::DemandStreamKeyStr_T,
59  NbOfEventsPair_T> NbOfEventsByDemandStreamMap_T;
60 
61 
62 // /////////////// Main: Unit Test Suite //////////////
63 
64 // Set the UTF configuration (re-direct the output to a specific file)
65 BOOST_GLOBAL_FIXTURE (UnitTestConfig);
66 
67 // Start the test suite
68 BOOST_AUTO_TEST_SUITE (master_test_suite)
69 
70 
73 BOOST_AUTO_TEST_CASE (trademgen_simple_simulation_test) {
74 
75  // Seed for the random generation
76  const stdair::RandomSeed_T lRandomSeed = stdair::DEFAULT_RANDOM_SEED;
77 
78 // Input file name
79  const stdair::Filename_T lInputFilename (STDAIR_SAMPLE_DIR "/demand01.csv");
80 
81  // Check that the file path given as input corresponds to an actual file
82  const bool doesExistAndIsReadable =
83  stdair::BasFileMgr::doesExistAndIsReadable (lInputFilename);
84  BOOST_CHECK_MESSAGE (doesExistAndIsReadable == true,
85  "The '" << lInputFilename
86  << "' input file can not be open and read");
87 
88  // Output log File
89  const stdair::Filename_T lLogFilename ("DemandGenerationTestSuite.log");
90 
91  // Set the log parameters
92  std::ofstream logOutputFile;
93  // open and clean the log outputfile
94  logOutputFile.open (lLogFilename.c_str());
95  logOutputFile.clear();
96 
97  // Initialise the TraDemGen service object
98  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
99  TRADEMGEN::TRADEMGEN_Service trademgenService (lLogParams, lRandomSeed);
100 
101  // Create the DemandStream objects, and insert them within the BOM tree
102  BOOST_CHECK_NO_THROW (trademgenService.parseAndLoad (lInputFilename));
103 
113  NbOfEventsByDemandStreamMap_T lNbOfEventsMap;
114  lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
115  value_type ("SIN-HND 2010-Feb-08 Y",
116  NbOfEventsPair_T (1, 10)));
117  lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
118  value_type ("SIN-HND 2010-Feb-09 Y",
119  NbOfEventsPair_T (1, 10)));
120  lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
121  value_type ("SIN-BKK 2010-Feb-08 Y",
122  NbOfEventsPair_T (1, 10)));
123  lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
124  value_type ("SIN-BKK 2010-Feb-09 Y",
125  NbOfEventsPair_T (1, 10)));
126  // Total number of events, for all the demand streams: 3
127  stdair::Count_T lRefExpectedNbOfEvents (40);
128 
129  // Retrieve the expected (mean value of the) number of events to be
130  // generated
131  const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
132  trademgenService.getExpectedTotalNumberOfRequestsToBeGenerated();
133 
134  BOOST_CHECK_EQUAL (lRefExpectedNbOfEvents,
135  std::floor (lExpectedNbOfEventsToBeGenerated));
136 
137  BOOST_CHECK_MESSAGE (lRefExpectedNbOfEvents ==
138  std::floor (lExpectedNbOfEventsToBeGenerated),
139  "Expected total number of requests to be generated: "
140  << lExpectedNbOfEventsToBeGenerated
141  << " (=> "
142  << std::floor (lExpectedNbOfEventsToBeGenerated)
143  << "). Reference value: " << lRefExpectedNbOfEvents);
144 
145  // Generate the date time of the requests with the statistic order method.
146  stdair::DemandGenerationMethod lDemandGenerationMethod (stdair::DemandGenerationMethod::STA_ORD);
147 
157  const stdair::Count_T& lActualNbOfEventsToBeGenerated =
158  trademgenService.generateFirstRequests(lDemandGenerationMethod);
159 
160  // DEBUG
161  STDAIR_LOG_DEBUG ("Expected number of events: "
162  << lExpectedNbOfEventsToBeGenerated << ", actual: "
163  << lActualNbOfEventsToBeGenerated);
164 
165  // Total number of events, for all the demand streams: 40
166  const stdair::Count_T lRefActualNbOfEvents (40);
167  BOOST_CHECK_EQUAL (lRefActualNbOfEvents, lActualNbOfEventsToBeGenerated);
168 
169  BOOST_CHECK_MESSAGE (lRefActualNbOfEvents == lActualNbOfEventsToBeGenerated,
170  "Actual total number of requests to be generated: "
171  << lExpectedNbOfEventsToBeGenerated
172  << " (=> "
173  << std::floor (lExpectedNbOfEventsToBeGenerated)
174  << "). Reference value: " << lRefActualNbOfEvents);
175 
177  const bool isQueueDone = trademgenService.isQueueDone();
178  BOOST_REQUIRE_MESSAGE (isQueueDone == false,
179  "The event queue should not be empty. You may check "
180  << "the input file: '" << lInputFilename << "'");
181 
189  stdair::Count_T idx = 1;
190  while (trademgenService.isQueueDone() == false) {
191 
192  // Get the next event from the event queue
193  stdair::EventStruct lEventStruct;
194  stdair::ProgressStatusSet lPPS = trademgenService.popEvent (lEventStruct);
195 
196  // DEBUG
197  STDAIR_LOG_DEBUG ("Poped event: '" << lEventStruct.describe() << "'.");
198 
199  // Extract the corresponding demand/booking request
200  const stdair::BookingRequestStruct& lPoppedRequest =
201  lEventStruct.getBookingRequest();
202 
203  // DEBUG
204  STDAIR_LOG_DEBUG ("Poped booking request: '"
205  << lPoppedRequest.describe() << "'.");
206 
207  // Retrieve the corresponding demand stream
208  const stdair::DemandGeneratorKey_T& lDemandStreamKey =
209  lPoppedRequest.getDemandGeneratorKey();
210 
211  // Check that the number of booking requests to be generated are correct
212  const NbOfEventsByDemandStreamMap_T::iterator itNbOfEventsMap =
213  lNbOfEventsMap.find (lDemandStreamKey);
214  BOOST_REQUIRE_MESSAGE (itNbOfEventsMap != lNbOfEventsMap.end(),
215  "The demand stream key '" << lDemandStreamKey
216  << "' is not expected in that test");
217 
227  const NbOfEventsPair_T& lNbOfEventsPair = itNbOfEventsMap->second;
228  stdair::Count_T lCurrentNbOfEvents = lNbOfEventsPair.first;
229  const stdair::Count_T& lExpectedTotalNbOfEvents = lNbOfEventsPair.second;
230 
231  // Assess whether more events should be generated for that demand stream
232  const bool stillHavingRequestsToBeGenerated = trademgenService.
233  stillHavingRequestsToBeGenerated (lDemandStreamKey, lPPS,
234  lDemandGenerationMethod);
235 
242  if (lCurrentNbOfEvents == 1) {
248  const stdair::ProgressStatus& lDemandStreamProgressStatus =
249  lPPS.getSpecificGeneratorStatus();
250  const stdair::Count_T& lNbOfRequests =
251  lDemandStreamProgressStatus.getExpectedNb();
252 
253  BOOST_CHECK_EQUAL (lNbOfRequests, lExpectedTotalNbOfEvents);
254  BOOST_CHECK_MESSAGE (lNbOfRequests == lExpectedTotalNbOfEvents,
255  "[" << lDemandStreamKey
256  << "] Total number of requests to be generated: "
257  << lNbOfRequests << "). Expected value: "
258  << lExpectedTotalNbOfEvents);
259  }
260 
261  // DEBUG
262  STDAIR_LOG_DEBUG ("=> [" << lDemandStreamKey << "][" << lCurrentNbOfEvents
263  << "/" << lExpectedTotalNbOfEvents
264  << "] is now processed. "
265  << "Still generate events for that demand stream? "
266  << stillHavingRequestsToBeGenerated);
267 
268  // If there are still events to be generated for that demand stream,
269  // generate and add them to the event queue
270  if (stillHavingRequestsToBeGenerated == true) {
271  const stdair::BookingRequestPtr_T lNextRequest_ptr =
272  trademgenService.generateNextRequest (lDemandStreamKey,
273  lDemandGenerationMethod);
274  assert (lNextRequest_ptr != NULL);
275 
281  const stdair::Duration_T lDuration =
282  lNextRequest_ptr->getRequestDateTime()
283  - lPoppedRequest.getRequestDateTime();
284  BOOST_REQUIRE_GT (lDuration.total_milliseconds(), 0);
285  BOOST_REQUIRE_MESSAGE (lDuration.total_milliseconds() > 0,
286  "[" << lDemandStreamKey
287  << "] The date-time of the generated event ("
288  << lNextRequest_ptr->getRequestDateTime()
289  << ") is lower than the date-time "
290  << "of the current event ("
291  << lPoppedRequest.getRequestDateTime() << ")");
292 
293  // DEBUG
294  STDAIR_LOG_DEBUG ("[" << lDemandStreamKey << "][" << lCurrentNbOfEvents
295  << "/" << lExpectedTotalNbOfEvents
296  << "] Added request: '" << lNextRequest_ptr->describe()
297  << "'. Is queue done? "
298  << trademgenService.isQueueDone());
299 
300  // Keep, within the dedicated map, the current counters of events updated.
301  ++lCurrentNbOfEvents;
302  itNbOfEventsMap->second = NbOfEventsPair_T (lCurrentNbOfEvents,
303  lExpectedTotalNbOfEvents);
304  }
305 
306  // Iterate
307  ++idx;
308  }
309  // Compensate for the last iteration
310  --idx;
311  //
312  BOOST_CHECK_EQUAL (idx, lRefActualNbOfEvents);
313  BOOST_CHECK_MESSAGE (idx == lRefActualNbOfEvents,
314  "The total actual number of events is "
315  << lRefActualNbOfEvents << ", but " << idx
316  << " events have been generated");
317 
320  trademgenService.reset();
321 
322  // DEBUG
323  STDAIR_LOG_DEBUG ("End of the simulation");
324 
325  // Close the log file
326  logOutputFile.close();
327 }
328 
329 // End the test suite
330 BOOST_AUTO_TEST_SUITE_END()
331 
332