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 /****************************************************************************/
17 // Class describing the thread that performs the loading of a simulation
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <iostream>
27 #include <ctime>
33 #include <utils/options/Option.h>
40 #include <utils/xml/XMLSubSys.h>
41 #include <guisim/GUINet.h>
42 #include <guisim/GUIEventControl.h>
44 #include <netload/NLBuilder.h>
45 #include <netload/NLHandler.h>
52 #include <microsim/MSGlobals.h>
53 #include <microsim/MSFrame.h>
57 #include "TraCIServerAPI_GUI.h"
58 #include "GUIApplicationWindow.h"
59 #include "GUILoadThread.h"
60 #include "GUIGlobals.h"
62 
63 
64 // ===========================================================================
65 // member method definitions
66 // ===========================================================================
69  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
70  myEventThrow(ev) {
75 }
76 
77 
79  delete myErrorRetriever;
80  delete myMessageRetriever;
81  delete myWarningRetriever;
82 }
83 
84 
85 FXint
87  // register message callbacks
91 
92  // try to load the given configuration
94  try {
95  oc.clear();
97  if (myFile != "") {
98  // triggered by menu option or reload
99  if (myLoadNet) {
100  oc.set("net-file", myFile);
101  } else {
102  oc.set("configuration-file", myFile);
103  }
104  oc.resetWritable(); // there may be command line options
106  } else {
107  // triggered at application start
109  if (oc.isSet("configuration-file")) {
110  myFile = oc.getString("configuration-file");
111  } else if (oc.isSet("net-file")) {
112  myFile = oc.getString("net-file");
113  myLoadNet = true;
114  }
115  myEventQue.push_back(new GUIEvent_Message("Loading '" + myFile + "'."));
117  myParent->addRecentFile(FXPath::absolute(myFile.c_str()), myLoadNet);
118  }
119  myTitle = myFile;
120  // within gui-based applications, nothing is reported to the console
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.set("verbose", "true");
129  }
131  if (!MSFrame::checkOptions()) {
132  throw ProcessError();
133  }
134  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
135  GUIGlobals::gRunAfterLoad = oc.getBool("start");
136  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
138  GUIGlobals::gTrackerInterval = oc.getFloat("tracker-interval");
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 VEHICLE_STATE_BUILT
176  std::map<int, TraCIServer::CmdExecutor> execs;
180 
181  eb = new GUIEdgeControlBuilder();
182  GUIDetectorBuilder db(*net);
183  NLJunctionControlBuilder jb(*net, db);
185  NLHandler handler("", *net, db, tb, *eb, jb);
186  tb.setHandler(&handler);
187  NLBuilder builder(oc, *net, *eb, jb, db, handler);
191  if (!builder.build()) {
192  throw ProcessError();
193  } else {
194  net->initGUIStructures();
195  simStartTime = string2time(oc.getString("begin"));
196  simEndTime = string2time(oc.getString("end"));
197  guiSettingsFiles = oc.getStringVector("gui-settings-file");
198 #ifdef HAVE_OSG
199  osgView = oc.getBool("osg-view");
200 #endif
201  if (oc.isSet("edgedata-files")) {
202  if (!oc.isUsableFileList("edgedata-files")) {
203  WRITE_ERROR("Could not load edgedata-files '" + oc.getString("edgedata-files") + "'");
204  } else {
205  for (const std::string& file : oc.getStringVector("edgedata-files")) {
206  net->loadEdgeData(file);
207  }
208  }
209  }
210  }
211  } catch (ProcessError& e) {
212  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
213  WRITE_ERROR(e.what());
214  }
215  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
216  delete net;
217  net = nullptr;
218 #ifndef _DEBUG
219  } catch (std::exception& e) {
220  WRITE_ERROR(e.what());
221  delete net;
222  net = nullptr;
223 #endif
224  }
225  if (net == nullptr) {
226  MSNet::clearAll();
227  }
228  delete eb;
229  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView,
230  oc.getBool("registry-viewport"));
231  return 0;
232 }
233 
234 
235 void
237  const SUMOTime simStartTime,
238  const SUMOTime simEndTime,
239  const std::vector<std::string>& guiSettingsFiles,
240  const bool osgView,
241  const bool viewportFromRegistry) {
242  // remove message callbacks
246  // inform parent about the process
247  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView, viewportFromRegistry);
250 }
251 
252 
253 void
254 GUILoadThread::loadConfigOrNet(const std::string& file, bool isNet) {
255  myFile = file;
256  myLoadNet = isNet;
257  if (myFile != "") {
258  OptionsIO::setArgs(0, nullptr);
259  }
260  start();
261 }
262 
263 
264 void
265 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
266  GUIEvent* e = new GUIEvent_Message(type, msg);
269 }
270 
271 
272 const std::string&
274  return myFile;
275 }
276 
277 
278 /****************************************************************************/
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:136
MsgHandler::MsgType
MsgType
Definition: MsgHandler.h:51
MsgHandler::MT_ERROR
The message is an error.
Definition: MsgHandler.h:57
MSFrame::fillOptions
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:61
NLBuilder::initRandomness
static void initRandomness()
initializes all RNGs
Definition: NLBuilder.cpp:266
GUIVisualizationSettings::UseMesoSim
static bool UseMesoSim
this should be set at the same time as MSGlobals::gUseMesoSim
Definition: GUIVisualizationSettings.h:435
libsumo::CMD_SET_GUI_VARIABLE
TRACI_CONST int CMD_SET_GUI_VARIABLE
Definition: TraCIConstants.h:274
NLBuilder
The main interface for loading a microsim.
Definition: NLBuilder.h:61
GUIEvent_SimulationLoaded
Definition: GUIEvent_SimulationLoaded.h:50
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
GUITriggerBuilder.h
MSDetectorControl.h
MSFrame::setMSGlobals
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:683
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:130
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:236
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:44
GUIVehicleControl.h
OptionsCont.h
MsgHandler::MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:53
OptionsCont::resetWritable
void resetWritable()
Resets all options to be writeable.
Definition: OptionsCont.cpp:445
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:244
GUILoadThread::myLoadNet
bool myLoadNet
Information whether only the network shall be loaded.
Definition: GUILoadThread.h:101
MsgHandler.h
MsgRetrievingFunction
Encapsulates an object's method for using it as a message retriever.
Definition: MsgRetrievingFunction.h:42
MsgHandler::MT_WARNING
The message is a warning.
Definition: MsgHandler.h:55
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:202
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:84
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
NLTriggerBuilder::setHandler
void setHandler(NLHandler *handler)
Sets the parent handler to use for nested parsing.
Definition: NLTriggerBuilder.cpp:67
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:223
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
MSGlobals::gUseMesoSim
static bool gUseMesoSim
Definition: MSGlobals.h:91
MSNet::clearAll
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:639
GUIEvent_Message.h
GUIVehicleControl
The class responsible for building and deletion of vehicles (gui-version)
Definition: GUIVehicleControl.h:47
GUILoadThread::GUILoadThread
GUILoadThread(FXApp *app, GUIApplicationWindow *mw, FXSynchQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
Definition: GUILoadThread.cpp:67
GUINet.h
GUILoadThread::getFileName
const std::string & getFileName() const
Definition: GUILoadThread.cpp:273
GUIMEVehicleControl
The class responsible for building and deletion of vehicles (gui-version)
Definition: GUIMEVehicleControl.h:41
FXSynchQue.h
MSDevice.h
GUIEventControl
Stores time-dependant events and executes them at the proper time (guisim)
Definition: GUIEventControl.h:41
GUILoadThread::myTitle
std::string myTitle
the title string for the application
Definition: GUILoadThread.h:90
GUIGlObjectStorage.h
FXEX::FXThreadEvent::signal
void signal()
Definition: FXThreadEvent.cpp:100
GUIAppEnum.h
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:595
NLJunctionControlBuilder
Builder of microsim-junctions and tls.
Definition: NLJunctionControlBuilder.h:63
NLHandler.h
GUIDetectorBuilder
Builds detectors for guisim.
Definition: GUIDetectorBuilder.h:49
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:360
GUIEdgeControlBuilder
Derivation of NLEdgeControlBuilder which builds gui-edges.
Definition: GUIEdgeControlBuilder.h:53
XMLSubSys::setValidation
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
MsgHandler::getWarningInstance
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:72
GUITriggerBuilder
Builds trigger objects for guisim.
Definition: GUITriggerBuilder.h:50
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:94
GUIApplicationWindow
The main window of the SUMO-gui.
Definition: GUIApplicationWindow.h:66
ProcessError
Definition: UtilExceptions.h:40
GUIApplicationWindow.h
GUILoadThread::myMessageRetriever
OutputDevice * myMessageRetriever
Definition: GUILoadThread.h:94
GUINet::initGUIStructures
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:258
GUILoadThread::myWarningRetriever
OutputDevice * myWarningRetriever
Definition: GUILoadThread.h:94
MSGlobals.h
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
GUITexturesHelper::allowTextures
static void allowTextures(const bool val)
switch texture drawing on and off
Definition: GUITexturesHelper.h:62
GUIGlobals::gQuitOnEnd
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:48
GUILoadThread::myFile
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:87
GUILoadThread::retrieveMessage
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
Definition: GUILoadThread.cpp:265
GUIEventControl.h
GUIDetectorBuilder.h
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
FXSynchQue< GUIEvent * >
GUIGlobals::gTrackerInterval
static double gTrackerInterval
the aggregation period for tracker windows in seconds
Definition: GUIGlobals.h:54
TraCIServer::openSocket
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
Definition: TraCIServer.cpp:284
OptionsCont::isDefault
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
Definition: OptionsCont.cpp:164
NLBuilder::build
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:117
OptionsIO::getOptions
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:209
libsumo::CMD_GET_GUI_VARIABLE
TRACI_CONST int CMD_GET_GUI_VARIABLE
Definition: TraCIConstants.h:270
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:55
GUILoadThread::myEventThrow
FXEX::FXThreadEvent & myEventThrow
Definition: GUILoadThread.h:98
Option.h
GUILoadThread.h
GUIEvent
Definition: GUIEvent.h:77
MsgHandler::addRetriever
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:166
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
GUIGlobals::gDemoAutoReload
static bool gDemoAutoReload
the simulation shall reload when it has ended (demo)
Definition: GUIGlobals.h:51
TraCIServer.h
MsgRetrievingFunction.h
NLJunctionControlBuilder.h
FXSynchQue::push_back
void push_back(T what)
Definition: FXSynchQue.h:91
GUILoadThread::loadConfigOrNet
void loadConfigOrNet(const std::string &file, bool isNet)
begins the loading of the given file
Definition: GUILoadThread.cpp:254
MSFrame.h
NLHandler
The XML-Handler for network loading.
Definition: NLHandler.h:81
config.h
MSVehicleControl
The class responsible for building and deletion of vehicles.
Definition: MSVehicleControl.h:72
RandHelper.h
FXSingleEventThread
Definition: FXSingleEventThread.h:35
GUIGlobals::gRunAfterLoad
static bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:45
FXEX::FXThreadEvent
Definition: FXThreadEvent.h:106
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
GUILoadThread::~GUILoadThread
virtual ~GUILoadThread()
destructor
Definition: GUILoadThread.cpp:78
GUINet
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:83
GUILoadThread::run
FXint run()
Definition: GUILoadThread.cpp:86
GUIApplicationWindow::addRecentFile
void addRecentFile(const FX::FXString &f, const bool isNet)
Definition: GUIApplicationWindow.cpp:1788
MSRouteHandler.h
OptionsCont::clear
void clear()
Removes all information from the container.
Definition: OptionsCont.cpp:460
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
MSFrame::checkOptions
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:559
GUIEvent_SimulationLoaded.h
GUIEvent_Message
Definition: GUIEvent_Message.h:39
OptionsCont::getStringVector
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:921
GUILoadThread::myEventQue
FXSynchQue< GUIEvent * > & myEventQue
Definition: GUILoadThread.h:96
OptionsIO.h
TraCIServerAPI_GUI.h
XMLSubSys.h
MsgHandler::removeRetriever
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:174
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:59