AirInv Logo  0.1.2
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InventoryHelper.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // StdAir
7 #include <stdair/bom/BomRetriever.hpp>
8 #include <stdair/bom/BomManager.hpp>
9 #include <stdair/bom/Inventory.hpp>
10 #include <stdair/bom/FlightDate.hpp>
11 #include <stdair/bom/SegmentDate.hpp>
12 #include <stdair/bom/SegmentCabin.hpp>
13 #include <stdair/bom/FareFamily.hpp>
14 #include <stdair/bom/BookingClass.hpp>
15 #include <stdair/bom/GuillotineBlock.hpp>
16 #include <stdair/bom/TravelSolutionStruct.hpp>
17 #include <stdair/service/Logger.hpp>
18 #include <stdair/bom/LegCabin.hpp>
19 // AirInv
24 
25 namespace AIRINV {
26 
27  // ////////////////////////////////////////////////////////////////////
28  void InventoryHelper::fillFromRouting (const stdair::Inventory& iInventory) {
29  const stdair::FlightDateList_T& lFlightDateList =
30  stdair::BomManager::getList<stdair::FlightDate> (iInventory);
31 
32  // Browse the list of flight-dates and update each flight-date.
33  for (stdair::FlightDateList_T::const_iterator itFlightDate =
34  lFlightDateList.begin();
35  itFlightDate != lFlightDateList.end(); ++itFlightDate) {
36  const stdair::FlightDate* lCurrentFlightDate_ptr = *itFlightDate;
37  assert (lCurrentFlightDate_ptr != NULL);
38  FlightDateHelper::fillFromRouting (*lCurrentFlightDate_ptr);
39  }
40  }
41 
42  // ////////////////////////////////////////////////////////////////////
44  calculateAvailability (const stdair::Inventory& iInventory,
45  const std::string& iFullSegmentDateKey,
46  stdair::TravelSolutionStruct& ioTravelSolution) {
47 
48  // Create the map of class/availability for the given segment date.
49  stdair::ClassAvailabilityMap_T lClassAvailabilityMap;
50 
51  // DEBUG
52  STDAIR_LOG_DEBUG (iFullSegmentDateKey);
53  //
54  stdair::SegmentDate* lSegmentDate_ptr =
55  stdair::BomRetriever::retrieveSegmentDateFromLongKey(iInventory,
56  iFullSegmentDateKey);
57  assert (lSegmentDate_ptr != NULL);
58 
59  // Browse the segment-cabins and fill the map with the availability of
60  // each booking class.
61  const stdair::SegmentCabinList_T& lSegmentCabinList =
62  stdair::BomManager::getList<stdair::SegmentCabin> (*lSegmentDate_ptr);
63  for (stdair::SegmentCabinList_T::const_iterator itCabin =
64  lSegmentCabinList.begin();
65  itCabin != lSegmentCabinList.end(); ++itCabin) {
66  stdair::SegmentCabin* lSegmentCabin_ptr = *itCabin;
67  assert (lSegmentCabin_ptr != NULL);
68 
69 
70  // Compute the availability using the AU and the cumulative
71  // booking counter.
72  SegmentCabinHelper::updateAvailabilities (*lSegmentCabin_ptr);
73  const stdair::BookingClassList_T& lBCList =
74  stdair::BomManager::getList<stdair::BookingClass> (*lSegmentCabin_ptr);
75  for (stdair::BookingClassList_T::const_reverse_iterator itBC =
76  lBCList.rbegin(); itBC != lBCList.rend(); ++itBC) {
77  stdair::BookingClass* lBC_ptr = *itBC;
78  assert (lBC_ptr != NULL);
79 
80  const stdair::Availability_T lAvl = lBC_ptr->getSegmentAvailability();
81 
82  const stdair::ClassCode_T& lClassCode = lBC_ptr->getClassCode();
83  const bool insertSuccessful = lClassAvailabilityMap.
84  insert (stdair::ClassAvailabilityMap_T::value_type (lClassCode,
85  lAvl)).second;
86  assert (insertSuccessful == true);
87  }
88  }
89 
90  //
91  ioTravelSolution.addClassAvailabilityMap (lClassAvailabilityMap);
92  }
93 
94 
95  // ////////////////////////////////////////////////////////////////////
97  getYieldAndBidPrice (const stdair::Inventory& iInventory,
98  const std::string& iFullSegmentDateKey,
99  stdair::TravelSolutionStruct& ioTravelSolution) {
100 
101  // Create the map of class/availability for the given segment date.
102  // stdair::ClassAvailabilityMap_T lClassAvailabilityMap;
103 
104  stdair::ClassYieldMap_T lClassYieldMap;
105 
106  stdair::ClassBpvMap_T lClassBpvMap;
107 
108  // DEBUG
109  STDAIR_LOG_DEBUG (iFullSegmentDateKey);
110  //
111  stdair::SegmentDate* lSegmentDate_ptr =
112  stdair::BomRetriever::retrieveSegmentDateFromLongKey (iInventory,
113  iFullSegmentDateKey);
114  assert (lSegmentDate_ptr != NULL);
115 
116  // Browse the segment-cabins and fill the maps with the bid price vector reference
117  // and yield of each booking class.
118  const stdair::SegmentCabinList_T& lSegmentCabinList =
119  stdair::BomManager::getList<stdair::SegmentCabin> (*lSegmentDate_ptr);
120  for (stdair::SegmentCabinList_T::const_iterator itCabin =
121  lSegmentCabinList.begin();
122  itCabin != lSegmentCabinList.end(); ++itCabin) {
123  stdair::SegmentCabin* lSegmentCabin_ptr = *itCabin;
124  assert (lSegmentCabin_ptr != NULL);
125 
126  stdair::BidPriceVector_T lBPV;
127 
128 
129  //stdair::BidPriceVector_T lBPV;
130  stdair::LegCabinList_T lLegCabinList =
131  stdair::BomManager::getList<stdair::LegCabin> (*lSegmentCabin_ptr);
132  assert (!lLegCabinList.empty());
133  if (lLegCabinList.size() > 1) {
134  // Compute the sum of bid prices and return a vector containing that value.
135  stdair::BidPrice_T lBidPriceValue = 0;
136  for (stdair::LegCabinList_T::const_iterator itLC = lLegCabinList.begin();
137  itLC != lLegCabinList.end(); ++itLC) {
138  const stdair::LegCabin* lLegCabin_ptr = *itLC;
139  const stdair::BidPriceVector_T& lLegCabinBPV = lLegCabin_ptr->getBidPriceVector();
140  if (!lLegCabinBPV.empty()) {
141  lBidPriceValue += lLegCabinBPV.back();
142  } else {
143  // If the remaining capacity is zero (empty bid price vector) on one of the legs,
144  // then the remaining capacity of the segment is also zero (return an empty bid price).
145  lBidPriceValue = std::numeric_limits<stdair::BidPrice_T>::max();
146  break;
147  }
148  }
149  if (lBidPriceValue < std::numeric_limits<stdair::BidPrice_T>::max()) {
150  lBPV.push_back(lBidPriceValue);
151  }
152 
153  } else {
154  const stdair::LegCabin* lLegCabin_ptr = lLegCabinList.front();
155  lBPV = lLegCabin_ptr->getBidPriceVector();
156  }
157 
158 
159  // const stdair::CabinCapacity_T& lCabinCapacity = lSegmentCabin_ptr->getCapacity();
160  // const stdair::CommittedSpace_T& lCommittedSpace = lSegmentCabin_ptr->getCommittedSpace();
161  // assert (lCabinCapacity - lCommittedSpace > 0);
162  // lBPV.resize(lCabinCapacity - lCommittedSpace);
163 
164  const stdair::Availability_T& lAvailabilityPool =
165  lSegmentCabin_ptr->getAvailabilityPool();
166  //assert (lAvailabilityPool > 0);
167 
168  if (lAvailabilityPool < lBPV.size()) {
169  lBPV.resize(lAvailabilityPool);
170  }
171 
172 
173  //
174  ioTravelSolution.addBidPriceVector (lBPV);
175 
176  const stdair::BidPriceVectorHolder_T& lBidPriceVectorHolder =
177  ioTravelSolution.getBidPriceVectorHolder();
178  const stdair::BidPriceVectorHolder_T::const_reverse_iterator itBPV =
179  lBidPriceVectorHolder.rbegin();
180  const stdair::BidPriceVector_T& lBpvRef = *itBPV;
181 
182  const stdair::FareFamilyList_T& lFFList =
183  stdair::BomManager::getList<stdair::FareFamily> (*lSegmentCabin_ptr);
184  for (stdair::FareFamilyList_T::const_iterator itFF = lFFList.begin();
185  itFF != lFFList.end(); ++itFF) {
186  const stdair::FareFamily* lFareFamily_ptr = *itFF;
187  assert (lFareFamily_ptr != NULL);
188 
189  const stdair::BookingClassList_T& lBCList =
190  stdair::BomManager::getList<stdair::BookingClass> (*lFareFamily_ptr);
191  for (stdair::BookingClassList_T::const_iterator itBC = lBCList.begin();
192  itBC != lBCList.end(); ++itBC) {
193  const stdair::BookingClass* lBC_ptr = *itBC;
194  assert (lBC_ptr != NULL);
195 
196  const stdair::ClassCode_T& lClassCode = lBC_ptr->getClassCode();
197 
198  const stdair::YieldValue_T lYld = lBC_ptr->getYield() ;
199  const bool insertYieldMapSuccessful = lClassYieldMap.
200  insert (stdair::ClassYieldMap_T::value_type (lClassCode,
201  lYld)).second;
202  assert (insertYieldMapSuccessful == true);
203 
204  const bool insertBpvMapSuccessful = lClassBpvMap.
205  insert (stdair::ClassBpvMap_T::value_type (lClassCode,
206  &lBpvRef)).second;
207  assert (insertBpvMapSuccessful == true);
208 
209  // DEBUG
210  // STDAIR_LOG_DEBUG ("Class: " << lClassCode
211  // << ", " << "Yield: " << lYld << ", "
212  // << "Bid price: " << lBpvRef.back() << ", "
213  // << "Remaining capacity: "
214  // << lCabinCapacity - lCommittedSpace);
215 
216  //
217  stdair::BidPrice_T lBpvVal = std::numeric_limits<double>::max();
218  if (lBpvRef.empty() == false) {
219  lBpvVal = lBpvRef.back();
220  }
221 
222  //lBpvVal = boost::lexical_cast<std::string> (lBpvRef.back());
223  STDAIR_LOG_DEBUG ("Class: " << lClassCode
224  << ", " << "Yield: " << lYld << ", "
225  << "Bid price: " << lBpvVal << ", "
226  << "Remaining capacity: " << lAvailabilityPool
227  << " Segment date: " << iFullSegmentDateKey);
228  }
229  }
230  }
231 
232  //
233  ioTravelSolution.addClassYieldMap (lClassYieldMap);
234  ioTravelSolution.addClassBpvMap (lClassBpvMap);
235  }
236 
237 
238  // ////////////////////////////////////////////////////////////////////
239  bool InventoryHelper::sell (stdair::Inventory& ioInventory,
240  const std::string& iFullSegmentDateKey,
241  const stdair::ClassCode_T& iClassCode,
242  const stdair::PartySize_T& iPartySize) {
243  bool hasSaleBeenSuccessful = false;
244 
245  // DEBUG
246  STDAIR_LOG_DEBUG ("Full key: '" << iFullSegmentDateKey
247  << "', " << iClassCode);
248 
249  //
250  stdair::BookingClass* lBookingClass_ptr =
251  stdair::BomRetriever::retrieveBookingClassFromLongKey(ioInventory,
252  iFullSegmentDateKey,
253  iClassCode);
254 
255  // DEBUG
256  const std::string hasFoundBookingClassStr =
257  (lBookingClass_ptr != NULL)?"Yes":"No";
258  STDAIR_LOG_DEBUG ("Found booking class? " << hasFoundBookingClassStr);
259 
260  if (lBookingClass_ptr != NULL) {
261  // Register the sale in the class.
262  lBookingClass_ptr->sell (iPartySize);
263 
264  //
265  stdair::FareFamily& lFareFamily =
266  stdair::BomManager::getParent<stdair::FareFamily> (*lBookingClass_ptr);
267 
268  //
269  stdair::SegmentCabin& lSegmentCabin =
270  stdair::BomManager::getParent<stdair::SegmentCabin> (lFareFamily);
271 
272  //
273  stdair::SegmentDate& lSegmentDate =
274  stdair::BomManager::getParent<stdair::SegmentDate,
275  stdair::SegmentCabin> (lSegmentCabin);
276 
277  //
278  stdair::FlightDate& lFlightDate =
279  stdair::BomManager::getParent<stdair::FlightDate,
280  stdair::SegmentDate> (lSegmentDate);
281 
282  // Update the commited space of the segment-cabins and the leg-cabins.
283  SegmentCabinHelper::updateFromReservation (lFlightDate, lSegmentCabin,
284  iPartySize);
285 
286  // STDAIR_LOG_NOTIFICATION (lFlightDate.getDepartureDate()
287  // << ";" << iClassCode);
288  hasSaleBeenSuccessful = true;
289  }
290 
291  return hasSaleBeenSuccessful;
292  }
293 
294  // ////////////////////////////////////////////////////////////////////
295  bool InventoryHelper::cancel (stdair::Inventory& ioInventory,
296  const std::string& iFullSegmentDateKey,
297  const stdair::ClassCode_T& iClassCode,
298  const stdair::PartySize_T& iPartySize) {
299  bool hasCancellationBeenSuccessful = false;
300 
301  // DEBUG
302  STDAIR_LOG_DEBUG ("Full key: '" << iFullSegmentDateKey
303  << "', " << iClassCode);
304 
305  //
306  stdair::BookingClass* lBookingClass_ptr =
307  stdair::BomRetriever::retrieveBookingClassFromLongKey(ioInventory,
308  iFullSegmentDateKey,
309  iClassCode);
310 
311  // DEBUG
312  const std::string hasFoundBookingClassStr =
313  (lBookingClass_ptr != NULL)?"Yes":"No";
314  STDAIR_LOG_DEBUG ("Found booking class? " << hasFoundBookingClassStr);
315 
316  if (lBookingClass_ptr != NULL) {
317  // Register the cancellation in the class.
318  lBookingClass_ptr->cancel (iPartySize);
319 
320  //
321  stdair::FareFamily& lFareFamily =
322  stdair::BomManager::getParent<stdair::FareFamily> (*lBookingClass_ptr);
323 
324  //
325  stdair::SegmentCabin& lSegmentCabin =
326  stdair::BomManager::getParent<stdair::SegmentCabin> (lFareFamily);
327 
328  //
329  stdair::SegmentDate& lSegmentDate =
330  stdair::BomManager::getParent<stdair::SegmentDate,
331  stdair::SegmentCabin> (lSegmentCabin);
332 
333  //
334  stdair::FlightDate& lFlightDate =
335  stdair::BomManager::getParent<stdair::FlightDate,
336  stdair::SegmentDate> (lSegmentDate);
337 
338  // Update the commited space of the segment-cabins and the leg-cabins.
339  SegmentCabinHelper::updateFromReservation (lFlightDate, lSegmentCabin,
340  -iPartySize);
341 
342  // STDAIR_LOG_NOTIFICATION (lFlightDate.getDepartureDate()
343  // << ";" << iClassCode);
344  hasCancellationBeenSuccessful = true;
345  }
346 
347  return hasCancellationBeenSuccessful;
348  }
349 
350  // ////////////////////////////////////////////////////////////////////
351  void InventoryHelper::takeSnapshots(const stdair::Inventory& iInventory,
352  const stdair::DateTime_T& iSnapshotTime) {
353  // Browse the guillotine block list and take the snapshots for
354  // each guillotine.
355  const stdair::GuillotineBlockList_T& lGuillotineBlockList =
356  stdair::BomManager::getList<stdair::GuillotineBlock> (iInventory);
357  for (stdair::GuillotineBlockList_T::const_iterator itGB =
358  lGuillotineBlockList.begin();
359  itGB != lGuillotineBlockList.end(); ++itGB) {
360  stdair::GuillotineBlock* lGuillotineBlock_ptr = *itGB;
361 
362  GuillotineBlockHelper::takeSnapshots(*lGuillotineBlock_ptr,iSnapshotTime);
363  }
364  }
365 }