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

airrac.cpp

Go to the documentation of this file.
00001 // STL
00002 #include <cassert>
00003 #include <iostream>
00004 #include <sstream>
00005 #include <fstream>
00006 #include <vector>
00007 #include <list>
00008 #include <string>
00009 // Boost (Extended STL)
00010 #include <boost/date_time/posix_time/posix_time.hpp>
00011 #include <boost/date_time/gregorian/gregorian.hpp>
00012 #include <boost/tokenizer.hpp>
00013 #include <boost/program_options.hpp>
00014 // StdAir
00015 #include <stdair/STDAIR_Service.hpp>
00016 #include <stdair/bom/TravelSolutionStruct.hpp>
00017 #include <stdair/service/Logger.hpp>
00018 // Airrac
00019 #include <airrac/AIRRAC_Service.hpp>
00020 #include <airrac/config/airrac-paths.hpp>
00021 
00022 // //////// Type definitions ///////
00023 typedef std::vector<std::string> WordList_T;
00024 
00025 
00026 // //////// Constants //////
00028 const std::string K_AIRRAC_DEFAULT_LOG_FILENAME ("airrac.log");
00029 
00031 const std::string K_AIRRAC_DEFAULT_YIELD_INPUT_FILENAME (STDAIR_SAMPLE_DIR
00032                                                          "/yieldstore01.csv");
00033 
00036 const bool K_AIRRAC_DEFAULT_BUILT_IN_INPUT = false;
00037 
00039 const int K_AIRRAC_EARLY_RETURN_STATUS = 99;
00040 
00041 // ///////// Parsing of Options & Configuration /////////
00042 // A helper function to simplify the main part.
00043 template<class T> std::ostream& operator<< (std::ostream& os,
00044                                             const std::vector<T>& v) {
00045   std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " ")); 
00046   return os;
00047 }
00048 
00050 int readConfiguration (int argc, char* argv[], bool& ioIsBuiltin,
00051                        stdair::Filename_T& ioYieldInputFilename,
00052                        std::string& ioLogFilename) {
00053 
00054   // Default for the built-in input
00055   ioIsBuiltin = K_AIRRAC_DEFAULT_BUILT_IN_INPUT;
00056 
00057   // Declare a group of options that will be allowed only on command line
00058   boost::program_options::options_description generic ("Generic options");
00059   generic.add_options()
00060     ("prefix", "print installation prefix")
00061     ("version,v", "print version string")
00062     ("help,h", "produce help message");
00063     
00064   // Declare a group of options that will be allowed both on command
00065   // line and in config file
00066   boost::program_options::options_description config ("Configuration");
00067   config.add_options()
00068     ("builtin,b",
00069      "The sample BOM tree can be either built-in or parsed from an input file. That latter must then be given with the -y/--yield option")
00070     ("yield,y",
00071      boost::program_options::value< std::string >(&ioYieldInputFilename)->default_value(K_AIRRAC_DEFAULT_YIELD_INPUT_FILENAME),
00072      "(CSV) input file for the yield rules")
00073     ("log,l",
00074      boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_AIRRAC_DEFAULT_LOG_FILENAME),
00075      "Filename for the logs")
00076     ;
00077 
00078   // Hidden options, will be allowed both on command line and
00079   // in config file, but will not be shown to the user.
00080   boost::program_options::options_description hidden ("Hidden options");
00081   hidden.add_options()
00082     ("copyright",
00083      boost::program_options::value< std::vector<std::string> >(),
00084      "Show the copyright (license)");
00085         
00086   boost::program_options::options_description cmdline_options;
00087   cmdline_options.add(generic).add(config).add(hidden);
00088 
00089   boost::program_options::options_description config_file_options;
00090   config_file_options.add(config).add(hidden);
00091 
00092   boost::program_options::options_description visible ("Allowed options");
00093   visible.add(generic).add(config);
00094         
00095   boost::program_options::positional_options_description p;
00096   p.add ("copyright", -1);
00097         
00098   boost::program_options::variables_map vm;
00099   boost::program_options::
00100     store (boost::program_options::command_line_parser (argc, argv).
00101            options (cmdline_options).positional(p).run(), vm);
00102 
00103   std::ifstream ifs ("airrac.cfg");
00104   boost::program_options::store (parse_config_file (ifs, config_file_options),
00105                                  vm);
00106   boost::program_options::notify (vm); if (vm.count ("help")) {
00107     std::cout << visible << std::endl;
00108     return K_AIRRAC_EARLY_RETURN_STATUS;
00109   }
00110 
00111   if (vm.count ("version")) {
00112     std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
00113     return K_AIRRAC_EARLY_RETURN_STATUS;
00114   }
00115 
00116   if (vm.count ("prefix")) {
00117     std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
00118     return K_AIRRAC_EARLY_RETURN_STATUS;
00119   }
00120 
00121   if (vm.count ("builtin")) {
00122     ioIsBuiltin = true;
00123   }
00124   const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
00125   std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
00126 
00127   if (ioIsBuiltin == false) {
00128 
00129     // The BOM tree should be built from parsing a yield (and O&D) file
00130     if (vm.count ("yield")) {
00131       ioYieldInputFilename = vm["yield"].as< std::string >();
00132       std::cout << "Input yield filename is: " << ioYieldInputFilename
00133                 << std::endl;
00134 
00135     } else {
00136       // The built-in option is not selected. However, no yield file
00137       // is specified
00138       std::cerr << "Either one among the -b/--builtin and -y/--yield "
00139                 << "options must be specified" << std::endl;
00140     }
00141   }
00142 
00143   if (vm.count ("log")) {
00144     ioLogFilename = vm["log"].as< std::string >();
00145     std::cout << "Log filename is: " << ioLogFilename << std::endl;
00146   }
00147   
00148   return 0;
00149 }
00150 
00151 
00152 // /////////////// M A I N /////////////////
00153 int main (int argc, char* argv[]) {
00154 
00155   // State whether the BOM tree should be built-in or parsed from an input file
00156   bool isBuiltin;
00157     
00158   // Yield input filename
00159   stdair::Filename_T lYieldInputFilename;
00160   
00161   // Output log File
00162   stdair::Filename_T lLogFilename;
00163     
00164   // Call the command-line option parser
00165   const int lOptionParserStatus = 
00166     readConfiguration (argc, argv, isBuiltin, lYieldInputFilename, lLogFilename);
00167     
00168   if (lOptionParserStatus == K_AIRRAC_EARLY_RETURN_STATUS) {
00169     return 0;
00170   }
00171     
00172   // Set the log parameters
00173   std::ofstream logOutputFile;
00174   // Open and clean the log outputfile
00175   logOutputFile.open (lLogFilename.c_str());
00176   logOutputFile.clear();
00177     
00178   // Initialise the AirRAC service object
00179   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00180 
00181   AIRRAC::AIRRAC_Service airracService (lLogParams);
00182 
00183   // DEBUG
00184   STDAIR_LOG_DEBUG ("Welcome to AirRAC");
00185 
00186   // Build a sample list of travel solutions
00187   stdair::TravelSolutionList_T lTravelSolutionList;
00188   airracService.buildSampleTravelSolutions (lTravelSolutionList);
00189   
00190   // Check wether or not a (CSV) input file should be read
00191   if (isBuiltin == true) {
00192 
00193     // Build the sample BOM tree (filled with yields) for AirRAC
00194     airracService.buildSampleBom();
00195 
00196   } else {
00197 
00198     // Build the BOM tree from parsing a yield file
00199     AIRRAC::YieldFilePath lYieldFilePath (lYieldInputFilename);
00200     airracService.parseAndLoad (lYieldFilePath);
00201    
00202   }
00203 
00204   // DEBUG: Display the whole BOM tree
00205   const std::string& lBOMCSVDump = airracService.csvDisplay();
00206   STDAIR_LOG_DEBUG ("BOM tree: " << lBOMCSVDump);
00207 
00208   // DEBUG: Display the travel solutions
00209   const std::string& lTSCSVDump =
00210     airracService.csvDisplay (lTravelSolutionList);
00211   STDAIR_LOG_DEBUG (lTSCSVDump);
00212 
00213   // Close the Log outputFile
00214   logOutputFile.close();
00215 
00216   /*
00217     Note: as that program is not intended to be run on a server in
00218     production, it is better not to catch the exceptions. When it
00219     happens (that an exception is throwned), that way we get the
00220     call stack.
00221   */
00222 
00223   return 0;     
00224 }