SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUILoadThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Class describing the thread that performs the loading of a simulation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <guisim/GUINet.h>
35 #include <guisim/GUIEventControl.h>
36 #include <netload/NLBuilder.h>
37 #include <netload/NLHandler.h>
46 #include <utils/options/Option.h>
50 #include <microsim/MSFrame.h>
52 #include "GUIApplicationWindow.h"
53 #include "GUILoadThread.h"
54 #include "GUIGlobals.h"
61 #include <ctime>
62 
63 #ifdef HAVE_INTERNAL
64 #include <mesogui/GUIMEVehicleControl.h>
65 #endif
66 
67 #ifdef CHECK_MEMORY_LEAKS
68 #include <foreign/nvwa/debug_new.h>
69 #endif // CHECK_MEMORY_LEAKS
70 
71 
72 // ===========================================================================
73 // member method definitions
74 // ===========================================================================
77  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
78  myEventThrow(ev) {
83 }
84 
85 
87  delete myErrorRetriever;
88  delete myMessageRetriever;
89  delete myWarningRetriever;
90 }
91 
92 
93 FXint
95  GUINet* net = 0;
96  int simStartTime = 0;
97  int simEndTime = 0;
98  std::vector<std::string> guiSettingsFiles;
99  bool osgView = false;
101 
102  // register message callbacks
106 
107  // try to load the given configuration
108  if (!initOptions()) {
109  // the options are not valid but maybe we want to quit
110  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
111  submitEndAndCleanup(net, simStartTime, simEndTime);
112  return 0;
113  }
114  // within gui-based applications, nothing is reported to the console
118  // do this once again to get parsed options
120  GUIGlobals::gRunAfterLoad = oc.getBool("start");
121  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
122 
123  if (!MSFrame::checkOptions()) {
124  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
125  submitEndAndCleanup(net, simStartTime, simEndTime);
126  return 0;
127  }
128 
129  // initialise global settings
133  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
134  MSVehicleControl* vehControl = 0;
135 #ifdef HAVE_INTERNAL
136  GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
138  vehControl = new GUIMEVehicleControl();
139  } else
140 #endif
141  vehControl = new GUIVehicleControl();
142 
143  GUIEdgeControlBuilder* eb = 0;
144  try {
145  net = new GUINet(
146  vehControl,
147  new GUIEventControl(),
148  new GUIEventControl(),
149  new GUIEventControl());
150  eb = new GUIEdgeControlBuilder();
151  GUIDetectorBuilder db(*net);
152  NLJunctionControlBuilder jb(*net, db);
154  NLHandler handler("", *net, db, tb, *eb, jb);
155  tb.setHandler(&handler);
156  NLBuilder builder(oc, *net, *eb, jb, db, handler);
160  if (!builder.build()) {
161  throw ProcessError();
162  } else {
163  net->initGUIStructures();
164  simStartTime = string2time(oc.getString("begin"));
165  simEndTime = string2time(oc.getString("end"));
166  guiSettingsFiles = oc.getStringVector("gui-settings-file");
167 #ifdef HAVE_INTERNAL
168  osgView = oc.getBool("osg-view");
169 #endif
170  }
171  } catch (ProcessError& e) {
172  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
173  WRITE_ERROR(e.what());
174  }
175  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
176  delete net;
177  net = 0;
178 #ifndef _DEBUG
179  } catch (std::exception& e) {
180  WRITE_ERROR(e.what());
181  delete net;
182  net = 0;
183 #endif
184  }
185  if (net == 0) {
186  MSNet::clearAll();
187  }
188  delete eb;
189  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView);
190  return 0;
191 }
192 
193 
194 
195 void
197  const SUMOTime simStartTime,
198  const SUMOTime simEndTime,
199  const std::vector<std::string>& guiSettingsFiles,
200  const bool osgView) {
201  // remove message callbacks
205  // inform parent about the process
206  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myFile, guiSettingsFiles, osgView);
207  myEventQue.add(e);
209 }
210 
211 
212 bool
214  try {
216  oc.clear();
218  if (myFile != "") {
219  if (myLoadNet) {
220  oc.set("net-file", myFile);
221  } else {
222  oc.set("configuration-file", myFile);
223  }
224  OptionsIO::getOptions(true, 1, 0);
225  } else {
226  OptionsIO::getOptions(true);
227  }
228  return true;
229  } catch (ProcessError& e) {
230  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
231  WRITE_ERROR(e.what());
232  }
233  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
234  }
235  return false;
236 }
237 
238 
239 void
240 GUILoadThread::load(const std::string& file, bool isNet) {
241  myFile = file;
242  myLoadNet = isNet;
243  start();
244 }
245 
246 
247 void
248 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
249  GUIEvent* e = new GUIEvent_Message(type, msg);
250  myEventQue.add(e);
252 }
253 
254 
255 const std::string&
257  return myFile;
258 }
259 
260 
261 
262 /****************************************************************************/
263 
void setHandler(NLHandler *handler)
Sets the parent handler to use for nested parsing.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
The message is only something to show.
Definition: MsgHandler.h:61
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
The class responsible for building and deletion of vehicles (gui-version)
OutputDevice * myWarningRetriever
Definition: GUILoadThread.h:98
static void getOptions(bool loadConfig, int argc=0, char **argv=0)
Parses the command line arguments and loads the configuration optionally.
Definition: OptionsIO.cpp:64
The main interface for loading a microsim.
Definition: NLBuilder.h:68
void load(const std::string &file, bool isNet)
begins the loading of the given file
GUILoadThread(FXApp *app, MFXInterThreadEventClient *mw, MFXEventQue &eq, FXEX::FXThreadEvent &ev)
constructor
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:52
FXEX::FXThreadEvent & myEventThrow
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:256
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:161
void submitEndAndCleanup(GUINet *net, const SUMOTime simStartTime, const SUMOTime simEndTime, const std::vector< std::string > &guiSettingsFiles=std::vector< std::string >(), const bool osgView=false)
Closes the loading process.
Derivation of NLEdgeControlBuilder which builds gui-edges.
Builds detectors for guisim.
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on...
Definition: GUILoadThread.h:98
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
void add(void *what)
Definition: MFXEventQue.cpp:57
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
OutputDevice * myMessageRetriever
Definition: GUILoadThread.h:98
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:68
Builds trigger objects for guisim.
virtual ~GUILoadThread()
destructor
virtual bool initOptions()
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void clear()
Removes all information from the container.
Builder of microsim-junctions and tls.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the.
Definition: MsgHandler.cpp:177
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
const std::string & getFileName() const
static MTRand myVehicleParamsRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
bool myLoadNet
Information whether only the network shall be loaded.
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:55
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:350
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:122
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
The message is a warning.
Definition: MsgHandler.h:63
Encapsulates an object&#39;s method for using it as a message retriever.
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:487
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:90
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
Stores time-dependant events and executes them at the proper time (guisim)
static const bool gUseMesoSim
Definition: MSGlobals.h:95
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:408
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:94
static void allowTextures(const bool val)
don&#39;t draw any textures
The class responsible for building and deletion of vehicles.
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
MFXEventQue & myEventQue
static void initOutputOptions()
Definition: MsgHandler.cpp:193
The message is an error.
Definition: MsgHandler.h:65