TraDemGen Logo  0.2.2
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
pytrademgen.cpp
Go to the documentation of this file.
1 // STL
2 #include <cassert>
3 #include <stdexcept>
4 #include <fstream>
5 #include <sstream>
6 #include <string>
7 #include <list>
8 #include <vector>
9 // Boost String
10 #include <boost/python.hpp>
11 // StdAir
12 #include <stdair/stdair_basic_types.hpp>
13 #include <stdair/stdair_exceptions.hpp>
14 #include <stdair/basic/BasFileMgr.hpp>
15 #include <stdair/basic/BasLogParams.hpp>
16 #include <stdair/basic/BasDBParams.hpp>
17 // TraDemGen
19 
20 namespace TRADEMGEN {
21 
22  struct Trademgener {
23  public:
25  std::string trademgen (const std::string& iQuery) {
26  std::ostringstream oStream;
27 
28  // Sanity check
29  if (_logOutputStream == NULL) {
30  oStream << "The log filepath is not valid." << std::endl;
31  return oStream.str();
32  }
33  assert (_logOutputStream != NULL);
34 
35  try {
36 
37  // DEBUG
38  *_logOutputStream << "Python search for '" << iQuery << "'"
39  << std::endl;
40 
41  if (_trademgenService == NULL) {
42  oStream << "The Trademgen service has not been initialised, "
43  << "i.e., the init() method has not been called "
44  << "correctly on the Trademgener object. Please "
45  << "check that all the parameters are not empty and "
46  << "point to actual files.";
47  *_logOutputStream << oStream.str();
48  return oStream.str();
49  }
50  assert (_trademgenService != NULL);
51 
52  // Do the trademgen
53  _trademgenService->displayAirlineListFromDB();
54 
55  // DEBUG
56  *_logOutputStream << "Python search for '" << iQuery
57  << "' returned '" << std::endl;
58 
59  // DEBUG
60  *_logOutputStream << "TraDemGen output: "
61  << oStream.str() << std::endl;
62 
63  } catch (const stdair::RootException& eTrademgenError) {
64  *_logOutputStream << "TraDemGen error: " << eTrademgenError.what()
65  << std::endl;
66 
67  } catch (const std::exception& eStdError) {
68  *_logOutputStream << "Error: " << eStdError.what() << std::endl;
69 
70  } catch (...) {
71  *_logOutputStream << "Unknown error" << std::endl;
72  }
73 
74  return oStream.str();
75  }
76 
77  public:
79  Trademgener() : _trademgenService (NULL), _logOutputStream (NULL) {
80  }
81 
83  Trademgener (const Trademgener& iTrademgener)
84  : _trademgenService (iTrademgener._trademgenService),
85  _logOutputStream (iTrademgener._logOutputStream) {
86  }
87 
90  _trademgenService = NULL;
91  _logOutputStream = NULL;
92  }
93 
95  bool init (const std::string& iLogFilepath,
96  const stdair::RandomSeed_T& iRandomSeed,
97  const stdair::Filename_T& iDemandInputFilename,
98  const std::string& iDBUser, const std::string& iDBPasswd,
99  const std::string& iDBHost, const std::string& iDBPort,
100  const std::string& iDBDBName) {
101  bool isEverythingOK = true;
102 
103  try {
104 
105  // Check that the file path given as input corresponds to an actual file
106  const bool isWriteable = (iLogFilepath.empty() == false);
107  // stdair::BasFileMgr::isWriteable (iLogFilepath);
108  if (isWriteable == false) {
109  isEverythingOK = false;
110  return isEverythingOK;
111  }
112 
113  // Set the log parameters
114  _logOutputStream = new std::ofstream;
115  assert (_logOutputStream != NULL);
116 
117  // Open and clean the log outputfile
118  _logOutputStream->open (iLogFilepath.c_str());
119  _logOutputStream->clear();
120 
121  // DEBUG
122  *_logOutputStream << "Python wrapper initialisation" << std::endl;
123  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG,
124  *_logOutputStream);
125 
126  // Initialise the context
127  stdair::BasDBParams lDBParams (iDBUser, iDBPasswd, iDBHost, iDBPort,
128  iDBDBName);
129  _trademgenService = new TRADEMGEN_Service (lLogParams, lDBParams,
130  iRandomSeed);
131  assert (_trademgenService != NULL);
132 
133  // Create the DemandStream objects, and insert them within the BOM tree
134  _trademgenService->parseAndLoad (iDemandInputFilename);
135 
136  // DEBUG
137  *_logOutputStream << "Python wrapper initialised" << std::endl;
138 
139  } catch (const stdair::RootException& eTrademgenError) {
140  *_logOutputStream << "Trademgen error: " << eTrademgenError.what()
141  << std::endl;
142 
143  } catch (const std::exception& eStdError) {
144  *_logOutputStream << "Error: " << eStdError.what() << std::endl;
145 
146  } catch (...) {
147  *_logOutputStream << "Unknown error" << std::endl;
148  }
149 
150  return isEverythingOK;
151  }
152 
153  private:
155  TRADEMGEN_Service* _trademgenService;
156  std::ofstream* _logOutputStream;
157  };
158 
159 }
160 
161 // /////////////////////////////////////////////////////////////
162 BOOST_PYTHON_MODULE(libpytrademgen) {
163  boost::python::class_<TRADEMGEN::Trademgener> ("Trademgener")
164  .def ("trademgen", &TRADEMGEN::Trademgener::trademgen)
165  .def ("init", &TRADEMGEN::Trademgener::init);
166 }