$treeview $search $mathjax
AirInv Logo  1.00.1
$projectbrief
$projectbrief
$searchbox

FlightPeriodStruct.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 // StdAir
00008 #include <stdair/basic/BasConst_Period_BOM.hpp>
00009 #include <stdair/service/Logger.hpp>
00010 // AIRINV
00011 #include <airinv/AIRINV_Types.hpp>
00012 #include <airinv/bom/FlightPeriodStruct.hpp>
00013 
00014 namespace AIRINV {
00015 
00016   // ////////////////////////////////////////////////////////////////////
00017   FlightPeriodStruct::FlightPeriodStruct ()
00018     : _dateRange (stdair::BOOST_DEFAULT_DATE_PERIOD),
00019       _dow (stdair::DEFAULT_DOW_STRING),
00020       _legAlreadyDefined (false), _itSeconds (0) {
00021   }
00022 
00023   // ////////////////////////////////////////////////////////////////////
00024   stdair::Date_T FlightPeriodStruct::getDate() const {
00025     return stdair::Date_T (_itYear, _itMonth, _itDay);
00026   }
00027 
00028   // ////////////////////////////////////////////////////////////////////
00029   stdair::Duration_T FlightPeriodStruct::getTime() const {
00030     return boost::posix_time::hours (_itHours)
00031       + boost::posix_time::minutes (_itMinutes)
00032       + boost::posix_time::seconds (_itSeconds);
00033   }
00034   
00035   // ////////////////////////////////////////////////////////////////////
00036   const std::string FlightPeriodStruct::describe() const {
00037     std::ostringstream ostr;
00038     ostr << _airlineCode << _flightNumber << ", " << _dateRange
00039          << " - " << _dow << std::endl;
00040       
00041     for (LegStructList_T::const_iterator itLeg = _legList.begin();
00042          itLeg != _legList.end(); ++itLeg) {
00043       const LegStruct& lLeg = *itLeg;
00044       ostr << lLeg.describe();
00045     }
00046 
00047     for (SegmentStructList_T::const_iterator itSegment = _segmentList.begin();
00048          itSegment != _segmentList.end(); ++itSegment) {
00049       const SegmentStruct& lSegment = *itSegment;
00050       ostr << lSegment.describe();
00051     }
00052 
00053     //ostr << "[Debug] - Staging Leg: ";
00054     //ostr << _itLeg.describe();
00055     //ostr << "[Debug] - Staging Cabin: ";
00056     //ostr << _itCabin.describe();
00057 
00058     return ostr.str();
00059   }
00060 
00061   // ////////////////////////////////////////////////////////////////////
00062   void FlightPeriodStruct::addAirport (const stdair::AirportCode_T& iAirport) {
00063     AirportList_T::const_iterator itAirport = _airportList.find (iAirport);
00064     if (itAirport == _airportList.end()) {
00065       // Add the airport code to the airport set
00066       const bool insertSuccessful = _airportList.insert (iAirport).second;
00067 
00068       if (insertSuccessful == false) {
00069         // TODO: throw an exception
00070       }
00071           
00072       // Add the airport code to the airport vector
00073       _airportOrderedList.push_back (iAirport);
00074     }
00075   }
00076 
00077   // ////////////////////////////////////////////////////////////////////
00078   void FlightPeriodStruct::buildSegments () {
00079     // The list of airports encompasses all the airports on which
00080     // the flight takes off or lands. Moreover, that list is
00081     // time-ordered: the first airport is the initial departure of
00082     // the flight, and the last airport is the eventual point of
00083     // rest of the flight.
00084     // Be l the size of the ordered list of airports.
00085     // We want to generate all the segment combinations from the legs
00086     // and, hence, from all the possible (time-ordered) airport pairs.
00087     // Thus, we both iterator on i=0...l-1 and j=i+1...l
00088     assert (_airportOrderedList.size() >= 2);
00089 
00090     _segmentList.clear();
00091     for (AirportOrderedList_T::const_iterator itAirport_i =
00092            _airportOrderedList.begin();
00093          itAirport_i != _airportOrderedList.end()-1; ++itAirport_i) {
00094       for (AirportOrderedList_T::const_iterator itAirport_j = itAirport_i + 1;
00095            itAirport_j != _airportOrderedList.end(); ++itAirport_j) {
00096         SegmentStruct lSegmentStruct;
00097         lSegmentStruct._boardingPoint = *itAirport_i;
00098         lSegmentStruct._offPoint = *itAirport_j;
00099           
00100         _segmentList.push_back (lSegmentStruct);
00101       }
00102     }
00103 
00104     // Clear the lists of airports, so that it is ready for the next flight
00105     _airportList.clear();
00106     _airportOrderedList.clear();
00107   }
00108       
00109   // ////////////////////////////////////////////////////////////////////
00110   void FlightPeriodStruct::
00111   addSegmentCabin (const SegmentStruct& iSegment,
00112                    const SegmentCabinStruct& iCabin) {
00113     // Retrieve the Segment structure corresponding to the (boarding, off) point
00114     // pair.
00115     SegmentStructList_T::iterator itSegment = _segmentList.begin();
00116     for ( ; itSegment != _segmentList.end(); ++itSegment) {
00117       const SegmentStruct& lSegment = *itSegment;
00118 
00119       const stdair::AirportCode_T& lBoardingPoint = iSegment._boardingPoint;
00120       const stdair::AirportCode_T& lOffPoint = iSegment._offPoint;
00121       if (lSegment._boardingPoint == lBoardingPoint
00122           && lSegment._offPoint == lOffPoint) {
00123         break;
00124       }
00125     }
00126 
00127     // If the segment key (airport pair) given in the schedule input file
00128     // does not correspond to the leg (boarding, off) points, throw an exception
00129     // so that the user knows the schedule input file is corrupted.
00130     if (itSegment == _segmentList.end()) {
00131       STDAIR_LOG_ERROR ("Within the schedule input file, there is a "
00132                         << "flight for which the airports of segments "
00133                         << "and those of the legs do not correspond.");
00134       throw SegmentDateNotFoundException ("Within the schedule input file, "
00135                                           "there is a flight for which the "
00136                                           "airports of segments and those of "
00137                                           "the legs do not correspond.");
00138     }
00139 
00140     // Add the Cabin structure to the Segment Cabin structure.
00141     assert (itSegment != _segmentList.end());
00142     SegmentStruct& lSegment = *itSegment;
00143     lSegment._cabinList.push_back (iCabin);
00144   }
00145     
00146   // ////////////////////////////////////////////////////////////////////
00147   void FlightPeriodStruct::
00148   addSegmentCabin (const SegmentCabinStruct& iCabin) {
00149     // Iterate on all the Segment structures (as they get the same cabin
00150     // definitions)
00151     for (SegmentStructList_T::iterator itSegment = _segmentList.begin();
00152          itSegment != _segmentList.end(); ++itSegment) {
00153       SegmentStruct& lSegment = *itSegment;
00154 
00155       lSegment._cabinList.push_back (iCabin);
00156     }
00157   }
00158 
00159   // ////////////////////////////////////////////////////////////////////
00160   void FlightPeriodStruct::
00161   addFareFamily (const SegmentStruct& iSegment,
00162                  const SegmentCabinStruct& iCabin,
00163                  const FareFamilyStruct& iFareFamily) {
00164     // Retrieve the Segment structure corresponding to the (boarding, off) point
00165     // pair.
00166     SegmentStructList_T::iterator itSegment = _segmentList.begin();
00167     for ( ; itSegment != _segmentList.end(); ++itSegment) {
00168       const SegmentStruct& lSegment = *itSegment;
00169 
00170       const stdair::AirportCode_T& lBoardingPoint = iSegment._boardingPoint;
00171       const stdair::AirportCode_T& lOffPoint = iSegment._offPoint;
00172       if (lSegment._boardingPoint == lBoardingPoint
00173           && lSegment._offPoint == lOffPoint) {
00174         break;
00175       }
00176     }
00177 
00178     // If the segment key (airport pair) given in the schedule input file
00179     // does not correspond to the leg (boarding, off) points, throw an exception
00180     // so that the user knows the schedule input file is corrupted.
00181     if (itSegment == _segmentList.end()) {
00182       STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight "
00183                         << "for which the airports of segments and "
00184                         << "those of the legs do not correspond.");
00185       throw SegmentDateNotFoundException ("Within the schedule input file, "
00186                                           "there is a flight for which the "
00187                                           "airports of segments and those of "
00188                                           "the legs do not correspond.");
00189     }
00190 
00191     // Add the Cabin structure to the Segment Cabin structure.
00192     assert (itSegment != _segmentList.end());
00193     SegmentStruct& lSegment = *itSegment;
00194 
00195     // Retrieve the Segment cabin structure given the cabin code
00196     SegmentCabinStructList_T::iterator itCabin = lSegment._cabinList.begin();
00197     for ( ; itCabin != lSegment._cabinList.end(); ++itCabin) {
00198       const SegmentCabinStruct& lCabin = *itCabin;
00199 
00200       const stdair::CabinCode_T& lCabinCode = lCabin._cabinCode;
00201       if (iCabin._cabinCode == lCabinCode) {
00202         break;
00203       }
00204     }
00205 
00206     // If the segmentCabin key (cabin code) given in the schedule input file
00207     // does not correspond to the stored cabin codes, throw an exception
00208     // so that the user knows the schedule input file is corrupted.
00209     if (itCabin == lSegment._cabinList.end()) {
00210       STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight "
00211                         << "for which the cabin code does not exist.");
00212       throw SegmentDateNotFoundException ("Within the schedule input file, "
00213                                           "there is a flight for which the "
00214                                           "cabin code does not exist.");
00215     }
00216 
00217     // Add the Cabin structure to the Segment Cabin structure.
00218     assert (itCabin != lSegment._cabinList.end());
00219     SegmentCabinStruct& lCabin = *itCabin;
00220     lCabin._fareFamilies.push_back(iFareFamily);
00221   }
00222     
00223   // ////////////////////////////////////////////////////////////////////
00224   void FlightPeriodStruct::
00225   addFareFamily (const SegmentCabinStruct& iCabin,
00226                  const FareFamilyStruct& iFareFamily) {
00227     // Iterate on all the Segment structures (as they get the same cabin
00228     // definitions)
00229       
00230     for (SegmentStructList_T::iterator itSegment = _segmentList.begin();
00231          itSegment != _segmentList.end(); ++itSegment) {
00232       SegmentStruct& lSegment = *itSegment;
00233 
00234       // Retrieve the Segment cabin structure given the cabin code
00235       SegmentCabinStructList_T::iterator itCabin = lSegment._cabinList.begin();
00236       for ( ; itCabin != lSegment._cabinList.end(); ++itCabin) {
00237         const SegmentCabinStruct& lCabin = *itCabin;
00238 
00239         const stdair::CabinCode_T& lCabinCode = lCabin._cabinCode;
00240         if (iCabin._cabinCode == lCabinCode) {
00241           break;
00242         }
00243       }
00244 
00245       // If the segmentCabin key (cabin code) given in the schedule input file
00246       // does not correspond to the stored cabin codes, throw an exception
00247       // so that the user knows the schedule input file is corrupted.
00248       if (itCabin == lSegment._cabinList.end()) {
00249         STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight"
00250                           << " for which the cabin code does not exist.");
00251         throw SegmentDateNotFoundException ("Within the schedule input file, "
00252                                             "there is a flight for which the "
00253                                             "cabin code does not exist.");
00254       }
00255 
00256       // Add the Cabin structure to the Segment Cabin structure.
00257       assert (itCabin != lSegment._cabinList.end());
00258       SegmentCabinStruct& lCabin = *itCabin;
00259       lCabin._fareFamilies.push_back(iFareFamily);
00260     }
00261   }
00262 
00263 }