Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Class describing the thread that performs the loading of a simulation
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <iostream>
25 #include <ctime>
31 #include <utils/options/Option.h>
38 #include <utils/xml/XMLSubSys.h>
39 #include <guisim/GUINet.h>
40 #include <guisim/GUIEventControl.h>
42 #include <netload/NLBuilder.h>
43 #include <netload/NLHandler.h>
50 #include <microsim/MSGlobals.h>
51 #include <microsim/MSFrame.h>
55 #include "TraCIServerAPI_GUI.h"
56 #include "GUIApplicationWindow.h"
57 #include "GUILoadThread.h"
58 #include "GUIGlobals.h"
60 
61 
62 // ===========================================================================
63 // member method definitions
64 // ===========================================================================
67  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
68  myEventThrow(ev) {
73 }
74 
75 
77  delete myErrorRetriever;
78  delete myMessageRetriever;
79  delete myWarningRetriever;
80 }
81 
82 
83 FXint
85  // register message callbacks
89 
90  // try to load the given configuration
92  try {
93  if (myFile != "") {
94  // triggered by menu option or reload
95  oc.clear();
98  oc.resetWritable(); // there may be command line options
100  } else {
101  // triggered at application start
103  if (oc.isSet("configuration-file")) {
104  myFile = oc.getString("configuration-file");
105  } else if (oc.isSet("net-file")) {
106  myFile = oc.getString("net-file");
107  }
108  myEventQue.push_back(new GUIEvent_Message("Loading '" + myFile + "'."));
110  myParent->addRecentFile(FXPath::absolute(myFile.c_str()));
111  }
112  myTitle = myFile;
113  // within gui-based applications, nothing is reported to the console
117  // do this once again to get parsed options
118  if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
119  // must be done before calling initOutputOptions (which checks option "verbose")
120  // but initOutputOptions must come before checkOptions (so that warnings are printed)
121  oc.set("verbose", "true");
122  }
124  if (!MSFrame::checkOptions()) {
125  throw ProcessError();
126  }
127  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
128  GUIGlobals::gRunAfterLoad = oc.getBool("start");
129  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
131  GUIGlobals::gTrackerInterval = oc.getFloat("tracker-interval");
132  } catch (ProcessError& e) {
133  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
134  WRITE_ERROR(e.what());
135  }
136  // the options are not valid but maybe we want to quit
137  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
138  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
139  submitEndAndCleanup(nullptr, 0, 0);
140  return 0;
141  }
142 
143  // initialise global settings
146  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
147 
148  MSVehicleControl* vehControl = nullptr;
151  vehControl = new GUIMEVehicleControl();
152  } else {
153  vehControl = new GUIVehicleControl();
154  }
155 
156  GUINet* net = nullptr;
157  SUMOTime simStartTime = 0;
158  SUMOTime simEndTime = 0;
159  std::vector<std::string> guiSettingsFiles;
160  bool osgView = false;
161  GUIEdgeControlBuilder* eb = nullptr;
162  try {
163  net = new GUINet(
164  vehControl,
165  new GUIEventControl(),
166  new GUIEventControl(),
167  new GUIEventControl());
168  // need to init TraCI-Server before loading routes to catch VEHICLE_STATE_BUILT
169  std::map<int, TraCIServer::CmdExecutor> execs;
173 
174  eb = new GUIEdgeControlBuilder();
175  GUIDetectorBuilder db(*net);
176  NLJunctionControlBuilder jb(*net, db);
178  NLHandler handler("", *net, db, tb, *eb, jb);
179  tb.setHandler(&handler);
180  NLBuilder builder(oc, *net, *eb, jb, db, handler);
184  if (!builder.build()) {
185  throw ProcessError();
186  } else {
187  net->initGUIStructures();
188  simStartTime = string2time(oc.getString("begin"));
189  simEndTime = string2time(oc.getString("end"));
190  guiSettingsFiles = oc.getStringVector("gui-settings-file");
191 #ifdef HAVE_OSG
192  osgView = oc.getBool("osg-view");
193 #endif
194  if (oc.isSet("edgedata-files")) {
195  if (!oc.isUsableFileList("edgedata-files")) {
196  WRITE_ERROR("Could not load edgedata-files '" + oc.getString("edgedata-files") + "'");
197  } else {
198  for (const std::string& file : oc.getStringVector("edgedata-files")) {
199  net->loadEdgeData(file);
200  }
201  }
202  }
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);
243 }
244 
245 
246 void
247 GUILoadThread::loadConfigOrNet(const std::string& file) {
248  myFile = file;
249  if (myFile != "") {
250  OptionsIO::setArgs(0, nullptr);
251  }
252  start();
253 }
254 
255 
256 void
257 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
258  GUIEvent* e = new GUIEvent_Message(type, msg);
261 }
262 
263 
264 const std::string&
266  return myFile;
267 }
268 
269 
270 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:31
void push_back(T what)
Definition: FXSynchQue.h:114
The main window of the SUMO-gui.
void addRecentFile(const FX::FXString &f)
Builds detectors for guisim.
Derivation of NLEdgeControlBuilder which builds gui-edges.
Stores time-dependant events and executes them at the proper time (guisim)
static double gTrackerInterval
the aggregation period for tracker windows in seconds
Definition: GUIGlobals.h:51
static bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:42
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:45
static bool gDemoAutoReload
the simulation shall reload when it has ended (demo)
Definition: GUIGlobals.h:48
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.
OutputDevice * myWarningRetriever
Definition: GUILoadThread.h:91
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:84
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on.
Definition: GUILoadThread.h:91
FXSynchQue< GUIEvent * > & myEventQue
Definition: GUILoadThread.h:93
FXEX::FXThreadEvent & myEventThrow
Definition: GUILoadThread.h:95
virtual ~GUILoadThread()
destructor
void loadConfigOrNet(const std::string &file)
begins the loading of the given file
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
const std::string & getFileName() const
std::string myTitle
the title string for the application
Definition: GUILoadThread.h:87
OutputDevice * myMessageRetriever
Definition: GUILoadThread.h:91
GUIApplicationWindow * myParent
the parent window to inform about the loading
Definition: GUILoadThread.h:81
GUILoadThread(FXApp *app, GUIApplicationWindow *mw, FXSynchQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
The class responsible for building and deletion of vehicles (gui-version)
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:81
bool loadEdgeData(const std::string &file)
load edgeData from file
Definition: GUINet.cpp:632
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:263
static void allowTextures(const bool val)
switch texture drawing on and off
Builds trigger objects for guisim.
The class responsible for building and deletion of vehicles (gui-version)
static bool UseMesoSim
this should be set at the same time as MSGlobals::gUseMesoSim
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:841
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:59
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:658
static bool gUseMesoSim
Definition: MSGlobals.h:88
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:743
The class responsible for building and deletion of vehicles.
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:175
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:217
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:67
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:159
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:183
@ MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:45
@ MT_WARNING
The message is a warning.
Definition: MsgHandler.h:47
@ MT_ERROR
The message is an error.
Definition: MsgHandler.h:49
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:54
Encapsulates an object's method for using it as a message retriever.
The main interface for loading a microsim.
Definition: NLBuilder.h:58
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:119
static void initRandomness()
initializes all RNGs
Definition: NLBuilder.cpp:312
The XML-Handler for network loading.
Definition: NLHandler.h:79
Builder of microsim-junctions and tls.
void setHandler(NLHandler *handler)
Sets the parent handler to use for nested parsing.
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void clear()
Removes all information from the container.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
bool setByRootElement(const std::string &name, const std::string &value)
Sets the given value for the option which can handle the given XML root.
void resetWritable()
Resets all options to be writeable.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file)
static void loadConfiguration()
Loads and parses the configuration.
Definition: OptionsIO.cpp:102
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:79
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:141
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get 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 openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
TRACI_CONST int CMD_SET_GUI_VARIABLE
TRACI_CONST int CMD_GET_GUI_VARIABLE