SUMO - Simulation of Urban MObility

GUILoadThread.cpp

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