TraDemGen Logo  0.2.2
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
pytrademgen.cpp
Go to the documentation of this file.
00001 // STL
00002 #include <cassert>
00003 #include <stdexcept>
00004 #include <fstream>
00005 #include <sstream>
00006 #include <string>
00007 #include <list>
00008 #include <vector>
00009 // Boost String
00010 #include <boost/python.hpp>
00011 // StdAir
00012 #include <stdair/stdair_basic_types.hpp>
00013 #include <stdair/stdair_exceptions.hpp>
00014 #include <stdair/basic/BasFileMgr.hpp>
00015 #include <stdair/basic/BasLogParams.hpp>
00016 #include <stdair/basic/BasDBParams.hpp>
00017 // TraDemGen
00018 #include <trademgen/TRADEMGEN_Service.hpp>
00019 
00020 namespace TRADEMGEN {
00021 
00022   struct Trademgener {
00023   public:
00025     std::string trademgen (const std::string& iQuery) {
00026       std::ostringstream oStream;
00027 
00028       // Sanity check
00029       if (_logOutputStream == NULL) {
00030         oStream << "The log filepath is not valid." << std::endl;
00031         return oStream.str();
00032       }
00033       assert (_logOutputStream != NULL);
00034       
00035       try {
00036 
00037         // DEBUG
00038         *_logOutputStream << "Python search for '" << iQuery << "'"
00039                           << std::endl;
00040       
00041         if (_trademgenService == NULL) {
00042           oStream << "The Trademgen service has not been initialised, "
00043                   << "i.e., the init() method has not been called "
00044                   << "correctly on the Trademgener object. Please "
00045                   << "check that all the parameters are not empty and "
00046                   << "point to actual files.";
00047           *_logOutputStream << oStream.str();
00048           return oStream.str();
00049         }
00050         assert (_trademgenService != NULL);
00051         
00052         // Do the trademgen
00053         _trademgenService->displayAirlineListFromDB();
00054 
00055         // DEBUG
00056         *_logOutputStream << "Python search for '" << iQuery
00057                           << "' returned '" << std::endl;
00058 
00059         // DEBUG
00060         *_logOutputStream << "TraDemGen output: "
00061                           << oStream.str() << std::endl;
00062 
00063       } catch (const stdair::RootException& eTrademgenError) {
00064         *_logOutputStream << "TraDemGen error: "  << eTrademgenError.what()
00065                           << std::endl;
00066         
00067       } catch (const std::exception& eStdError) {
00068         *_logOutputStream << "Error: "  << eStdError.what() << std::endl;
00069         
00070       } catch (...) {
00071         *_logOutputStream << "Unknown error" << std::endl;
00072       }
00073 
00074       return oStream.str();
00075     }
00076 
00077   public:
00079     Trademgener() : _trademgenService (NULL), _logOutputStream (NULL) {
00080     }
00081     
00083     Trademgener (const Trademgener& iTrademgener)
00084       : _trademgenService (iTrademgener._trademgenService),
00085         _logOutputStream (iTrademgener._logOutputStream) {
00086     }
00087 
00089     ~Trademgener() {
00090       _trademgenService = NULL;
00091       _logOutputStream = NULL;
00092     }
00093     
00095     bool init (const std::string& iLogFilepath,
00096                const stdair::RandomSeed_T& iRandomSeed,
00097                const stdair::Filename_T& iDemandInputFilename,
00098                const std::string& iDBUser, const std::string& iDBPasswd,
00099                const std::string& iDBHost, const std::string& iDBPort,
00100                const std::string& iDBDBName) {
00101       bool isEverythingOK = true;
00102 
00103       try {
00104         
00105         // Check that the file path given as input corresponds to an actual file
00106         const bool isWriteable = (iLogFilepath.empty() == false);
00107         // stdair::BasFileMgr::isWriteable (iLogFilepath);
00108         if (isWriteable == false) {
00109           isEverythingOK = false;
00110           return isEverythingOK;
00111         }
00112         
00113         // Set the log parameters
00114         _logOutputStream = new std::ofstream;
00115         assert (_logOutputStream != NULL);
00116         
00117         // Open and clean the log outputfile
00118         _logOutputStream->open (iLogFilepath.c_str());
00119         _logOutputStream->clear();
00120         
00121         // DEBUG
00122         *_logOutputStream << "Python wrapper initialisation" << std::endl;
00123         const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG,
00124                                                *_logOutputStream);
00125         
00126         // Initialise the context
00127         stdair::BasDBParams lDBParams (iDBUser, iDBPasswd, iDBHost, iDBPort,
00128                                        iDBDBName);
00129         _trademgenService = new TRADEMGEN_Service (lLogParams, lDBParams,
00130                                                    iRandomSeed);
00131         assert (_trademgenService != NULL);
00132 
00133         // Create the DemandStream objects, and insert them within the BOM tree
00134         _trademgenService->parseAndLoad (iDemandInputFilename);
00135 
00136         // DEBUG
00137         *_logOutputStream << "Python wrapper initialised" << std::endl;
00138         
00139       } catch (const stdair::RootException& eTrademgenError) {
00140         *_logOutputStream << "Trademgen error: "  << eTrademgenError.what()
00141                           << std::endl;
00142         
00143       } catch (const std::exception& eStdError) {
00144         *_logOutputStream << "Error: "  << eStdError.what() << std::endl;
00145         
00146       } catch (...) {
00147         *_logOutputStream << "Unknown error" << std::endl;
00148       }
00149       
00150       return isEverythingOK;
00151     }
00152 
00153   private:
00155     TRADEMGEN_Service* _trademgenService;
00156     std::ofstream* _logOutputStream;
00157   };
00158 
00159 }
00160 
00161 // /////////////////////////////////////////////////////////////
00162 BOOST_PYTHON_MODULE(libpytrademgen) {
00163   boost::python::class_<TRADEMGEN::Trademgener> ("Trademgener")
00164     .def ("trademgen", &TRADEMGEN::Trademgener::trademgen)
00165     .def ("init", &TRADEMGEN::Trademgener::init);
00166 }