SUMO - Simulation of Urban MObility

GUILoadThread.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Class describing the thread that performs the loading of a simulation
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <iostream>
00031 #include <guisim/GUINet.h>
00032 #include <guisim/GUIEventControl.h>
00033 #include <netload/NLBuilder.h>
00034 #include <netload/NLHandler.h>
00035 #include <netload/NLJunctionControlBuilder.h>
00036 #include <guinetload/GUIEdgeControlBuilder.h>
00037 #include <guinetload/GUIDetectorBuilder.h>
00038 #include <guinetload/GUITriggerBuilder.h>
00039 #include <guisim/GUIVehicleControl.h>
00040 #include <microsim/output/MSDetectorControl.h>
00041 #include <utils/common/UtilExceptions.h>
00042 #include <utils/options/OptionsCont.h>
00043 #include <utils/options/Option.h>
00044 #include <utils/options/OptionsIO.h>
00045 #include <utils/common/MsgHandler.h>
00046 #include <utils/foxtools/MFXEventQue.h>
00047 #include <microsim/MSFrame.h>
00048 #include <utils/common/MsgRetrievingFunction.h>
00049 #include "GUIApplicationWindow.h"
00050 #include "GUILoadThread.h"
00051 #include "GUIGlobals.h"
00052 #include "GUIEvent_SimulationLoaded.h"
00053 #include <utils/gui/events/GUIEvent_Message.h>
00054 #include <utils/gui/windows/GUIAppEnum.h>
00055 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00056 #include <utils/gui/windows/GUIAppGlobals.h>
00057 #include <utils/common/RandHelper.h>
00058 #include <ctime>
00059 
00060 #ifdef HAVE_MESOSIM
00061 #include <mesosim/MEVehicleControl.h>
00062 #endif
00063 
00064 #ifdef CHECK_MEMORY_LEAKS
00065 #include <foreign/nvwa/debug_new.h>
00066 #endif // CHECK_MEMORY_LEAKS
00067 
00068 
00069 // ===========================================================================
00070 // member method definitions
00071 // ===========================================================================
00072 GUILoadThread::GUILoadThread(MFXInterThreadEventClient* mw,
00073                              MFXEventQue& eq, FXEX::FXThreadEvent& ev)
00074     : FXSingleEventThread(gFXApp, mw), myParent(mw), myEventQue(eq),
00075       myEventThrow(ev) {
00076     myErrorRetriever = new MsgRetrievingFunction<GUILoadThread>(this, &GUILoadThread::retrieveMessage, MsgHandler::MT_ERROR);
00077     myMessageRetriever = new MsgRetrievingFunction<GUILoadThread>(this, &GUILoadThread::retrieveMessage, MsgHandler::MT_MESSAGE);
00078     myWarningRetriever = new MsgRetrievingFunction<GUILoadThread>(this, &GUILoadThread::retrieveMessage, MsgHandler::MT_WARNING);
00079     MsgHandler::getErrorInstance()->addRetriever(myErrorRetriever);
00080 }
00081 
00082 
00083 GUILoadThread::~GUILoadThread() {
00084     delete myErrorRetriever;
00085     delete myMessageRetriever;
00086     delete myWarningRetriever;
00087 }
00088 
00089 
00090 FXint
00091 GUILoadThread::run() {
00092     GUINet* net = 0;
00093     int simStartTime = 0;
00094     int simEndTime = 0;
00095 
00096     // remove old options
00097     OptionsCont& oc = OptionsCont::getOptions();
00098     oc.clear();
00099     // within gui-based applications, nothing is reported to the console
00100     MsgHandler::getErrorInstance()->report2cout(false);
00101     MsgHandler::getErrorInstance()->report2cerr(false);
00102     MsgHandler::getWarningInstance()->report2cout(false);
00103     MsgHandler::getWarningInstance()->report2cerr(false);
00104     MsgHandler::getMessageInstance()->report2cout(false);
00105     MsgHandler::getMessageInstance()->report2cerr(false);
00106     // register message callbacks
00107     MsgHandler::getMessageInstance()->addRetriever(myMessageRetriever);
00108     MsgHandler::getErrorInstance()->addRetriever(myErrorRetriever);
00109     MsgHandler::getWarningInstance()->addRetriever(myWarningRetriever);
00110 
00111     // try to load the given configuration
00112     if (!initOptions()) {
00113         // the options are not valid
00114         submitEndAndCleanup(net, simStartTime, simEndTime);
00115         return 0;
00116     }
00117     MsgHandler::initOutputOptions(true);
00118     if (!MSFrame::checkOptions()) {
00119         // the options are not valid
00120         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00121         submitEndAndCleanup(net, simStartTime, simEndTime);
00122         return 0;
00123     }
00124 
00125     RandHelper::initRandGlobal();
00126     // try to load
00127     MSFrame::setMSGlobals(oc);
00128 #ifdef HAVE_MESOSIM
00129     GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
00130     if (MSGlobals::gUseMesoSim) {
00131         net = new GUINet(new MEVehicleControl(), new GUIEventControl(),
00132                          new GUIEventControl(), new GUIEventControl());
00133     } else {
00134 #endif
00135         net = new GUINet(new GUIVehicleControl(), new GUIEventControl(),
00136                          new GUIEventControl(), new GUIEventControl());
00137 #ifdef HAVE_MESOSIM
00138     }
00139 #endif
00140     GUIEdgeControlBuilder* eb = new GUIEdgeControlBuilder();
00141     GUIDetectorBuilder db(*net);
00142     NLJunctionControlBuilder jb(*net, db);
00143     GUITriggerBuilder tb;
00144     NLHandler handler("", *net, db, tb, *eb, jb);
00145     tb.setHandler(&handler);
00146     NLBuilder builder(oc, *net, *eb, jb, db, handler);
00147     try {
00148         MsgHandler::getErrorInstance()->clear();
00149         MsgHandler::getWarningInstance()->clear();
00150         MsgHandler::getMessageInstance()->clear();
00151         if (!builder.build()) {
00152             throw ProcessError();
00153         } else {
00154             net->initGUIStructures();
00155             simStartTime = string2time(oc.getString("begin"));
00156             simEndTime = string2time(oc.getString("end"));
00157         }
00158     } catch (ProcessError& e) {
00159         if (std::string(e.what())!=std::string("Process Error") && std::string(e.what())!=std::string("")) {
00160             WRITE_ERROR(e.what());
00161         }
00162         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00163         delete net;
00164         net = 0;
00165 #ifndef _DEBUG
00166     } catch (std::exception& e) {
00167         WRITE_ERROR(e.what());
00168         delete net;
00169         net = 0;
00170 #endif
00171     }
00172     if (net==0) {
00173         MSNet::clearAll();
00174     }
00175     delete eb;
00176     submitEndAndCleanup(net, simStartTime, simEndTime);
00177     return 0;
00178 }
00179 
00180 
00181 
00182 void
00183 GUILoadThread::submitEndAndCleanup(GUINet* net,
00184                                    SUMOTime simStartTime,
00185                                    SUMOTime simEndTime) {
00186     // remove message callbacks
00187     MsgHandler::getErrorInstance()->removeRetriever(myErrorRetriever);
00188     MsgHandler::getWarningInstance()->removeRetriever(myWarningRetriever);
00189     MsgHandler::getMessageInstance()->removeRetriever(myMessageRetriever);
00190     // inform parent about the process
00191     GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myFile,
00192             OptionsCont::getOptions().getString("gui-settings-file"));
00193     myEventQue.add(e);
00194     myEventThrow.signal();
00195 }
00196 
00197 
00198 bool
00199 GUILoadThread::initOptions() {
00200     try {
00201         OptionsCont& oc = OptionsCont::getOptions();
00202         oc.clear();
00203         MSFrame::fillOptions();
00204         if (myLoadNet) {
00205             oc.set("net-file", myFile);
00206         } else {
00207             oc.set("configuration-file", myFile);
00208         }
00209         OptionsIO::getOptions(true, 1, 0);
00210         return true;
00211     } catch (ProcessError& e) {
00212         if (std::string(e.what())!=std::string("Process Error") && std::string(e.what())!=std::string("")) {
00213             WRITE_ERROR(e.what());
00214         }
00215         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00216     }
00217     return false;
00218 }
00219 
00220 
00221 void
00222 GUILoadThread::load(const std::string& file, bool isNet) {
00223     myFile = file;
00224     myLoadNet = isNet;
00225     start();
00226 }
00227 
00228 
00229 void
00230 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
00231     GUIEvent* e = new GUIEvent_Message(type, msg);
00232     myEventQue.add(e);
00233     myEventThrow.signal();
00234 }
00235 
00236 
00237 const std::string&
00238 GUILoadThread::getFileName() const {
00239     return myFile;
00240 }
00241 
00242 
00243 
00244 /****************************************************************************/
00245 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines