AirSched Logo  0.1.3
C++ Simulated Airline Schedule Manager Library
airsched.cpp
Go to the documentation of this file.
00001 // STL
00002 #include <cassert>
00003 #include <sstream>
00004 #include <fstream>
00005 #include <string>
00006 // Boost (Extended STL)
00007 #include <boost/date_time/posix_time/posix_time.hpp>
00008 #include <boost/date_time/gregorian/gregorian.hpp>
00009 #include <boost/program_options.hpp>
00010 #include <boost/tokenizer.hpp>
00011 #include <boost/lexical_cast.hpp>
00012 // StdAir
00013 #include <stdair/STDAIR_Service.hpp>
00014 #include <stdair/bom/BomDisplay.hpp>
00015 #include <stdair/bom/BookingRequestStruct.hpp>
00016 #include <stdair/bom/TravelSolutionStruct.hpp>
00017 #include <stdair/service/Logger.hpp>
00018 // AirSched
00019 #include <airsched/AIRSCHED_Service.hpp>
00020 #include <airsched/batches/BookingRequestParser.hpp>
00021 #include <airsched/config/airsched-paths.hpp>
00022 
00023 // //////// Type definitions ///////
00024 typedef std::vector<std::string> WordList_T;
00025 
00026 
00027 // //////// Constants //////
00031 const std::string K_AIRSCHED_DEFAULT_LOG_FILENAME ("airsched.log");
00032 
00036 const std::string K_AIRSCHED_DEFAULT_INPUT_FILENAME (STDAIR_SAMPLE_DIR
00037                                                      "/schedule03.csv");
00038 
00044 const bool K_AIRSCHED_DEFAULT_BUILT_IN_INPUT = false;
00045 
00050 const bool K_AIRSCHED_DEFAULT_BOOKING_REQUEST_MODE = false;
00051 
00056 const std::string K_AIRSCHED_DEFAULT_BOOKING_REQUEST ("NCE BKK NCE 2007-04-21 2007-03-21 08:32:00 C 1 DF RO 5 NONE 10:00:00 2000.0 20.0");
00057 
00058 // //////////////////////////////////////////////////////////////////////
00059 std::string createStringFromWordList (const WordList_T& iWordList) {
00060   std::ostringstream oStr;
00061 
00062   unsigned short idx = iWordList.size();
00063   for (WordList_T::const_iterator itWord = iWordList.begin();
00064        itWord != iWordList.end(); ++itWord, --idx) {
00065     const std::string& lWord = *itWord;
00066     oStr << lWord;
00067     if (idx > 1) {
00068       oStr << " ";
00069     }
00070   }
00071   
00072   return oStr.str();
00073 }
00074 
00075 // ///////// Parsing of Options & Configuration /////////
00076 // A helper function to simplify the main part.
00077 template<class T> std::ostream& operator<< (std::ostream& os,
00078                                             const std::vector<T>& v) {
00079   std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " ")); 
00080   return os;
00081 }
00082 
00084 const int K_AIRSCHED_EARLY_RETURN_STATUS = 99;
00085 
00087 int readConfiguration (int argc, char* argv[],
00088                        bool& ioIsBuiltin, bool& ioReadBookingRequestFromCmdLine,
00089                        stdair::Filename_T& ioInputFilename,
00090                        std::string& ioLogFilename,
00091                        std::string& ioBookingRequestString) {
00092   // Default for the booking request mode (whether it is read from command-line)
00093   ioReadBookingRequestFromCmdLine = K_AIRSCHED_DEFAULT_BOOKING_REQUEST_MODE;
00094 
00095   //
00096   WordList_T lWordList;
00097 
00098   // Declare a group of options that will be allowed only on command line
00099   boost::program_options::options_description generic ("Generic options");
00100   generic.add_options()
00101     ("prefix", "print installation prefix")
00102     ("version,v", "print version string")
00103     ("help,h", "produce help message");
00104     
00105   // Declare a group of options that will be allowed both on command
00106   // line and in config file
00107   boost::program_options::options_description config ("Configuration");
00108   config.add_options()
00109     ("builtin,b",
00110      "The sample BOM tree can be either built-in or parsed from input files. In that latter case, the -i/--input option must be specified as well")
00111     ("input,i",
00112      boost::program_options::value< std::string >(&ioInputFilename)->default_value(K_AIRSCHED_DEFAULT_INPUT_FILENAME),
00113      "(CSV) input file specifying the schedule (flight-period) entries")
00114     ("log,l",
00115      boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_AIRSCHED_DEFAULT_LOG_FILENAME),
00116      "Filename for the logs")
00117     ("read_booking_request,r",
00118      "Indicates that a booking request is given as a command-line option. That latter must then be given with the -b/--bkg_req option")
00119     ("bkg_req,q",
00120      boost::program_options::value< WordList_T >(&lWordList)->multitoken(),
00121      "Booking request word list (e.g. 'NCE BKK NCE 2007-04-21 2007-04-21 10:00:00 C 1 DF RO 5 NONE 10:0:0 2000.0 20.0'), which should be located at the end of the command line (otherwise, the other options would be interpreted as part of that booking request word list)")
00122     ;
00123 
00124   // Hidden options, will be allowed both on command line and
00125   // in config file, but will not be shown to the user.
00126   boost::program_options::options_description hidden ("Hidden options");
00127   hidden.add_options()
00128     ("copyright",
00129      boost::program_options::value< std::vector<std::string> >(),
00130      "Show the copyright (license)");
00131         
00132   boost::program_options::options_description cmdline_options;
00133   cmdline_options.add(generic).add(config).add(hidden);
00134 
00135   boost::program_options::options_description config_file_options;
00136   config_file_options.add(config).add(hidden);
00137 
00138   boost::program_options::options_description visible ("Allowed options");
00139   visible.add(generic).add(config);
00140         
00141   boost::program_options::positional_options_description p;
00142   p.add ("copyright", -1);
00143         
00144   boost::program_options::variables_map vm;
00145   boost::program_options::
00146     store (boost::program_options::command_line_parser (argc, argv).
00147            options (cmdline_options).positional(p).run(), vm);
00148 
00149   std::ifstream ifs ("airsched.cfg");
00150   boost::program_options::store (parse_config_file (ifs, config_file_options),
00151                                  vm);
00152   boost::program_options::notify (vm);
00153     
00154   if (vm.count ("help")) {
00155     std::cout << visible << std::endl;
00156     return K_AIRSCHED_EARLY_RETURN_STATUS;
00157   }
00158 
00159   if (vm.count ("version")) {
00160     std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
00161     return K_AIRSCHED_EARLY_RETURN_STATUS;
00162   }
00163 
00164   if (vm.count ("prefix")) {
00165     std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
00166     return K_AIRSCHED_EARLY_RETURN_STATUS;
00167   }
00168 
00169   if (vm.count ("builtin")) {
00170     ioIsBuiltin = true;
00171   }
00172   const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
00173   std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
00174 
00175   //
00176   std::ostringstream oErrorMessageStr;
00177   oErrorMessageStr << "Either the -b/--builtin option, or the -i/--input option"
00178                    << " must be specified";
00179 
00180   if (ioIsBuiltin == false) {
00181     if (vm.count ("input")) {
00182       ioInputFilename = vm["input"].as< std::string >();
00183       std::cout << "Input filename is: " << ioInputFilename << std::endl;
00184 
00185     } else {
00186       // The built-in option is not selected. However, no schedule input file
00187       // is specified
00188       std::cerr << oErrorMessageStr.str() << std::endl;
00189     }
00190   }
00191 
00192   //
00193   if (vm.count ("read_booking_request")) {
00194     ioReadBookingRequestFromCmdLine = true;
00195   }
00196   const std::string readBookingRequestFromCmdLineStr =
00197     (ioReadBookingRequestFromCmdLine == true)?"yes":"no";
00198   std::cout << "A booking request is to be given as command-line option? "
00199             << readBookingRequestFromCmdLineStr << std::endl;
00200 
00201   if (ioReadBookingRequestFromCmdLine == true) {
00202 
00203     if (lWordList.empty() == true) {
00204       std::cerr << "When the --read_booking_request/-r option is given, "
00205                 << "a query must also be provided (with the --bkg_req/-b "
00206                 << "option at the end of the command-line)" << std::endl;
00207       return K_AIRSCHED_EARLY_RETURN_STATUS;
00208     }
00209     
00210     // Rebuild the booking request query string
00211     ioBookingRequestString = createStringFromWordList (lWordList);
00212     std::cout << "The booking request string is: " << ioBookingRequestString
00213               << std::endl;
00214   }
00215 
00216   if (vm.count ("log")) {
00217     ioLogFilename = vm["log"].as< std::string >();
00218     std::cout << "Log filename is: " << ioLogFilename << std::endl;
00219   }
00220 
00221   return 0;
00222 }
00223 
00224 // //////////////////////////////////////////////////////////////
00225 stdair::BookingRequestStruct
00226 parseBookingRequest (const std::string& iRequestOption) {
00227   typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00228   boost::char_separator<char> sep(" -:");
00229 
00230   tokenizer tokens (iRequestOption, sep);
00231 
00232   // Origin (e.g., "NCE")
00233   tokenizer::iterator tok_iter = tokens.begin();
00234   assert (tok_iter != tokens.end());
00235   const stdair::AirportCode_T iOrigin (*tok_iter);
00236 
00237   // Destination (e.g., "BKK")
00238   ++tok_iter; assert (tok_iter != tokens.end());
00239   const stdair::AirportCode_T iDestination (*tok_iter);
00240   
00241   // POS (e.g., "NCE")
00242   ++tok_iter; assert (tok_iter != tokens.end());
00243   const stdair::AirportCode_T iPOS (*tok_iter);
00244   
00245   // Preferred departure date (e.g., "2007-04-21")
00246   ++tok_iter; assert (tok_iter != tokens.end());
00247   const short lDepDateYear = boost::lexical_cast<short> (*tok_iter);
00248   ++tok_iter; assert (tok_iter != tokens.end());
00249   const short lDepDateMonth = boost::lexical_cast<short> (*tok_iter);
00250   ++tok_iter; assert (tok_iter != tokens.end());
00251   const short lDepDateDay = boost::lexical_cast<short> (*tok_iter);
00252   const stdair::Date_T iDepartureDate(lDepDateYear, lDepDateMonth, lDepDateDay);
00253 
00254   // Request date (e.g., "2007-03-21")
00255   ++tok_iter; assert (tok_iter != tokens.end());
00256   const short lReqDateYear = boost::lexical_cast<short> (*tok_iter);
00257   ++tok_iter; assert (tok_iter != tokens.end());
00258   const short lReqDateMonth = boost::lexical_cast<short> (*tok_iter);
00259   ++tok_iter; assert (tok_iter != tokens.end());
00260   const short lReqDateDay = boost::lexical_cast<short> (*tok_iter);
00261   const stdair::Date_T iRequestDate (lReqDateYear, lReqDateMonth, lReqDateDay);
00262 
00263   // Request time (e.g., "08:34:23")
00264   ++tok_iter; assert (tok_iter != tokens.end());
00265   const short lReqTimeHours = boost::lexical_cast<short> (*tok_iter);
00266   ++tok_iter; assert (tok_iter != tokens.end());
00267   const short lReqTimeMinutes = boost::lexical_cast<short> (*tok_iter);
00268   ++tok_iter; assert (tok_iter != tokens.end());
00269   const short lReqTimeSeconds = boost::lexical_cast<short> (*tok_iter);
00270   const stdair::Duration_T iRequestTime (lReqTimeHours, lReqTimeMinutes,
00271                                          lReqTimeSeconds);
00272 
00273   // Request date-time (aggregation of the two items above)
00274   const stdair::DateTime_T iRequestDateTime (iRequestDate, iRequestTime);
00275   
00276   // Preferred cabin (e.g., "C")
00277   ++tok_iter; assert (tok_iter != tokens.end());
00278   const stdair::CabinCode_T iPreferredCabin (*tok_iter);
00279   
00280   // Party size (e.g., 1)
00281   ++tok_iter; assert (tok_iter != tokens.end());
00282   const stdair::NbOfSeats_T iPartySize = 1;
00283   
00284   // Channel (e.g., "DF")
00285   ++tok_iter; assert (tok_iter != tokens.end());
00286   const stdair::ChannelLabel_T iChannel (*tok_iter);
00287   
00288   // Trip type (e.g., "RO")
00289   ++tok_iter; assert (tok_iter != tokens.end());
00290   const stdair::TripType_T iTripType (*tok_iter);
00291   
00292   // Stay duration (e.g., 5)
00293   ++tok_iter; assert (tok_iter != tokens.end());
00294   const stdair::DayDuration_T iStayDuration = 5;
00295   
00296   // Frequent flyer (e.g., "NONE")
00297   ++tok_iter; assert (tok_iter != tokens.end());
00298   const stdair::FrequentFlyer_T iFrequentFlyerType ("NONE");
00299   
00300   // Preferred departure time (e.g., "10:00:00")
00301   ++tok_iter; assert (tok_iter != tokens.end());
00302   const short lPrefTimeHours = boost::lexical_cast<short> (*tok_iter);
00303   ++tok_iter; assert (tok_iter != tokens.end());
00304   const short lPrefTimeMinutes = boost::lexical_cast<short> (*tok_iter);
00305   ++tok_iter; assert (tok_iter != tokens.end());
00306   const short lPrefTimeSeconds = boost::lexical_cast<short> (*tok_iter);
00307   const stdair::Duration_T iPreferredDepartureTime (lPrefTimeHours,
00308                                                     lPrefTimeMinutes,
00309                                                     lPrefTimeSeconds);
00310 
00311   // Willingness-to-pay (e.g., 2000.0)
00312   ++tok_iter; assert (tok_iter != tokens.end());
00313   const stdair::WTP_T iWTP = 2000.0;
00314   
00315   // Value of time (e.g., 20.0)
00316   ++tok_iter; assert (tok_iter != tokens.end());
00317   const stdair::PriceValue_T iValueOfTime = 20.0;
00318 
00319   // Build and return the booking request structure
00320   return stdair::BookingRequestStruct (iOrigin,
00321                                        iDestination, iPOS,
00322                                        iDepartureDate, iRequestDateTime,
00323                                        iPreferredCabin, iPartySize,
00324                                        iChannel, iTripType, iStayDuration,
00325                                        iFrequentFlyerType,
00326                                        iPreferredDepartureTime, iWTP,
00327                                        iValueOfTime);
00328 }
00329 
00330 // ///////// M A I N ////////////
00331 int main (int argc, char* argv[]) {
00332 
00333   // State whether the BOM tree should be built-in or parsed from an
00334   // input file
00335   bool isBuiltin;
00336 
00337   // A booking request should be given as command-line option
00338   bool readBookingRequestFromCmdLine;
00339     
00340   // Input file name
00341   stdair::Filename_T lInputFilename;
00342 
00343   // Output log File
00344   stdair::Filename_T lLogFilename;
00345 
00346   // Booking request string
00347   std::string lBookingRequestString;
00348     
00349   // Call the command-line option parser
00350   const int lOptionParserStatus = 
00351     readConfiguration (argc, argv, isBuiltin, readBookingRequestFromCmdLine,
00352                        lInputFilename, lLogFilename, lBookingRequestString);
00353 
00354   if (lOptionParserStatus == K_AIRSCHED_EARLY_RETURN_STATUS) {
00355     return 0;
00356   }
00357 
00358   // Set the log parameters
00359   std::ofstream logOutputFile;
00360   // Open and clean the log outputfile
00361   logOutputFile.open (lLogFilename.c_str());
00362   logOutputFile.clear();
00363 
00364   // Initialise the AirSched service object
00365   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00366   AIRSCHED::AIRSCHED_Service airschedService (lLogParams);
00367 
00368   // Check wether or not (CSV) input files should be read
00369   if (isBuiltin == true) {
00370 
00371     // Build the sample BOM tree
00372     airschedService.buildSampleBom();
00373 
00374   } else {
00375     // Build the BOM tree from parsing input files
00376     airschedService.parseAndLoad (lInputFilename);
00377   }
00378 
00379   // Check wether or not a booking request is given as a command-line option
00380   if (readBookingRequestFromCmdLine == false) {
00381     lBookingRequestString = K_AIRSCHED_DEFAULT_BOOKING_REQUEST;
00382   }    
00383 
00384   // DEBUG
00385   STDAIR_LOG_DEBUG("Booking request string: '" << lBookingRequestString << "'");
00386 
00387   // Create a booking request object
00388   const stdair::BookingRequestStruct& lBookingRequest =
00389     parseBookingRequest (lBookingRequestString);
00390 
00391   //
00392   stdair::TravelSolutionList_T lTravelSolutionList;
00393   airschedService.buildSegmentPathList (lTravelSolutionList, lBookingRequest);
00394   
00395   // DEBUG
00396   STDAIR_LOG_DEBUG ("Parsed booking request: " << lBookingRequest);
00397 
00398   // DEBUG
00399   std::ostringstream oStream;
00400   stdair::BomDisplay::csvDisplay (oStream, lTravelSolutionList);
00401   STDAIR_LOG_DEBUG (oStream.str());
00402 
00403   // Close the Log outputFile
00404   logOutputFile.close();
00405 
00406   return 0;     
00407 }