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-2017 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>
59 #include <microsim/MSGlobals.h>
60 #include <microsim/MSFrame.h>
62 #include "GUIApplicationWindow.h"
63 #include "GUILoadThread.h"
64 #include "GUIGlobals.h"
66 
68 
69 #ifndef NO_TRACI
71 #include "TraCIServerAPI_GUI.h"
72 #endif
73 
74 
75 // ===========================================================================
76 // member method definitions
77 // ===========================================================================
80  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
81  myEventThrow(ev) {
86 }
87 
88 
90  delete myErrorRetriever;
91  delete myMessageRetriever;
92  delete myWarningRetriever;
93 }
94 
95 
96 FXint
98  // register message callbacks
102 
103  // try to load the given configuration
105  try {
106  oc.clear();
108  if (myFile != "") {
109  // triggered by menu option or reload
110  if (myLoadNet) {
111  oc.set("net-file", myFile);
112  } else {
113  oc.set("configuration-file", myFile);
114  }
115  oc.resetWritable(); // there may be command line options
117  } else {
118  // triggered at application start
120  if (oc.isSet("configuration-file")) {
121  myFile = oc.getString("configuration-file");
122  } else if (oc.isSet("net-file")) {
123  myFile = oc.getString("net-file");
124  myLoadNet = true;
125  }
126  myEventQue.add(new GUIEvent_Message("Loading '" + myFile + "'."));
128  myParent->addRecentFile(FXPath::absolute(myFile.c_str()), myLoadNet);
129  }
130  myTitle = myFile;
131  // within gui-based applications, nothing is reported to the console
135  // do this once again to get parsed options
136  if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
137  // must be done before calling initOutputOptions (which checks option "verbose")
138  // but initOutputOptions must come before checkOptions (so that warnings are printed)
139  oc.set("verbose", "true");
140  }
142  if (!MSFrame::checkOptions()) {
143  throw ProcessError();
144  }
145  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
146  GUIGlobals::gRunAfterLoad = oc.getBool("start");
147  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
149  GUIGlobals::gTrackerInterval = oc.getFloat("tracker-interval");
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
166  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
167 
168  MSVehicleControl* vehControl = 0;
171  vehControl = new GUIMEVehicleControl();
172  } else {
173  vehControl = new GUIVehicleControl();
174  }
175 
176  GUINet* net = 0;
177  SUMOTime simStartTime = 0;
178  SUMOTime simEndTime = 0;
179  std::vector<std::string> guiSettingsFiles;
180  bool osgView = false;
181  GUIEdgeControlBuilder* eb = 0;
182  try {
183  net = new GUINet(
184  vehControl,
185  new GUIEventControl(),
186  new GUIEventControl(),
187  new GUIEventControl());
188 #ifndef NO_TRACI
189  // need to init TraCI-Server before loading routes to catch VEHICLE_STATE_BUILT
190  std::map<int, TraCIServer::CmdExecutor> execs;
194 #endif
195 
196  eb = new GUIEdgeControlBuilder();
197  GUIDetectorBuilder db(*net);
198  NLJunctionControlBuilder jb(*net, db);
200  NLHandler handler("", *net, db, tb, *eb, jb);
201  tb.setHandler(&handler);
202  NLBuilder builder(oc, *net, *eb, jb, db, handler);
206  if (!builder.build()) {
207  throw ProcessError();
208  } else {
209  net->initGUIStructures();
210  simStartTime = string2time(oc.getString("begin"));
211  simEndTime = string2time(oc.getString("end"));
212  guiSettingsFiles = oc.getStringVector("gui-settings-file");
213 #ifdef HAVE_OSG
214  osgView = oc.getBool("osg-view");
215 #endif
216  }
217  } catch (ProcessError& e) {
218  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
219  WRITE_ERROR(e.what());
220  }
221  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
222  delete net;
223  net = 0;
224 #ifndef _DEBUG
225  } catch (std::exception& e) {
226  WRITE_ERROR(e.what());
227  delete net;
228  net = 0;
229 #endif
230  }
231  if (net == 0) {
232  MSNet::clearAll();
233  }
234  delete eb;
235  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView);
236  return 0;
237 }
238 
239 
240 void
242  const SUMOTime simStartTime,
243  const SUMOTime simEndTime,
244  const std::vector<std::string>& guiSettingsFiles,
245  const bool osgView) {
246  // remove message callbacks
250  // inform parent about the process
251  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView);
252  myEventQue.add(e);
254 }
255 
256 
257 void
258 GUILoadThread::loadConfigOrNet(const std::string& file, bool isNet) {
259  myFile = file;
260  myLoadNet = isNet;
261  if (myFile != "") {
262  OptionsIO::setArgs(0, 0);
263  }
264  start();
265 }
266 
267 
268 void
269 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
270  GUIEvent* e = new GUIEvent_Message(type, msg);
271  myEventQue.add(e);
273 }
274 
275 
276 const std::string&
278  return myFile;
279 }
280 
281 
282 /****************************************************************************/
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:67
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:76
The class responsible for building and deletion of vehicles (gui-version)
OutputDevice * myWarningRetriever
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
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:65
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
void add(T what)
Definition: MFXEventQue.h:59
void addRecentFile(const FX::FXString &f, const bool isNet)
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 bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:52
FXEX::FXThreadEvent & myEventThrow
The class responsible for building and deletion of vehicles (gui-version)
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:260
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:157
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 gDemoAutoReload
the simulation shall reload when it has ended (demo)
Definition: GUIGlobals.h:58
static std::mt19937 * getEquipmentRNG()
Definition: MSDevice.h:91
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:62
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
OutputDevice * myMessageRetriever
static std::mt19937 * getParsingRNG()
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:66
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.
static double gTrackerInterval
the aggregation period for tracker windows in seconds
Definition: GUIGlobals.h:61
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:47
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:171
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:58
#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:88
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:495
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:126
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
Encapsulates an object&#39;s method for using it as a message retriever.
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:621
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:89
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:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
Stores time-dependant events and executes them at the proper time (guisim)
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:77
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:588
long long int SUMOTime
Definition: TraCIDefs.h:52
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.
static bool UseMesoSim
this should be set at the same time as MSGlobals::gUseMesoSim
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:145
static void initOutputOptions()
Definition: MsgHandler.cpp:193
static bool gUseMesoSim
Definition: MSGlobals.h:98
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.