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-2019 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 /****************************************************************************/
16 // Class describing the thread that performs the loading of a simulation
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <iostream>
26 #include <ctime>
32 #include <utils/options/Option.h>
39 #include <utils/xml/XMLSubSys.h>
40 #include <guisim/GUINet.h>
41 #include <guisim/GUIEventControl.h>
43 #include <netload/NLBuilder.h>
44 #include <netload/NLHandler.h>
51 #include <microsim/MSGlobals.h>
52 #include <microsim/MSFrame.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 // ===========================================================================
68  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
69  myEventThrow(ev) {
74 }
75 
76 
78  delete myErrorRetriever;
79  delete myMessageRetriever;
80  delete myWarningRetriever;
81 }
82 
83 
84 FXint
86  // register message callbacks
90 
91  // try to load the given configuration
93  try {
94  oc.clear();
96  if (myFile != "") {
97  // triggered by menu option or reload
98  if (myLoadNet) {
99  oc.set("net-file", myFile);
100  } else {
101  oc.set("configuration-file", myFile);
102  }
103  oc.resetWritable(); // there may be command line options
105  } else {
106  // triggered at application start
108  if (oc.isSet("configuration-file")) {
109  myFile = oc.getString("configuration-file");
110  } else if (oc.isSet("net-file")) {
111  myFile = oc.getString("net-file");
112  myLoadNet = true;
113  }
114  myEventQue.push_back(new GUIEvent_Message("Loading '" + myFile + "'."));
116  myParent->addRecentFile(FXPath::absolute(myFile.c_str()), myLoadNet);
117  }
118  myTitle = myFile;
119  // within gui-based applications, nothing is reported to the console
123  // do this once again to get parsed options
124  if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
125  // must be done before calling initOutputOptions (which checks option "verbose")
126  // but initOutputOptions must come before checkOptions (so that warnings are printed)
127  oc.set("verbose", "true");
128  }
130  if (!MSFrame::checkOptions()) {
131  throw ProcessError();
132  }
133  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
134  GUIGlobals::gRunAfterLoad = oc.getBool("start");
135  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
137  GUIGlobals::gTrackerInterval = oc.getFloat("tracker-interval");
138  } catch (ProcessError& e) {
139  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
140  WRITE_ERROR(e.what());
141  }
142  // the options are not valid but maybe we want to quit
143  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
144  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
145  submitEndAndCleanup(nullptr, 0, 0);
146  return 0;
147  }
148 
149  // initialise global settings
152  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
153 
154  MSVehicleControl* vehControl = nullptr;
157  vehControl = new GUIMEVehicleControl();
158  } else {
159  vehControl = new GUIVehicleControl();
160  }
161 
162  GUINet* net = nullptr;
163  SUMOTime simStartTime = 0;
164  SUMOTime simEndTime = 0;
165  std::vector<std::string> guiSettingsFiles;
166  bool osgView = false;
167  GUIEdgeControlBuilder* eb = nullptr;
168  try {
169  net = new GUINet(
170  vehControl,
171  new GUIEventControl(),
172  new GUIEventControl(),
173  new GUIEventControl());
174  // need to init TraCI-Server before loading routes to catch VEHICLE_STATE_BUILT
175  std::map<int, TraCIServer::CmdExecutor> execs;
179 
180  eb = new GUIEdgeControlBuilder();
181  GUIDetectorBuilder db(*net);
182  NLJunctionControlBuilder jb(*net, db);
184  NLHandler handler("", *net, db, tb, *eb, jb);
185  tb.setHandler(&handler);
186  NLBuilder builder(oc, *net, *eb, jb, db, handler);
190  if (!builder.build()) {
191  throw ProcessError();
192  } else {
193  net->initGUIStructures();
194  simStartTime = string2time(oc.getString("begin"));
195  simEndTime = string2time(oc.getString("end"));
196  guiSettingsFiles = oc.getStringVector("gui-settings-file");
197 #ifdef HAVE_OSG
198  osgView = oc.getBool("osg-view");
199 #endif
200  if (oc.isSet("edgedata-files")) {
201  if (!oc.isUsableFileList("edgedata-files")) {
202  WRITE_ERROR("Could not load edgedata-files '" + oc.getString("edgedata-files") + "'");
203  } else {
204  for (const std::string& file : oc.getStringVector("edgedata-files")) {
205  net->loadEdgeData(file);
206  }
207  }
208  }
209  }
210  } catch (ProcessError& e) {
211  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
212  WRITE_ERROR(e.what());
213  }
214  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
215  delete net;
216  net = nullptr;
217 #ifndef _DEBUG
218  } catch (std::exception& e) {
219  WRITE_ERROR(e.what());
220  delete net;
221  net = nullptr;
222 #endif
223  }
224  if (net == nullptr) {
225  MSNet::clearAll();
226  }
227  delete eb;
228  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView,
229  oc.getBool("registry-viewport"));
230  return 0;
231 }
232 
233 
234 void
236  const SUMOTime simStartTime,
237  const SUMOTime simEndTime,
238  const std::vector<std::string>& guiSettingsFiles,
239  const bool osgView,
240  const bool viewportFromRegistry) {
241  // remove message callbacks
245  // inform parent about the process
246  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView, viewportFromRegistry);
249 }
250 
251 
252 void
253 GUILoadThread::loadConfigOrNet(const std::string& file, bool isNet) {
254  myFile = file;
255  myLoadNet = isNet;
256  if (myFile != "") {
257  OptionsIO::setArgs(0, nullptr);
258  }
259  start();
260 }
261 
262 
263 void
264 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
265  GUIEvent* e = new GUIEvent_Message(type, msg);
268 }
269 
270 
271 const std::string&
273  return myFile;
274 }
275 
276 
277 /****************************************************************************/
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
MsgHandler::MsgType
MsgType
Definition: MsgHandler.h:45
MsgHandler::MT_ERROR
@ MT_ERROR
The message is an error.
Definition: MsgHandler.h:51
MSFrame::fillOptions
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:60
NLBuilder::initRandomness
static void initRandomness()
initializes all RNGs
Definition: NLBuilder.cpp:271
GUIVisualizationSettings::UseMesoSim
static bool UseMesoSim
this should be set at the same time as MSGlobals::gUseMesoSim
Definition: GUIVisualizationSettings.h:437
libsumo::CMD_SET_GUI_VARIABLE
TRACI_CONST int CMD_SET_GUI_VARIABLE
Definition: TraCIConstants.h:273
NLBuilder
The main interface for loading a microsim.
Definition: NLBuilder.h:60
GUIEvent_SimulationLoaded
Definition: GUIEvent_SimulationLoaded.h:49
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:216
GUITriggerBuilder.h
MSDetectorControl.h
MSFrame::setMSGlobals
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:713
TraCIServerAPI_GUI::processSet
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
Definition: TraCIServerAPI_GUI.cpp:129
GUILoadThread::submitEndAndCleanup
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.
Definition: GUILoadThread.cpp:235
GUIEdgeControlBuilder.h
NLBuilder.h
TraCIServerAPI_GUI::processGet
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
Definition: TraCIServerAPI_GUI.cpp:43
GUIVehicleControl.h
OptionsCont.h
MsgHandler::MT_MESSAGE
@ MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:47
OptionsCont::resetWritable
void resetWritable()
Resets all options to be writeable.
Definition: OptionsCont.cpp:441
GUIMEVehicleControl.h
OptionsCont::set
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
Definition: OptionsCont.cpp:241
GUILoadThread::myLoadNet
bool myLoadNet
Information whether only the network shall be loaded.
Definition: GUILoadThread.h:100
MsgHandler.h
MsgRetrievingFunction
Encapsulates an object's method for using it as a message retriever.
Definition: MsgRetrievingFunction.h:41
MsgHandler::MT_WARNING
@ MT_WARNING
The message is a warning.
Definition: MsgHandler.h:49
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
GUILoadThread::myParent
GUIApplicationWindow * myParent
the parent window to inform about the loading
Definition: GUILoadThread.h:83
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
NLTriggerBuilder::setHandler
void setHandler(NLHandler *handler)
Sets the parent handler to use for nested parsing.
Definition: NLTriggerBuilder.cpp:66
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSGlobals::gUseMesoSim
static bool gUseMesoSim
Definition: MSGlobals.h:90
MSNet::clearAll
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:653
GUIEvent_Message.h
GUIVehicleControl
The class responsible for building and deletion of vehicles (gui-version)
Definition: GUIVehicleControl.h:46
GUILoadThread::GUILoadThread
GUILoadThread(FXApp *app, GUIApplicationWindow *mw, FXSynchQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
Definition: GUILoadThread.cpp:66
GUINet.h
GUILoadThread::getFileName
const std::string & getFileName() const
Definition: GUILoadThread.cpp:272
GUIMEVehicleControl
The class responsible for building and deletion of vehicles (gui-version)
Definition: GUIMEVehicleControl.h:40
FXSynchQue.h
MSDevice.h
GUIEventControl
Stores time-dependant events and executes them at the proper time (guisim)
Definition: GUIEventControl.h:40
GUILoadThread::myTitle
std::string myTitle
the title string for the application
Definition: GUILoadThread.h:89
GUIGlObjectStorage.h
FXEX::FXThreadEvent::signal
void signal()
Definition: FXThreadEvent.cpp:99
GUIAppEnum.h
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
MsgHandler::clear
virtual void clear()
Clears information whether an error occurred previously.
Definition: MsgHandler.cpp:160
GUINet::loadEdgeData
bool loadEdgeData(const std::string &file)
load edgeData from file
Definition: GUINet.cpp:594
NLJunctionControlBuilder
Builder of microsim-junctions and tls.
Definition: NLJunctionControlBuilder.h:62
NLHandler.h
GUIDetectorBuilder
Builds detectors for guisim.
Definition: GUIDetectorBuilder.h:48
OptionsCont::isUsableFileList
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file)
Definition: OptionsCont.cpp:356
GUIEdgeControlBuilder
Derivation of NLEdgeControlBuilder which builds gui-edges.
Definition: GUIEdgeControlBuilder.h:52
XMLSubSys::setValidation
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:58
MsgHandler::getWarningInstance
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:68
GUITriggerBuilder
Builds trigger objects for guisim.
Definition: GUITriggerBuilder.h:49
GUITexturesHelper.h
GUIGlobals.h
GUILoadThread::myErrorRetriever
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on.
Definition: GUILoadThread.h:93
GUIApplicationWindow
The main window of the SUMO-gui.
Definition: GUIApplicationWindow.h:66
ProcessError
Definition: UtilExceptions.h:39
GUIApplicationWindow.h
GUILoadThread::myMessageRetriever
OutputDevice * myMessageRetriever
Definition: GUILoadThread.h:93
GUINet::initGUIStructures
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:257
GUILoadThread::myWarningRetriever
OutputDevice * myWarningRetriever
Definition: GUILoadThread.h:93
MSGlobals.h
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
GUITexturesHelper::allowTextures
static void allowTextures(const bool val)
switch texture drawing on and off
Definition: GUITexturesHelper.h:61
GUIGlobals::gQuitOnEnd
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:47
GUILoadThread::myFile
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:86
GUILoadThread::retrieveMessage
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
Definition: GUILoadThread.cpp:264
GUIEventControl.h
GUIDetectorBuilder.h
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
FXSynchQue< GUIEvent * >
GUIGlobals::gTrackerInterval
static double gTrackerInterval
the aggregation period for tracker windows in seconds
Definition: GUIGlobals.h:53
TraCIServer::openSocket
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
Definition: TraCIServer.cpp:285
OptionsCont::isDefault
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
Definition: OptionsCont.cpp:163
NLBuilder::build
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:120
OptionsIO::getOptions
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:75
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
libsumo::CMD_GET_GUI_VARIABLE
TRACI_CONST int CMD_GET_GUI_VARIABLE
Definition: TraCIConstants.h:269
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:54
GUILoadThread::myEventThrow
FXEX::FXThreadEvent & myEventThrow
Definition: GUILoadThread.h:97
Option.h
GUILoadThread.h
GUIEvent
Definition: GUIEvent.h:76
MsgHandler::addRetriever
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:174
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:54
GUIGlobals::gDemoAutoReload
static bool gDemoAutoReload
the simulation shall reload when it has ended (demo)
Definition: GUIGlobals.h:50
TraCIServer.h
MsgRetrievingFunction.h
NLJunctionControlBuilder.h
FXSynchQue::push_back
void push_back(T what)
Definition: FXSynchQue.h:115
GUILoadThread::loadConfigOrNet
void loadConfigOrNet(const std::string &file, bool isNet)
begins the loading of the given file
Definition: GUILoadThread.cpp:253
MSFrame.h
NLHandler
The XML-Handler for network loading.
Definition: NLHandler.h:80
config.h
MSVehicleControl
The class responsible for building and deletion of vehicles.
Definition: MSVehicleControl.h:71
RandHelper.h
FXSingleEventThread
Definition: FXSingleEventThread.h:34
GUIGlobals::gRunAfterLoad
static bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:44
FXEX::FXThreadEvent
Definition: FXThreadEvent.h:105
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
GUILoadThread::~GUILoadThread
virtual ~GUILoadThread()
destructor
Definition: GUILoadThread.cpp:77
GUINet
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:82
GUILoadThread::run
FXint run()
Definition: GUILoadThread.cpp:85
GUIApplicationWindow::addRecentFile
void addRecentFile(const FX::FXString &f, const bool isNet)
Definition: GUIApplicationWindow.cpp:1831
MSRouteHandler.h
OptionsCont::clear
void clear()
Removes all information from the container.
Definition: OptionsCont.cpp:456
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
MSFrame::checkOptions
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:565
GUIEvent_SimulationLoaded.h
GUIEvent_Message
Definition: GUIEvent_Message.h:38
GUILoadThread::myEventQue
FXSynchQue< GUIEvent * > & myEventQue
Definition: GUILoadThread.h:95
OptionsIO.h
TraCIServerAPI_GUI.h
XMLSubSys.h
MsgHandler::removeRetriever
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:182
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:55