SimFQT Logo  0.1.3
C++ Simulated Fare Quote System Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
SIMFQT_Service.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // Boost
7 #include <boost/make_shared.hpp>
8 // StdAir
9 #include <stdair/basic/BasChronometer.hpp>
10 #include <stdair/bom/BomDisplay.hpp>
11 #include <stdair/bom/TravelSolutionStruct.hpp>
12 #include <stdair/bom/BookingRequestStruct.hpp>
13 #include <stdair/service/Logger.hpp>
14 #include <stdair/STDAIR_Service.hpp>
15 // Simfqt
22 
23 namespace SIMFQT {
24 
25  // //////////////////////////////////////////////////////////////////////
26  SIMFQT_Service::SIMFQT_Service() : _simfqtServiceContext (NULL) {
27  assert (false);
28  }
29 
30  // //////////////////////////////////////////////////////////////////////
31  SIMFQT_Service::SIMFQT_Service (const SIMFQT_Service& iService) {
32  assert (false);
33  }
34 
35  // ////////////////////////////////////////////////////////////////////
36  SIMFQT_Service::SIMFQT_Service (const stdair::BasLogParams& iLogParams)
37  : _simfqtServiceContext (NULL) {
38 
39  // Initialise the STDAIR service handler
40  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
41  initStdAirService (iLogParams);
42 
43  // Initialise the service context
44  initServiceContext();
45 
46  // Add the StdAir service context to the SIMFQT service context
47  // \note SIMFQT owns the STDAIR service resources here.
48  const bool ownStdairService = true;
49  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
50 
51  // Initialise the (remaining of the) context
52  initSimfqtService();
53  }
54 
55  // ////////////////////////////////////////////////////////////////////
56  SIMFQT_Service::SIMFQT_Service (const stdair::BasLogParams& iLogParams,
57  const stdair::BasDBParams& iDBParams)
58  : _simfqtServiceContext (NULL) {
59 
60  // Initialise the STDAIR service handler
61  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
62  initStdAirService (iLogParams, iDBParams);
63 
64  // Initialise the service context
65  initServiceContext();
66 
67  // Add the StdAir service context to the SIMFQT service context
68  // \note SIMFQT owns the STDAIR service resources here.
69  const bool ownStdairService = true;
70  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
71 
72  // Initialise the (remaining of the) context
73  initSimfqtService();
74  }
75 
76  // ////////////////////////////////////////////////////////////////////
77  SIMFQT_Service::
78  SIMFQT_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr)
79  : _simfqtServiceContext (NULL) {
80 
81  // Initialise the service context
82  initServiceContext();
83 
84  // Store the STDAIR service object within the (SIMFQT) service context
85  // \note Simfqt does not own the STDAIR service resources here.
86  const bool doesNotOwnStdairService = false;
87  addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
88 
89  // Initialise the context
90  initSimfqtService();
91  }
92 
93  // //////////////////////////////////////////////////////////////////////
95  // Delete/Clean all the objects from memory
96  finalise();
97  }
98 
99  // //////////////////////////////////////////////////////////////////////
100  void SIMFQT_Service::finalise() {
101  assert (_simfqtServiceContext != NULL);
102  // Reset the (Boost.)Smart pointer pointing on the STDAIR_Service object.
103  _simfqtServiceContext->reset();
104  }
105 
106  // //////////////////////////////////////////////////////////////////////
107  void SIMFQT_Service::initServiceContext() {
108  // Initialise the service context
109  SIMFQT_ServiceContext& lSIMFQT_ServiceContext =
111  _simfqtServiceContext = &lSIMFQT_ServiceContext;
112  }
113 
114  // ////////////////////////////////////////////////////////////////////
115  void SIMFQT_Service::
116  addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
117  const bool iOwnStdairService) {
118 
119  // Retrieve the SimFQT service context
120  assert (_simfqtServiceContext != NULL);
121  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
122 
123  // Store the STDAIR service object within the (SimFQT) service context
124  lSIMFQT_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
125  iOwnStdairService);
126  }
127 
128  // //////////////////////////////////////////////////////////////////////
129  stdair::STDAIR_ServicePtr_T SIMFQT_Service::
130  initStdAirService (const stdair::BasLogParams& iLogParams,
131  const stdair::BasDBParams& iDBParams) {
132 
139  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
140  boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
141  assert (lSTDAIR_Service_ptr != NULL);
142 
143  return lSTDAIR_Service_ptr;
144  }
145 
146  // //////////////////////////////////////////////////////////////////////
147  stdair::STDAIR_ServicePtr_T SIMFQT_Service::
148  initStdAirService (const stdair::BasLogParams& iLogParams) {
149 
156  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
157  boost::make_shared<stdair::STDAIR_Service> (iLogParams);
158  assert (lSTDAIR_Service_ptr != NULL);
159 
160  return lSTDAIR_Service_ptr;
161  }
162 
163  // ////////////////////////////////////////////////////////////////////
164  void SIMFQT_Service::initSimfqtService() {
165  // Do nothing at this stage. A sample BOM tree may be built by
166  // calling the buildSampleBom() method
167  }
168 
169  // ////////////////////////////////////////////////////////////////////
170  void SIMFQT_Service::
171  parseAndLoad (const FareFilePath& iFareFilename) {
172 
173  // Retrieve the BOM root object.
174  assert (_simfqtServiceContext != NULL);
175  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
176  stdair::STDAIR_Service& lSTDAIR_Service =
177  lSIMFQT_ServiceContext.getSTDAIR_Service();
178  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
179 
180  // Initialise the airline inventories
181  FareParser::fareRuleGeneration (iFareFilename, lBomRoot);
182  }
183 
184  // ////////////////////////////////////////////////////////////////////
186 
187  // Retrieve the SimFQT service context
188  if (_simfqtServiceContext == NULL) {
189  throw stdair::NonInitialisedServiceException ("The SimFQT service "
190  "has not been initialised");
191  }
192  assert (_simfqtServiceContext != NULL);
193 
194  // Retrieve the SimFQT service context and whether it owns the Stdair
195  // service
196  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
197  const bool doesOwnStdairService =
198  lSIMFQT_ServiceContext.getOwnStdairServiceFlag();
199 
200  // Retrieve the StdAir service object from the (SimFQT) service context
201  stdair::STDAIR_Service& lSTDAIR_Service =
202  lSIMFQT_ServiceContext.getSTDAIR_Service();
203 
208  if (doesOwnStdairService == true) {
209  //
210  lSTDAIR_Service.buildSampleBom();
211  }
212 
228  }
229 
230  // //////////////////////////////////////////////////////////////////////
231  stdair::BookingRequestStruct SIMFQT_Service::buildBookingRequest(const bool isForCRS) {
232 
233  // Retrieve the SIMFQT service context
234  if (_simfqtServiceContext == NULL) {
235  throw stdair::NonInitialisedServiceException ("The Simfqt service has not "
236  "been initialised");
237  }
238  assert (_simfqtServiceContext != NULL);
239 
240  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
241 
242  // Retrieve the STDAIR service object from the (Simfqt) service context
243  stdair::STDAIR_Service& lSTDAIR_Service =
244  lSIMFQT_ServiceContext.getSTDAIR_Service();
245 
246  // Delegate the BOM building to the dedicated service
247  stdair::BookingRequestStruct oBookingRequest =
248  lSTDAIR_Service.buildSampleBookingRequest (isForCRS);
249 
250  return oBookingRequest;
251  }
252 
253  // //////////////////////////////////////////////////////////////////////
254  void SIMFQT_Service::
255  buildSampleTravelSolutions(stdair::TravelSolutionList_T& ioTravelSolutionList){
256 
257  // Retrieve the SIMFQT service context
258  if (_simfqtServiceContext == NULL) {
259  throw stdair::NonInitialisedServiceException ("The Simfqt service has not "
260  "been initialised");
261  }
262  assert (_simfqtServiceContext != NULL);
263 
264  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
265 
266  // Retrieve the STDAIR service object from the (Simfqt) service context
267  stdair::STDAIR_Service& lSTDAIR_Service =
268  lSIMFQT_ServiceContext.getSTDAIR_Service();
269 
270  // Delegate the BOM building to the dedicated service
271  lSTDAIR_Service.buildSampleTravelSolutionForPricing (ioTravelSolutionList);
272  }
273 
274 
275  // ////////////////////////////////////////////////////////////////////
276  std::string SIMFQT_Service::csvDisplay() const {
277 
278  // Retrieve the SIMFQT service context
279  if (_simfqtServiceContext == NULL) {
280  throw stdair::NonInitialisedServiceException ("The SimFQT service "
281  "has not been initialised");
282  }
283  assert (_simfqtServiceContext != NULL);
284 
285  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
286 
287  // Retrieve the STDAIR service object from the (SimFQT) service context
288  stdair::STDAIR_Service& lSTDAIR_Service =
289  lSIMFQT_ServiceContext.getSTDAIR_Service();
290 
291  // Get the root of the BOM tree, on which all of the other BOM objects
292  // are attached
293  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
294 
295  // Delegate the BOM display to the dedicated service
296  std::ostringstream oCSVStr;
297  stdair::BomDisplay::csvSimFQTAirRACDisplay (oCSVStr, lBomRoot);
298  return oCSVStr.str();
299  }
300 
301  // //////////////////////////////////////////////////////////////////////
302  std::string SIMFQT_Service::
303  csvDisplay (const stdair::TravelSolutionList_T& ioTravelSolutionList) const {
304 
305  // Retrieve the Simfqt service context
306  if (_simfqtServiceContext == NULL) {
307  throw stdair::NonInitialisedServiceException ("The Simfqt service has not "
308  "been initialised");
309  }
310  assert (_simfqtServiceContext != NULL);
311 
312  // Retrieve the Simfqt service context
313  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
314 
315  // Retrieve the STDAIR service object from the (Simfqt) service context
316  stdair::STDAIR_Service& lSTDAIR_Service =
317  lSIMFQT_ServiceContext.getSTDAIR_Service();
318 
319  // Delegate the BOM building to the dedicated service
320  return lSTDAIR_Service.csvDisplay (ioTravelSolutionList);
321  }
322 
323  // //////////////////////////////////////////////////////////////////////
324  std::string SIMFQT_Service::
325  csvDisplay (const stdair::AirportCode_T& iOrigin,
326  const stdair::AirportCode_T& iDestination,
327  const stdair::Date_T& iDepartureDate) const {
328 
329  // Retrieve the SIMFQT service context
330  if (_simfqtServiceContext == NULL) {
331  throw stdair::NonInitialisedServiceException ("The Simfqt service "
332  "has not been initialised");
333  }
334  assert (_simfqtServiceContext != NULL);
335 
336  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
337 
338  // Retrieve the STDAIR service object from the (SIMFQT) service context
339  stdair::STDAIR_Service& lSTDAIR_Service =
340  lSIMFQT_ServiceContext.getSTDAIR_Service();
341 
342  // Delegate the BOM display to the dedicated service
343  return lSTDAIR_Service.csvDisplay (iOrigin, iDestination,
344  iDepartureDate);
345  }
346 
347  // //////////////////////////////////////////////////////////////////////
348  std::string SIMFQT_Service::list() const {
349 
350  // Retrieve the SIMFQT service context
351  if (_simfqtServiceContext == NULL) {
352  throw stdair::NonInitialisedServiceException ("The Simfqt service "
353  "has not been initialised");
354  }
355  assert (_simfqtServiceContext != NULL);
356 
357  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
358 
359  // Retrieve the STDAIR service object from the (SIMFQT) service context
360  stdair::STDAIR_Service& lSTDAIR_Service =
361  lSIMFQT_ServiceContext.getSTDAIR_Service();
362 
363  // Delegate the BOM display to the dedicated service
364  return lSTDAIR_Service.listAirportPairDateRange ();
365  }
366 
367  // ////////////////////////////////////////////////////////////////////
368  bool SIMFQT_Service::
369  check (const stdair::AirportCode_T& iOrigin,
370  const stdair::AirportCode_T& iDestination,
371  const stdair::Date_T& iDepartureDate) const {
372  std::ostringstream oFlightListStr;
373 
374  if (_simfqtServiceContext == NULL) {
375  throw stdair::NonInitialisedServiceException ("The Simfqt service "
376  "has not been initialised");
377  }
378  assert (_simfqtServiceContext != NULL);
379  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
380 
381  // Retrieve the STDAIR service object from the (SIMFQT) service context
382  stdair::STDAIR_Service& lSTDAIR_Service =
383  lSIMFQT_ServiceContext.getSTDAIR_Service();
384 
385  // Delegate the BOM display to the dedicated service
386  return lSTDAIR_Service.check (iOrigin, iDestination, iDepartureDate);
387  }
388 
389  // ////////////////////////////////////////////////////////////////////
390  void SIMFQT_Service::
391  quotePrices (const stdair::BookingRequestStruct& iBookingRequest,
392  stdair::TravelSolutionList_T& ioTravelSolutionList) {
393 
394  // Retrieve the Simfqt service context
395  if (_simfqtServiceContext == NULL) {
396  throw stdair::NonInitialisedServiceException ("The SimFQT service "
397  "has not been initialised");
398  }
399  assert (_simfqtServiceContext != NULL);
400 
401  SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
402 
403  // Retrieve the StdAir service context
404  stdair::STDAIR_Service& lSTDAIR_Service =
405  lSIMFQT_ServiceContext.getSTDAIR_Service();
406 
407  // Get the root of the BOM tree, on which all of the other BOM objects
408  // will be attached
409  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
410 
411  // Delegate the action to the dedicated command
412  stdair::BasChronometer lFareQuoteRetrievalChronometer;
413  lFareQuoteRetrievalChronometer.start();
414  FareQuoter::priceQuote (iBookingRequest, ioTravelSolutionList, lBomRoot);
415 
416  // DEBUG
417  const double lFareQuoteRetrievalMeasure =
418  lFareQuoteRetrievalChronometer.elapsed();
419  STDAIR_LOG_DEBUG ("Fare Quote retrieving: " << lFareQuoteRetrievalMeasure
420  << " - " << lSIMFQT_ServiceContext.display());
421  }
422 
423 }