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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Class describing the thread that performs the loading of a simulation
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <iostream>
33 #include <ctime>
39 #include <utils/options/Option.h>
46 #include <utils/xml/XMLSubSys.h>
47 #include <guisim/GUINet.h>
48 #include <guisim/GUIEventControl.h>
50 #include <netload/NLBuilder.h>
51 #include <netload/NLHandler.h>
58 #include <microsim/MSGlobals.h>
59 #include <microsim/MSFrame.h>
61 #include "GUIApplicationWindow.h"
62 #include "GUILoadThread.h"
63 #include "GUIGlobals.h"
65 
67 
68 #ifndef NO_TRACI
70 #include "TraCIServerAPI_GUI.h"
71 #endif
72 
73 
74 // ===========================================================================
75 // member method definitions
76 // ===========================================================================
79  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
80  myEventThrow(ev) {
85 }
86 
87 
89  delete myErrorRetriever;
90  delete myMessageRetriever;
91  delete myWarningRetriever;
92 }
93 
94 
95 FXint
97  // register message callbacks
101 
102  // try to load the given configuration
104  try {
105  oc.clear();
107  if (myFile != "") {
108  // triggered by menu option or reload
109  if (myLoadNet) {
110  oc.set("net-file", myFile);
111  } else {
112  oc.set("configuration-file", myFile);
113  }
114  oc.resetWritable(); // there may be command line options
116  } else {
117  // triggered at application start
119  if (oc.isSet("configuration-file")) {
120  myFile = oc.getString("configuration-file");
121  } else if (oc.isSet("net-file")) {
122  myFile = oc.getString("net-file");
123  myLoadNet = true;
124  }
125  myEventQue.add(new GUIEvent_Message("Loading '" + myFile + "'."));
127  myParent->addRecentFile(FXPath::absolute(myFile.c_str()), myLoadNet);
128  }
129  myTitle = myFile;
130  // within gui-based applications, nothing is reported to the console
134  // do this once again to get parsed options
135  if (oc.getBool("duration-log.statistics") && oc.isDefault("verbose")) {
136  // must be done before calling initOutputOptions (which checks option "verbose")
137  // but initOutputOptions must come before checkOptions (so that warnings are printed)
138  oc.set("verbose", "true");
139  }
141  if (!MSFrame::checkOptions()) {
142  throw ProcessError();
143  }
144  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
145  GUIGlobals::gRunAfterLoad = oc.getBool("start");
146  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
148  GUIGlobals::gTrackerInterval = oc.getFloat("tracker-interval");
149  } catch (ProcessError& e) {
150  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
151  WRITE_ERROR(e.what());
152  }
153  // the options are not valid but maybe we want to quit
154  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
155  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
156  submitEndAndCleanup(0, 0, 0);
157  return 0;
158  }
159 
160  // initialise global settings
165  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
166 
167  MSVehicleControl* vehControl = 0;
170  vehControl = new GUIMEVehicleControl();
171  } else {
172  vehControl = new GUIVehicleControl();
173  }
174 
175  GUINet* net = 0;
176  SUMOTime simStartTime = 0;
177  SUMOTime simEndTime = 0;
178  std::vector<std::string> guiSettingsFiles;
179  bool osgView = false;
180  GUIEdgeControlBuilder* eb = 0;
181  try {
182  net = new GUINet(
183  vehControl,
184  new GUIEventControl(),
185  new GUIEventControl(),
186  new GUIEventControl());
187 #ifndef NO_TRACI
188  // need to init TraCI-Server before loading routes to catch VEHICLE_STATE_BUILT
189  std::map<int, TraCIServer::CmdExecutor> execs;
193 #endif
194 
195  eb = new GUIEdgeControlBuilder();
196  GUIDetectorBuilder db(*net);
197  NLJunctionControlBuilder jb(*net, db);
199  NLHandler handler("", *net, db, tb, *eb, jb);
200  tb.setHandler(&handler);
201  NLBuilder builder(oc, *net, *eb, jb, db, handler);
205  if (!builder.build()) {
206  throw ProcessError();
207  } else {
208  net->initGUIStructures();
209  simStartTime = string2time(oc.getString("begin"));
210  simEndTime = string2time(oc.getString("end"));
211  guiSettingsFiles = oc.getStringVector("gui-settings-file");
212 #ifdef HAVE_OSG
213  osgView = oc.getBool("osg-view");
214 #endif
215  }
216  } catch (ProcessError& e) {
217  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
218  WRITE_ERROR(e.what());
219  }
220  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
221  delete net;
222  net = 0;
223 #ifndef _DEBUG
224  } catch (std::exception& e) {
225  WRITE_ERROR(e.what());
226  delete net;
227  net = 0;
228 #endif
229  }
230  if (net == 0) {
231  MSNet::clearAll();
232  }
233  delete eb;
234  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView);
235  return 0;
236 }
237 
238 
239 void
241  const SUMOTime simStartTime,
242  const SUMOTime simEndTime,
243  const std::vector<std::string>& guiSettingsFiles,
244  const bool osgView) {
245  // remove message callbacks
249  // inform parent about the process
250  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myTitle, guiSettingsFiles, osgView);
251  myEventQue.add(e);
253 }
254 
255 
256 void
257 GUILoadThread::loadConfigOrNet(const std::string& file, bool isNet) {
258  myFile = file;
259  myLoadNet = isNet;
260  if (myFile != "") {
261  OptionsIO::setArgs(0, 0);
262  }
263  start();
264 }
265 
266 
267 void
268 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
269  GUIEvent* e = new GUIEvent_Message(type, msg);
270  myEventQue.add(e);
272 }
273 
274 
275 const std::string&
277  return myFile;
278 }
279 
280 
281 /****************************************************************************/
void setHandler(NLHandler *handler)
Sets the parent handler to use for nested parsing.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:66
The message is only something to show.
Definition: MsgHandler.h:61
MFXEventQue< GUIEvent * > & myEventQue
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:75
The class responsible for building and deletion of vehicles (gui-version)
OutputDevice * myWarningRetriever
Definition: GUILoadThread.h:99
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
void resetWritable()
Resets all options to be writeable.
GUILoadThread(FXApp *app, GUIApplicationWindow *mw, MFXEventQue< GUIEvent *> &eq, FXEX::FXThreadEvent &ev)
constructor
The main interface for loading a microsim.
Definition: NLBuilder.h:67
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:64
void add(T what)
Definition: MFXEventQue.h:58
const std::string & getFileName() const
void addRecentFile(const FX::FXString &f, const bool isNet)
static bool gRunAfterLoad
the simulation shall start direct after loading
Definition: GUIGlobals.h:51
FXEX::FXThreadEvent & myEventThrow
The class responsible for building and deletion of vehicles (gui-version)
void initGUIStructures()
Initialises gui wrappers.
Definition: GUINet.cpp:259
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:156
void submitEndAndCleanup(GUINet *net, const SUMOTime simStartTime, const SUMOTime simEndTime, const std::vector< std::string > &guiSettingsFiles=std::vector< std::string >(), const bool osgView=false)
Closes the loading process.
Derivation of NLEdgeControlBuilder which builds gui-edges.
Builds detectors for guisim.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on...
Definition: GUILoadThread.h:99
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
#define CMD_SET_GUI_VARIABLE
static bool gDemoAutoReload
the simulation shall reload when it has ended (demo)
Definition: GUIGlobals.h:57
static std::mt19937 * getEquipmentRNG()
Definition: MSDevice.h:90
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:61
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
OutputDevice * myMessageRetriever
Definition: GUILoadThread.h:99
static std::mt19937 * getParsingRNG()
static void fillOptions()
Inserts options used by the simulation into the OptionsCont-singleton.
Definition: MSFrame.cpp:66
Builds trigger objects for guisim.
virtual ~GUILoadThread()
destructor
void loadConfigOrNet(const std::string &file, bool isNet)
begins the loading of the given file
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
GUIApplicationWindow * myParent
the parent window to inform about the loading
Definition: GUILoadThread.h:89
void clear()
Removes all information from the container.
static double gTrackerInterval
the aggregation period for tracker windows in seconds
Definition: GUIGlobals.h:60
std::string myTitle
the title string for the application
Definition: GUILoadThread.h:95
Builder of microsim-junctions and tls.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
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) ...
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:170
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:57
#define CMD_GET_GUI_VARIABLE
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool myLoadNet
Information whether only the network shall be loaded.
The XML-Handler for network loading.
Definition: NLHandler.h:87
static bool gQuitOnEnd
the window shall be closed when the simulation has ended
Definition: GUIGlobals.h:54
static bool checkOptions()
Checks the set options.
Definition: MSFrame.cpp:515
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:126
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
The message is a warning.
Definition: MsgHandler.h:63
Encapsulates an object&#39;s method for using it as a message retriever.
static void clearAll()
Clears all dictionaries.
Definition: MSNet.cpp:621
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:88
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:84
A storage for options typed value containers)
Definition: OptionsCont.h:98
Stores time-dependant events and executes them at the proper time (guisim)
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:76
static void setMSGlobals(OptionsCont &oc)
Sets the global microsim-options.
Definition: MSFrame.cpp:615
long long int SUMOTime
Definition: TraCIDefs.h:51
std::string myFile
the path to load the simulation from
Definition: GUILoadThread.h:92
static void allowTextures(const bool val)
switch texture drawing on and off
The class responsible for building and deletion of vehicles.
static bool UseMesoSim
this should be set at the same time as MSGlobals::gUseMesoSim
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:144
static void initOutputOptions()
Definition: MsgHandler.cpp:192
static bool gUseMesoSim
Definition: MSGlobals.h:97
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
The message is an error.
Definition: MsgHandler.h:65
The main window of the SUMO-gui.