AirSched Logo  0.1.4
C++ Simulated Airline Schedule Manager Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AIRSCHED_Service.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // Boost
8 #include <boost/make_shared.hpp>
9 // StdAir
10 #include <stdair/basic/BasChronometer.hpp>
11 #include <stdair/bom/BomManager.hpp>
12 #include <stdair/bom/BookingRequestStruct.hpp>
13 #include <stdair/bom/TravelSolutionStruct.hpp>
14 #include <stdair/service/Logger.hpp>
15 #include <stdair/STDAIR_Service.hpp>
16 // AirSched
27 
28 namespace AIRSCHED {
29 
30  // ////////////////////////////////////////////////////////////////////
31  AIRSCHED_Service::AIRSCHED_Service() : _airschedServiceContext (NULL) {
32  assert (false);
33  }
34 
35  // ////////////////////////////////////////////////////////////////////
36  AIRSCHED_Service::AIRSCHED_Service (const AIRSCHED_Service& iService)
37  : _airschedServiceContext (NULL) {
38  assert (false);
39  }
40 
41  // ////////////////////////////////////////////////////////////////////
42  AIRSCHED_Service::AIRSCHED_Service (const stdair::BasLogParams& iLogParams)
43  : _airschedServiceContext (NULL) {
44 
45  // Initialise the STDAIR service handler
46  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
47  initStdAirService (iLogParams);
48 
49  // Initialise the service context
50  initServiceContext();
51 
52  // Add the StdAir service context to the AirSched service context
53  // \note AirSched owns the STDAIR service resources here.
54  const bool ownStdairService = true;
55  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
56 
57  // Initialise the (remaining of the) context
58  initAirschedService();
59  }
60 
61  // ////////////////////////////////////////////////////////////////////
62  AIRSCHED_Service::AIRSCHED_Service (const stdair::BasLogParams& iLogParams,
63  const stdair::BasDBParams& iDBParams)
64  : _airschedServiceContext (NULL) {
65 
66  // Initialise the STDAIR service handler
67  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
68  initStdAirService (iLogParams, iDBParams);
69 
70  // Initialise the service context
71  initServiceContext();
72 
73  // Add the StdAir service context to the AirSched service context
74  // \note AirSched owns the STDAIR service resources here.
75  const bool ownStdairService = true;
76  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
77 
78  // Initialise the (remaining of the) context
79  initAirschedService();
80  }
81 
82  // ////////////////////////////////////////////////////////////////////
83  AIRSCHED_Service::
84  AIRSCHED_Service (stdair::STDAIR_ServicePtr_T ioSTDAIRServicePtr)
85  : _airschedServiceContext (NULL) {
86 
87  // Initialise the service context
88  initServiceContext();
89 
90  // Add the StdAir service context to the AirSched service context.
91  // \note AirSched does not own the STDAIR service resources here.
92  const bool doesNotOwnStdairService = false;
93  addStdAirService (ioSTDAIRServicePtr, doesNotOwnStdairService);
94 
95  // Initialise the context
96  initAirschedService();
97  }
98 
99  // ////////////////////////////////////////////////////////////////////
101  // Delete/Clean all the objects from memory
102  finalise();
103  }
104 
105  // ////////////////////////////////////////////////////////////////////
106  void AIRSCHED_Service::finalise() {
107  assert (_airschedServiceContext != NULL);
108  // Reset the (Boost.)Smart pointer pointing on the STDAIR_Service object.
109  _airschedServiceContext->reset();
110  }
111 
112  // //////////////////////////////////////////////////////////////////////
113  void AIRSCHED_Service::initServiceContext() {
114  // Initialise the service context
115  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
117  _airschedServiceContext = &lAIRSCHED_ServiceContext;
118  }
119 
120  // ////////////////////////////////////////////////////////////////////
121  void AIRSCHED_Service::
122  addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
123  const bool iOwnStdairService) {
124 
125  // Retrieve the AirSched service context
126  assert (_airschedServiceContext != NULL);
127  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
128  *_airschedServiceContext;
129 
130  // Store the STDAIR service object within the (AirSched) service context
131  lAIRSCHED_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
132  iOwnStdairService);
133  }
134 
135  // //////////////////////////////////////////////////////////////////////
136  stdair::STDAIR_ServicePtr_T AIRSCHED_Service::
137  initStdAirService (const stdair::BasLogParams& iLogParams) {
138 
146  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
147  boost::make_shared<stdair::STDAIR_Service> (iLogParams);
148 
149  return lSTDAIR_Service_ptr;
150  }
151 
152  // //////////////////////////////////////////////////////////////////////
153  stdair::STDAIR_ServicePtr_T AIRSCHED_Service::
154  initStdAirService (const stdair::BasLogParams& iLogParams,
155  const stdair::BasDBParams& iDBParams) {
156 
164  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
165  boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
166 
167  return lSTDAIR_Service_ptr;
168  }
169 
170  // ////////////////////////////////////////////////////////////////////
171  void AIRSCHED_Service::initAirschedService() {
172  // Do nothing at this stage. A sample BOM tree may be built by
173  // calling the buildSampleBom() method
174  }
175 
176  // ////////////////////////////////////////////////////////////////////
177  void AIRSCHED_Service::
178  parseAndLoad (const stdair::Filename_T& iScheduleInputFilename) {
179 
180  // Retrieve the BOM root object.
181  assert (_airschedServiceContext != NULL);
182  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
183  *_airschedServiceContext;
184  stdair::STDAIR_Service& lSTDAIR_Service =
185  lAIRSCHED_ServiceContext.getSTDAIR_Service();
186  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
187 
188  // Parse the schedule input file, and generate the Inventories
189  stdair::BasChronometer lINVGeneration; lINVGeneration.start();
190  ScheduleParser::generateInventories (iScheduleInputFilename, lBomRoot);
191  const double lGenerationMeasure = lINVGeneration.elapsed();
192 
193  // DEBUG
194  STDAIR_LOG_DEBUG ("Inventory generation time: " << lGenerationMeasure);
195  }
196 
197  // ////////////////////////////////////////////////////////////////////
198  void AIRSCHED_Service::
199  parseAndLoad (const stdair::Filename_T& iScheduleInputFilename,
200  const stdair::Filename_T& iODInputFilename) {
201 
202  // First, build the airline inventories from the schedule file
203  parseAndLoad (iScheduleInputFilename);
204 
205  // Retrieve the BOM tree root
206  assert (_airschedServiceContext != NULL);
207  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
208  *_airschedServiceContext;
209  stdair::STDAIR_Service& lSTDAIR_Service =
210  lAIRSCHED_ServiceContext.getSTDAIR_Service();
211  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
212 
213  // Parse the O&D input file, and generate the O&D periods
214  stdair::BasChronometer lOnDGeneration; lOnDGeneration.start();
215  OnDParser::generateOnDPeriods (iODInputFilename, lBomRoot);
216  const double lGenerationMeasure = lOnDGeneration.elapsed();
217 
218  // DEBUG
219  STDAIR_LOG_DEBUG ("O&D generation time: " << lGenerationMeasure);
220  }
221 
222  // //////////////////////////////////////////////////////////////////////
224 
225  // Retrieve the AirSched service context
226  if (_airschedServiceContext == NULL) {
227  throw stdair::NonInitialisedServiceException ("The AirSched service has "
228  "not been initialised");
229  }
230  assert (_airschedServiceContext != NULL);
231 
232  // Retrieve the AirSched service context and whether it owns the Stdair
233  // service
234  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
235  *_airschedServiceContext;
236  const bool doesOwnStdairService =
237  lAIRSCHED_ServiceContext.getOwnStdairServiceFlag();
238 
239  // Retrieve the StdAir service object from the (AirSched) service context
240  stdair::STDAIR_Service& lSTDAIR_Service =
241  lAIRSCHED_ServiceContext.getSTDAIR_Service();
242 
247  if (doesOwnStdairService == true) {
248  //
249  lSTDAIR_Service.buildSampleBom();
250  }
251 
268  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
270  }
271 
272  // ////////////////////////////////////////////////////////////////////
273  std::string AIRSCHED_Service::
274  jsonExport (const stdair::AirlineCode_T& iAirlineCode,
275  const stdair::FlightNumber_T& iFlightNumber,
276  const stdair::Date_T& iDepartureDate) const {
277 
278  // Retrieve the AirSched service context
279  if (_airschedServiceContext == NULL) {
280  throw stdair::NonInitialisedServiceException ("The AirSched service "
281  "has not been initialised");
282  }
283  assert (_airschedServiceContext != NULL);
284 
285  // Retrieve the StdAir service object from the (AirSched) service context
286  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
287  *_airschedServiceContext;
288  stdair::STDAIR_Service& lSTDAIR_Service =
289  lAIRSCHED_ServiceContext.getSTDAIR_Service();
290 
291  // Delegate the JSON export to the dedicated service
292  return lSTDAIR_Service.jsonExport (iAirlineCode, iFlightNumber,
293  iDepartureDate);
294  }
295 
296  // //////////////////////////////////////////////////////////////////////
297  std::string AIRSCHED_Service::csvDisplay() const {
298 
299  // Retrieve the AirSched service context
300  if (_airschedServiceContext == NULL) {
301  throw stdair::NonInitialisedServiceException ("The AirSched service has "
302  "not been initialised");
303  }
304  assert (_airschedServiceContext != NULL);
305 
306  // Retrieve the STDAIR service object from the (AirSched) service context
307  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
308  *_airschedServiceContext;
309  stdair::STDAIR_Service& lSTDAIR_Service =
310  lAIRSCHED_ServiceContext.getSTDAIR_Service();
311 
312  // Delegate the BOM building to the dedicated service
313  return lSTDAIR_Service.csvDisplay();
314  }
315 
316  // ////////////////////////////////////////////////////////////////////
317  std::string AIRSCHED_Service::
318  csvDisplay (const stdair::AirlineCode_T& iAirlineCode,
319  const stdair::FlightNumber_T& iFlightNumber,
320  const stdair::Date_T& iDepartureDate) const {
321 
322  // Retrieve the AirSched service context
323  if (_airschedServiceContext == NULL) {
324  throw stdair::NonInitialisedServiceException ("The AirSched service has "
325  "not been initialised");
326  }
327  assert (_airschedServiceContext != NULL);
328 
329  // Retrieve the STDAIR service object from the (AirSched) service context
330  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
331  *_airschedServiceContext;
332  stdair::STDAIR_Service& lSTDAIR_Service =
333  lAIRSCHED_ServiceContext.getSTDAIR_Service();
334 
335  // Delegate the BOM display to the dedicated service
336  return lSTDAIR_Service.csvDisplay (iAirlineCode, iFlightNumber,
337  iDepartureDate);
338  }
339 
340  // ////////////////////////////////////////////////////////////////////
342 
343  // Retrieve the AirSched service context
344  if (_airschedServiceContext == NULL) {
345  throw stdair::NonInitialisedServiceException ("The AirSched service has "
346  "not been initialised");
347  }
348  assert (_airschedServiceContext != NULL);
349 
350  // Retrieve the BOM tree root
351  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
352  *_airschedServiceContext;
353  stdair::STDAIR_Service& lSTDAIR_Service =
354  lAIRSCHED_ServiceContext.getSTDAIR_Service();
355  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
356 
357  // Call the underlying Use Case (command)
358  stdair::BasChronometer lSimulateChronometer; lSimulateChronometer.start();
359  Simulator::simulate (lBomRoot);
360  const double lSimulateMeasure = lSimulateChronometer.elapsed();
361 
362  // DEBUG
363  STDAIR_LOG_DEBUG ("Simulation: " << lSimulateMeasure << " - "
364  << lAIRSCHED_ServiceContext.display());
365  }
366 
367  // ////////////////////////////////////////////////////////////////////
368  void AIRSCHED_Service::
369  buildSegmentPathList (stdair::TravelSolutionList_T& ioTravelSolutionList,
370  const stdair::BookingRequestStruct& iBookingRequest) {
371 
372  if (_airschedServiceContext == NULL) {
373  throw stdair::NonInitialisedServiceException ("The AirSched service has "
374  "not been initialised");
375  }
376  assert (_airschedServiceContext != NULL);
377 
378  // Retrieve the BOM tree root
379  AIRSCHED_ServiceContext& lAIRSCHED_ServiceContext =
380  *_airschedServiceContext;
381  stdair::STDAIR_Service& lSTDAIR_Service =
382  lAIRSCHED_ServiceContext.getSTDAIR_Service();
383  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
384 
385  // Delegate the call to the dedicated command
386  stdair::BasChronometer lBuildChronometer; lBuildChronometer.start();
387  SegmentPathProvider::buildSegmentPathList (ioTravelSolutionList,
388  lBomRoot, iBookingRequest);
389  const double lBuildMeasure = lBuildChronometer.elapsed();
390 
391  // DEBUG
392  STDAIR_LOG_DEBUG ("Segment-path build: " << lBuildMeasure << " - "
393  << lAIRSCHED_ServiceContext.display());
394  }
395 
396 }