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

stdair/bom/ConfigHolderStruct.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 #if BOOST_VERSION >= 104100
00008 #include <boost/property_tree/ptree.hpp>
00009 #include <boost/property_tree/json_parser.hpp>
00010 #include <boost/foreach.hpp>
00011 #endif // BOOST_VERSION >= 104100
00012 // StdAir
00013 #include <stdair/stdair_exceptions.hpp>
00014 #include <stdair/basic/ForecastingMethod.hpp>
00015 #include <stdair/basic/UnconstrainingMethod.hpp>
00016 #include <stdair/basic/PartnershipTechnique.hpp>
00017 #include <stdair/basic/PreOptimisationMethod.hpp>
00018 #include <stdair/basic/OptimisationMethod.hpp>
00019 #include <stdair/bom/AirlineFeature.hpp>
00020 #include <stdair/bom/ConfigHolderStruct.hpp>
00021 #include <stdair/bom/BomRetriever.hpp>
00022 #include <stdair/service/Logger.hpp>
00023 
00024 namespace stdair {
00025   
00026   // ////////////////////////////////////////////////////////////////////
00027   ConfigHolderStruct::ConfigHolderStruct() {
00028   }
00029     
00030   // ////////////////////////////////////////////////////////////////////
00031   ConfigHolderStruct::
00032   ConfigHolderStruct (const ConfigHolderStruct& iConfigHolderStruct)
00033     : _pt (iConfigHolderStruct._pt) {
00034   }
00035   
00036   // ////////////////////////////////////////////////////////////////////
00037   ConfigHolderStruct::~ConfigHolderStruct() {
00038   }
00039 
00040   // ////////////////////////////////////////////////////////////////////
00041   void ConfigHolderStruct::toStream (std::ostream& ioOut) const {
00042     ioOut << describe();
00043   }
00044 
00045   // ////////////////////////////////////////////////////////////////////
00046   void ConfigHolderStruct::fromStream (std::istream& ioIn) {
00047   }
00048   
00049   // ////////////////////////////////////////////////////////////////////
00050   const std::string ConfigHolderStruct::describe() const {
00051     std::ostringstream oStr;
00052     oStr << "Configuration Display:" << std::endl;
00053 
00054     // Look for the start and end date values.
00055     stdair::Date_T lStartDate;
00056     const bool hasStartDateBeenRetrieved = 
00057       exportValue<Date_T> (lStartDate, "date.start"); 
00058     if (hasStartDateBeenRetrieved == true) {
00059       oStr << "  Start date: " << lStartDate << std::endl;
00060     }
00061     stdair::Date_T lEndDate;
00062     const bool hasEndDateBeenRetrieved = 
00063       exportValue<Date_T> (lEndDate, "date.end");
00064     if (hasEndDateBeenRetrieved == true) {
00065       oStr << "  End date: " << lEndDate << std::endl;
00066     }
00067     
00068     // Look for the random seed value.
00069     RandomSeed_T lRandomSeed;
00070     const bool hasSeedBeenRetrieved = 
00071       exportValue<RandomSeed_T> (lRandomSeed, "random.seed");
00072     if (hasSeedBeenRetrieved == true) {
00073       oStr << "  Random Seed: " << lRandomSeed << std::endl;
00074     }
00075 
00076     // Look for the demand generation method.
00077     char lChar;
00078     const bool hasDemandGenMethodBeenRetrieved = 
00079       exportValue<char> (lChar, "demand generation.method"); 
00080     if (hasDemandGenMethodBeenRetrieved == true) {
00081       oStr << "  Demand Generation method: " << lChar << std::endl;
00082     }
00083    
00084     // Look for the number of runs value.
00085     Count_T lTotalNumberOfRuns;
00086     const bool hasNumberOfRunsBeenRetrieved = 
00087       exportValue<Count_T> (lTotalNumberOfRuns, "runs.number");
00088     if (hasNumberOfRunsBeenRetrieved == true) {
00089       oStr << "  Number Of Runs: " << lTotalNumberOfRuns << std::endl;
00090     }
00091     
00092     // Look for the input files.
00093     stdair::Filename_T lFilename (""); 
00094     const bool hasScheduleFileBeenRetrieved = 
00095       exportValue<stdair::Filename_T> (lFilename, "input.schedule"); 
00096     if (hasScheduleFileBeenRetrieved == true) {
00097       oStr << "  Schedule input file: " << lFilename << std::endl;
00098     }
00099     const bool hasODFileBeenRetrieved = 
00100       exportValue<stdair::Filename_T> (lFilename, "input.ond");  
00101     if (hasODFileBeenRetrieved == true) {
00102       oStr << "  OnD input file: " << lFilename << std::endl;
00103     }     
00104     const bool hasFrat5FileBeenRetrieved = 
00105       exportValue<stdair::Filename_T> (lFilename, "input.frat5");  
00106     if (hasFrat5FileBeenRetrieved == true) {
00107       oStr << "  Frat5 input file: " << lFilename << std::endl;
00108     }
00109     const bool hasFFdisutilityFileBeenRetrieved = 
00110       exportValue<stdair::Filename_T> (lFilename, "input.ffdisutility");  
00111     if (hasFFdisutilityFileBeenRetrieved == true) {
00112       oStr << "  FFdisutility input file: " << lFilename << std::endl;
00113     }
00114     const bool hasYieldFileBeenRetrieved = 
00115       exportValue<stdair::Filename_T> (lFilename, "input.yield");  
00116     if (hasYieldFileBeenRetrieved == true) {
00117       oStr << "  Yield input file: " << lFilename << std::endl;
00118     }
00119     const bool hasFareFileBeenRetrieved = 
00120       exportValue<stdair::Filename_T> (lFilename, "input.fare");  
00121     if (hasFareFileBeenRetrieved == true) {
00122       oStr << "  Fare input file: " << lFilename << std::endl;
00123     }
00124     const bool hasDemandFileBeenRetrieved = 
00125       exportValue<stdair::Filename_T> (lFilename, "input.demand");  
00126     if  (hasDemandFileBeenRetrieved == true) {
00127       oStr << "  Demand input file: " << lFilename << std::endl;
00128     }
00129     
00130     return oStr.str();
00131   } 
00132  
00133   // ////////////////////////////////////////////////////////////////////
00134   const std::string ConfigHolderStruct::jsonExport() const {    
00135     std::ostringstream oStr; 
00136 #if BOOST_VERSION >= 104100
00137     // Write the property tree into the JSON stream.
00138     write_json (oStr, _pt);
00139 #endif // BOOST_VERSION >= 104100
00140     return oStr.str();
00141   }
00142   
00143   // ////////////////////////////////////////////////////////////////////
00144   void ConfigHolderStruct::add (const bpt::ptree& iConfigPropertyTree) {
00145     // Call the dedicated recursive method with an empty path in order to merge
00146     // the config property tree with the given new one.
00147     std::string lEmptyPath ("");        
00148     add (iConfigPropertyTree, lEmptyPath);
00149   }
00150 
00151   // ////////////////////////////////////////////////////////////////////
00152   void ConfigHolderStruct::add (const bpt::ptree& iConfigPropertyTree,
00153                                 const std::string& iPath) {
00154 
00155     // Are there any more children to browse?
00156     bool isThereAnyChild = false;
00157 
00158 #if BOOST_VERSION >= 104100
00159 
00160     // Browse the children nodes
00161     BOOST_FOREACH(bpt::ptree::value_type itChild, iConfigPropertyTree) {
00162 
00163       isThereAnyChild = true;
00164 
00165       // Build the current path
00166       std::ostringstream lCurrentPathStr;
00167       const bool isPathEmptyForNow = iPath.empty();
00168       if (isPathEmptyForNow == false) { 
00169         lCurrentPathStr << iPath << ".";
00170       }
00171       // Add the current node name
00172       lCurrentPathStr << itChild.first.data(); 
00173       const std::string lCurrentPath (lCurrentPathStr.str()); 
00174       
00175       // Get the child tree
00176       const bpt::ptree& lChildTree = itChild.second;   
00177       add(lChildTree, lCurrentPath);
00178     }
00179 
00180     // If there is no child for this node, create the specified path and add 
00181     // the correponding value
00182     if (isThereAnyChild == false) {
00183       std::string lValue (iConfigPropertyTree.data());
00184       const bool hasInsertionBeenSuccessful = addValue (lValue, iPath);
00185       assert (hasInsertionBeenSuccessful == true);   
00186     } 
00187 #endif // BOOST_VERSION >= 104100
00188   }
00189 
00190   // ////////////////////////////////////////////////////////////////////
00191   bool ConfigHolderStruct::addValue (const std::string& iValue,
00192                                      const std::string& iPath) {  
00193     bool hasInsertionBeenSuccessful = true;
00194     // Create the given specified path and add the corresponding given value,
00195     // or replace the value if the path already exists. 
00196 #if BOOST_VERSION >= 104100
00197 
00198     try {
00199       std::size_t found;
00200       const std::string lPrefix ("config");
00201       std::string lFinalPath;
00202       found = iPath.find(lPrefix);
00203       if (found == std::string::npos) {
00204         lFinalPath += lPrefix;
00205         lFinalPath += ".";
00206       }
00207       lFinalPath += iPath;
00208       if (lFinalPath != lPrefix) {
00209         _pt.put (lFinalPath, iValue);
00210       }
00211     } catch (bpt::ptree_bad_data& bptException) {
00212       hasInsertionBeenSuccessful = false;
00213     }
00214 #endif // BOOST_VERSION >= 104100
00215 
00216     return hasInsertionBeenSuccessful;
00217   } 
00218 
00219   // ////////////////////////////////////////////////////////////////////
00220   void ConfigHolderStruct::updateAirlineFeatures (BomRoot& iBomRoot) {  
00221 
00222     AirlineCode_T lAirlineCode ("");
00223 
00224     // Browse the children nodes
00225     BOOST_FOREACH(bpt::ptree::value_type itChild, _pt) { 
00226       std::ostringstream lPathStr;
00227       lPathStr << itChild.first.data() << ".airline_code";
00228       const bool hasAirlineCodeBeenRetrieved = 
00229         exportValue<AirlineCode_T> (lAirlineCode , lPathStr.str());
00230       if (hasAirlineCodeBeenRetrieved == true) {
00231         AirlineFeature* lAirlineFeature_ptr = 
00232           BomRetriever::retrieveAirlineFeatureFromKey (iBomRoot, lAirlineCode);
00233         if (lAirlineFeature_ptr != NULL) { 
00234 
00235           try {
00236 
00237             std::ostringstream lPathStr;
00238             char lChar;
00239 
00240             // Try to extract the forecasting method from the config tree
00241             lPathStr << itChild.first.data() << ".forecasting_method";
00242             const bool hasForecastingMethodBeenRetrieved =    
00243               exportValue<char> (lChar, lPathStr.str());         
00244             if (hasForecastingMethodBeenRetrieved == true) {
00245               const ForecastingMethod lForecastingMethod (lChar);
00246               lAirlineFeature_ptr->setForecastingMethod(lForecastingMethod);  
00247             } 
00248 
00249             // Try to extract the unconstraining method from the config tree
00250             lPathStr.str("");  
00251             lPathStr << itChild.first.data() << ".unconstraining_method"; 
00252             const bool hasUnconstrainingMethodBeenRetrieved =    
00253               exportValue<char> (lChar, lPathStr.str());
00254             if (hasUnconstrainingMethodBeenRetrieved == true) {
00255               const UnconstrainingMethod lUnconstrainingMethod (lChar);
00256               lAirlineFeature_ptr->setUnconstrainingMethod(lUnconstrainingMethod);  
00257             } 
00258 
00259             // Try to extract the partnership technique from the config tree 
00260             lPathStr.str("");    
00261             lPathStr << itChild.first.data() << ".partnership_technique"; 
00262             const bool hasPartnershipTechniqueBeenRetrieved =    
00263               exportValue<char> (lChar, lPathStr.str());
00264             if (hasPartnershipTechniqueBeenRetrieved == true) {
00265               const PartnershipTechnique lPartnershipTechnique (lChar);
00266               lAirlineFeature_ptr->setPartnershipTechnique(lPartnershipTechnique);  
00267             } 
00268 
00269             // Try to extract the pre optimisation method from the config tree  
00270             lPathStr.str("");
00271             lPathStr << itChild.first.data() << ".pre_optimisation_method"; 
00272             const bool hasPreOptMethodBeenRetrieved =    
00273               exportValue<char> (lChar, lPathStr.str());
00274             if (hasPreOptMethodBeenRetrieved == true) {
00275               const PreOptimisationMethod lPreOptimisationMethod (lChar);
00276               lAirlineFeature_ptr->setPreOptimisationMethod(lPreOptimisationMethod);  
00277             } 
00278 
00279             // Try to extract the optimisation method from the config tree   
00280             lPathStr.str("");
00281             lPathStr << itChild.first.data() << ".optimisation_method"; 
00282             const bool hasOptMethodBeenRetrieved =    
00283               exportValue<char> (lChar, lPathStr.str());
00284             if (hasOptMethodBeenRetrieved == true) {
00285               const OptimisationMethod lOptimisationMethod (lChar);
00286               lAirlineFeature_ptr->setOptimisationMethod(lOptimisationMethod);  
00287             }
00288 
00289           } catch (CodeConversionException& lCodeConversionException) {
00290             std::ostringstream oMessage;
00291             oMessage << "Wrong input features for the airline '" 
00292                      << lAirlineCode << "' in the input configuration file: "
00293                      << lCodeConversionException.what();
00294             STDAIR_LOG_ERROR (oMessage.str());
00295             throw CodeConversionException (oMessage.str());
00296           }
00297         }
00298       }
00299     }
00300   }
00301 }