Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2026 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>
39#include <utils/xml/XMLSubSys.h>
40#include <guisim/GUINet.h>
43#include <netload/NLBuilder.h>
44#include <netload/NLHandler.h>
51#include <microsim/MSGlobals.h>
52#include <microsim/MSFrame.h>
55#include <libsumo/Helper.h>
57#include "TraCIServerAPI_GUI.h"
59#include "GUILoadThread.h"
60#include "GUIGlobals.h"
62
63
64// ===========================================================================
65// member method definitions
66// ===========================================================================
76
77
83
84
85FXint
87 // register message callbacks
90 if (!OptionsCont::getOptions().getBool("no-warnings")) {
92 }
93
94 // try to load the given configuration
96 try {
97 if (myFile != "") {
98 // triggered by menu option or reload
99 oc.clear();
102 oc.resetWritable(); // there may be command line options
104 } else {
105 // triggered at application start
106 if (oc.isSet("configuration-file")) {
107 // remember the file before parsing so it can be reloaded after an error
108 myFile = oc.getString("configuration-file");
109 }
111 if (oc.isSet("configuration-file")) {
112 myFile = oc.getString("configuration-file");
113 myParent->addRecentConfig(FXPath::absolute(myFile.c_str()));
114
115 } else if (oc.isSet("net-file")) {
116 myFile = oc.getString("net-file");
117 myParent->addRecentNetwork(FXPath::absolute(myFile.c_str()));
118 }
119 myEventQue.push_back(new GUIEvent_Message(TLF("Loading '%'.", myFile)));
121 }
122 myTitle = myFile;
123 if (!myAmLibsumo) {
124 // within gui-based applications, nothing is reported to the console
128 }
129 // do this once again to get parsed options
130 if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
131 // must be done before calling initOutputOptions (which checks option "verbose")
132 // but initOutputOptions must come before checkOptions (so that warnings are printed)
133 oc.setDefault("verbose", "true");
134 }
136 if (!MSFrame::checkOptions()) {
137 throw ProcessError();
138 }
139 XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
140 GUIGlobals::gRunAfterLoad = oc.getBool("start") || (myAmLibsumo && std::getenv("LIBSUMO_GUI") != nullptr);
141 GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
144 } catch (ProcessError& e) {
145 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
146 WRITE_ERROR(e.what());
147 }
148 // the options are not valid but maybe we want to quit
149 GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
150 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
151 submitEndAndCleanup(nullptr, 0, 0);
152 return 0;
153 }
154
155 // initialise global settings
158 GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
159
160 MSVehicleControl* vehControl = nullptr;
163 vehControl = new GUIMEVehicleControl();
164 } else {
165 vehControl = new GUIVehicleControl();
166 }
167
168 GUINet* net = nullptr;
169 SUMOTime simStartTime = 0;
170 SUMOTime simEndTime = 0;
171 std::vector<std::string> guiSettingsFiles;
172 bool osgView = false;
173 GUIEdgeControlBuilder* eb = nullptr;
174 try {
175 net = new GUINet(
176 vehControl,
177 new GUIEventControl(),
178 new GUIEventControl(),
179 new GUIEventControl());
180 // need to init TraCI-Server before loading routes to catch VehicleState::BUILT
181 std::map<int, TraCIServer::CmdExecutor> execs;
185 if (myAmLibsumo) {
187 }
188
189 eb = new GUIEdgeControlBuilder();
190 GUIDetectorBuilder db(*net);
191 NLJunctionControlBuilder jb(*net, db);
193 NLHandler handler("", *net, db, tb, *eb, jb);
194 tb.setHandler(&handler);
195 NLBuilder builder(oc, *net, *eb, jb, db, handler);
199 if (!builder.build()) {
200 throw ProcessError();
201 } else {
202 net->initGUIStructures();
203 simStartTime = string2time(oc.getString("begin"));
204 simEndTime = string2time(oc.getString("end"));
205 guiSettingsFiles = oc.getStringVector("gui-settings-file");
206#ifdef HAVE_OSG
207 osgView = oc.getBool("osg-view");
208#endif
209 if (oc.isSet("edgedata-files")) {
210 if (!oc.isUsableFileList("edgedata-files")) {
211 WRITE_ERRORF(TL("Could not load edgedata-files '%'"), oc.getString("edgedata-files"));
212 } else {
213 for (const std::string& file : oc.getStringVector("edgedata-files")) {
214 net->loadEdgeData(file);
215 }
216 }
217 }
218 }
219 } catch (ProcessError& e) {
220 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
221 WRITE_ERROR(e.what());
222 }
223 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
224 delete net;
225 net = nullptr;
226#ifndef _DEBUG
227 } catch (std::exception& e) {
228 WRITE_ERROR(e.what());
229 delete net;
230 net = nullptr;
231#endif
232 }
233 if (net == nullptr) {
235 }
236 delete eb;
237 submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView,
238 oc.getBool("registry-viewport"));
239 return 0;
240}
241
242
243void
245 const SUMOTime simStartTime,
246 const SUMOTime simEndTime,
247 const std::vector<std::string>& guiSettingsFiles,
248 const bool osgView,
249 const bool viewportFromRegistry) {
250 // remove message callbacks
254 // inform parent about the process
255 GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView, viewportFromRegistry);
258}
259
260
261void
262GUILoadThread::loadConfigOrNet(const std::string& file) {
263 myFile = file;
264 if (myFile != "") {
265 OptionsIO::setArgs(0, nullptr);
266 }
267 start();
268}
269
270
271void
272GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
273 GUIEvent* e = new GUIEvent_Message(type, msg);
276}
277
278
279const std::string&
281 return myFile;
282}
283
284
285/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_ERRORF(...)
Definition MsgHandler.h:308
#define WRITE_ERROR(msg)
Definition MsgHandler.h:307
#define TL(string)
Definition MsgHandler.h:316
#define TLF(string,...)
Definition MsgHandler.h:318
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:47
#define STEPS2TIME(x)
Definition SUMOTime.h:58
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
std::string myFile
the path to load the simulation from
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on.
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
OutputDevice * myMessageRetriever
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
FXEX::MFXThreadEvent & myEventThrow
const bool myAmLibsumo
whether we are running in libsumo
MFXSynchQue< GUIEvent * > & myEventQue
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:719
void initGUIStructures()
Initialises gui wrappers.
Definition GUINet.cpp:276
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)
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition MSFrame.cpp:1184
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition MSFrame.cpp:63
static bool checkOptions()
Checks the set options.
Definition MSFrame.cpp:951
static bool gUseMesoSim
Definition MSGlobals.h:106
static void clearAll()
Clears all dictionaries.
Definition MSNet.cpp:1063
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.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
@ 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.
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.
static void initRandomness()
initializes all RNGs
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:92
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.
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:99
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition OptionsIO.cpp:59
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition OptionsIO.cpp:75
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
static void initOutputOptions()
init message output (console and log files) according to the options
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:87
static void registerStateListener()
Definition Helper.cpp:739
TRACI_CONST int CMD_SET_GUI_VARIABLE
TRACI_CONST int CMD_GET_GUI_VARIABLE