AirInv Logo  1.00.11
C++ Simulated Airline Inventory Management System Library
Loading...
Searching...
No Matches
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/SegmentSnapshotTable.hpp>
16#include <stdair/bom/TravelSolutionStruct.hpp>
17#include <stdair/service/Logger.hpp>
18#include <stdair/bom/LegCabin.hpp>
19// AirInv
24
25namespace 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 // Create the map of class/object ID for the given segment date.
52 stdair::ClassObjectIDMap_T lClassObjectIDMap;
53
54 // DEBUG
55 STDAIR_LOG_DEBUG (iFullSegmentDateKey);
56 //
57 stdair::SegmentDate* lSegmentDate_ptr =
58 stdair::BomRetriever::retrieveSegmentDateFromLongKey(iInventory,
59 iFullSegmentDateKey);
60 assert (lSegmentDate_ptr != NULL);
61
62 // Browse the segment-cabins and fill the map with the availability of
63 // each booking class.
64 const stdair::SegmentCabinList_T& lSegmentCabinList =
65 stdair::BomManager::getList<stdair::SegmentCabin> (*lSegmentDate_ptr);
66 for (stdair::SegmentCabinList_T::const_iterator itCabin =
67 lSegmentCabinList.begin();
68 itCabin != lSegmentCabinList.end(); ++itCabin) {
69 stdair::SegmentCabin* lSegmentCabin_ptr = *itCabin;
70 assert (lSegmentCabin_ptr != NULL);
71
72
73 // Compute the availability using the AU and the cumulative
74 // booking counter.
75 // SegmentCabinHelper::updateAvailabilities (*lSegmentCabin_ptr);
76 const stdair::BookingClassList_T& lBCList =
77 stdair::BomManager::getList<stdair::BookingClass> (*lSegmentCabin_ptr);
78 for (stdair::BookingClassList_T::const_reverse_iterator itBC =
79 lBCList.rbegin(); itBC != lBCList.rend(); ++itBC) {
80 stdair::BookingClass* lBC_ptr = *itBC;
81 assert (lBC_ptr != NULL);
82
83 const stdair::Availability_T lAvl = lBC_ptr->getSegmentAvailability();
84
85 const stdair::ClassCode_T& lClassCode = lBC_ptr->getClassCode();
86 bool insertSuccessful = lClassAvailabilityMap.
87 insert (stdair::ClassAvailabilityMap_T::value_type (lClassCode,
88 lAvl)).second;
89 assert (insertSuccessful == true);
90
91 stdair::BookingClassID_T lBCID (*lBC_ptr);
92 insertSuccessful = lClassObjectIDMap.
93 insert (stdair::ClassObjectIDMap_T::value_type (lClassCode,
94 lBCID)).second;
95 assert (insertSuccessful == true);
96 }
97 }
98
99 //
100 ioTravelSolution.addClassAvailabilityMap (lClassAvailabilityMap);
101 ioTravelSolution.addClassObjectIDMap (lClassObjectIDMap);
102 }
103
104
105 // ////////////////////////////////////////////////////////////////////
107 getYieldAndBidPrice (const stdair::Inventory& iInventory,
108 const std::string& iFullSegmentDateKey,
109 stdair::TravelSolutionStruct& ioTravelSolution) {
110
111 // Create the map of class/availability for the given segment date.
112 // stdair::ClassAvailabilityMap_T lClassAvailabilityMap;
113
114 stdair::ClassYieldMap_T lClassYieldMap;
115
116 stdair::ClassBpvMap_T lClassBpvMap;
117
118 // DEBUG
119 STDAIR_LOG_DEBUG (iFullSegmentDateKey);
120 //
121 stdair::SegmentDate* lSegmentDate_ptr =
122 stdair::BomRetriever::retrieveSegmentDateFromLongKey (iInventory,
123 iFullSegmentDateKey);
124 assert (lSegmentDate_ptr != NULL);
125
126 // Browse the segment-cabins and fill the maps with the bid price vector reference
127 // and yield of each booking class.
128 const stdair::SegmentCabinList_T& lSegmentCabinList =
129 stdair::BomManager::getList<stdair::SegmentCabin> (*lSegmentDate_ptr);
130 for (stdair::SegmentCabinList_T::const_iterator itCabin =
131 lSegmentCabinList.begin();
132 itCabin != lSegmentCabinList.end(); ++itCabin) {
133 stdair::SegmentCabin* lSegmentCabin_ptr = *itCabin;
134 assert (lSegmentCabin_ptr != NULL);
135
136 stdair::BidPriceVector_T lBPV;
137
138
139 //stdair::BidPriceVector_T lBPV;
140 stdair::LegCabinList_T lLegCabinList =
141 stdair::BomManager::getList<stdair::LegCabin> (*lSegmentCabin_ptr);
142 assert (!lLegCabinList.empty());
143 if (lLegCabinList.size() > 1) {
144 // Compute the sum of bid prices and return a vector containing that value.
145 stdair::BidPrice_T lBidPriceValue = 0;
146 for (stdair::LegCabinList_T::const_iterator itLC = lLegCabinList.begin();
147 itLC != lLegCabinList.end(); ++itLC) {
148 const stdair::LegCabin* lLegCabin_ptr = *itLC;
149 const stdair::BidPriceVector_T& lLegCabinBPV = lLegCabin_ptr->getBidPriceVector();
150 if (!lLegCabinBPV.empty()) {
151 lBidPriceValue += lLegCabinBPV.back();
152 } else {
153 // If the remaining capacity is zero (empty bid price vector) on one of the legs,
154 // then the remaining capacity of the segment is also zero (return an empty bid price).
155 lBidPriceValue = std::numeric_limits<stdair::BidPrice_T>::max();
156 break;
157 }
158 }
159 if (lBidPriceValue < std::numeric_limits<stdair::BidPrice_T>::max()) {
160 lBPV.push_back(lBidPriceValue);
161 }
162
163 } else {
164 const stdair::LegCabin* lLegCabin_ptr = lLegCabinList.front();
165 lBPV = lLegCabin_ptr->getBidPriceVector();
166 }
167
168
169 // const stdair::CabinCapacity_T& lCabinCapacity = lSegmentCabin_ptr->getCapacity();
170 // const stdair::CommittedSpace_T& lCommittedSpace = lSegmentCabin_ptr->getCommittedSpace();
171 // assert (lCabinCapacity - lCommittedSpace > 0);
172 // lBPV.resize(lCabinCapacity - lCommittedSpace);
173
174 const stdair::Availability_T& lAvailabilityPool =
175 lSegmentCabin_ptr->getAvailabilityPool();
176 //assert (lAvailabilityPool > 0);
177
178 if (lAvailabilityPool < lBPV.size()) {
179 lBPV.resize(lAvailabilityPool);
180 }
181
182
183 //
184 ioTravelSolution.addBidPriceVector (lBPV);
185
186 const stdair::BidPriceVectorHolder_T& lBidPriceVectorHolder =
187 ioTravelSolution.getBidPriceVectorHolder();
188 const stdair::BidPriceVectorHolder_T::const_reverse_iterator itBPV =
189 lBidPriceVectorHolder.rbegin();
190 const stdair::BidPriceVector_T& lBpvRef = *itBPV;
191
192 const stdair::FareFamilyList_T& lFFList =
193 stdair::BomManager::getList<stdair::FareFamily> (*lSegmentCabin_ptr);
194 for (stdair::FareFamilyList_T::const_iterator itFF = lFFList.begin();
195 itFF != lFFList.end(); ++itFF) {
196 const stdair::FareFamily* lFareFamily_ptr = *itFF;
197 assert (lFareFamily_ptr != NULL);
198
199 const stdair::BookingClassList_T& lBCList =
200 stdair::BomManager::getList<stdair::BookingClass> (*lFareFamily_ptr);
201 for (stdair::BookingClassList_T::const_iterator itBC = lBCList.begin();
202 itBC != lBCList.end(); ++itBC) {
203 const stdair::BookingClass* lBC_ptr = *itBC;
204 assert (lBC_ptr != NULL);
205
206 const stdair::ClassCode_T& lClassCode = lBC_ptr->getClassCode();
207
208 const stdair::YieldValue_T lYld = lBC_ptr->getYield() ;
209 const bool insertYieldMapSuccessful = lClassYieldMap.
210 insert (stdair::ClassYieldMap_T::value_type (lClassCode,
211 lYld)).second;
212 assert (insertYieldMapSuccessful == true);
213
214 const bool insertBpvMapSuccessful = lClassBpvMap.
215 insert (stdair::ClassBpvMap_T::value_type (lClassCode,
216 &lBpvRef)).second;
217 assert (insertBpvMapSuccessful == true);
218
219 // DEBUG
220 // STDAIR_LOG_DEBUG ("Class: " << lClassCode
221 // << ", " << "Yield: " << lYld << ", "
222 // << "Bid price: " << lBpvRef.back() << ", "
223 // << "Remaining capacity: "
224 // << lCabinCapacity - lCommittedSpace);
225
226 //
227 stdair::BidPrice_T lBpvVal = std::numeric_limits<double>::max();
228 if (lBpvRef.empty() == false) {
229 lBpvVal = lBpvRef.back();
230 }
231
232 //lBpvVal = boost::lexical_cast<std::string> (lBpvRef.back());
233 STDAIR_LOG_DEBUG ("Class: " << lClassCode
234 << ", " << "Yield: " << lYld << ", "
235 << "Bid price: " << lBpvVal << ", "
236 << "Remaining capacity: " << lAvailabilityPool
237 << " Segment date: " << iFullSegmentDateKey);
238 }
239 }
240 }
241
242 //
243 ioTravelSolution.addClassYieldMap (lClassYieldMap);
244 ioTravelSolution.addClassBpvMap (lClassBpvMap);
245 }
246
247 // ////////////////////////////////////////////////////////////////////
248 bool InventoryHelper::sell (stdair::Inventory& ioInventory,
249 const std::string& iFullSegmentDateKey,
250 const stdair::ClassCode_T& iClassCode,
251 const stdair::PartySize_T& iPartySize) {
252 bool hasSaleBeenSuccessful = false;
253
254 // DEBUG
255 STDAIR_LOG_DEBUG ("Full key: '" << iFullSegmentDateKey
256 << "', " << iClassCode);
257
258 //
259 stdair::BookingClass* lBookingClass_ptr =
260 stdair::BomRetriever::retrieveBookingClassFromLongKey(ioInventory,
261 iFullSegmentDateKey,
262 iClassCode);
263
264 // DEBUG
265 const std::string hasFoundBookingClassStr =
266 (lBookingClass_ptr != NULL)?"Yes":"No";
267 STDAIR_LOG_DEBUG ("Found booking class? " << hasFoundBookingClassStr);
268
269 if (lBookingClass_ptr != NULL) {
270
271 // Register the sale in the class.
272 hasSaleBeenSuccessful =
273 InventoryHelper::sell (*lBookingClass_ptr, iPartySize);
274
275 return hasSaleBeenSuccessful;
276 }
277
278 return hasSaleBeenSuccessful;
279 }
280
281 // ////////////////////////////////////////////////////////////////////
282 bool InventoryHelper::sell (const stdair::BookingClassID_T& iClassID,
283 const stdair::PartySize_T& iPartySize) {
284
285 //
286 stdair::BookingClass& lBookingClass = iClassID.getObject();
287
288 // Register the sale in the class.
289 const bool hasSaleBeenSuccessful =
290 InventoryHelper::sell (lBookingClass, iPartySize);
291
292 return hasSaleBeenSuccessful;
293 }
294
295 // ////////////////////////////////////////////////////////////////////
296 bool InventoryHelper::sell (stdair::BookingClass& iBookingClass,
297 const stdair::PartySize_T& iPartySize) {
298
299 // Register the sale in the class.
300 iBookingClass.sell (iPartySize);
301
302 //
303 stdair::FareFamily& lFareFamily =
304 stdair::BomManager::getParent<stdair::FareFamily> (iBookingClass);
305
306 //
307 stdair::SegmentCabin& lSegmentCabin =
308 stdair::BomManager::getParent<stdair::SegmentCabin> (lFareFamily);
309
310 //
311 stdair::SegmentDate& lSegmentDate =
312 stdair::BomManager::getParent<stdair::SegmentDate,
313 stdair::SegmentCabin> (lSegmentCabin);
314
315 //
316 stdair::FlightDate& lFlightDate =
317 stdair::BomManager::getParent<stdair::FlightDate,
318 stdair::SegmentDate> (lSegmentDate);
319
320 // Update the commited space of the segment-cabins and the leg-cabins.
321 SegmentCabinHelper::updateFromReservation (lFlightDate, lSegmentCabin,
322 iPartySize);
323
324 return true;
325 }
326
327 // ////////////////////////////////////////////////////////////////////
328 bool InventoryHelper::cancel (stdair::Inventory& ioInventory,
329 const std::string& iFullSegmentDateKey,
330 const stdair::ClassCode_T& iClassCode,
331 const stdair::PartySize_T& iPartySize) {
332 bool hasCancellationBeenSuccessful = false;
333
334 // DEBUG
335 STDAIR_LOG_DEBUG ("Full key: '" << iFullSegmentDateKey
336 << "', " << iClassCode);
337
338 //
339 stdair::BookingClass* lBookingClass_ptr =
340 stdair::BomRetriever::retrieveBookingClassFromLongKey(ioInventory,
341 iFullSegmentDateKey,
342 iClassCode);
343
344 // DEBUG
345 const std::string hasFoundBookingClassStr =
346 (lBookingClass_ptr != NULL)?"Yes":"No";
347 STDAIR_LOG_DEBUG ("Found booking class? " << hasFoundBookingClassStr);
348
349 if (lBookingClass_ptr != NULL) {
350
351 // Register the cancellation in the class.
352 hasCancellationBeenSuccessful =
353 cancel (*lBookingClass_ptr, iPartySize);
354
355 return hasCancellationBeenSuccessful;
356 }
357
358 return hasCancellationBeenSuccessful;
359 }
360
361 // ////////////////////////////////////////////////////////////////////
362 bool InventoryHelper::cancel (const stdair::BookingClassID_T& iClassID,
363 const stdair::PartySize_T& iPartySize) {
364
365 stdair::BookingClass& lBookingClass = iClassID.getObject();
366
367 // Register the cancellation in the class.
368 const bool hasCancellationBeenSuccessful =
369 cancel (lBookingClass, iPartySize);
370
371 return hasCancellationBeenSuccessful;
372 }
373
374 // ////////////////////////////////////////////////////////////////////
375 bool InventoryHelper::cancel (stdair::BookingClass& iBookingClass,
376 const stdair::PartySize_T& iPartySize) {
377
378 // Register the cancellation in the class.
379 iBookingClass.cancel (iPartySize);
380
381 //
382 stdair::FareFamily& lFareFamily =
383 stdair::BomManager::getParent<stdair::FareFamily> (iBookingClass);
384
385 //
386 stdair::SegmentCabin& lSegmentCabin =
387 stdair::BomManager::getParent<stdair::SegmentCabin> (lFareFamily);
388
389 //
390 stdair::SegmentDate& lSegmentDate =
391 stdair::BomManager::getParent<stdair::SegmentDate,
392 stdair::SegmentCabin> (lSegmentCabin);
393
394 //
395 stdair::FlightDate& lFlightDate =
396 stdair::BomManager::getParent<stdair::FlightDate,
397 stdair::SegmentDate> (lSegmentDate);
398
399 // Update the commited space of the segment-cabins and the leg-cabins.
400 SegmentCabinHelper::updateFromReservation (lFlightDate, lSegmentCabin,
401 -iPartySize);
402
403 return true;
404 }
405
406 // ////////////////////////////////////////////////////////////////////
407 void InventoryHelper::takeSnapshots(const stdair::Inventory& iInventory,
408 const stdair::DateTime_T& iSnapshotTime) {
409 // Browse the guillotine block list and take the snapshots for
410 // each guillotine.
411 const stdair::SegmentSnapshotTableList_T& lSegmentSnapshotTableList =
412 stdair::BomManager::getList<stdair::SegmentSnapshotTable> (iInventory);
413 for (stdair::SegmentSnapshotTableList_T::const_iterator itGB =
414 lSegmentSnapshotTableList.begin();
415 itGB != lSegmentSnapshotTableList.end(); ++itGB) {
416 stdair::SegmentSnapshotTable* lSegmentSnapshotTable_ptr = *itGB;
417
418 SegmentSnapshotTableHelper::takeSnapshots(*lSegmentSnapshotTable_ptr,
419 iSnapshotTime);
420 }
421 }
422}
static void fillFromRouting(const stdair::FlightDate &)
static void fillFromRouting(const stdair::Inventory &)
static bool sell(stdair::Inventory &, const std::string &iSegmentDateKey, const stdair::ClassCode_T &, const stdair::PartySize_T &)
static void calculateAvailability(const stdair::Inventory &, const std::string &, stdair::TravelSolutionStruct &)
static void getYieldAndBidPrice(const stdair::Inventory &, const std::string &, stdair::TravelSolutionStruct &)
static bool cancel(stdair::Inventory &, const std::string &iSegmentDateKey, const stdair::ClassCode_T &, const stdair::PartySize_T &)
static void takeSnapshots(const stdair::Inventory &, const stdair::DateTime_T &)
static void updateFromReservation(const stdair::FlightDate &, stdair::SegmentCabin &, const stdair::PartySize_T &)
static void takeSnapshots(stdair::SegmentSnapshotTable &, const stdair::DateTime_T &)