StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
stdair.cpp
Go to the documentation of this file.
00001 
00005 // STL
00006 #include <cassert>
00007 #include <iostream>
00008 #include <sstream>
00009 #include <fstream>
00010 #include <string>
00011 // Boost (Extended STL)
00012 #include <boost/date_time/posix_time/posix_time.hpp>
00013 #include <boost/date_time/gregorian/gregorian.hpp>
00014 #include <boost/program_options.hpp>
00015 #include <boost/tokenizer.hpp>
00016 #include <boost/lexical_cast.hpp>
00017 // StdAir
00018 #include <stdair/stdair_types.hpp>
00019 #include <stdair/bom/BomArchive.hpp>
00020 #include <stdair/bom/BookingRequestStruct.hpp>
00021 #include <stdair/bom/TravelSolutionStruct.hpp>
00022 #include <stdair/service/Logger.hpp>
00023 #include <stdair/STDAIR_Service.hpp>
00024 #include <stdair/config/stdair-paths.hpp>
00025 
00026 // //////// Constants //////
00030 const std::string K_STDAIR_DEFAULT_LOG_FILENAME ("stdair.log");
00031 
00035 const std::string K_STDAIR_DEFAULT_INPUT_FILENAME (STDAIR_SAMPLE_DIR
00036                                                    "/schedule01.csv");
00037 
00042 const bool K_STDAIR_DEFAULT_BUILT_IN_INPUT = false;
00043 
00049 const bool K_STDAIR_DEFAULT_BUILT_FOR_RMOL = false;
00050 
00056 const bool K_STDAIR_DEFAULT_BUILT_FOR_CRS = false;
00057 
00062 const int K_STDAIR_EARLY_RETURN_STATUS = 99;
00063 
00064 // ///////// Parsing of Options & Configuration /////////
00065 // A helper function to simplify the main part.
00066 template<class T> std::ostream& operator<< (std::ostream& os,
00067                                             const std::vector<T>& v) {
00068   std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " ")); 
00069   return os;
00070 }
00071 
00073 int readConfiguration (int argc, char* argv[], bool& ioIsBuiltin,
00074                        bool& ioIsForRMOL, bool& ioIsForCRS,
00075                        stdair::Filename_T& ioInputFilename,
00076                        std::string& ioLogFilename) {
00077   // Default for the built-in input
00078   ioIsBuiltin = K_STDAIR_DEFAULT_BUILT_IN_INPUT;
00079 
00080   // Default for the RMOL input
00081   ioIsForRMOL = K_STDAIR_DEFAULT_BUILT_FOR_RMOL;
00082   
00083   // Default for the CRS input
00084   ioIsForCRS = K_STDAIR_DEFAULT_BUILT_FOR_CRS;
00085   
00086   // Declare a group of options that will be allowed only on command line
00087   boost::program_options::options_description generic ("Generic options");
00088   generic.add_options()
00089     ("prefix", "print installation prefix")
00090     ("version,v", "print version string")
00091     ("help,h", "produce help message");
00092     
00093   // Declare a group of options that will be allowed both on command
00094   // line and in config file
00095 
00096   boost::program_options::options_description config ("Configuration");
00097   config.add_options()
00098     ("builtin,b",
00099      "The sample BOM tree can be either built-in or parsed from an input file. That latter must then be given with the -i/--input option")
00100     ("rmol,r",
00101      "Build a sample BOM tree for RMOL (i.e., a dummy flight-date with a single leg-cabin)")
00102     ("crs,c",
00103      "Build a sample BOM tree for CRS")
00104     ("input,i",
00105      boost::program_options::value< std::string >(&ioInputFilename)->default_value(K_STDAIR_DEFAULT_INPUT_FILENAME),
00106      "(CVS) input file for the demand distributions")
00107     ("log,l",
00108      boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_STDAIR_DEFAULT_LOG_FILENAME),
00109      "Filename for the logs")
00110     ;
00111 
00112   // Hidden options, will be allowed both on command line and
00113   // in config file, but will not be shown to the user.
00114   boost::program_options::options_description hidden ("Hidden options");
00115   hidden.add_options()
00116     ("copyright",
00117      boost::program_options::value< std::vector<std::string> >(),
00118      "Show the copyright (license)");
00119         
00120   boost::program_options::options_description cmdline_options;
00121   cmdline_options.add(generic).add(config).add(hidden);
00122 
00123   boost::program_options::options_description config_file_options;
00124   config_file_options.add(config).add(hidden);
00125   boost::program_options::options_description visible ("Allowed options");
00126   visible.add(generic).add(config);
00127         
00128   boost::program_options::positional_options_description p;
00129   p.add ("copyright", -1);
00130         
00131   boost::program_options::variables_map vm;
00132   boost::program_options::
00133     store (boost::program_options::command_line_parser (argc, argv).
00134            options (cmdline_options).positional(p).run(), vm);
00135 
00136   std::ifstream ifs ("stdair.cfg");
00137   boost::program_options::store (parse_config_file (ifs, config_file_options),
00138                                  vm);
00139   boost::program_options::notify (vm);
00140     
00141   if (vm.count ("help")) {
00142     std::cout << visible << std::endl;
00143     return K_STDAIR_EARLY_RETURN_STATUS;
00144   }
00145 
00146   if (vm.count ("version")) {
00147     std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
00148     return K_STDAIR_EARLY_RETURN_STATUS;
00149   }
00150 
00151   if (vm.count ("prefix")) {
00152     std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
00153     return K_STDAIR_EARLY_RETURN_STATUS;
00154   }
00155 
00156   if (vm.count ("builtin")) {
00157     ioIsBuiltin = true;
00158   }
00159 
00160   if (vm.count ("rmol")) {
00161     ioIsForRMOL = true;
00162 
00163     // The RMOL sample tree takes precedence over the default built-in BOM tree
00164     ioIsBuiltin = false;
00165   }
00166 
00167   if (vm.count ("crs")) {
00168     ioIsForCRS = true;
00169 
00170     // The RMOL sample tree takes precedence over the default built-in BOM tree
00171     ioIsBuiltin = false;
00172   }
00173 
00174   const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
00175   std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
00176 
00177   const std::string isForRMOLStr = (ioIsForRMOL == true)?"yes":"no";
00178   std::cout << "The BOM should be built-in for RMOL? " << isForRMOLStr
00179             << std::endl;
00180 
00181   const std::string isForCRSStr = (ioIsForCRS == true)?"yes":"no";
00182   std::cout << "The BOM should be built-in for CRS? " << isForCRSStr
00183             << std::endl;
00184 
00185   if (ioIsBuiltin == false && ioIsForRMOL == false && ioIsForCRS == false) {
00186     if (vm.count ("input")) {
00187       ioInputFilename = vm["input"].as< std::string >();
00188       std::cout << "Input filename is: " << ioInputFilename << std::endl;
00189 
00190     } else {
00191       std::cerr << "Either one among the -b/--builtin, -r/--rmol, -c/--crs "
00192                 << "or -i/--input options must be specified" << std::endl;
00193     }
00194   }
00195 
00196   if (vm.count ("log")) {
00197     ioLogFilename = vm["log"].as< std::string >();
00198     std::cout << "Log filename is: " << ioLogFilename << std::endl;
00199   }
00200 
00201   return 0;
00202 }
00203 
00204 
00205 // ///////////////// M A I N ////////////////////
00206 int main (int argc, char* argv[]) {
00207 
00208   // State whether the BOM tree should be built-in or parsed from an
00209   // input file
00210   bool isBuiltin;
00211 
00212   // State whether a sample BOM tree should be built for RMOL.
00213   bool isForRMOL;
00214     
00215   // State whether a sample BOM tree should be built for the CRS.
00216   bool isForCRS;
00217     
00218   // Input file name
00219   stdair::Filename_T lInputFilename;
00220 
00221   // Output log File
00222   std::string lLogFilename;
00223     
00224   // Call the command-line option parser
00225   const int lOptionParserStatus =
00226     readConfiguration (argc, argv, isBuiltin, isForRMOL, isForCRS,
00227                        lInputFilename, lLogFilename);
00228 
00229   if (lOptionParserStatus == K_STDAIR_EARLY_RETURN_STATUS) {
00230     return 0;
00231   }
00232 
00233   // Set the log parameters
00234   std::ofstream logOutputFile;
00235   // Open and clean the log outputfile
00236   logOutputFile.open (lLogFilename.c_str());
00237   logOutputFile.clear();
00238 
00239   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00240   stdair::STDAIR_Service stdairService (lLogParams);
00241 
00242   // DEBUG
00243   STDAIR_LOG_DEBUG ("Welcome to stdair");
00244 
00245   // Check wether or not a (CSV) input file should be read
00246   if (isBuiltin == true || isForRMOL == true || isForCRS == true) {
00247 
00248     if (isForRMOL == true) {
00249       // Build the sample BOM tree for RMOL
00250       stdairService.buildDummyInventory (300);
00251 
00252     } else if (isForCRS == true) {
00253       //
00254       stdair::TravelSolutionList_T lTravelSolutionList;
00255       stdairService.buildSampleTravelSolutions (lTravelSolutionList);
00256 
00257       // Build the sample BOM tree for CRS
00258       const stdair::BookingRequestStruct& lBookingRequest =
00259         stdairService.buildSampleBookingRequest();
00260 
00261       // DEBUG: Display the travel solution and booking request
00262       STDAIR_LOG_DEBUG ("Booking request: " << lBookingRequest.display());
00263 
00264       const std::string& lCSVDump =
00265         stdairService.csvDisplay (lTravelSolutionList);
00266       STDAIR_LOG_DEBUG (lCSVDump);
00267 
00268     } else {
00269       assert (isBuiltin == true);
00270 
00271       // Build a sample BOM tree
00272       stdairService.buildSampleBom();
00273     }
00274       
00275   } else {
00276     // Read the input file
00277     //stdairService.readFromInputFile (lInputFilename);
00278 
00279     // DEBUG
00280     STDAIR_LOG_DEBUG ("StdAir will parse " << lInputFilename
00281                       << " and build the corresponding BOM tree.");
00282   }
00283 
00284   // DEBUG: Display the whole BOM tree
00285   const std::string& lCSVDump = stdairService.csvDisplay();
00286   STDAIR_LOG_DEBUG (lCSVDump);
00287 
00288   // Close the Log outputFile
00289   logOutputFile.close();
00290 
00291   /*
00292     Note: as that program is not intended to be run on a server in
00293     production, it is better not to catch the exceptions. When it
00294     happens (that an exception is throwned), that way we get the
00295     call stack.
00296   */
00297     
00298   return 0;     
00299 }
00300