SimFQT Logo  1.00.0
C++ Simulated Fare Quote System Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
simfqt_parseFareRules.cpp
Go to the documentation of this file.
1 // STL
2 #include <cassert>
3 #include <iostream>
4 #include <sstream>
5 #include <fstream>
6 #include <vector>
7 #include <list>
8 #include <string>
9 // Boost (Extended STL)
10 #include <boost/date_time/posix_time/posix_time.hpp>
11 #include <boost/date_time/gregorian/gregorian.hpp>
12 #include <boost/tokenizer.hpp>
13 #include <boost/program_options.hpp>
14 // StdAir
15 #include <stdair/STDAIR_Service.hpp>
16 #include <stdair/bom/TravelSolutionStruct.hpp>
17 #include <stdair/bom/BookingRequestStruct.hpp>
18 #include <stdair/service/Logger.hpp>
19 // Simfqt
21 #include <simfqt/config/simfqt-paths.hpp>
22 
23 // //////// Type definitions ///////
24 typedef std::vector<std::string> WordList_T;
25 
26 
27 // //////// Constants //////
29 const std::string K_SIMFQT_DEFAULT_LOG_FILENAME ("simfqt_parseFareRules.log");
30 
32 const std::string K_SIMFQT_DEFAULT_FARE_INPUT_FILENAME (STDAIR_SAMPLE_DIR
33  "/fare01.csv");
34 
38 
41 
42 // ///////// Parsing of Options & Configuration /////////
43 // A helper function to simplify the main part.
44 template<class T> std::ostream& operator<< (std::ostream& os,
45  const std::vector<T>& v) {
46  std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " "));
47  return os;
48 }
49 
51 int readConfiguration (int argc, char* argv[], bool& ioIsBuiltin,
52  stdair::Filename_T& ioFareInputFilename,
53  std::string& ioLogFilename) {
54 
55  // Default for the built-in input
56  ioIsBuiltin = K_SIMFQT_DEFAULT_BUILT_IN_INPUT;
57 
58  // Declare a group of options that will be allowed only on command line
59  boost::program_options::options_description generic ("Generic options");
60  generic.add_options()
61  ("prefix", "print installation prefix")
62  ("version,v", "print version string")
63  ("help,h", "produce help message");
64 
65  // Declare a group of options that will be allowed both on command
66  // line and in config file
67  boost::program_options::options_description config ("Configuration");
68  config.add_options()
69  ("builtin,b",
70  "The sample BOM tree can be either built-in or parsed from an input file. That latter must then be given with the -f/--fare option")
71  ("fare,f",
72  boost::program_options::value< std::string >(&ioFareInputFilename)->default_value(K_SIMFQT_DEFAULT_FARE_INPUT_FILENAME),
73  "(CSV) input file for the fare rules")
74  ("log,l",
75  boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_SIMFQT_DEFAULT_LOG_FILENAME),
76  "Filename for the logs")
77  ;
78 
79  // Hidden options, will be allowed both on command line and
80  // in config file, but will not be shown to the user.
81  boost::program_options::options_description hidden ("Hidden options");
82  hidden.add_options()
83  ("copyright",
84  boost::program_options::value< std::vector<std::string> >(),
85  "Show the copyright (license)");
86 
87  boost::program_options::options_description cmdline_options;
88  cmdline_options.add(generic).add(config).add(hidden);
89 
90  boost::program_options::options_description config_file_options;
91  config_file_options.add(config).add(hidden);
92 
93  boost::program_options::options_description visible ("Allowed options");
94  visible.add(generic).add(config);
95 
96  boost::program_options::positional_options_description p;
97  p.add ("copyright", -1);
98 
99  boost::program_options::variables_map vm;
100  boost::program_options::
101  store (boost::program_options::command_line_parser (argc, argv).
102  options (cmdline_options).positional(p).run(), vm);
103 
104  std::ifstream ifs ("simfqt.cfg");
105  boost::program_options::store (parse_config_file (ifs, config_file_options),
106  vm);
107  boost::program_options::notify (vm); if (vm.count ("help")) {
108  std::cout << visible << std::endl;
110  }
111 
112  if (vm.count ("version")) {
113  std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
115  }
116 
117  if (vm.count ("prefix")) {
118  std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
120  }
121 
122  if (vm.count ("builtin")) {
123  ioIsBuiltin = true;
124  }
125  const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
126  std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
127 
128  if (ioIsBuiltin == false) {
129 
130  // The BOM tree should be built from parsing a fare (and O&D) file
131  if (vm.count ("fare")) {
132  ioFareInputFilename = vm["fare"].as< std::string >();
133  std::cout << "Input fare filename is: " << ioFareInputFilename
134  << std::endl;
135 
136  } else {
137  // The built-in option is not selected. However, no fare file
138  // is specified
139  std::cerr << "Either one among the -b/--builtin and -f/--fare "
140  << "options must be specified" << std::endl;
141  }
142  }
143 
144  if (vm.count ("log")) {
145  ioLogFilename = vm["log"].as< std::string >();
146  std::cout << "Log filename is: " << ioLogFilename << std::endl;
147  }
148 
149  return 0;
150 }
151 
152 
153 // /////////////// M A I N /////////////////
154 int main (int argc, char* argv[]) {
155 
156  // State whether the BOM tree should be built-in or parsed from an input file
157  bool isBuiltin;
158 
159  // Fare input filename
160  stdair::Filename_T lFareInputFilename;
161 
162  // Output log File
163  stdair::Filename_T lLogFilename;
164 
165  // Call the command-line option parser
166  const int lOptionParserStatus =
167  readConfiguration (argc, argv, isBuiltin, lFareInputFilename, lLogFilename);
168 
169  if (lOptionParserStatus == K_SIMFQT_EARLY_RETURN_STATUS) {
170  return 0;
171  }
172 
173  // Set the log parameters
174  std::ofstream logOutputFile;
175  // Open and clean the log outputfile
176  logOutputFile.open (lLogFilename.c_str());
177  logOutputFile.clear();
178 
179  // Initialise the Simfqt service object
180  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
181 
182  SIMFQT::SIMFQT_Service simfqtService (lLogParams);
183 
184  // DEBUG
185  STDAIR_LOG_DEBUG ("Welcome to Simfqt");
186 
187  // Build a default sample list of travel solutions
188  stdair::TravelSolutionList_T lTravelSolutionList;
189  simfqtService.buildSampleTravelSolutions (lTravelSolutionList);
190 
191  // Build a default booking request
192  stdair::BookingRequestStruct lBookingRequest =
193  simfqtService.buildBookingRequest();
194 
195  // Check wether or not a (CSV) input file should be read
196  if (isBuiltin == true) {
197 
198  // Build the default sample BOM tree (filled with fares) for Simfqt
199  simfqtService.buildSampleBom();
200 
201  } else {
202 
203  // Build the BOM tree from parsing a fare file
204  SIMFQT::FareFilePath lFareFilePath (lFareInputFilename);
205  simfqtService.parseAndLoad (lFareFilePath);
206 
207  }
208 
209  // DEBUG: Display the travel solutions
210  const std::string& lTSCSVDump =
211  simfqtService.csvDisplay (lTravelSolutionList);
212  STDAIR_LOG_DEBUG (lTSCSVDump);
213 
214  // FareQuote the sample list of travel solutions
215  simfqtService.quotePrices (lBookingRequest, lTravelSolutionList);
216 
217  // DEBUG: Display the whole BOM tree
218  const std::string& lBOMCSVDump = simfqtService.csvDisplay();
219  STDAIR_LOG_DEBUG ("BOM tree: " << lBOMCSVDump);
220 
221  // DEBUG: Display the travel solutions
222  const std::string& lTSCSVDumpEnd
223  = simfqtService.csvDisplay (lTravelSolutionList);
224  STDAIR_LOG_DEBUG (lTSCSVDumpEnd);
225 
226  // Close the Log outputFile
227  logOutputFile.close();
228 
229  /*
230  Note: as that program is not intended to be run on a server in
231  production, it is better not to catch the exceptions. When it
232  happens (that an exception is throwned), that way we get the
233  call stack.
234  */
235 
236  return 0;
237 }
238