TravelCCM Logo  0.5.0
C++ Travel Customer Choice Model Library
travelccm.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/program_options.hpp>
00013 // StdAir
00014 #include <stdair/basic/BasLogParams.hpp>
00015 #include <stdair/basic/BasDBParams.hpp>
00016 #include <stdair/bom/BookingRequestStruct.hpp>
00017 #include <stdair/bom/TravelSolutionStruct.hpp>
00018 #include <stdair/service/Logger.hpp>
00019 // TravelCCM
00020 #include <travelccm/TRAVELCCM_Service.hpp>
00021 #include <travelccm/config/travelccm-paths.hpp>
00022 
00023 // //////// Constants //////
00025 const std::string K_TRAVELCCM_DEFAULT_LOG_FILENAME ("travelccm.log");
00026 
00028 const std::string K_TRAVELCCM_DEFAULT_INPUT_FILENAME (STDAIR_SAMPLE_DIR
00029                                                       "/ccm_01.csv");
00030 
00031 
00032 // ///////// Parsing of Options & Configuration /////////
00033 // A helper function to simplify the main part.
00034 template<class T> std::ostream& operator<< (std::ostream& os,
00035                                             const std::vector<T>& v) {
00036   std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " ")); 
00037   return os;
00038 }
00039 
00041 const int K_TRAVELCCM_EARLY_RETURN_STATUS = 99;
00042 
00044 int readConfiguration (int argc, char* argv[], std::string& lInputFilename,
00045                        std::string& lLogFilename) {
00046   
00047     
00048   // Declare a group of options that will be allowed only on command line
00049   boost::program_options::options_description generic ("Generic options");
00050   generic.add_options()
00051     ("prefix", "print installation prefix")
00052     ("version,v", "print version string")
00053     ("help,h", "produce help message");
00054     
00055   // Declare a group of options that will be allowed both on command
00056   // line and in config file
00057   boost::program_options::options_description config ("Configuration");
00058   config.add_options()
00059     ("input,i",
00060      boost::program_options::value< std::string >(&lInputFilename)->default_value(K_TRAVELCCM_DEFAULT_INPUT_FILENAME),
00061      "(CVS) input file for customer choice")
00062     ("log,l",
00063      boost::program_options::value< std::string >(&lLogFilename)->default_value(K_TRAVELCCM_DEFAULT_LOG_FILENAME),
00064      "Filename for the logs")
00065     ;
00066 
00067   // Hidden options, will be allowed both on command line and
00068   // in config file, but will not be shown to the user.
00069   boost::program_options::options_description hidden ("Hidden options");
00070   hidden.add_options()
00071     ("copyright",
00072      boost::program_options::value< std::vector<std::string> >(),
00073      "Show the copyright (license)");
00074         
00075   boost::program_options::options_description cmdline_options;
00076   cmdline_options.add(generic).add(config).add(hidden);
00077 
00078   boost::program_options::options_description config_file_options;
00079   config_file_options.add(config).add(hidden);
00080 
00081   boost::program_options::options_description visible ("Allowed options");
00082   visible.add(generic).add(config);
00083         
00084   boost::program_options::positional_options_description p;
00085   p.add ("copyright", -1);
00086         
00087   boost::program_options::variables_map vm;
00088   boost::program_options::
00089     store (boost::program_options::command_line_parser (argc, argv).
00090            options (cmdline_options).positional(p).run(), vm);
00091 
00092   std::ifstream ifs ("travelccm.cfg");
00093   boost::program_options::store (parse_config_file (ifs, config_file_options),
00094                                  vm);
00095   boost::program_options::notify (vm);
00096     
00097   if (vm.count ("help")) {
00098     std::cout << visible << std::endl;
00099     return K_TRAVELCCM_EARLY_RETURN_STATUS;
00100   }
00101 
00102   if (vm.count ("version")) {
00103     std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
00104     return K_TRAVELCCM_EARLY_RETURN_STATUS;
00105   }
00106 
00107   if (vm.count ("prefix")) {
00108     std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
00109     return K_TRAVELCCM_EARLY_RETURN_STATUS;
00110   }
00111 
00112   if (vm.count ("input")) {
00113     lInputFilename = vm["input"].as< std::string >();
00114     std::cout << "Input filename is: " << lInputFilename << std::endl;
00115   }
00116 
00117   if (vm.count ("log")) {
00118     lLogFilename = vm["log"].as< std::string >();
00119     std::cout << "Log filename is: " << lLogFilename << std::endl;
00120   }
00121   
00122   return 0;
00123 }
00124 
00125 
00126 // ///////// M A I N ////////////
00127 int main (int argc, char* argv[]) {
00128     
00129   // Input file name
00130   std::string lInputFilename;
00131   
00132   // Output log File
00133   std::string lLogFilename;
00134   
00135   // Call the command-line option parser
00136   const int lOptionParserStatus = 
00137     readConfiguration (argc, argv, lInputFilename, lLogFilename);
00138   
00139   if (lOptionParserStatus == K_TRAVELCCM_EARLY_RETURN_STATUS) {
00140     return 0;
00141   }
00142   
00143   // Set the log parameters
00144   std::ofstream logOutputFile;
00145   // Open and clean the log outputfile
00146   logOutputFile.open (lLogFilename.c_str());
00147   logOutputFile.clear();
00148   
00149   // Initialise the service context
00150   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00151   
00152   // Build the BOM tree
00153   TRAVELCCM::TRAVELCCM_Service travelccmService (lLogParams);
00154 
00155   // DEBUG
00156   STDAIR_LOG_DEBUG ("Welcome to TravelCCM");
00157 
00158   // Build a list of travel solutions
00159   const stdair::BookingRequestStruct& lBookingRequest =
00160     travelccmService.buildSampleBookingRequest();
00161 
00162   // DEBUG
00163   STDAIR_LOG_DEBUG ("Booking request: " << lBookingRequest.display());
00164 
00165   // Build the sample BOM tree
00166   stdair::TravelSolutionList_T lTSList;
00167   travelccmService.buildSampleTravelSolutions (lTSList);
00168 
00169   // DEBUG: Display the list of travel solutions
00170   const std::string& lCSVDump = travelccmService.csvDisplay (lTSList);
00171   STDAIR_LOG_DEBUG (lCSVDump);
00172   
00173   // Choose a travel solution
00174   const stdair::TravelSolutionStruct* lTS_ptr =
00175     travelccmService.chooseTravelSolution (lTSList, lBookingRequest);
00176 
00177   if (lTS_ptr != NULL) {
00178     // DEBUG
00179     STDAIR_LOG_DEBUG ("Chosen travel solution: " << lTS_ptr->display());
00180 
00181   } else {
00182     // DEBUG
00183     STDAIR_LOG_DEBUG ("No travel solution can be found for "
00184                       << lBookingRequest.display()
00185                       << " within the following list of travel solutions");
00186     STDAIR_LOG_DEBUG (lCSVDump);
00187   }
00188 
00189   // Close the Log outputFile
00190   logOutputFile.close();
00191 
00192   /*
00193     Note: as that program is not intended to be run on a server in
00194     production, it is better not to catch the exceptions. When it
00195     happens (that an exception is throwned), that way we get the
00196     call stack.
00197   */
00198 
00199   return 0;     
00200 }