SUMO - Simulation of Urban MObility
GUILoadThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Class describing the thread that performs the loading of a simulation
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <iostream>
27 #include <ctime>
33 #include <utils/options/Option.h>
40 #include <utils/xml/XMLSubSys.h>
41 #include <guisim/GUINet.h>
42 #include <guisim/GUIEventControl.h>
44 #include <netload/NLBuilder.h>
45 #include <netload/NLHandler.h>
52 #include <microsim/MSGlobals.h>
53 #include <microsim/MSFrame.h>
57 #include "TraCIServerAPI_GUI.h"
58 #include "GUIApplicationWindow.h"
59 #include "GUILoadThread.h"
60 #include "GUIGlobals.h"
62 
63 
64 // ===========================================================================
65 // member method definitions
66 // ===========================================================================
69  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
70  myEventThrow(ev) {
75 }
76 
77 
79  delete myErrorRetriever;
80  delete myMessageRetriever;
81  delete myWarningRetriever;
82 }
83 
84 
85 FXint
87  // register message callbacks
91 
92  // try to load the given configuration
94  try {
95  oc.clear();
97  if (myFile != "") {
98  // triggered by menu option or reload
99  if (myLoadNet) {
100  oc.set("net-file", myFile);
101  } else {
102  oc.set("configuration-file", myFile);
103  }
104  oc.resetWritable(); // there may be command line options
106  } else {
107  // triggered at application start
109  if (oc.isSet("configuration-file")) {
110  myFile = oc.getString("configuration-file");
111  } else if (oc.isSet("net-file")) {
112  myFile = oc.getString("net-file");
113  myLoadNet = true;
114  }
115  myEventQue.add(new GUIEvent_Message("Loading '" + myFile + "'."));
117  myParent->addRecentFile(FXPath::absolute(myFile.c_str()), myLoadNet);
118  }
119  myTitle = myFile;
120  // within gui-based applications, nothing is reported to the console
124  // do this once again to get parsed options
125  if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
126  // must be done before calling initOutputOptions (which checks option "verbose")
127  // but initOutputOptions must come before checkOptions (so that warnings are printed)
128  oc.set("verbose", "true");
129  }
131  if (!MSFrame::checkOptions()) {
132  throw ProcessError();
133  }
134  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
135  GUIGlobals::gRunAfterLoad = oc.getBool("start");
136  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
138  GUIGlobals::gTrackerInterval = oc.getFloat("tracker-interval");
139  } catch (ProcessError& e) {
140  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
141  WRITE_ERROR(e.what());
142  }
143  // the options are not valid but maybe we want to quit
144  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
145  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
146  submitEndAndCleanup(nullptr, 0, 0);
147  return 0;
148  }
149 
150  // initialise global settings
155  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
156 
157  MSVehicleControl* vehControl = nullptr;
160  vehControl = new GUIMEVehicleControl();
161  } else {
162  vehControl = new GUIVehicleControl();
163  }
164 
165  GUINet* net = nullptr;
166  SUMOTime simStartTime = 0;
167  SUMOTime simEndTime = 0;
168  std::vector<std::string> guiSettingsFiles;
169  bool osgView = false;
170  GUIEdgeControlBuilder* eb = nullptr;
171  try {
172  net = new GUINet(
173  vehControl,
174  new GUIEventControl(),
175  new GUIEventControl(),
176  new GUIEventControl());
177  // need to init TraCI-Server before loading routes to catch VEHICLE_STATE_BUILT
178  std::map<int, TraCIServer::CmdExecutor> execs;
182 
183  eb = new GUIEdgeControlBuilder();
184  GUIDetectorBuilder db(*net);
185  NLJunctionControlBuilder jb(*net, db);
187  NLHandler handler("", *net, db, tb, *eb, jb);
188  tb.setHandler(&handler);
189  NLBuilder builder(oc, *net, *eb, jb, db, handler);
193  if (!builder.build()) {
194  throw ProcessError();
195  } else {
196  net->initGUIStructures();
197  simStartTime = string2time(oc.getString("begin"));
198  simEndTime = string2time(oc.getString("end"));
199  guiSettingsFiles = oc.getStringVector("gui-settings-file");
200 #ifdef HAVE_OSG
201  osgView = oc.getBool("osg-view");
202 #endif
203  }
204  } catch (ProcessError& e) {
205  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
206  WRITE_ERROR(e.what());
207  }
208  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
209  delete net;
210  net = nullptr;
211 #ifndef _DEBUG
212  } catch (std::exception& e) {
213  WRITE_ERROR(e.what());
214  delete net;
215  net = nullptr;
216 #endif
217  }
218  if (net == nullptr) {
219  MSNet::clearAll();
220  }
221  delete eb;
222  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView,
223  oc.getBool("registry-viewport"));
224  return 0;
225 }
226 
227 
228 void
230  const SUMOTime simStartTime,
231  const SUMOTime simEndTime,
232  const std::vector<std::string>& guiSettingsFiles,
233  const bool osgView,
234  const bool viewportFromRegistry) {
235  // remove message callbacks
239  // inform parent about the process
240  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView, viewportFromRegistry);
241  myEventQue.add(e);
243 }
244 
245 
246 void
247 GUILoadThread::loadConfigOrNet(const std::string& file, bool isNet) {
248  myFile = file;
249  myLoadNet = isNet;
250  if (myFile != "") {
251  OptionsIO::setArgs(0, nullptr);
252  }
253  start();
254 }
255 
256 
257 void
258 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
259  GUIEvent* e = new GUIEvent_Message(type, msg);
260  myEventQue.add(e);
262 }
263 
264 
265 const std::string&
267  return myFile;
268 }
269 
270 
271 /****************************************************************************/
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:55
MFXEventQue< GUIEvent * > & myEventQue
Definition: GUILoadThread.h:96
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
Definition: GUILoadThread.h:94
long long int SUMOTime
Definition: SUMOTime.h:36
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
void resetWritable()
Resets all options to be writeable.
GUILoadThread(FXApp *app, GUIApplicationWindow *mw, MFXEventQue< GUIEvent *> &eq, FXEX::FXThreadEvent &ev)
constructor
The main interface for loading a microsim.
Definition: NLBuilder.h:61
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
void add(T what)
Definition: MFXEventQue.h:52
const std::string & getFileName() const
void addRecentFile(const FX::FXString &f, const bool isNet)
static bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:45
FXEX::FXThreadEvent & myEventThrow
Definition: GUILoadThread.h:98
The class responsible for building and deletion of vehicles (gui-version)
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:254
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:185
Derivation of NLEdgeControlBuilder which builds gui-edges.
Builds detectors for guisim.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on...
Definition: GUILoadThread.h:94
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
#define CMD_SET_GUI_VARIABLE
static bool gDemoAutoReload
the simulation shall reload when it has ended (demo)
Definition: GUIGlobals.h:51
static std::mt19937 * getEquipmentRNG()
Definition: MSDevice.h:90
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:55
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
OutputDevice * myMessageRetriever
Definition: GUILoadThread.h:94
static std::mt19937 * getParsingRNG()
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:61
Builds trigger objects for guisim.
virtual ~GUILoadThread()
destructor
void loadConfigOrNet(const std::string &file, bool isNet)
begins the loading of the given file
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
GUIApplicationWindow * myParent
the parent window to inform about the loading
Definition: GUILoadThread.h:84
void clear()
Removes all information from the container.
static double gTrackerInterval
the aggregation period for tracker windows in seconds
Definition: GUIGlobals.h:54
std::string myTitle
the title string for the application
Definition: GUILoadThread.h:90
Builder of microsim-junctions and tls.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void submitEndAndCleanup(GUINet *net, const SUMOTime simStartTime, const SUMOTime simEndTime, const std::vector< std::string > &guiSettingsFiles=std::vector< std::string >(), const bool osgView=false, const bool viewportFromRegistry=false)
Closes the loading process.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
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) ...
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:199
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:58
#define CMD_GET_GUI_VARIABLE
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool myLoadNet
Information whether only the network shall be loaded.
The XML-Handler for network loading.
Definition: NLHandler.h:81
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:48
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:529
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:117
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:247
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
The message is a warning.
Definition: MsgHandler.h:57
Encapsulates an object&#39;s method for using it as a message retriever.
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:620
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:82
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:113
A storage for options typed value containers)
Definition: OptionsCont.h:92
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:72
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:650
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:87
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 occurred previously.
Definition: MsgHandler.cpp:173
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:239
static bool gUseMesoSim
Definition: MSGlobals.h:91
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:59
The main window of the SUMO-gui.