AirRAC Logo  0.2.3
C++ Simulated Revenue Accounting (RAC) System Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
AIRRAC_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/service/Logger.hpp>
12 #include <stdair/STDAIR_Service.hpp>
13 // Airrac
20 
21 namespace AIRRAC {
22 
23  // //////////////////////////////////////////////////////////////////////
24  AIRRAC_Service::AIRRAC_Service() : _airracServiceContext (NULL) {
25  assert (false);
26  }
27 
28  // //////////////////////////////////////////////////////////////////////
29  AIRRAC_Service::AIRRAC_Service (const AIRRAC_Service& iService) {
30  assert (false);
31  }
32 
33  // ////////////////////////////////////////////////////////////////////
34  AIRRAC_Service::AIRRAC_Service (const stdair::BasLogParams& iLogParams)
35  : _airracServiceContext (NULL) {
36 
37  // Initialise the STDAIR service handler
38  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
39  initStdAirService (iLogParams);
40 
41  // Initialise the service context
42  initServiceContext();
43 
44  // Add the StdAir service context to the AIRRAC service context
45  // \note AIRRAC owns the STDAIR service resources here.
46  const bool ownStdairService = true;
47  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
48 
49  // Initialise the (remaining of the) context
50  initAirracService();
51  }
52 
53  // ////////////////////////////////////////////////////////////////////
54  AIRRAC_Service::AIRRAC_Service (const stdair::BasLogParams& iLogParams,
55  const stdair::BasDBParams& iDBParams)
56  : _airracServiceContext (NULL) {
57 
58  // Initialise the STDAIR service handler
59  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
60  initStdAirService (iLogParams, iDBParams);
61 
62  // Initialise the service context
63  initServiceContext();
64 
65  // Add the StdAir service context to the AIRRAC service context
66  // \note AIRRAC owns the STDAIR service resources here.
67  const bool ownStdairService = true;
68  addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
69 
70  // Initialise the (remaining of the) context
71  initAirracService();
72  }
73 
74  // ////////////////////////////////////////////////////////////////////
75  AIRRAC_Service::
76  AIRRAC_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr)
77  : _airracServiceContext (NULL) {
78 
79  // Initialise the service context
80  initServiceContext();
81 
82  // Store the STDAIR service object within the (AIRRAC) service context
83  // \note Airrac does not own the STDAIR service resources here.
84  const bool doesNotOwnStdairService = false;
85  addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
86 
87  // Initialise the context
88  initAirracService();
89  }
90 
91  // //////////////////////////////////////////////////////////////////////
93  // Delete/Clean all the objects from memory
94  finalise();
95  }
96 
97  // //////////////////////////////////////////////////////////////////////
98  void AIRRAC_Service::finalise() {
99  assert (_airracServiceContext != NULL);
100  // Reset the (Boost.)Smart pointer pointing on the STDAIR_Service object.
101  _airracServiceContext->reset();
102  }
103 
104  // //////////////////////////////////////////////////////////////////////
105  void AIRRAC_Service::initServiceContext() {
106  // Initialise the service context
107  AIRRAC_ServiceContext& lAIRRAC_ServiceContext =
109  _airracServiceContext = &lAIRRAC_ServiceContext;
110  }
111 
112  // //////////////////////////////////////////////////////////////////////
113  stdair::STDAIR_ServicePtr_T AIRRAC_Service::
114  initStdAirService (const stdair::BasLogParams& iLogParams,
115  const stdair::BasDBParams& iDBParams) {
116 
124  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
125  boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
126 
127  return lSTDAIR_Service_ptr;
128 
129  }
130 
131  // //////////////////////////////////////////////////////////////////////
132  stdair::STDAIR_ServicePtr_T AIRRAC_Service::
133  initStdAirService (const stdair::BasLogParams& iLogParams) {
134 
142  stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
143  boost::make_shared<stdair::STDAIR_Service> (iLogParams);
144 
145  return lSTDAIR_Service_ptr;
146  }
147 
148  // ////////////////////////////////////////////////////////////////////
149  void AIRRAC_Service::
150  addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
151  const bool iOwnStdairService) {
152 
153  // Retrieve the Airrac service context
154  assert (_airracServiceContext != NULL);
155  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
156 
157  // Store the STDAIR service object within the (AIRRAC) service context
158  lAIRRAC_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
159  iOwnStdairService);
160  }
161 
162  // ////////////////////////////////////////////////////////////////////
163  void AIRRAC_Service::initAirracService() {
164  // Do nothing at this stage. A sample BOM tree may be built by
165  // calling the buildSampleBom() method
166  }
167 
168  // ////////////////////////////////////////////////////////////////////
169  void AIRRAC_Service::
170  parseAndLoad (const YieldFilePath& iYieldFilename) {
171 
172  // Retrieve the BOM root object.
173  assert (_airracServiceContext != NULL);
174  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
175  stdair::STDAIR_Service& lSTDAIR_Service =
176  lAIRRAC_ServiceContext.getSTDAIR_Service();
177  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
178 
179  // Initialise the airline inventories
180  YieldParser::generateYieldStore (iYieldFilename, lBomRoot);
181  }
182 
183  // ////////////////////////////////////////////////////////////////////
185 
186  // Retrieve the AirRAC service context
187  if (_airracServiceContext == NULL) {
188  throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
189  " been initialised");
190  }
191  assert (_airracServiceContext != NULL);
192 
193  // Retrieve the AirRAC service context and whether it owns the Stdair
194  // service
195  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
196  const bool doesOwnStdairService =
197  lAIRRAC_ServiceContext.getOwnStdairServiceFlag();
198 
199  // Retrieve the StdAir service object from the (AirRAC) service context
200  stdair::STDAIR_Service& lSTDAIR_Service =
201  lAIRRAC_ServiceContext.getSTDAIR_Service();
202 
207  if (doesOwnStdairService == true) {
208  //
209  lSTDAIR_Service.buildSampleBom();
210  }
211 
227  }
228 
229  // //////////////////////////////////////////////////////////////////////
230  void AIRRAC_Service::
231  buildSampleTravelSolutions(stdair::TravelSolutionList_T& ioTravelSolutionList){
232 
233  // Retrieve the AIRRAC service context
234  if (_airracServiceContext == NULL) {
235  throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
236  " been initialised");
237  }
238  assert (_airracServiceContext != NULL);
239 
240  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
241 
242  // Retrieve the STDAIR service object from the (AirRAC) service context
243  stdair::STDAIR_Service& lSTDAIR_Service =
244  lAIRRAC_ServiceContext.getSTDAIR_Service();
245 
246  // Delegate the BOM building to the dedicated service
247  lSTDAIR_Service.buildSampleTravelSolutions (ioTravelSolutionList);
248  }
249 
250  // //////////////////////////////////////////////////////////////////////
251  std::string AIRRAC_Service::csvDisplay() const {
252 
253  // Retrieve the AIRRAC service context
254  if (_airracServiceContext == NULL) {
255  throw stdair::NonInitialisedServiceException ("The Airrac service "
256  "has not been initialised");
257  }
258  assert (_airracServiceContext != NULL);
259 
260  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
261 
262  // Retrieve the STDAIR service object from the (Airrac) service context
263  stdair::STDAIR_Service& lSTDAIR_Service =
264  lAIRRAC_ServiceContext.getSTDAIR_Service();
265 
266  // Get the root of the BOM tree, on which all of the other BOM objects
267  // are attached
268  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
269 
270  // Delegate the BOM display to the dedicated service
271  std::ostringstream oCSVStr;
272  stdair::BomDisplay::csvSimFQTAirRACDisplay (oCSVStr, lBomRoot);
273  return oCSVStr.str();
274 
275  }
276 
277  // //////////////////////////////////////////////////////////////////////
278  std::string AIRRAC_Service::
279  csvDisplay (const stdair::TravelSolutionList_T& ioTravelSolutionList) const {
280 
281  // Retrieve the AirRAC service context
282  if (_airracServiceContext == NULL) {
283  throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
284  " been initialised");
285  }
286  assert (_airracServiceContext != NULL);
287 
288  // Retrieve the AirRAC service context
289  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
290 
291  // Retrieve the STDAIR service object from the (AirRAC) service context
292  stdair::STDAIR_Service& lSTDAIR_Service =
293  lAIRRAC_ServiceContext.getSTDAIR_Service();
294 
295  // Delegate the BOM building to the dedicated service
296  return lSTDAIR_Service.csvDisplay (ioTravelSolutionList);
297  }
298 
299  // ////////////////////////////////////////////////////////////////////
300  void AIRRAC_Service::
301  calculateYields (stdair::TravelSolutionList_T& ioTravelSolutionList) {
302 
303  // Retrieve the Airrac service context
304  if (_airracServiceContext == NULL) {
305  throw stdair::NonInitialisedServiceException ("The AirRAC service has not"
306  " been initialised");
307  }
308  assert (_airracServiceContext != NULL);
309  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
310 
311  // Retrieve the StdAir service context
312  stdair::STDAIR_Service& lSTDAIR_Service =
313  lAIRRAC_ServiceContext.getSTDAIR_Service();
314 
315  // Get the root of the BOM tree, on which all of the other BOM objects
316  // will be attached
317  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
318 
319  // Delegate the booking to the dedicated command: set the yields
320  // for each travel solution of the given list
321  stdair::BasChronometer lYieldChronometer;
322  lYieldChronometer.start();
323  YieldManager::calculateYield (ioTravelSolutionList, lBomRoot);
324  const double lYieldMeasure = lYieldChronometer.elapsed();
325 
326  // DEBUG
327  STDAIR_LOG_DEBUG ("Yield calculation: " << lYieldMeasure << " - "
328  << lAIRRAC_ServiceContext.display());
329  }
330 
331  // ////////////////////////////////////////////////////////////////////
333  // Retrieve the AirRAC service context
334  assert (_airracServiceContext != NULL);
335  AIRRAC_ServiceContext& lAIRRAC_ServiceContext = *_airracServiceContext;
336 
337  // Retrieve the StdAir service context
338  stdair::STDAIR_Service& lSTDAIR_Service =
339  lAIRRAC_ServiceContext.getSTDAIR_Service();
340 
341  // Get the root of the BOM tree, on which all of the other BOM objects
342  // will be attached
343  stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
344 
345  // Update the default yields to the booking classes.
346  YieldManager::updateYields (lBomRoot);
347  }
348 }