$treeview $search $mathjax
RMOL Logo  1.00.0
$projectbrief
$projectbrief
$searchbox

rmol/command/InventoryParser.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <sstream>
00006 #include <fstream>
00007 #include <cassert>
00008 // StdAir
00009 #include <stdair/stdair_inventory_types.hpp>
00010 #include <stdair/stdair_maths_types.hpp>
00011 #include <stdair/stdair_exceptions.hpp>
00012 #include <stdair/basic/BasConst_DefaultObject.hpp>
00013 #include <stdair/basic/BasConst_Inventory.hpp>
00014 #include <stdair/basic/BasFileMgr.hpp>
00015 #include <stdair/bom/BomRetriever.hpp>
00016 #include <stdair/bom/BomManager.hpp>
00017 #include <stdair/bom/BomRoot.hpp>
00018 #include <stdair/bom/Inventory.hpp>
00019 #include <stdair/bom/FlightDate.hpp>
00020 #include <stdair/bom/SegmentDate.hpp>
00021 #include <stdair/bom/SegmentCabin.hpp>
00022 #include <stdair/bom/LegDate.hpp>
00023 #include <stdair/bom/LegCabin.hpp>
00024 #include <stdair/bom/BookingClass.hpp>
00025 #include <stdair/bom/VirtualClassStruct.hpp>
00026 #include <stdair/factory/FacBom.hpp>
00027 #include <stdair/factory/FacBomManager.hpp>
00028 #include <stdair/service/Logger.hpp>
00029 // RMOL
00030 #include <rmol/command/InventoryParser.hpp>
00031 
00032 namespace RMOL {
00033 
00034   // ////////////////////////////////////////////////////////////////////
00035   bool InventoryParser::
00036   parseInputFileAndBuildBom (const std::string& iInputFileName,
00037                              stdair::BomRoot& ioBomRoot) {
00038     bool hasReadBeenSuccessful = false;
00039 
00040     // Check that the file path given as input corresponds to an actual file
00041     const bool doesExistAndIsReadable =
00042       stdair::BasFileMgr::doesExistAndIsReadable (iInputFileName);
00043     if (doesExistAndIsReadable == false) {
00044       std::ostringstream oMessage;
00045       oMessage << "The input file, '" << iInputFileName
00046                << "', can not be retrieved on the file-system";
00047       throw stdair::FileNotFoundException (oMessage.str());
00048     }
00049 
00050     // Retrieve the (sample) leg-cabin
00051     stdair::LegCabin& lLegCabin =
00052       stdair::BomRetriever::retrieveDummyLegCabin (ioBomRoot);
00053 
00054     // Retrieve the (sample) segment-cabin
00055     stdair::SegmentCabin& lSegmentCabin =
00056       stdair::BomRetriever::retrieveDummySegmentCabin (ioBomRoot);
00057 
00058     // Open the input file
00059     std::ifstream inputFile (iInputFileName.c_str());
00060     if (! inputFile) {
00061       STDAIR_LOG_ERROR ("Can not open input file '" << iInputFileName << "'");
00062       throw new stdair::FileNotFoundException ("Can not open input file '"
00063                                                + iInputFileName + "'");
00064     }
00065     
00066     char buffer[80];
00067     double dval;
00068     short i = 1;
00069     bool hasAllPArams = true;
00070     stdair::Yield_T lYield;
00071     stdair::MeanValue_T lMean;
00072     stdair::StdDevValue_T lStdDev;
00073     stdair::BookingClassKey lBCKey (stdair::DEFAULT_CLASS_CODE);
00074     
00075     while (inputFile.getline (buffer, sizeof (buffer), ';')) {
00076       std::istringstream iStringStr (buffer);
00077 
00078       if (i == 1) {
00079         hasAllPArams = true;
00080       }
00081       
00082       if (iStringStr >> dval) {
00083         if (i == 1) {
00084           lYield = dval;
00085           // std::cout << "Yield[" << i << "] = '" << dval << "'" << std::endl;
00086 
00087         } else if (i == 2) {
00088           lMean = dval;
00089           // std::cout << "Mean[" << i << "] = '" << dval << "'" << std::endl;
00090 
00091         } else if (i == 3) {
00092           lStdDev = dval;
00093           //std::cout << "stdDev[" << i << "] = '" << dval << "'" << std::endl;
00094           i = 0;
00095         }
00096         i++;
00097           
00098       } else {
00099         hasAllPArams = false;
00100       }
00101 
00102       if (hasAllPArams && i == 1) {
00103         stdair::BookingClass& lBookingClass =
00104           stdair::FacBom<stdair::BookingClass>::instance().create (lBCKey);
00105         stdair::FacBomManager::addToList (lSegmentCabin, lBookingClass);
00106         lBookingClass.setYield (lYield);
00107         lBookingClass.setMean (lMean);
00108         lBookingClass.setStdDev (lStdDev);
00109         stdair::BookingClassList_T lBookingClassList;
00110         lBookingClassList.push_back(&lBookingClass);
00111         stdair::VirtualClassStruct lVirtualClass (lBookingClassList);
00112         lVirtualClass.setYield (lYield);
00113         lVirtualClass.setMean (lMean);
00114         lVirtualClass.setStdDev (lStdDev);
00115         lLegCabin.addVirtualClass (lVirtualClass);
00116       }
00117     }
00118     
00119     //
00120     if (!inputFile.eof()) {
00121       STDAIR_LOG_ERROR ("Problem when reading input file '" << iInputFileName
00122                         << "'");
00123       return hasReadBeenSuccessful;
00124     }
00125 
00126     //
00127     hasReadBeenSuccessful = true;
00128     return hasReadBeenSuccessful;
00129   }
00130   
00131 }