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-2022 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
88  if (!OptionsCont::getOptions().getBool("no-warnings")) {
90  }
91 
92  // try to load the given configuration
94  try {
95  if (myFile != "") {
96  // triggered by menu option or reload
97  oc.clear();
100  oc.resetWritable(); // there may be command line options
102  } else {
103  // triggered at application start
105  if (oc.isSet("configuration-file")) {
106  myFile = oc.getString("configuration-file");
107  } else if (oc.isSet("net-file")) {
108  myFile = oc.getString("net-file");
109  }
110  myEventQue.push_back(new GUIEvent_Message("Loading '" + myFile + "'."));
112  myParent->addRecentFile(FXPath::absolute(myFile.c_str()));
113  }
114  myTitle = myFile;
115  // within gui-based applications, nothing is reported to the console
119  // do this once again to get parsed options
120  if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
121  // must be done before calling initOutputOptions (which checks option "verbose")
122  // but initOutputOptions must come before checkOptions (so that warnings are printed)
123  oc.set("verbose", "true");
124  }
126  if (!MSFrame::checkOptions()) {
127  throw ProcessError();
128  }
129  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
130  GUIGlobals::gRunAfterLoad = oc.getBool("start");
131  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
134  } catch (ProcessError& e) {
135  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
136  WRITE_ERROR(e.what());
137  }
138  // the options are not valid but maybe we want to quit
139  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
140  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
141  submitEndAndCleanup(nullptr, 0, 0);
142  return 0;
143  }
144 
145  // initialise global settings
148  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
149 
150  MSVehicleControl* vehControl = nullptr;
153  vehControl = new GUIMEVehicleControl();
154  } else {
155  vehControl = new GUIVehicleControl();
156  }
157 
158  GUINet* net = nullptr;
159  SUMOTime simStartTime = 0;
160  SUMOTime simEndTime = 0;
161  std::vector<std::string> guiSettingsFiles;
162  bool osgView = false;
163  GUIEdgeControlBuilder* eb = nullptr;
164  try {
165  net = new GUINet(
166  vehControl,
167  new GUIEventControl(),
168  new GUIEventControl(),
169  new GUIEventControl());
170  // need to init TraCI-Server before loading routes to catch VehicleState::BUILT
171  std::map<int, TraCIServer::CmdExecutor> execs;
175 
176  eb = new GUIEdgeControlBuilder();
177  GUIDetectorBuilder db(*net);
178  NLJunctionControlBuilder jb(*net, db);
180  NLHandler handler("", *net, db, tb, *eb, jb);
181  tb.setHandler(&handler);
182  NLBuilder builder(oc, *net, *eb, jb, db, handler);
186  if (!builder.build()) {
187  throw ProcessError();
188  } else {
189  net->initGUIStructures();
190  simStartTime = string2time(oc.getString("begin"));
191  simEndTime = string2time(oc.getString("end"));
192  guiSettingsFiles = oc.getStringVector("gui-settings-file");
193 #ifdef HAVE_OSG
194  osgView = oc.getBool("osg-view");
195 #endif
196  if (oc.isSet("edgedata-files")) {
197  if (!oc.isUsableFileList("edgedata-files")) {
198  WRITE_ERROR("Could not load edgedata-files '" + oc.getString("edgedata-files") + "'");
199  } else {
200  for (const std::string& file : oc.getStringVector("edgedata-files")) {
201  net->loadEdgeData(file);
202  }
203  }
204  }
205  }
206  } catch (ProcessError& e) {
207  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
208  WRITE_ERROR(e.what());
209  }
210  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
211  delete net;
212  net = nullptr;
213 #ifndef _DEBUG
214  } catch (std::exception& e) {
215  WRITE_ERROR(e.what());
216  delete net;
217  net = nullptr;
218 #endif
219  }
220  if (net == nullptr) {
221  MSNet::clearAll();
222  }
223  delete eb;
224  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView,
225  oc.getBool("registry-viewport"));
226  return 0;
227 }
228 
229 
230 void
232  const SUMOTime simStartTime,
233  const SUMOTime simEndTime,
234  const std::vector<std::string>& guiSettingsFiles,
235  const bool osgView,
236  const bool viewportFromRegistry) {
237  // remove message callbacks
241  // inform parent about the process
242  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView, viewportFromRegistry);
245 }
246 
247 
248 void
249 GUILoadThread::loadConfigOrNet(const std::string& file) {
250  myFile = file;
251  if (myFile != "") {
252  OptionsIO::setArgs(0, nullptr);
253  }
254  start();
255 }
256 
257 
258 void
259 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
260  GUIEvent* e = new GUIEvent_Message(type, msg);
263 }
264 
265 
266 const std::string&
268  return myFile;
269 }
270 
271 
272 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
long long int SUMOTime
Definition: SUMOTime.h:32
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:641
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:261
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:922
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:730
static bool gUseMesoSim
Definition: MSGlobals.h:94
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:799
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:186
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:228
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:162
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:194
@ 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: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:120
static void initRandomness()
initializes all RNGs
Definition: NLBuilder.cpp:336
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.
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:108
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:85
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:152
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