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-2014 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 <ctime>
40 #include <utils/options/Option.h>
47 #include <utils/xml/XMLSubSys.h>
48 #include <guisim/GUINet.h>
49 #include <guisim/GUIEventControl.h>
51 #include <netload/NLBuilder.h>
52 #include <netload/NLHandler.h>
58 #include <microsim/MSFrame.h>
59 #include "GUIApplicationWindow.h"
60 #include "GUILoadThread.h"
61 #include "GUIGlobals.h"
63 
64 #ifdef HAVE_INTERNAL
65 #include <mesogui/GUIMEVehicleControl.h>
66 #endif
67 
68 #ifdef CHECK_MEMORY_LEAKS
69 #include <foreign/nvwa/debug_new.h>
70 #endif // CHECK_MEMORY_LEAKS
71 
72 
73 // ===========================================================================
74 // member method definitions
75 // ===========================================================================
78  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
79  myEventThrow(ev) {
84 }
85 
86 
88  delete myErrorRetriever;
89  delete myMessageRetriever;
90  delete myWarningRetriever;
91 }
92 
93 
94 FXint
96  GUINet* net = 0;
97  int simStartTime = 0;
98  int simEndTime = 0;
99  std::vector<std::string> guiSettingsFiles;
100  bool osgView = false;
102 
103  // register message callbacks
107 
108  // try to load the given configuration
109  if (!initOptions()) {
110  // the options are not valid but maybe we want to quit
111  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
112  submitEndAndCleanup(net, simStartTime, simEndTime);
113  return 0;
114  }
115  // within gui-based applications, nothing is reported to the console
119  // do this once again to get parsed options
121  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
122  GUIGlobals::gRunAfterLoad = oc.getBool("start");
123  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
124 
125  if (!MSFrame::checkOptions()) {
126  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
127  submitEndAndCleanup(net, simStartTime, simEndTime);
128  return 0;
129  }
130 
131  // initialise global settings
135  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
136  MSVehicleControl* vehControl = 0;
137 #ifdef HAVE_INTERNAL
138  GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
140  vehControl = new GUIMEVehicleControl();
141  } else
142 #endif
143  vehControl = new GUIVehicleControl();
144 
145  GUIEdgeControlBuilder* eb = 0;
146  try {
147  net = new GUINet(
148  vehControl,
149  new GUIEventControl(),
150  new GUIEventControl(),
151  new GUIEventControl());
152  eb = new GUIEdgeControlBuilder();
153  GUIDetectorBuilder db(*net);
154  NLJunctionControlBuilder jb(*net, db);
156  NLHandler handler("", *net, db, tb, *eb, jb);
157  tb.setHandler(&handler);
158  NLBuilder builder(oc, *net, *eb, jb, db, handler);
162  if (!builder.build()) {
163  throw ProcessError();
164  } else {
165  net->initGUIStructures();
166  simStartTime = string2time(oc.getString("begin"));
167  simEndTime = string2time(oc.getString("end"));
168  guiSettingsFiles = oc.getStringVector("gui-settings-file");
169 #ifdef HAVE_INTERNAL
170  osgView = oc.getBool("osg-view");
171 #endif
172  }
173  } catch (ProcessError& e) {
174  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
175  WRITE_ERROR(e.what());
176  }
177  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
178  delete net;
179  net = 0;
180 #ifndef _DEBUG
181  } catch (std::exception& e) {
182  WRITE_ERROR(e.what());
183  delete net;
184  net = 0;
185 #endif
186  }
187  if (net == 0) {
188  MSNet::clearAll();
189  }
190  delete eb;
191  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView);
192  return 0;
193 }
194 
195 
196 
197 void
199  const SUMOTime simStartTime,
200  const SUMOTime simEndTime,
201  const std::vector<std::string>& guiSettingsFiles,
202  const bool osgView) {
203  // remove message callbacks
207  // inform parent about the process
208  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myFile, guiSettingsFiles, osgView);
209  myEventQue.add(e);
211 }
212 
213 
214 bool
216  try {
218  oc.clear();
220  if (myFile != "") {
221  if (myLoadNet) {
222  oc.set("net-file", myFile);
223  } else {
224  oc.set("configuration-file", myFile);
225  }
226  OptionsIO::getOptions(true, 1, 0);
227  } else {
228  OptionsIO::getOptions(true);
229  }
230  return true;
231  } catch (ProcessError& e) {
232  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
233  WRITE_ERROR(e.what());
234  }
235  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
236  }
237  return false;
238 }
239 
240 
241 void
242 GUILoadThread::load(const std::string& file, bool isNet) {
243  myFile = file;
244  myLoadNet = isNet;
245  start();
246 }
247 
248 
249 void
250 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
251  GUIEvent* e = new GUIEvent_Message(type, msg);
252  myEventQue.add(e);
254 }
255 
256 
257 const std::string&
259  return myFile;
260 }
261 
262 
263 
264 /****************************************************************************/
265 
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
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
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:69
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.
The XML-Handler for network loading.
Definition: NLHandler.h:72
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:362
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's method for using it as a message retriever.
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:504
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:98
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:416
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:94
static void allowTextures(const bool val)
don'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