SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2015 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/MSGlobals.h>
59 #include <microsim/MSFrame.h>
61 #include "GUIApplicationWindow.h"
62 #include "GUILoadThread.h"
63 #include "GUIGlobals.h"
65 
66 #ifdef HAVE_INTERNAL
67 #include <mesogui/GUIMEVehicleControl.h>
68 #endif
69 
70 #ifndef NO_TRACI
72 #include "TraCIServerAPI_GUI.h"
73 #endif
74 
75 #ifdef CHECK_MEMORY_LEAKS
76 #include <foreign/nvwa/debug_new.h>
77 #endif // CHECK_MEMORY_LEAKS
78 
79 
80 // ===========================================================================
81 // member method definitions
82 // ===========================================================================
85  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
86  myEventThrow(ev) {
91 }
92 
93 
95  delete myErrorRetriever;
96  delete myMessageRetriever;
97  delete myWarningRetriever;
98 }
99 
100 
101 FXint
103  // register message callbacks
107 
108  // try to load the given configuration
110  try {
111  oc.clear();
113  if (myFile != "") {
114  // triggered by menu option or reload
115  if (myLoadNet) {
116  oc.set("net-file", myFile);
117  } else {
118  oc.set("configuration-file", myFile);
119  }
120  oc.resetWritable(); // there may be command line options
122  } else {
123  // triggered at application start
125  if (oc.isSet("configuration-file")) {
126  myFile = oc.getString("configuration-file");
127  } else if (oc.isSet("net-file")) {
128  myFile = oc.getString("net-file");
129  myLoadNet = true;
130  }
131  }
132  myTitle = myFile;
133  // within gui-based applications, nothing is reported to the console
137  // do this once again to get parsed options
138  if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
139  // must be done before calling initOutputOptions (which checks option "verbose")
140  // but initOutputOptions must come before checkOptions (so that warnings are printed)
141  oc.set("verbose", "true");
142  }
144  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
145  GUIGlobals::gRunAfterLoad = oc.getBool("start");
146  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
147  if (!MSFrame::checkOptions()) {
148  throw ProcessError();
149  }
150  } catch (ProcessError& e) {
151  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
152  WRITE_ERROR(e.what());
153  }
154  // the options are not valid but maybe we want to quit
155  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
156  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
157  submitEndAndCleanup(0, 0, 0);
158  return 0;
159  }
160 
161  // initialise global settings
165  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
166  if (oc.getBool("game")) {
167  myParent->onCmdGaming(0, 0, 0);
168  }
169  MSVehicleControl* vehControl = 0;
170 #ifdef HAVE_INTERNAL
171  GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
173  vehControl = new GUIMEVehicleControl();
174  } else
175 #endif
176  vehControl = new GUIVehicleControl();
177 
178  GUINet* net = 0;
179  int simStartTime = 0;
180  int simEndTime = 0;
181  std::vector<std::string> guiSettingsFiles;
182  bool osgView = false;
183  GUIEdgeControlBuilder* eb = 0;
184  try {
185  net = new GUINet(
186  vehControl,
187  new GUIEventControl(),
188  new GUIEventControl(),
189  new GUIEventControl());
190 #ifndef NO_TRACI
191  // need to init TraCI-Server before loading routes to catch VEHICLE_STATE_BUILT
192  std::map<int, TraCIServer::CmdExecutor> execs;
196 #endif
197 
198  eb = new GUIEdgeControlBuilder();
199  GUIDetectorBuilder db(*net);
200  NLJunctionControlBuilder jb(*net, db);
202  NLHandler handler("", *net, db, tb, *eb, jb);
203  tb.setHandler(&handler);
204  NLBuilder builder(oc, *net, *eb, jb, db, handler);
208  if (!builder.build()) {
209  throw ProcessError();
210  } else {
211  net->initGUIStructures();
212  simStartTime = string2time(oc.getString("begin"));
213  simEndTime = string2time(oc.getString("end"));
214  guiSettingsFiles = oc.getStringVector("gui-settings-file");
215 #ifdef HAVE_INTERNAL
216  osgView = oc.getBool("osg-view");
217 #endif
218  }
219  } catch (ProcessError& e) {
220  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
221  WRITE_ERROR(e.what());
222  }
223  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
224  delete net;
225  net = 0;
226 #ifndef _DEBUG
227  } catch (std::exception& e) {
228  WRITE_ERROR(e.what());
229  delete net;
230  net = 0;
231 #endif
232  }
233  if (net == 0) {
234  MSNet::clearAll();
235  }
236  delete eb;
237  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView);
238  return 0;
239 }
240 
241 
242 void
244  const SUMOTime simStartTime,
245  const SUMOTime simEndTime,
246  const std::vector<std::string>& guiSettingsFiles,
247  const bool osgView) {
248  // remove message callbacks
252  // inform parent about the process
253  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView);
254  myEventQue.add(e);
256 }
257 
258 
259 void
260 GUILoadThread::loadConfigOrNet(const std::string& file, bool isNet) {
261  myFile = file;
262  myLoadNet = isNet;
263  if (myFile != "") {
264  OptionsIO::setArgs(0, 0);
265  }
266  start();
267 }
268 
269 
270 void
271 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
272  GUIEvent* e = new GUIEvent_Message(type, msg);
273  myEventQue.add(e);
275 }
276 
277 
278 const std::string&
280  return myFile;
281 }
282 
283 
284 /****************************************************************************/
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:62
MFXEventQue< GUIEvent * > & myEventQue
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
long long int SUMOTime
Definition: SUMOTime.h:43
void resetWritable()
Resets all options to be writeable.
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 add(T what)
Definition: MFXEventQue.h:59
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
GUILoadThread(FXApp *app, GUIApplicationWindow *mw, MFXEventQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
static MTRand * getParsingRNG()
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:268
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...
#define CMD_SET_GUI_VARIABLE
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:64
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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
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
void loadConfigOrNet(const std::string &file, bool isNet)
begins the loading of the given file
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
GUIApplicationWindow * myParent
the parent window to inform about the loading
Definition: GUILoadThread.h:90
void clear()
Removes all information from the container.
std::string myTitle
the title string for the application
Definition: GUILoadThread.h:96
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 handler.
Definition: MsgHandler.cpp:175
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
#define CMD_GET_GUI_VARIABLE
const std::string & getFileName() const
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
bool myLoadNet
Information whether only the network shall be loaded.
The XML-Handler for network loading.
Definition: NLHandler.h:84
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:55
long onCmdGaming(FXObject *, FXSelector, void *)
Toggle gaming mode.
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:416
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:122
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
The message is a warning.
Definition: MsgHandler.h:64
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:71
Encapsulates an object&#39;s method for using it as a message retriever.
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:573
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
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:91
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:102
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:478
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:93
static void allowTextures(const bool val)
switch texture drawing on and off
The class responsible for building and deletion of vehicles.
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
static void initOutputOptions()
Definition: MsgHandler.cpp:197
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
The message is an error.
Definition: MsgHandler.h:66
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
The main window of the SUMO-gui.