StdAir Logo  1.00.0
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConfigHolderStruct.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 #if BOOST_VERSION >= 104100
8 #include <boost/property_tree/ptree.hpp>
9 #include <boost/property_tree/json_parser.hpp>
10 #include <boost/foreach.hpp>
11 #endif // BOOST_VERSION >= 104100
12 // StdAir
23 
24 namespace stdair {
25 
26  // ////////////////////////////////////////////////////////////////////
28  }
29 
30  // ////////////////////////////////////////////////////////////////////
32  ConfigHolderStruct (const ConfigHolderStruct& iConfigHolderStruct)
33  : _pt (iConfigHolderStruct._pt) {
34  }
35 
36  // ////////////////////////////////////////////////////////////////////
38  }
39 
40  // ////////////////////////////////////////////////////////////////////
41  void ConfigHolderStruct::toStream (std::ostream& ioOut) const {
42  ioOut << describe();
43  }
44 
45  // ////////////////////////////////////////////////////////////////////
46  void ConfigHolderStruct::fromStream (std::istream& ioIn) {
47  }
48 
49  // ////////////////////////////////////////////////////////////////////
50  const std::string ConfigHolderStruct::describe() const {
51  std::ostringstream oStr;
52  oStr << "Configuration Display:" << std::endl;
53 
54  // Look for the start and end date values.
55  stdair::Date_T lStartDate;
56  const bool hasStartDateBeenRetrieved =
57  exportValue<Date_T> (lStartDate, "date.start");
58  if (hasStartDateBeenRetrieved == true) {
59  oStr << " Start date: " << lStartDate << std::endl;
60  }
61  stdair::Date_T lEndDate;
62  const bool hasEndDateBeenRetrieved =
63  exportValue<Date_T> (lEndDate, "date.end");
64  if (hasEndDateBeenRetrieved == true) {
65  oStr << " End date: " << lEndDate << std::endl;
66  }
67 
68  // Look for the random seed value.
69  RandomSeed_T lRandomSeed;
70  const bool hasSeedBeenRetrieved =
71  exportValue<RandomSeed_T> (lRandomSeed, "random.seed");
72  if (hasSeedBeenRetrieved == true) {
73  oStr << " Random Seed: " << lRandomSeed << std::endl;
74  }
75 
76  // Look for the demand generation method.
77  char lChar;
78  const bool hasDemandGenMethodBeenRetrieved =
79  exportValue<char> (lChar, "demand generation.method");
80  if (hasDemandGenMethodBeenRetrieved == true) {
81  oStr << " Demand Generation method: " << lChar << std::endl;
82  }
83 
84  // Look for the number of runs value.
85  Count_T lTotalNumberOfRuns;
86  const bool hasNumberOfRunsBeenRetrieved =
87  exportValue<Count_T> (lTotalNumberOfRuns, "runs.number");
88  if (hasNumberOfRunsBeenRetrieved == true) {
89  oStr << " Number Of Runs: " << lTotalNumberOfRuns << std::endl;
90  }
91 
92  // Look for the input files.
93  stdair::Filename_T lFilename ("");
94  const bool hasScheduleFileBeenRetrieved =
95  exportValue<stdair::Filename_T> (lFilename, "input.schedule");
96  if (hasScheduleFileBeenRetrieved == true) {
97  oStr << " Schedule input file: " << lFilename << std::endl;
98  }
99  const bool hasODFileBeenRetrieved =
100  exportValue<stdair::Filename_T> (lFilename, "input.ond");
101  if (hasODFileBeenRetrieved == true) {
102  oStr << " OnD input file: " << lFilename << std::endl;
103  }
104  const bool hasFrat5FileBeenRetrieved =
105  exportValue<stdair::Filename_T> (lFilename, "input.frat5");
106  if (hasFrat5FileBeenRetrieved == true) {
107  oStr << " Frat5 input file: " << lFilename << std::endl;
108  }
109  const bool hasFFdisutilityFileBeenRetrieved =
110  exportValue<stdair::Filename_T> (lFilename, "input.ffdisutility");
111  if (hasFFdisutilityFileBeenRetrieved == true) {
112  oStr << " FFdisutility input file: " << lFilename << std::endl;
113  }
114  const bool hasYieldFileBeenRetrieved =
115  exportValue<stdair::Filename_T> (lFilename, "input.yield");
116  if (hasYieldFileBeenRetrieved == true) {
117  oStr << " Yield input file: " << lFilename << std::endl;
118  }
119  const bool hasFareFileBeenRetrieved =
120  exportValue<stdair::Filename_T> (lFilename, "input.fare");
121  if (hasFareFileBeenRetrieved == true) {
122  oStr << " Fare input file: " << lFilename << std::endl;
123  }
124  const bool hasDemandFileBeenRetrieved =
125  exportValue<stdair::Filename_T> (lFilename, "input.demand");
126  if (hasDemandFileBeenRetrieved == true) {
127  oStr << " Demand input file: " << lFilename << std::endl;
128  }
129 
130  return oStr.str();
131  }
132 
133  // ////////////////////////////////////////////////////////////////////
134  const std::string ConfigHolderStruct::jsonExport() const {
135  std::ostringstream oStr;
136 #if BOOST_VERSION >= 104100
137  // Write the property tree into the JSON stream.
138  write_json (oStr, _pt);
139 #endif // BOOST_VERSION >= 104100
140  return oStr.str();
141  }
142 
143  // ////////////////////////////////////////////////////////////////////
144  void ConfigHolderStruct::add (const bpt::ptree& iConfigPropertyTree) {
145  // Call the dedicated recursive method with an empty path in order to merge
146  // the config property tree with the given new one.
147  std::string lEmptyPath ("");
148  add (iConfigPropertyTree, lEmptyPath);
149  }
150 
151  // ////////////////////////////////////////////////////////////////////
152  void ConfigHolderStruct::add (const bpt::ptree& iConfigPropertyTree,
153  const std::string& iPath) {
154 
155  // Are there any more children to browse?
156  bool isThereAnyChild = false;
157 
158 #if BOOST_VERSION >= 104100
159 
160  // Browse the children nodes
161  BOOST_FOREACH(bpt::ptree::value_type itChild, iConfigPropertyTree) {
162 
163  isThereAnyChild = true;
164 
165  // Build the current path
166  std::ostringstream lCurrentPathStr;
167  const bool isPathEmptyForNow = iPath.empty();
168  if (isPathEmptyForNow == false) {
169  lCurrentPathStr << iPath << ".";
170  }
171  // Add the current node name
172  lCurrentPathStr << itChild.first.data();
173  const std::string lCurrentPath (lCurrentPathStr.str());
174 
175  // Get the child tree
176  const bpt::ptree& lChildTree = itChild.second;
177  add(lChildTree, lCurrentPath);
178  }
179 
180  // If there is no child for this node, create the specified path and add
181  // the correponding value
182  if (isThereAnyChild == false) {
183  std::string lValue (iConfigPropertyTree.data());
184  const bool hasInsertionBeenSuccessful = addValue (lValue, iPath);
185  assert (hasInsertionBeenSuccessful == true);
186  }
187 #endif // BOOST_VERSION >= 104100
188  }
189 
190  // ////////////////////////////////////////////////////////////////////
191  bool ConfigHolderStruct::addValue (const std::string& iValue,
192  const std::string& iPath) {
193  bool hasInsertionBeenSuccessful = true;
194  // Create the given specified path and add the corresponding given value,
195  // or replace the value if the path already exists.
196 #if BOOST_VERSION >= 104100
197 
198  try {
199  std::size_t found;
200  const std::string lPrefix ("config");
201  std::string lFinalPath;
202  found = iPath.find(lPrefix);
203  if (found == std::string::npos) {
204  lFinalPath += lPrefix;
205  lFinalPath += ".";
206  }
207  lFinalPath += iPath;
208  if (lFinalPath != lPrefix) {
209  _pt.put (lFinalPath, iValue);
210  }
211  } catch (bpt::ptree_bad_data& bptException) {
212  hasInsertionBeenSuccessful = false;
213  }
214 #endif // BOOST_VERSION >= 104100
215 
216  return hasInsertionBeenSuccessful;
217  }
218 
219  // ////////////////////////////////////////////////////////////////////
221 
222  AirlineCode_T lAirlineCode ("");
223 
224  // Browse the children nodes
225  BOOST_FOREACH(bpt::ptree::value_type itChild, _pt) {
226  std::ostringstream lPathStr;
227  lPathStr << itChild.first.data() << ".airline_code";
228  const bool hasAirlineCodeBeenRetrieved =
229  exportValue<AirlineCode_T> (lAirlineCode , lPathStr.str());
230  if (hasAirlineCodeBeenRetrieved == true) {
231  AirlineFeature* lAirlineFeature_ptr =
232  BomRetriever::retrieveAirlineFeatureFromKey (iBomRoot, lAirlineCode);
233  if (lAirlineFeature_ptr != NULL) {
234 
235  try {
236 
237  std::ostringstream lPathStr;
238  char lChar;
239 
240  // Try to extract the forecasting method from the config tree
241  lPathStr << itChild.first.data() << ".forecasting_method";
242  const bool hasForecastingMethodBeenRetrieved =
243  exportValue<char> (lChar, lPathStr.str());
244  if (hasForecastingMethodBeenRetrieved == true) {
245  const ForecastingMethod lForecastingMethod (lChar);
246  lAirlineFeature_ptr->setForecastingMethod(lForecastingMethod);
247  }
248 
249  // Try to extract the unconstraining method from the config tree
250  lPathStr.str("");
251  lPathStr << itChild.first.data() << ".unconstraining_method";
252  const bool hasUnconstrainingMethodBeenRetrieved =
253  exportValue<char> (lChar, lPathStr.str());
254  if (hasUnconstrainingMethodBeenRetrieved == true) {
255  const UnconstrainingMethod lUnconstrainingMethod (lChar);
256  lAirlineFeature_ptr->setUnconstrainingMethod(lUnconstrainingMethod);
257  }
258 
259  // Try to extract the partnership technique from the config tree
260  lPathStr.str("");
261  lPathStr << itChild.first.data() << ".partnership_technique";
262  const bool hasPartnershipTechniqueBeenRetrieved =
263  exportValue<char> (lChar, lPathStr.str());
264  if (hasPartnershipTechniqueBeenRetrieved == true) {
265  const PartnershipTechnique lPartnershipTechnique (lChar);
266  lAirlineFeature_ptr->setPartnershipTechnique(lPartnershipTechnique);
267  }
268 
269  // Try to extract the pre optimisation method from the config tree
270  lPathStr.str("");
271  lPathStr << itChild.first.data() << ".pre_optimisation_method";
272  const bool hasPreOptMethodBeenRetrieved =
273  exportValue<char> (lChar, lPathStr.str());
274  if (hasPreOptMethodBeenRetrieved == true) {
275  const PreOptimisationMethod lPreOptimisationMethod (lChar);
276  lAirlineFeature_ptr->setPreOptimisationMethod(lPreOptimisationMethod);
277  }
278 
279  // Try to extract the optimisation method from the config tree
280  lPathStr.str("");
281  lPathStr << itChild.first.data() << ".optimisation_method";
282  const bool hasOptMethodBeenRetrieved =
283  exportValue<char> (lChar, lPathStr.str());
284  if (hasOptMethodBeenRetrieved == true) {
285  const OptimisationMethod lOptimisationMethod (lChar);
286  lAirlineFeature_ptr->setOptimisationMethod(lOptimisationMethod);
287  }
288 
289  } catch (CodeConversionException& lCodeConversionException) {
290  std::ostringstream oMessage;
291  oMessage << "Wrong input features for the airline '"
292  << lAirlineCode << "' in the input configuration file: "
293  << lCodeConversionException.what();
294  STDAIR_LOG_ERROR (oMessage.str());
295  throw CodeConversionException (oMessage.str());
296  }
297  }
298  }
299  }
300  }
301 }