AirInv Logo  0.1.2
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
InventoryHelper.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 // StdAir
00007 #include <stdair/bom/BomRetriever.hpp>
00008 #include <stdair/bom/BomManager.hpp>
00009 #include <stdair/bom/Inventory.hpp>
00010 #include <stdair/bom/FlightDate.hpp>
00011 #include <stdair/bom/SegmentDate.hpp>
00012 #include <stdair/bom/SegmentCabin.hpp>
00013 #include <stdair/bom/FareFamily.hpp>
00014 #include <stdair/bom/BookingClass.hpp>
00015 #include <stdair/bom/GuillotineBlock.hpp>
00016 #include <stdair/bom/TravelSolutionStruct.hpp>
00017 #include <stdair/service/Logger.hpp>
00018 #include <stdair/bom/LegCabin.hpp>
00019 // AirInv
00020 #include <airinv/bom/InventoryHelper.hpp>
00021 #include <airinv/bom/FlightDateHelper.hpp>
00022 #include <airinv/bom/GuillotineBlockHelper.hpp>
00023 #include <airinv/bom/SegmentCabinHelper.hpp>
00024 
00025 namespace AIRINV {
00026 
00027   // ////////////////////////////////////////////////////////////////////
00028   void InventoryHelper::fillFromRouting (const stdair::Inventory& iInventory) {
00029     const stdair::FlightDateList_T& lFlightDateList =
00030       stdair::BomManager::getList<stdair::FlightDate> (iInventory);
00031 
00032     // Browse the list of flight-dates and update each flight-date.
00033     for (stdair::FlightDateList_T::const_iterator itFlightDate =
00034            lFlightDateList.begin();
00035          itFlightDate != lFlightDateList.end(); ++itFlightDate) {
00036       const stdair::FlightDate* lCurrentFlightDate_ptr = *itFlightDate;
00037       assert (lCurrentFlightDate_ptr != NULL);
00038       FlightDateHelper::fillFromRouting (*lCurrentFlightDate_ptr);
00039     }
00040   }
00041 
00042   // ////////////////////////////////////////////////////////////////////
00043   void InventoryHelper::
00044   calculateAvailability (const stdair::Inventory& iInventory, 
00045                          const std::string& iFullSegmentDateKey,
00046                          stdair::TravelSolutionStruct& ioTravelSolution) {
00047 
00048     // Create the map of class/availability for the given segment date.
00049     stdair::ClassAvailabilityMap_T lClassAvailabilityMap;
00050  
00051     // DEBUG
00052     STDAIR_LOG_DEBUG (iFullSegmentDateKey);
00053     //
00054     stdair::SegmentDate* lSegmentDate_ptr =
00055       stdair::BomRetriever::retrieveSegmentDateFromLongKey(iInventory,
00056                                                            iFullSegmentDateKey);
00057     assert (lSegmentDate_ptr != NULL);
00058     
00059     // Browse the segment-cabins and fill the map with the availability of
00060     // each booking class.
00061     const stdair::SegmentCabinList_T& lSegmentCabinList =
00062       stdair::BomManager::getList<stdair::SegmentCabin> (*lSegmentDate_ptr);
00063     for (stdair::SegmentCabinList_T::const_iterator itCabin =
00064            lSegmentCabinList.begin();
00065          itCabin != lSegmentCabinList.end(); ++itCabin) {
00066       stdair::SegmentCabin* lSegmentCabin_ptr = *itCabin;
00067       assert (lSegmentCabin_ptr != NULL);
00068 
00069 
00070       // Compute the availability using the AU and the cumulative
00071       // booking counter.
00072       SegmentCabinHelper::updateAvailabilities (*lSegmentCabin_ptr);
00073       const stdair::BookingClassList_T& lBCList =
00074         stdair::BomManager::getList<stdair::BookingClass> (*lSegmentCabin_ptr);
00075       for (stdair::BookingClassList_T::const_reverse_iterator itBC =
00076            lBCList.rbegin(); itBC != lBCList.rend(); ++itBC) {
00077         stdair::BookingClass* lBC_ptr = *itBC;
00078         assert (lBC_ptr != NULL);
00079         
00080         const stdair::Availability_T lAvl = lBC_ptr->getSegmentAvailability();
00081         
00082         const stdair::ClassCode_T& lClassCode = lBC_ptr->getClassCode();        
00083         const bool insertSuccessful = lClassAvailabilityMap.
00084           insert (stdair::ClassAvailabilityMap_T::value_type (lClassCode,
00085                                                               lAvl)).second;
00086         assert (insertSuccessful == true);
00087       }
00088     }
00089 
00090     //
00091     ioTravelSolution.addClassAvailabilityMap (lClassAvailabilityMap);
00092   }
00093 
00094   
00095   // ////////////////////////////////////////////////////////////////////
00096   void InventoryHelper::
00097   getYieldAndBidPrice (const stdair::Inventory& iInventory, 
00098                          const std::string& iFullSegmentDateKey,
00099                          stdair::TravelSolutionStruct& ioTravelSolution) {
00100 
00101     // Create the map of class/availability for the given segment date.
00102     // stdair::ClassAvailabilityMap_T lClassAvailabilityMap;
00103     
00104     stdair::ClassYieldMap_T lClassYieldMap; 
00105 
00106     stdair::ClassBpvMap_T lClassBpvMap; 
00107  
00108     // DEBUG
00109     STDAIR_LOG_DEBUG (iFullSegmentDateKey);
00110     //
00111     stdair::SegmentDate* lSegmentDate_ptr =
00112       stdair::BomRetriever::retrieveSegmentDateFromLongKey (iInventory,
00113                                                             iFullSegmentDateKey);
00114     assert (lSegmentDate_ptr != NULL);
00115     
00116     // Browse the segment-cabins and fill the maps with the bid price vector reference
00117     // and yield of each booking class.
00118     const stdair::SegmentCabinList_T& lSegmentCabinList =
00119       stdair::BomManager::getList<stdair::SegmentCabin> (*lSegmentDate_ptr);
00120     for (stdair::SegmentCabinList_T::const_iterator itCabin =
00121            lSegmentCabinList.begin();
00122          itCabin != lSegmentCabinList.end(); ++itCabin) {
00123       stdair::SegmentCabin* lSegmentCabin_ptr = *itCabin;
00124       assert (lSegmentCabin_ptr != NULL);
00125 
00126       stdair::BidPriceVector_T lBPV;
00127       
00128       
00129       //stdair::BidPriceVector_T lBPV;
00130       stdair::LegCabinList_T lLegCabinList =
00131         stdair::BomManager::getList<stdair::LegCabin> (*lSegmentCabin_ptr);
00132       assert (!lLegCabinList.empty());
00133       if (lLegCabinList.size() > 1) {
00134         // Compute the sum of bid prices and return a vector containing that value.
00135         stdair::BidPrice_T lBidPriceValue = 0;
00136         for (stdair::LegCabinList_T::const_iterator itLC = lLegCabinList.begin();
00137              itLC != lLegCabinList.end(); ++itLC) {
00138           const stdair::LegCabin* lLegCabin_ptr = *itLC;
00139           const stdair::BidPriceVector_T& lLegCabinBPV = lLegCabin_ptr->getBidPriceVector();
00140           if (!lLegCabinBPV.empty()) {
00141             lBidPriceValue += lLegCabinBPV.back();
00142           } else {
00143             // If the remaining capacity is zero (empty bid price vector) on one of the legs,
00144             // then the remaining capacity of the segment is also zero (return an empty bid price).
00145             lBidPriceValue = std::numeric_limits<stdair::BidPrice_T>::max();
00146             break;
00147           }
00148         }
00149         if (lBidPriceValue < std::numeric_limits<stdair::BidPrice_T>::max()) {
00150           lBPV.push_back(lBidPriceValue);
00151         }
00152 
00153       } else {
00154         const stdair::LegCabin* lLegCabin_ptr = lLegCabinList.front();
00155         lBPV = lLegCabin_ptr->getBidPriceVector();
00156       }
00157       
00158       
00159       // const stdair::CabinCapacity_T& lCabinCapacity = lSegmentCabin_ptr->getCapacity();
00160       // const stdair::CommittedSpace_T& lCommittedSpace = lSegmentCabin_ptr->getCommittedSpace();
00161       // assert (lCabinCapacity - lCommittedSpace > 0);
00162       // lBPV.resize(lCabinCapacity - lCommittedSpace);
00163 
00164       const stdair::Availability_T& lAvailabilityPool =
00165         lSegmentCabin_ptr->getAvailabilityPool();
00166       //assert (lAvailabilityPool > 0);
00167 
00168       if (lAvailabilityPool < lBPV.size()) {
00169         lBPV.resize(lAvailabilityPool);
00170       }
00171 
00172       
00173       //
00174       ioTravelSolution.addBidPriceVector (lBPV);
00175 
00176       const stdair::BidPriceVectorHolder_T& lBidPriceVectorHolder =
00177         ioTravelSolution.getBidPriceVectorHolder();
00178       const stdair::BidPriceVectorHolder_T::const_reverse_iterator itBPV =
00179         lBidPriceVectorHolder.rbegin();
00180       const stdair::BidPriceVector_T& lBpvRef = *itBPV;
00181  
00182       const stdair::FareFamilyList_T& lFFList =
00183         stdair::BomManager::getList<stdair::FareFamily> (*lSegmentCabin_ptr);
00184       for (stdair::FareFamilyList_T::const_iterator itFF = lFFList.begin();
00185            itFF != lFFList.end(); ++itFF) {
00186         const stdair::FareFamily* lFareFamily_ptr = *itFF;
00187         assert (lFareFamily_ptr != NULL);
00188 
00189         const stdair::BookingClassList_T& lBCList =
00190           stdair::BomManager::getList<stdair::BookingClass> (*lFareFamily_ptr);
00191         for (stdair::BookingClassList_T::const_iterator itBC = lBCList.begin();
00192              itBC != lBCList.end(); ++itBC) {
00193           const stdair::BookingClass* lBC_ptr = *itBC;
00194           assert (lBC_ptr != NULL);
00195           
00196           const stdair::ClassCode_T& lClassCode = lBC_ptr->getClassCode();
00197 
00198           const stdair::YieldValue_T lYld = lBC_ptr->getYield() ;  
00199           const bool insertYieldMapSuccessful = lClassYieldMap.
00200             insert (stdair::ClassYieldMap_T::value_type (lClassCode,
00201                                                          lYld)).second;
00202           assert (insertYieldMapSuccessful == true);
00203 
00204           const bool insertBpvMapSuccessful = lClassBpvMap.
00205             insert (stdair::ClassBpvMap_T::value_type (lClassCode,
00206                                                        &lBpvRef)).second;
00207           assert (insertBpvMapSuccessful == true);
00208           
00209           // DEBUG
00210           // STDAIR_LOG_DEBUG ("Class: " << lClassCode
00211           //                   << ", " << "Yield: " << lYld << ", "
00212           //                   << "Bid price: " << lBpvRef.back() << ", "
00213           //                   << "Remaining capacity: "
00214           //                   << lCabinCapacity - lCommittedSpace);
00215   
00216           //
00217           stdair::BidPrice_T lBpvVal = std::numeric_limits<double>::max();
00218           if (lBpvRef.empty() == false) {
00219             lBpvVal = lBpvRef.back();
00220           }
00221 
00222           //lBpvVal = boost::lexical_cast<std::string> (lBpvRef.back());
00223           STDAIR_LOG_DEBUG ("Class: " << lClassCode
00224                             << ", " << "Yield: " << lYld << ", "
00225                             << "Bid price: " << lBpvVal << ", "
00226                             << "Remaining capacity: " << lAvailabilityPool
00227                             << " Segment date: " << iFullSegmentDateKey);
00228         }
00229       }
00230     }
00231 
00232     //
00233     ioTravelSolution.addClassYieldMap (lClassYieldMap);
00234     ioTravelSolution.addClassBpvMap (lClassBpvMap);
00235   }
00236   
00237 
00238   // ////////////////////////////////////////////////////////////////////
00239   bool InventoryHelper::sell (stdair::Inventory& ioInventory, 
00240                               const std::string& iFullSegmentDateKey,
00241                               const stdair::ClassCode_T& iClassCode,
00242                               const stdair::PartySize_T& iPartySize) {
00243     bool hasSaleBeenSuccessful = false;
00244 
00245     // DEBUG
00246     STDAIR_LOG_DEBUG ("Full key: '" << iFullSegmentDateKey
00247                       << "', " << iClassCode);
00248 
00249     //
00250     stdair::BookingClass* lBookingClass_ptr =
00251       stdair::BomRetriever::retrieveBookingClassFromLongKey(ioInventory,
00252                                                             iFullSegmentDateKey,
00253                                                             iClassCode);
00254 
00255     // DEBUG
00256     const std::string hasFoundBookingClassStr =
00257       (lBookingClass_ptr != NULL)?"Yes":"No";
00258     STDAIR_LOG_DEBUG ("Found booking class? " << hasFoundBookingClassStr);
00259 
00260     if (lBookingClass_ptr != NULL) {
00261       // Register the sale in the class.
00262       lBookingClass_ptr->sell (iPartySize);
00263 
00264       //
00265       stdair::FareFamily& lFareFamily =
00266         stdair::BomManager::getParent<stdair::FareFamily> (*lBookingClass_ptr);
00267 
00268       //
00269       stdair::SegmentCabin& lSegmentCabin =
00270         stdair::BomManager::getParent<stdair::SegmentCabin> (lFareFamily);
00271 
00272       //
00273       stdair::SegmentDate& lSegmentDate =
00274         stdair::BomManager::getParent<stdair::SegmentDate,
00275                                       stdair::SegmentCabin> (lSegmentCabin);
00276 
00277       //
00278       stdair::FlightDate& lFlightDate =
00279         stdair::BomManager::getParent<stdair::FlightDate,
00280                                       stdair::SegmentDate> (lSegmentDate);
00281       
00282       // Update the commited space of the segment-cabins and the leg-cabins.
00283       SegmentCabinHelper::updateFromReservation (lFlightDate, lSegmentCabin,
00284                                                  iPartySize);
00285       
00286       // STDAIR_LOG_NOTIFICATION (lFlightDate.getDepartureDate()
00287       //                          << ";" << iClassCode);
00288       hasSaleBeenSuccessful = true;
00289     }
00290 
00291     return hasSaleBeenSuccessful;
00292   }  
00293 
00294   // ////////////////////////////////////////////////////////////////////
00295   bool InventoryHelper::cancel (stdair::Inventory& ioInventory, 
00296                               const std::string& iFullSegmentDateKey,
00297                               const stdair::ClassCode_T& iClassCode,
00298                               const stdair::PartySize_T& iPartySize) {
00299     bool hasCancellationBeenSuccessful = false;
00300 
00301     // DEBUG
00302     STDAIR_LOG_DEBUG ("Full key: '" << iFullSegmentDateKey
00303                       << "', " << iClassCode);
00304 
00305     //
00306     stdair::BookingClass* lBookingClass_ptr =
00307       stdair::BomRetriever::retrieveBookingClassFromLongKey(ioInventory,
00308                                                             iFullSegmentDateKey,
00309                                                             iClassCode);
00310 
00311     // DEBUG
00312     const std::string hasFoundBookingClassStr =
00313       (lBookingClass_ptr != NULL)?"Yes":"No";
00314     STDAIR_LOG_DEBUG ("Found booking class? " << hasFoundBookingClassStr);
00315 
00316     if (lBookingClass_ptr != NULL) {
00317       // Register the cancellation in the class.
00318       lBookingClass_ptr->cancel (iPartySize);
00319 
00320       //
00321       stdair::FareFamily& lFareFamily =
00322         stdair::BomManager::getParent<stdair::FareFamily> (*lBookingClass_ptr);
00323 
00324       //
00325       stdair::SegmentCabin& lSegmentCabin =
00326         stdair::BomManager::getParent<stdair::SegmentCabin> (lFareFamily);
00327 
00328       //
00329       stdair::SegmentDate& lSegmentDate =
00330         stdair::BomManager::getParent<stdair::SegmentDate,
00331                                       stdair::SegmentCabin> (lSegmentCabin);
00332 
00333       //
00334       stdair::FlightDate& lFlightDate =
00335         stdair::BomManager::getParent<stdair::FlightDate,
00336                                       stdair::SegmentDate> (lSegmentDate);
00337       
00338       // Update the commited space of the segment-cabins and the leg-cabins.
00339       SegmentCabinHelper::updateFromReservation (lFlightDate, lSegmentCabin,
00340                                                  -iPartySize);
00341       
00342       // STDAIR_LOG_NOTIFICATION (lFlightDate.getDepartureDate()
00343       //                          << ";" << iClassCode);
00344       hasCancellationBeenSuccessful = true;
00345     }
00346 
00347     return hasCancellationBeenSuccessful;
00348   }
00349 
00350   // ////////////////////////////////////////////////////////////////////
00351   void InventoryHelper::takeSnapshots(const stdair::Inventory& iInventory,
00352                                       const stdair::DateTime_T& iSnapshotTime) {
00353     // Browse the guillotine block list and take the snapshots for
00354     // each guillotine.
00355     const stdair::GuillotineBlockList_T& lGuillotineBlockList =
00356       stdair::BomManager::getList<stdair::GuillotineBlock> (iInventory);
00357     for (stdair::GuillotineBlockList_T::const_iterator itGB =
00358            lGuillotineBlockList.begin();
00359          itGB != lGuillotineBlockList.end(); ++itGB) {
00360       stdair::GuillotineBlock* lGuillotineBlock_ptr = *itGB;
00361 
00362       GuillotineBlockHelper::takeSnapshots(*lGuillotineBlock_ptr,iSnapshotTime);
00363     }    
00364   }
00365 }