StdAir Logo  1.00.13
C++ Standard Airline IT Object Library
Loading...
Searching...
No Matches
stdair.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/date_time/posix_time/posix_time.hpp>
13#include <boost/date_time/gregorian/gregorian.hpp>
14#include <boost/program_options.hpp>
15#include <boost/tokenizer.hpp>
16#include <boost/lexical_cast.hpp>
17// StdAir
24#include <stdair/config/stdair-paths.hpp>
25
26// //////// Constants //////
30const std::string K_STDAIR_DEFAULT_LOG_FILENAME ("stdair.log");
31
35const std::string K_STDAIR_DEFAULT_INPUT_FILENAME (STDAIR_SAMPLE_DIR
36 "/schedule01.csv");
37
42const bool K_STDAIR_DEFAULT_BUILT_IN_INPUT = false;
43
49const bool K_STDAIR_DEFAULT_BUILT_FOR_RMOL = false;
50
56const bool K_STDAIR_DEFAULT_BUILT_FOR_CRS = false;
57
62const int K_STDAIR_EARLY_RETURN_STATUS = 99;
63
64// ///////// Parsing of Options & Configuration /////////
65// A helper function to simplify the main part.
66template<class T> std::ostream& operator<< (std::ostream& os,
67 const std::vector<T>& v) {
68 std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " "));
69 return os;
70}
71
73int readConfiguration (int argc, char* argv[], bool& ioIsBuiltin,
74 bool& ioIsForRMOL, bool& ioIsForCRS,
75 stdair::Filename_T& ioInputFilename,
76 std::string& ioLogFilename) {
77 // Default for the built-in input
78 ioIsBuiltin = K_STDAIR_DEFAULT_BUILT_IN_INPUT;
79
80 // Default for the RMOL input
81 ioIsForRMOL = K_STDAIR_DEFAULT_BUILT_FOR_RMOL;
82
83 // Default for the CRS input
84 ioIsForCRS = K_STDAIR_DEFAULT_BUILT_FOR_CRS;
85
86 // Declare a group of options that will be allowed only on command line
87 boost::program_options::options_description generic ("Generic options");
88 generic.add_options()
89 ("prefix", "print installation prefix")
90 ("version,v", "print version string")
91 ("help,h", "produce help message");
92
93 // Declare a group of options that will be allowed both on command
94 // line and in config file
95
96 boost::program_options::options_description config ("Configuration");
97 config.add_options()
98 ("builtin,b",
99 "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")
100 ("rmol,r",
101 "Build a sample BOM tree for RMOL (i.e., a dummy flight-date with a single leg-cabin)")
102 ("crs,c",
103 "Build a sample BOM tree for CRS")
104 ("input,i",
105 boost::program_options::value< std::string >(&ioInputFilename)->default_value(K_STDAIR_DEFAULT_INPUT_FILENAME),
106 "(CVS) input file for the demand distributions")
107 ("log,l",
108 boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_STDAIR_DEFAULT_LOG_FILENAME),
109 "Filename for the logs")
110 ;
111
112 // Hidden options, will be allowed both on command line and
113 // in config file, but will not be shown to the user.
114 boost::program_options::options_description hidden ("Hidden options");
115 hidden.add_options()
116 ("copyright",
117 boost::program_options::value< std::vector<std::string> >(),
118 "Show the copyright (license)");
119
120 boost::program_options::options_description cmdline_options;
121 cmdline_options.add(generic).add(config).add(hidden);
122
123 boost::program_options::options_description config_file_options;
124 config_file_options.add(config).add(hidden);
125 boost::program_options::options_description visible ("Allowed options");
126 visible.add(generic).add(config);
127
128 boost::program_options::positional_options_description p;
129 p.add ("copyright", -1);
130
131 boost::program_options::variables_map vm;
132 boost::program_options::
133 store (boost::program_options::command_line_parser (argc, argv).
134 options (cmdline_options).positional(p).run(), vm);
135
136 std::ifstream ifs ("stdair.cfg");
137 boost::program_options::store (parse_config_file (ifs, config_file_options),
138 vm);
139 boost::program_options::notify (vm);
140
141 if (vm.count ("help")) {
142 std::cout << visible << std::endl;
143 return K_STDAIR_EARLY_RETURN_STATUS;
144 }
145
146 if (vm.count ("version")) {
147 std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
148 return K_STDAIR_EARLY_RETURN_STATUS;
149 }
150
151 if (vm.count ("prefix")) {
152 std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
153 return K_STDAIR_EARLY_RETURN_STATUS;
154 }
155
156 if (vm.count ("builtin")) {
157 ioIsBuiltin = true;
158 }
159
160 if (vm.count ("rmol")) {
161 ioIsForRMOL = true;
162
163 // The RMOL sample tree takes precedence over the default built-in BOM tree
164 ioIsBuiltin = false;
165 }
166
167 if (vm.count ("crs")) {
168 ioIsForCRS = true;
169
170 // The RMOL sample tree takes precedence over the default built-in BOM tree
171 ioIsBuiltin = false;
172 }
173
174 const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
175 std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
176
177 const std::string isForRMOLStr = (ioIsForRMOL == true)?"yes":"no";
178 std::cout << "The BOM should be built-in for RMOL? " << isForRMOLStr
179 << std::endl;
180
181 const std::string isForCRSStr = (ioIsForCRS == true)?"yes":"no";
182 std::cout << "The BOM should be built-in for CRS? " << isForCRSStr
183 << std::endl;
184
185 if (ioIsBuiltin == false && ioIsForRMOL == false && ioIsForCRS == false) {
186 if (vm.count ("input")) {
187 ioInputFilename = vm["input"].as< std::string >();
188 std::cout << "Input filename is: " << ioInputFilename << std::endl;
189
190 } else {
191 std::cerr << "Either one among the -b/--builtin, -r/--rmol, -c/--crs "
192 << "or -i/--input options must be specified" << std::endl;
193 }
194 }
195
196 if (vm.count ("log")) {
197 ioLogFilename = vm["log"].as< std::string >();
198 std::cout << "Log filename is: " << ioLogFilename << std::endl;
199 }
200
201 return 0;
202}
203
204
205// ///////////////// M A I N ////////////////////
206int main (int argc, char* argv[]) {
207
208 // State whether the BOM tree should be built-in or parsed from an
209 // input file
210 bool isBuiltin;
211
212 // State whether a sample BOM tree should be built for RMOL.
213 bool isForRMOL;
214
215 // State whether a sample BOM tree should be built for the CRS.
216 bool isForCRS;
217
218 // Input file name
219 stdair::Filename_T lInputFilename;
220
221 // Output log File
222 std::string lLogFilename;
223
224 // Call the command-line option parser
225 const int lOptionParserStatus =
226 readConfiguration (argc, argv, isBuiltin, isForRMOL, isForCRS,
227 lInputFilename, lLogFilename);
228
229 if (lOptionParserStatus == K_STDAIR_EARLY_RETURN_STATUS) {
230 return 0;
231 }
232
233 // Set the log parameters
234 std::ofstream logOutputFile;
235 // Open and clean the log outputfile
236 logOutputFile.open (lLogFilename.c_str());
237 logOutputFile.clear();
238
239 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
240 stdair::STDAIR_Service stdairService (lLogParams);
241
242 // DEBUG
243 STDAIR_LOG_DEBUG ("Welcome to stdair");
244
245 // Check wether or not a (CSV) input file should be read
246 if (isBuiltin == true || isForRMOL == true || isForCRS == true) {
247
248 if (isForRMOL == true) {
249 // Build the sample BOM tree for RMOL
250 stdairService.buildDummyInventory (300);
251
252 } else if (isForCRS == true) {
253 //
254 stdair::TravelSolutionList_T lTravelSolutionList;
255 stdairService.buildSampleTravelSolutions (lTravelSolutionList);
256
257 // Build the sample BOM tree for CRS
258 const stdair::BookingRequestStruct& lBookingRequest =
259 stdairService.buildSampleBookingRequest();
260
261 // DEBUG: Display the travel solution and booking request
262 STDAIR_LOG_DEBUG ("Booking request: " << lBookingRequest.display());
263
264 const std::string& lCSVDump =
265 stdairService.csvDisplay (lTravelSolutionList);
266 STDAIR_LOG_DEBUG (lCSVDump);
267
268 } else {
269 assert (isBuiltin == true);
270
271 // Build a sample BOM tree
272 stdairService.buildSampleBom();
273 }
274
275 } else {
276 // Read the input file
277 //stdairService.readFromInputFile (lInputFilename);
278
279 // DEBUG
280 STDAIR_LOG_DEBUG ("StdAir will parse " << lInputFilename
281 << " and build the corresponding BOM tree.");
282 }
283
284 // DEBUG: Display the whole persistent BOM tree
285 const std::string& lCSVDump = stdairService.csvDisplay ();
286 STDAIR_LOG_DEBUG (lCSVDump);
287
288 // Close the Log outputFile
289 logOutputFile.close();
290
291 /*
292 Note: as that program is not intended to be run on a server in
293 production, it is better not to catch the exceptions. When it
294 happens (that an exception is throwned), that way we get the
295 call stack.
296 */
297
298 return 0;
299}
300
#define STDAIR_LOG_DEBUG(iToBeLogged)
Definition Logger.hpp:32
int main(int argc, char *argv[])
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &ioOut, const stdair::StructAbstract &iStruct)
std::list< TravelSolutionStruct > TravelSolutionList_T
std::string Filename_T
Structure holding parameters for logging.
Structure holding the elements of a booking request.
const std::string display() const
Interface for the STDAIR Services.