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