TravelCCM Logo  1.00.1
C++ Travel Customer Choice Model Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
travelccm.cpp
Go to the documentation of this file.
1 
5 // STL
6 #include <cassert>
7 #include <iostream>
8 #include <sstream>
9 #include <fstream>
10 #include <string>
11 // Boost (Extended STL)
12 #include <boost/program_options.hpp>
13 // StdAir
14 #include <stdair/basic/BasLogParams.hpp>
15 #include <stdair/basic/BasDBParams.hpp>
16 #include <stdair/bom/BookingRequestStruct.hpp>
17 #include <stdair/bom/TravelSolutionStruct.hpp>
18 #include <stdair/service/Logger.hpp>
19 // TravelCCM
21 #include <travelccm/config/travelccm-paths.hpp>
22 
23 // //////// Constants //////
27 const std::string K_TRAVELCCM_DEFAULT_LOG_FILENAME ("travelccm.log");
28 
32 const std::string K_TRAVELCCM_DEFAULT_INPUT_FILENAME (STDAIR_SAMPLE_DIR
33  "/ccm_01.csv");
34 
39 const bool K_TRAVELCCM_DEFAULT_BUILT_IN_INPUT = false;
40 
44 const int K_TRAVELCCM_EARLY_RETURN_STATUS = 99;
45 
46 
47 // ///////// Parsing of Options & Configuration /////////
48 // A helper function to simplify the main part.
49 template<class T> std::ostream& operator<< (std::ostream& os,
50  const std::vector<T>& v) {
51  std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " "));
52  return os;
53 }
54 
58 int readConfiguration (int argc, char* argv[], bool& ioIsBuiltin,
59  stdair::Filename_T& lInputFilename,
60  stdair::Filename_T& lLogFilename) {
61 
62  // Default for the built-in input
63  ioIsBuiltin = K_TRAVELCCM_DEFAULT_BUILT_IN_INPUT;
64 
65  // Declare a group of options that will be allowed only on command line
66  boost::program_options::options_description generic ("Generic options");
67  generic.add_options()
68  ("prefix", "print installation prefix")
69  ("version,v", "print version string")
70  ("help,h", "produce help message");
71 
72  // Declare a group of options that will be allowed both on command
73  // line and in config file
74  boost::program_options::options_description config ("Configuration");
75  config.add_options()
76  ("builtin,b",
77  "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")
78  ("input,i",
79  boost::program_options::value< std::string >(&lInputFilename)->default_value(K_TRAVELCCM_DEFAULT_INPUT_FILENAME),
80  "(CSV) input file for the customer choice rule sets")
81  ("log,l",
82  boost::program_options::value< std::string >(&lLogFilename)->default_value(K_TRAVELCCM_DEFAULT_LOG_FILENAME),
83  "Filename for the logs")
84  ;
85 
86  // Hidden options, will be allowed both on command line and
87  // in config file, but will not be shown to the user.
88  boost::program_options::options_description hidden ("Hidden options");
89  hidden.add_options()
90  ("copyright",
91  boost::program_options::value< std::vector<std::string> >(),
92  "Show the copyright (license)");
93 
94  boost::program_options::options_description cmdline_options;
95  cmdline_options.add(generic).add(config).add(hidden);
96 
97  boost::program_options::options_description config_file_options;
98  config_file_options.add(config).add(hidden);
99 
100  boost::program_options::options_description visible ("Allowed options");
101  visible.add(generic).add(config);
102 
103  boost::program_options::positional_options_description p;
104  p.add ("copyright", -1);
105 
106  boost::program_options::variables_map vm;
107  boost::program_options::
108  store (boost::program_options::command_line_parser (argc, argv).
109  options (cmdline_options).positional(p).run(), vm);
110 
111  std::ifstream ifs ("travelccm.cfg");
112  boost::program_options::store (parse_config_file (ifs, config_file_options),
113  vm);
114  boost::program_options::notify (vm);
115 
116  if (vm.count ("help")) {
117  std::cout << visible << std::endl;
118  return K_TRAVELCCM_EARLY_RETURN_STATUS;
119  }
120 
121  if (vm.count ("version")) {
122  std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
123  return K_TRAVELCCM_EARLY_RETURN_STATUS;
124  }
125 
126  if (vm.count ("prefix")) {
127  std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
128  return K_TRAVELCCM_EARLY_RETURN_STATUS;
129  }
130 
131  if (vm.count ("builtin")) {
132  ioIsBuiltin = true;
133  }
134  const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
135  std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
136 
137  if (ioIsBuiltin == false) {
138 
139  // The BOM tree should be built from parsing a customer-choice rule file
140  if (vm.count ("input")) {
141  lInputFilename = vm["input"].as< std::string >();
142  std::cout << "Input filename is: " << lInputFilename << std::endl;
143 
144  } else {
145  // The built-in option is not selected. However, no demand input file
146  // is specified
147  std::cerr << "Either one among the -b/--builtin and -i/--input "
148  << "options must be specified" << std::endl;
149  }
150  }
151 
152  if (vm.count ("log")) {
153  lLogFilename = vm["log"].as< std::string >();
154  std::cout << "Log filename is: " << lLogFilename << std::endl;
155  }
156 
157  return 0;
158 }
159 
160 
161 // ///////// M A I N ////////////
162 int main (int argc, char* argv[]) {
163 
164  // State whether the BOM tree should be built-in or parsed from an input file
165  bool isBuiltin;
166 
167  // Input file name
168  stdair::Filename_T lInputFilename;
169 
170  // Output log File
171  stdair::Filename_T lLogFilename;
172 
173  // Call the command-line option parser
174  const int lOptionParserStatus =
175  readConfiguration (argc, argv, isBuiltin, lInputFilename, lLogFilename);
176 
177  if (lOptionParserStatus == K_TRAVELCCM_EARLY_RETURN_STATUS) {
178  return 0;
179  }
180 
181  // Set the log parameters
182  std::ofstream logOutputFile;
183  // Open and clean the log outputfile
184  logOutputFile.open (lLogFilename.c_str());
185  logOutputFile.clear();
186 
187  // Initialise the service context
188  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
189 
190  // Build the BOM tree
191  TRAVELCCM::TRAVELCCM_Service travelccmService (lLogParams);
192 
193  // DEBUG
194  STDAIR_LOG_DEBUG ("Welcome to TravelCCM");
195 
196  // Check wether or not a (CSV) input file should be read
197  if (isBuiltin == true) {
198  // Create a sample Customer-Choice rule object, and insert it
199  // within the BOM tree
200  travelccmService.buildSampleBom();
201 
202  } else {
209  // travelccmService.parseAndLoad (lInputFilename);
210  }
211 
212  // Build a list of travel solutions
213  const stdair::BookingRequestStruct& lBookingRequest =
214  travelccmService.buildSampleBookingRequest();
215 
216  // DEBUG
217  STDAIR_LOG_DEBUG ("Booking request: " << lBookingRequest.display());
218 
219  // Build the sample BOM tree
220  stdair::TravelSolutionList_T lTSList;
221  travelccmService.buildSampleTravelSolutions (lTSList);
222 
223  // DEBUG: Display the list of travel solutions
224  const std::string& lCSVDump = travelccmService.csvDisplay (lTSList);
225  STDAIR_LOG_DEBUG (lCSVDump);
226 
227  // Choose a travel solution
228  const stdair::TravelSolutionStruct* lTS_ptr =
229  travelccmService.chooseTravelSolution (lTSList, lBookingRequest);
230 
231  if (lTS_ptr != NULL) {
232  // DEBUG
233  STDAIR_LOG_DEBUG ("Chosen travel solution: " << lTS_ptr->display());
234 
235  } else {
236  // DEBUG
237  STDAIR_LOG_DEBUG ("No travel solution can be found for "
238  << lBookingRequest.display()
239  << " within the following list of travel solutions");
240  STDAIR_LOG_DEBUG (lCSVDump);
241  }
242 
243  // Close the Log outputFile
244  logOutputFile.close();
245 
253  return 0;
254 }