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.dev/sumo
3 // Copyright (C) 2001-2024 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>
54 #include <libsumo/Helper.h>
56 #include "TraCIServerAPI_GUI.h"
57 #include "GUIApplicationWindow.h"
58 #include "GUILoadThread.h"
59 #include "GUIGlobals.h"
61 
62 
63 // ===========================================================================
64 // member method definitions
65 // ===========================================================================
67  MFXSynchQue<GUIEvent*>& eq, FXEX::MFXThreadEvent& ev, const bool isLibsumo)
68  : MFXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
69  myEventThrow(ev), myAmLibsumo(isLibsumo) {
74 }
75 
76 
78  delete myErrorRetriever;
79  delete myMessageRetriever;
80  delete myWarningRetriever;
81 }
82 
83 
84 FXint
86  // register message callbacks
89  if (!OptionsCont::getOptions().getBool("no-warnings")) {
91  }
92 
93  // try to load the given configuration
95  try {
96  if (myFile != "") {
97  // triggered by menu option or reload
98  oc.clear();
101  oc.resetWritable(); // there may be command line options
103  } else {
104  // triggered at application start
106  if (oc.isSet("configuration-file")) {
107  myFile = oc.getString("configuration-file");
108  myParent->addRecentConfig(FXPath::absolute(myFile.c_str()));
109 
110  } else if (oc.isSet("net-file")) {
111  myFile = oc.getString("net-file");
112  myParent->addRecentNetwork(FXPath::absolute(myFile.c_str()));
113  }
114  myEventQue.push_back(new GUIEvent_Message("Loading '" + myFile + "'."));
116  }
117  myTitle = myFile;
118  if (!myAmLibsumo) {
119  // within gui-based applications, nothing is reported to the console
123  }
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.setDefault("verbose", "true");
129  }
131  if (!MSFrame::checkOptions()) {
132  throw ProcessError();
133  }
134  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
135  GUIGlobals::gRunAfterLoad = oc.getBool("start") || (myAmLibsumo && std::getenv("LIBSUMO_GUI") != nullptr);
136  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
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
153  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
154 
155  MSVehicleControl* vehControl = nullptr;
158  vehControl = new GUIMEVehicleControl();
159  } else {
160  vehControl = new GUIVehicleControl();
161  }
162 
163  GUINet* net = nullptr;
164  SUMOTime simStartTime = 0;
165  SUMOTime simEndTime = 0;
166  std::vector<std::string> guiSettingsFiles;
167  bool osgView = false;
168  GUIEdgeControlBuilder* eb = nullptr;
169  try {
170  net = new GUINet(
171  vehControl,
172  new GUIEventControl(),
173  new GUIEventControl(),
174  new GUIEventControl());
175  // need to init TraCI-Server before loading routes to catch VehicleState::BUILT
176  std::map<int, TraCIServer::CmdExecutor> execs;
180  if (myAmLibsumo) {
182  }
183 
184  eb = new GUIEdgeControlBuilder();
185  GUIDetectorBuilder db(*net);
186  NLJunctionControlBuilder jb(*net, db);
188  NLHandler handler("", *net, db, tb, *eb, jb);
189  tb.setHandler(&handler);
190  NLBuilder builder(oc, *net, *eb, jb, db, handler);
194  if (!builder.build()) {
195  throw ProcessError();
196  } else {
197  net->initGUIStructures();
198  simStartTime = string2time(oc.getString("begin"));
199  simEndTime = string2time(oc.getString("end"));
200  guiSettingsFiles = oc.getStringVector("gui-settings-file");
201 #ifdef HAVE_OSG
202  osgView = oc.getBool("osg-view");
203 #endif
204  if (oc.isSet("edgedata-files")) {
205  if (!oc.isUsableFileList("edgedata-files")) {
206  WRITE_ERRORF(TL("Could not load edgedata-files '%'"), oc.getString("edgedata-files"));
207  } else {
208  for (const std::string& file : oc.getStringVector("edgedata-files")) {
209  net->loadEdgeData(file);
210  }
211  }
212  }
213  }
214  } catch (ProcessError& e) {
215  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
216  WRITE_ERROR(e.what());
217  }
218  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
219  delete net;
220  net = nullptr;
221 #ifndef _DEBUG
222  } catch (std::exception& e) {
223  WRITE_ERROR(e.what());
224  delete net;
225  net = nullptr;
226 #endif
227  }
228  if (net == nullptr) {
229  MSNet::clearAll();
230  }
231  delete eb;
232  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView,
233  oc.getBool("registry-viewport"));
234  return 0;
235 }
236 
237 
238 void
240  const SUMOTime simStartTime,
241  const SUMOTime simEndTime,
242  const std::vector<std::string>& guiSettingsFiles,
243  const bool osgView,
244  const bool viewportFromRegistry) {
245  // remove message callbacks
249  // inform parent about the process
250  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView, viewportFromRegistry);
253 }
254 
255 
256 void
257 GUILoadThread::loadConfigOrNet(const std::string& file) {
258  myFile = file;
259  if (myFile != "") {
260  OptionsIO::setArgs(0, nullptr);
261  }
262  start();
263 }
264 
265 
266 void
267 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
268  GUIEvent* e = new GUIEvent_Message(type, msg);
271 }
272 
273 
274 const std::string&
276  return myFile;
277 }
278 
279 
280 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
#define TL(string)
Definition: MsgHandler.h:315
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
The main window of the SUMO-gui.
void addRecentConfig(const FX::FXString &f)
add recent config to recent file list
void addRecentNetwork(const FX::FXString &f)
add recent network to recent file list
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:46
static bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:37
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:40
static bool gDemoAutoReload
the simulation shall reload when it has ended (demo)
Definition: GUIGlobals.h:43
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
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
GUILoadThread(FXApp *app, GUIApplicationWindow *mw, MFXSynchQue< GUIEvent * > &eq, FXEX::MFXThreadEvent &ev, const bool isLibsumo)
constructor
GUIApplicationWindow * myParent
the parent window to inform about the loading
Definition: GUILoadThread.h:81
FXEX::MFXThreadEvent & myEventThrow
Definition: GUILoadThread.h:95
const bool myAmLibsumo
whether we are running in libsumo
Definition: GUILoadThread.h:98
MFXSynchQue< GUIEvent * > & myEventQue
Definition: GUILoadThread.h:93
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:82
bool loadEdgeData(const std::string &file)
load edgeData from file
Definition: GUINet.cpp:697
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:267
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
void push_back(T what)
Definition: MFXSynchQue.h:113
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:1037
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:60
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:836
static bool gUseMesoSim
Definition: MSGlobals.h:103
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:950
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:237
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:92
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:154
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:316
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:79
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:213
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:245
@ MT_MESSAGE
The message is only something to show.
@ MT_ERROR
The message is an error.
@ MT_WARNING
The message is a warning.
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:66
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:124
static void initRandomness()
initializes all RNGs
Definition: NLBuilder.cpp:363
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.
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.
bool setDefault(const std::string &name, const std::string &value)
Sets the given value for the named option as new 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:60
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:98
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:74
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:142
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
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:66
static void registerStateListener()
Definition: Helper.cpp:692
TRACI_CONST int CMD_SET_GUI_VARIABLE
TRACI_CONST int CMD_GET_GUI_VARIABLE