AirInv Logo  1.00.0
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SegmentSnapshotTableHelper.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <cmath>
7 // StdAir
8 #include <stdair/basic/BasConst_Inventory.hpp>
9 #include <stdair/bom/BomRetriever.hpp>
10 #include <stdair/bom/BomManager.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/SegmentSnapshotTable.hpp>
16 #include <stdair/service/Logger.hpp>
17 // AirInv
22 
23 namespace AIRINV {
24 
25  // ////////////////////////////////////////////////////////////////////
27  takeSnapshots (stdair::SegmentSnapshotTable& ioSegmentSnapshotTable,
28  const stdair::DateTime_T& iSnapshotTime) {
29  // Retrieve the segment-cabin index and take the snapshots for
30  // each segment-cabin.
31  const stdair::SegmentCabinIndexMap_T& lSegmentCabinIndexMap =
32  ioSegmentSnapshotTable.getSegmentCabinIndexMap();
33  for (stdair::SegmentCabinIndexMap_T::const_iterator itSCIdx =
34  lSegmentCabinIndexMap.begin();
35  itSCIdx != lSegmentCabinIndexMap.end(); ++itSCIdx) {
36  const stdair::SegmentCabin* lSC_ptr = itSCIdx->first;
37  assert (lSC_ptr != NULL);
38  const stdair::SegmentDataID_T& lSCIdx = itSCIdx->second;
39 
40  const stdair::Date_T& lSnapshotDate = iSnapshotTime.date();
41 
42  // Compare the date of the snapshot time and the departure date of
43  // the segment-cabin in order to verify the necescity of taking snapshots.
44  const stdair::SegmentDate& lSegmentDate =
45  stdair::BomManager::getParent<stdair::SegmentDate> (*lSC_ptr);
46  const stdair::Date_T& lDepartureDate = lSegmentDate.getBoardingDate();
47  const stdair::DateOffset_T lDateOffset = lDepartureDate - lSnapshotDate;
48  const stdair::DTD_T lDTD = lDateOffset.days() + 1;
49 
50  if (lDTD >= 0 && lDTD <= stdair::DEFAULT_MAX_DTD) {
52  takeSnapshots (ioSegmentSnapshotTable, lDTD, *lSC_ptr, lSCIdx);
53  registerProductAndPriceOrientedBookings (ioSegmentSnapshotTable,
54  lDTD, *lSC_ptr, lSCIdx);
55  }
56  }
57  }
58 
59  // ////////////////////////////////////////////////////////////////////
61  takeSnapshots (stdair::SegmentSnapshotTable& ioSegmentSnapshotTable,
62  const stdair::DTD_T& iDTD,
63  const stdair::SegmentCabin& iSegmentCabin,
64  const stdair::SegmentDataID_T iSegmentCabinIdx) {
65 
66  // Extract the views for the corresponding DTD and segment-cabin.
67  stdair::SegmentCabinDTDSnapshotView_T lBookingView = ioSegmentSnapshotTable.
68  getSegmentCabinDTDBookingSnapshotView (iSegmentCabinIdx,
69  iSegmentCabinIdx, iDTD);
70  stdair::SegmentCabinDTDSnapshotView_T lCancellationView = ioSegmentSnapshotTable.
71  getSegmentCabinDTDCancellationSnapshotView (iSegmentCabinIdx,
72  iSegmentCabinIdx, iDTD);
73  stdair::SegmentCabinDTDSnapshotView_T lAvailabilityView = ioSegmentSnapshotTable.
74  getSegmentCabinDTDAvailabilitySnapshotView (iSegmentCabinIdx,
75  iSegmentCabinIdx, iDTD);
76 
77  // Retrieve the block index of the segment-cabin.
78  std::ostringstream lSCMapKey;
79  lSCMapKey << stdair::DEFAULT_SEGMENT_CABIN_VALUE_TYPE
80  << iSegmentCabin.describeKey();
81  const stdair::ClassIndex_T& lCabinIdx =
82  ioSegmentSnapshotTable.getClassIndex (lSCMapKey.str());
83  lAvailabilityView[lCabinIdx] = iSegmentCabin.getAvailabilityPool();
84  lBookingView[lCabinIdx] = iSegmentCabin.getCommittedSpace();
85 
86 
87  // Browse the booking class list
88  const stdair::BookingClassList_T& lBCList =
89  stdair::BomManager::getList<stdair::BookingClass> (iSegmentCabin);
90  for (stdair::BookingClassList_T::const_iterator itBC = lBCList.begin();
91  itBC != lBCList.end(); ++itBC) {
92  const stdair::BookingClass* lBookingClass_ptr = *itBC;
93  assert (lBookingClass_ptr != NULL);
94 
95  // Retrieve the block index of the booking class.
96  const stdair::ClassIndex_T& lIdx =
97  ioSegmentSnapshotTable.getClassIndex (lBookingClass_ptr->describeKey());
98 
99  // DEBUG
100  // STDAIR_LOG_DEBUG ("Taking snapshot for "
101  // << iSegmentCabin.describeKey() << ", "
102  // << lBookingClass_ptr->describeKey()
103  // << ", DTD: " << iDTD << ", nb of bookings: "
104  // << lBookingClass_ptr->getNbOfBookings());
105 
106  // Write the snapshot.
107  lBookingView[lIdx]=lBookingClass_ptr->getNbOfBookings();
108  lCancellationView[lIdx] =
109  lBookingClass_ptr->getNbOfCancellations();
110  lAvailabilityView[lIdx] =
111  lBookingClass_ptr->getSegmentAvailability();
112  }
113  }
114 
115  // ////////////////////////////////////////////////////////////////////
116  void SegmentSnapshotTableHelper::registerProductAndPriceOrientedBookings
117  (stdair::SegmentSnapshotTable& ioSegmentSnapshotTable, const stdair::DTD_T& iDTD,
118  const stdair::SegmentCabin& iSegmentCabin,
119  const stdair::SegmentDataID_T iSegmentCabinIdx) {
120 
121  // Extract the views for the corresponding DTD and segment-cabin.
122  stdair::SegmentCabinDTDRangeSnapshotView_T lRangeBookingView =
123  ioSegmentSnapshotTable.getSegmentCabinDTDRangeBookingSnapshotView (iSegmentCabinIdx, iSegmentCabinIdx, iDTD, iDTD + 1);
124  stdair::SegmentCabinDTDRangeSnapshotView_T lRangeCancellationView =
125  ioSegmentSnapshotTable.getSegmentCabinDTDRangeCancellationSnapshotView (iSegmentCabinIdx, iSegmentCabinIdx, iDTD, iDTD + 1);
126  stdair::SegmentCabinDTDSnapshotView_T lProductOrientedGrossBookingView =
127  ioSegmentSnapshotTable.getSegmentCabinDTDProductOrientedGrossBookingSnapshotView (iSegmentCabinIdx, iSegmentCabinIdx, iDTD);
128  stdair::SegmentCabinDTDSnapshotView_T lPriceOrientedGrossBookingView =
129  ioSegmentSnapshotTable.getSegmentCabinDTDPriceOrientedGrossBookingSnapshotView (iSegmentCabinIdx, iSegmentCabinIdx, iDTD);
130  stdair::SegmentCabinDTDSnapshotView_T lProductOrientedNetBookingView =
131  ioSegmentSnapshotTable.getSegmentCabinDTDProductOrientedNetBookingSnapshotView (iSegmentCabinIdx, iSegmentCabinIdx, iDTD);
132  stdair::SegmentCabinDTDSnapshotView_T lPriceOrientedNetBookingView =
133  ioSegmentSnapshotTable.getSegmentCabinDTDPriceOrientedNetBookingSnapshotView (iSegmentCabinIdx, iSegmentCabinIdx, iDTD);
134 
135  // Retrieve the lowest yield and the lowest class and treat the number of gross
136  // bookings of this class the price oriented bookings.
137  const stdair::BookingClassList_T& lBCList =
138  stdair::BomManager::getList<stdair::BookingClass> (iSegmentCabin);
139  stdair::BookingClassList_T::const_iterator iterBC = lBCList.begin();
140  assert (iterBC != lBCList.end());
141  stdair::BookingClass* lLowestClass_ptr = *iterBC;
142  assert (lLowestClass_ptr != NULL);
143  stdair::Yield_T lLowestYield = lLowestClass_ptr->getYield();
144  for (; iterBC != lBCList.end(); iterBC++) {
145  const stdair::BookingClass* lBookingClass_ptr = *iterBC;
146  assert (lBookingClass_ptr != NULL);
147  const stdair::Yield_T& lYield = lBookingClass_ptr->getYield();
148  if (lYield < lLowestYield) {
149  lLowestYield = lYield;
150  lLowestClass_ptr = *iterBC;
151  }
152  }
153  assert (lLowestClass_ptr != NULL);
154 
155  // Retrieve the block index of the booking class.
156  const stdair::ClassIndex_T& lClassIdx =
157  ioSegmentSnapshotTable.getClassIndex (lLowestClass_ptr->describeKey());
158 
159  // Compute the number of gross bookings for this class.
160  const stdair::NbOfBookings_T lNbOfNetBkgs =
161  lRangeBookingView[lClassIdx][0] - lRangeBookingView[lClassIdx][1];
162  const stdair::NbOfCancellations_T lNbOfCx =
163  lRangeCancellationView[lClassIdx][0]-lRangeCancellationView[lClassIdx][1];
164  const stdair::NbOfBookings_T lNbOfGrossBkgs = lNbOfNetBkgs + lNbOfCx;
165 
166  // Write these numbern of bookings to the price-oriented value.
167  lPriceOrientedGrossBookingView[lClassIdx] = lNbOfGrossBkgs;
168  lPriceOrientedNetBookingView[lClassIdx] = lNbOfNetBkgs;
169 
170  // Boolean for "no lower class available" verification.
171  bool noLowerClassAvl = true;
172  if (lLowestClass_ptr->getSegmentAvailability() >= 1.0) {
173  noLowerClassAvl = false;
174  }
175 
176  // Browse the booking class list
177  stdair::BookingClassList_T::const_reverse_iterator itBC = lBCList.rbegin();
178  for (; itBC != lBCList.rend(); itBC++) {
179  const stdair::BookingClass* lBookingClass_ptr = *itBC;
180  assert (lBookingClass_ptr != NULL);
181 
182  // Retrieve the yield of the this class.
183  const stdair::Yield_T& lYield = lBookingClass_ptr->getYield();
184  assert (lYield >= lLowestYield);
185  if (lYield > lLowestYield) {
186 
187  // Retrieve the block index of the booking class.
188  const stdair::ClassIndex_T& lIdx =
189  ioSegmentSnapshotTable.getClassIndex (lBookingClass_ptr->describeKey());
190 
191  // Compute the number of gross bookings for this class.
192  const stdair::NbOfBookings_T lNetBkgs =
193  lRangeBookingView[lIdx][0] - lRangeBookingView[lIdx][1];
194  const stdair::NbOfCancellations_T lCx =
195  lRangeCancellationView[lIdx][0] - lRangeCancellationView[lIdx][1];
196  const stdair::NbOfBookings_T lGrossBkgs = lNetBkgs + lCx;
197 
198  // If there is a lower class available, these gross bookings
199  // will be considered product-oriented. Otherwise, they will be
200  // considered price-oriented
201  if (noLowerClassAvl == false) {
202  lProductOrientedGrossBookingView[lIdx] = lGrossBkgs;
203  lProductOrientedNetBookingView[lIdx] = lNetBkgs;
204  } else {
205  lPriceOrientedGrossBookingView[lIdx] = lGrossBkgs;
206  lPriceOrientedNetBookingView[lIdx] = lNetBkgs;
207 
208  if (lBookingClass_ptr->getSegmentAvailability() >= 1.0) {
209  noLowerClassAvl = false;
210  }
211  }
212  }
213  }
214  }
215 
216 }