AirInv Logo  0.1.2
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InventoryGenerator.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // Boost
7 #include <boost/date_time/date_iterator.hpp>
8 // StdAir
9 #include <stdair/stdair_types.hpp>
10 #include <stdair/basic/BasConst_Inventory.hpp>
11 #include <stdair/bom/BomManager.hpp>
12 #include <stdair/bom/BomRoot.hpp>
13 #include <stdair/bom/Inventory.hpp>
14 #include <stdair/bom/FlightDate.hpp>
15 #include <stdair/bom/SegmentDate.hpp>
16 #include <stdair/bom/SegmentCabin.hpp>
17 #include <stdair/bom/FareFamily.hpp>
18 #include <stdair/bom/BookingClass.hpp>
19 #include <stdair/bom/LegDate.hpp>
20 #include <stdair/bom/LegCabin.hpp>
21 #include <stdair/bom/Bucket.hpp>
22 #include <stdair/factory/FacBomManager.hpp>
23 #include <stdair/service/Logger.hpp>
24 // AirInv
27 
28 namespace AIRINV {
29 
30  // ////////////////////////////////////////////////////////////////////
31  void InventoryGenerator::
32  createFlightDate (stdair::BomRoot& ioBomRoot,
33  const FlightPeriodStruct& iFlightPeriod) {
34  const stdair::AirlineCode_T& lAirlineCode = iFlightPeriod._airlineCode;
35 
36  // Instantiate an inventory object (if not exist)
37  // for the given key (airline code)
38  stdair::Inventory* lInventory_ptr = stdair::BomManager::
39  getObjectPtr<stdair::Inventory> (ioBomRoot, lAirlineCode);
40  if (lInventory_ptr == NULL) {
41  stdair::InventoryKey lKey (lAirlineCode);
42  lInventory_ptr =
43  &stdair::FacBom<stdair::Inventory>::instance().create (lKey);
44  stdair::FacBomManager::addToListAndMap (ioBomRoot, *lInventory_ptr);
45  stdair::FacBomManager::linkWithParent (ioBomRoot, *lInventory_ptr);
46  }
47  assert (lInventory_ptr != NULL);
48 
49  // Generate all the dates corresponding to the period
50  // and create the corresponding flight-dates.
51  const stdair::DatePeriod_T lDateRange = iFlightPeriod._dateRange;
52 
53  for (boost::gregorian::day_iterator itDate = lDateRange.begin();
54  itDate != lDateRange.end(); ++itDate) {
55  const stdair::Date_T& currentDate = *itDate;
56 
57  // Retrieve, for the current day, the Day-Of-the-Week (thanks to Boost)
58  const unsigned short currentDoW = currentDate.day_of_week().as_number();
59 
60  // The FlightPeriod structure stores which Days (-Of-the-Week) are
61  // active within the week. For each day (Mon., Tue., etc.), a boolean
62  // states whether the Flight is active for that day.
63  const stdair::DoWStruct& lDoWList = iFlightPeriod._dow;
64  const bool isDoWActive = lDoWList.getStandardDayOfWeek (currentDoW);
65 
66  if (isDoWActive == true) {
67  createFlightDate (*lInventory_ptr, currentDate, iFlightPeriod);
68  }
69  }
70  }
71 
72  // ////////////////////////////////////////////////////////////////////
73  void InventoryGenerator::
74  createFlightDate (stdair::Inventory& ioInventory,
75  const stdair::Date_T& iFlightDate,
76  const FlightPeriodStruct& iFlightPeriod) {
77  // Create the FlightDateKey
78  const stdair::FlightNumber_T& lFlightNumber = iFlightPeriod._flightNumber;
79  stdair::FlightDateKey lFlightDateKey (lFlightNumber, iFlightDate);
80 
81  // DEBUG
82  // STDAIR_LOG_DEBUG ("Creating flight-date: " << lFlightDateKey.toString());
83 
84  // Check that the flight-date object is not already existing. If a
85  // FlightDate object with the same key has already been created,
86  // it means that the schedule input file is invalid (two flight-periods
87  // are overlapping).
88  stdair::FlightDate* lFlightDate_ptr = stdair::BomManager::
89  getObjectPtr<stdair::FlightDate> (ioInventory, lFlightDateKey.toString());
90  if (lFlightDate_ptr != NULL) {
91  std::ostringstream oMessage;
92  oMessage << ioInventory.describeKey() << ", "
93  << lFlightDate_ptr->describeKey();
94  throw FlightDateDuplicationException (oMessage.str());
95  }
96 
97  // Instantiate a fligh-date object with the given key (flight number and
98  // flight date)
99  lFlightDate_ptr =
100  &stdair::FacBom<stdair::FlightDate>::instance().create (lFlightDateKey);
101  stdair::FacBomManager::addToListAndMap (ioInventory, *lFlightDate_ptr);
102  stdair::FacBomManager::linkWithParent (ioInventory, *lFlightDate_ptr);
103 
104  // Iterate on the leg-dates
105  stdair::Duration_T currentOffTime (0, 0, 0);
106  stdair::AirportCode_T previousOffPoint;
107  const LegStructList_T& lLegList = iFlightPeriod._legList;
108  for (LegStructList_T::const_iterator itLeg = lLegList.begin();
109  itLeg != lLegList.end(); ++itLeg) {
110  const LegStruct& lLeg = *itLeg;
111 
112  // Create the leg-branch of the flight-date BOM
113  stdair::LegDate& lLegDate =
114  createLegDate (*lFlightDate_ptr, iFlightDate, lLeg);
115 
116  // TODO: Check that the boarding date/time of the next leg is greated
117  // than the off date/time of the current leg. Throw an exception
118  // otherwise.
119 
120  // TODO: specify, in the schedule input file specifications, that the
121  // legs should be given in their natural order.
122  // Then, replace the assertion by a thrown exception.
123  //
124  // Check that the legs are given in their natural order. If the schedule
125  // input does not respect that assumption, the following assertion will
126  // fail.
127  if (itLeg != lLegList.begin()) {
128  const stdair::AirportCode_T& currentBoardingPoint =
129  lLegDate.getBoardingPoint();
130  assert (currentBoardingPoint == previousOffPoint);
131  }
132 
133  // Set the local variable for the next iteration
134  previousOffPoint = lLegDate.getOffPoint();
135  }
136 
137  // Iterate on the segment structures
138  const SegmentStructList_T& lSegmentList = iFlightPeriod._segmentList;
139  for (SegmentStructList_T::const_iterator itSegment = lSegmentList.begin();
140  itSegment != lSegmentList.end(); ++itSegment) {
141  const SegmentStruct& lSegment = *itSegment;
142 
143  createSegmentDate (*lFlightDate_ptr, lSegment);
144  }
145  }
146 
147  // ////////////////////////////////////////////////////////////////////
148  stdair::LegDate& InventoryGenerator::
149  createLegDate (stdair::FlightDate& ioFlightDate,
150  const stdair::Date_T& iReferenceDate,
151  const LegStruct& iLeg) {
152  // Create the leg-date corresponding to the boarding point.
153  stdair::LegDateKey lKey (iLeg._boardingPoint);
154  stdair::LegDate& lLegDate =
155  stdair::FacBom<stdair::LegDate>::instance().create (lKey);
156  stdair::FacBomManager::addToListAndMap (ioFlightDate, lLegDate);
157  stdair::FacBomManager::linkWithParent (ioFlightDate, lLegDate);
158 
159  // Set the leg-date attributes
160  iLeg.fill (iReferenceDate, lLegDate);
161 
162  // Iterate on the cabins
163  const LegCabinStructList_T& lCabinList = iLeg._cabinList;
164  for (LegCabinStructList_T::const_iterator itCabin = lCabinList.begin();
165  itCabin != lCabinList.end(); ++itCabin) {
166  const LegCabinStruct& lCabin = *itCabin;
167 
168  // Create the leg-cabin-branch of the leg-date
169  createLegCabin (lLegDate, lCabin);
170  }
171 
172  return lLegDate;
173  }
174 
175  // ////////////////////////////////////////////////////////////////////
176  void InventoryGenerator::
177  createLegCabin (stdair::LegDate& ioLegDate,
178  const LegCabinStruct& iCabin) {
179  // Instantiate an leg-cabin object with the corresponding cabin code
180  const stdair::LegCabinKey lKey (iCabin._cabinCode);
181  stdair::LegCabin& lLegCabin =
182  stdair::FacBom<stdair::LegCabin>::instance().create (lKey);
183  stdair::FacBomManager::addToListAndMap (ioLegDate, lLegCabin);
184  stdair::FacBomManager::linkWithParent (ioLegDate, lLegCabin);
185 
186  // Set the Leg-Cabin attributes
187  iCabin.fill (lLegCabin);
188 
189  // Iterate on the bucket
190  const BucketStructList_T& lBucketList = iCabin._bucketList;
191  for (BucketStructList_T::const_iterator itBucket = lBucketList.begin();
192  itBucket != lBucketList.end(); ++itBucket) {
193  const BucketStruct& lBucket = *itBucket;
194 
195  // Create the bucket of the leg-cabin
196  createBucket (lLegCabin, lBucket);
197  }
198  }
199 
200  // ////////////////////////////////////////////////////////////////////
201  void InventoryGenerator::createBucket (stdair::LegCabin& ioLegCabin,
202  const BucketStruct& iBucket) {
203  // Instantiate a bucket object with the corresponding seat index
204  const stdair::BucketKey lKey (iBucket._seatIndex);
205  stdair::Bucket& lBucket =
206  stdair::FacBom<stdair::Bucket>::instance().create (lKey);
207  stdair::FacBomManager::addToListAndMap (ioLegCabin, lBucket);
208  stdair::FacBomManager::linkWithParent (ioLegCabin, lBucket);
209 
210  // Set the Bucket attributes
211  iBucket.fill (lBucket);
212  }
213 
214  // ////////////////////////////////////////////////////////////////////
215  void InventoryGenerator::
216  createSegmentDate (stdair::FlightDate& ioFlightDate,
217  const SegmentStruct& iSegment) {
218  // Set the segment-date primary key
219  const stdair::AirportCode_T& lBoardingPoint = iSegment._boardingPoint;
220  const stdair::AirportCode_T& lOffPoint = iSegment._offPoint;
221  stdair::SegmentDateKey lSegmentDateKey (lBoardingPoint, lOffPoint);
222  // Instantiate an segment-date object with the key.
223  stdair::SegmentDate& lSegmentDate =
224  stdair::FacBom<stdair::SegmentDate>::instance().create (lSegmentDateKey);
225  stdair::FacBomManager::addToListAndMap (ioFlightDate, lSegmentDate);
226  stdair::FacBomManager::linkWithParent (ioFlightDate, lSegmentDate);
227 
228  // Set the segment-date attributes
229  iSegment.fill (lSegmentDate);
230 
231  // Iterate on the Cabins
232  const SegmentCabinStructList_T& lCabinList = iSegment._cabinList;
233  for (SegmentCabinStructList_T::const_iterator itCabin =
234  lCabinList.begin(); itCabin != lCabinList.end(); ++itCabin) {
235  const SegmentCabinStruct& lCabin = *itCabin;
236 
237  // Create the segment-cabin-branch of the segment-date BOM
238  createSegmentCabin (lSegmentDate, lCabin);
239  }
240  }
241 
242  // ////////////////////////////////////////////////////////////////////
243  void InventoryGenerator::
244  createSegmentCabin (stdair::SegmentDate& ioSegmentDate,
245  const SegmentCabinStruct& iCabin) {
246 
247  // Instantiate an segment-cabin object with the corresponding cabin code
248  stdair::SegmentCabinKey lKey (iCabin._cabinCode);
249  stdair::SegmentCabin& lSegmentCabin =
250  stdair::FacBom<stdair::SegmentCabin>::instance().create (lKey);
251 
252  // Link the segment-cabin to its parent, the segment-date
253  stdair::FacBomManager::addToListAndMap (ioSegmentDate, lSegmentCabin);
254  stdair::FacBomManager::linkWithParent (ioSegmentDate, lSegmentCabin);
255 
256  // Set the segment-cabin attributes
257  iCabin.fill (lSegmentCabin);
258 
259  // Create the list of fare families
260  for (FareFamilyStructList_T::const_iterator itFareFamily =
261  iCabin._fareFamilies.begin();
262  itFareFamily != iCabin._fareFamilies.end(); itFareFamily++) {
263  const FareFamilyStruct& lFareFamilyStruct = *itFareFamily;
264 
265  //
266  createFareFamily (lSegmentCabin, lFareFamilyStruct);
267  }
268  }
269 
270  // ////////////////////////////////////////////////////////////////////
271  void InventoryGenerator::
272  createFareFamily (stdair::SegmentCabin& ioSegmentCabin,
273  const FareFamilyStruct& iFF) {
274  // Instantiate an segment-cabin object with the corresponding cabin code
275  stdair::FareFamilyKey lKey (iFF._familyCode);
276  stdair::FareFamily& lFareFamily =
277  stdair::FacBom<stdair::FareFamily>::instance().create (lKey);
278 
279  // Link the fare family to its parent, the segment-cabin
280  stdair::FacBomManager::addToListAndMap (ioSegmentCabin,
281  lFareFamily);
282  stdair::FacBomManager::linkWithParent (ioSegmentCabin,
283  lFareFamily);
284 
285  // Set the fare family attributes
286  iFF.fill (lFareFamily);
287 
288  // Iterate on the classes
289  const stdair::ClassList_String_T& lClassList = iFF._classes;
290  for (stdair::ClassList_String_T::const_iterator itClass =
291  lClassList.begin(); itClass != lClassList.end(); ++itClass) {
292  // Transform the single-character class code into a STL string
293  std::ostringstream ostr;
294  ostr << *itClass;
295  const stdair::ClassCode_T lClassCode (ostr.str());
296 
297  // Create the booking class branch of the segment-cabin BOM
298  createClass (lFareFamily, lClassCode);
299  }
300  }
301 
302  // ////////////////////////////////////////////////////////////////////
303  void InventoryGenerator::createClass (stdair::FareFamily& ioFareFamily,
304  const stdair::ClassCode_T& iClassCode) {
305 
306  // Instantiate a booking class object with the given class code
307  const stdair::BookingClassKey lClassKey (iClassCode);
308  stdair::BookingClass& lClass =
309  stdair::FacBom<stdair::BookingClass>::instance().create (lClassKey);
310 
311  // Link the booking-class to the fare family
312  stdair::FacBomManager::addToListAndMap (ioFareFamily, lClass);
313  stdair::FacBomManager::linkWithParent (ioFareFamily, lClass);
314 
315  // Link the booking-class to the segment-cabin
316  stdair::SegmentCabin& lSegmentCabin =
317  stdair::BomManager::getParent<stdair::SegmentCabin> (ioFareFamily);
318  stdair::FacBomManager::addToListAndMap (lSegmentCabin, lClass);
319 
320  // Link the booking-class to the segment-date
321  stdair::SegmentDate& lSegmentDate =
322  stdair::BomManager::getParent<stdair::SegmentDate> (lSegmentCabin);
323  stdair::FacBomManager::addToListAndMap (lSegmentDate, lClass);
324  }
325 }