RMOL Logo  1.00.0
C++ library of Revenue Management and Optimisation classes and functions
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
InventoryParser.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <sstream>
6 #include <fstream>
7 #include <cassert>
8 // StdAir
9 #include <stdair/stdair_inventory_types.hpp>
10 #include <stdair/stdair_maths_types.hpp>
11 #include <stdair/stdair_exceptions.hpp>
12 #include <stdair/basic/BasConst_DefaultObject.hpp>
13 #include <stdair/basic/BasConst_Inventory.hpp>
14 #include <stdair/basic/BasFileMgr.hpp>
15 #include <stdair/bom/BomRetriever.hpp>
16 #include <stdair/bom/BomManager.hpp>
17 #include <stdair/bom/BomRoot.hpp>
18 #include <stdair/bom/Inventory.hpp>
19 #include <stdair/bom/FlightDate.hpp>
20 #include <stdair/bom/SegmentDate.hpp>
21 #include <stdair/bom/SegmentCabin.hpp>
22 #include <stdair/bom/LegDate.hpp>
23 #include <stdair/bom/LegCabin.hpp>
24 #include <stdair/bom/BookingClass.hpp>
25 #include <stdair/bom/VirtualClassStruct.hpp>
26 #include <stdair/factory/FacBom.hpp>
27 #include <stdair/factory/FacBomManager.hpp>
28 #include <stdair/service/Logger.hpp>
29 // RMOL
31 
32 namespace RMOL {
33 
34  // ////////////////////////////////////////////////////////////////////
36  parseInputFileAndBuildBom (const std::string& iInputFileName,
37  stdair::BomRoot& ioBomRoot) {
38  bool hasReadBeenSuccessful = false;
39 
40  // Check that the file path given as input corresponds to an actual file
41  const bool doesExistAndIsReadable =
42  stdair::BasFileMgr::doesExistAndIsReadable (iInputFileName);
43  if (doesExistAndIsReadable == false) {
44  std::ostringstream oMessage;
45  oMessage << "The input file, '" << iInputFileName
46  << "', can not be retrieved on the file-system";
47  throw stdair::FileNotFoundException (oMessage.str());
48  }
49 
50  // Retrieve the (sample) leg-cabin
51  stdair::LegCabin& lLegCabin =
52  stdair::BomRetriever::retrieveDummyLegCabin (ioBomRoot);
53 
54  // Retrieve the (sample) segment-cabin
55  stdair::SegmentCabin& lSegmentCabin =
56  stdair::BomRetriever::retrieveDummySegmentCabin (ioBomRoot);
57 
58  // Open the input file
59  std::ifstream inputFile (iInputFileName.c_str());
60  if (! inputFile) {
61  STDAIR_LOG_ERROR ("Can not open input file '" << iInputFileName << "'");
62  throw new stdair::FileNotFoundException ("Can not open input file '"
63  + iInputFileName + "'");
64  }
65 
66  char buffer[80];
67  double dval;
68  short i = 1;
69  bool hasAllPArams = true;
70  stdair::Yield_T lYield;
71  stdair::MeanValue_T lMean;
72  stdair::StdDevValue_T lStdDev;
73  stdair::BookingClassKey lBCKey (stdair::DEFAULT_CLASS_CODE);
74 
75  while (inputFile.getline (buffer, sizeof (buffer), ';')) {
76  std::istringstream iStringStr (buffer);
77 
78  if (i == 1) {
79  hasAllPArams = true;
80  }
81 
82  if (iStringStr >> dval) {
83  if (i == 1) {
84  lYield = dval;
85  // std::cout << "Yield[" << i << "] = '" << dval << "'" << std::endl;
86 
87  } else if (i == 2) {
88  lMean = dval;
89  // std::cout << "Mean[" << i << "] = '" << dval << "'" << std::endl;
90 
91  } else if (i == 3) {
92  lStdDev = dval;
93  //std::cout << "stdDev[" << i << "] = '" << dval << "'" << std::endl;
94  i = 0;
95  }
96  i++;
97 
98  } else {
99  hasAllPArams = false;
100  }
101 
102  if (hasAllPArams && i == 1) {
103  stdair::BookingClass& lBookingClass =
104  stdair::FacBom<stdair::BookingClass>::instance().create (lBCKey);
105  stdair::FacBomManager::addToList (lSegmentCabin, lBookingClass);
106  lBookingClass.setYield (lYield);
107  lBookingClass.setMean (lMean);
108  lBookingClass.setStdDev (lStdDev);
109  stdair::BookingClassList_T lBookingClassList;
110  lBookingClassList.push_back(&lBookingClass);
111  stdair::VirtualClassStruct lVirtualClass (lBookingClassList);
112  lVirtualClass.setYield (lYield);
113  lVirtualClass.setMean (lMean);
114  lVirtualClass.setStdDev (lStdDev);
115  lLegCabin.addVirtualClass (lVirtualClass);
116  }
117  }
118 
119  //
120  if (!inputFile.eof()) {
121  STDAIR_LOG_ERROR ("Problem when reading input file '" << iInputFileName
122  << "'");
123  return hasReadBeenSuccessful;
124  }
125 
126  //
127  hasReadBeenSuccessful = true;
128  return hasReadBeenSuccessful;
129  }
130 
131 }