Eclipse SUMO - Simulation of Urban MObility
GNELoadThread.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-2020 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 /****************************************************************************/
18 // The thread that performs the loading of a Netedit-net (adapted from
19 // GUILoadThread)
20 /****************************************************************************/
21 #include <netbuild/NBFrame.h>
22 #include <netbuild/NBNetBuilder.h>
23 #include <netimport/NIFrame.h>
24 #include <netimport/NILoader.h>
25 #include <netwrite/NWFrame.h>
32 #include <utils/xml/XMLSubSys.h>
33 
34 #include "GNEEvent_NetworkLoaded.h"
35 #include "GNELoadThread.h"
36 #include "GNENet.h"
37 
38 
39 // ===========================================================================
40 // member method definitions
41 // ===========================================================================
43  FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
44  myEventThrow(ev) {
51 }
52 
53 
55  delete myDebugRetriever;
56  delete myGLDebugRetriever;
57  delete myErrorRetriever;
58  delete myMessageRetriever;
59  delete myWarningRetriever;
60 }
61 
62 
63 FXint
65  // register message callbacks
71 
72  GNENet* net = nullptr;
73 
74  // try to load the given configuration
76  if (myFile != "" || oc.getString("sumo-net-file") != "") {
77  oc.clear();
78  if (!initOptions()) {
80  return 0;
81  }
82  }
83  if (oc.isDefault("aggregate-warnings")) {
84  oc.set("aggregate-warnings", "5");
85  }
87  if (!(NIFrame::checkOptions() &&
91  // options are not valid
92  WRITE_ERROR("Invalid Options. Nothing loaded");
94  return 0;
95  }
101 
103  if (!GeoConvHelper::init(oc)) {
104  WRITE_ERROR("Could not build projection!");
105  submitEndAndCleanup(net);
106  return 0;
107  }
108  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
109  // check if Debug has to be enabled
110  MsgHandler::enableDebugMessages(oc.getBool("gui-testing-debug"));
111  // check if GL Debug has to be enabled
112  MsgHandler::enableDebugGLMessages(oc.getBool("gui-testing-debug-gl"));
113  // this netbuilder instance becomes the responsibility of the GNENet
114  NBNetBuilder* netBuilder = new NBNetBuilder();
115 
116  netBuilder->applyOptions(oc);
117 
118  if (myNewNet) {
119  // create new network
120  net = new GNENet(netBuilder);
121  } else {
122  NILoader nl(*netBuilder);
123  try {
124  nl.load(oc);
125 
126  if (!myLoadNet) {
127  WRITE_MESSAGE("Performing initial computation ...\n");
128  // perform one-time processing (i.e. edge removal)
129  netBuilder->compute(oc);
130  // @todo remove one-time processing options!
131  } else {
132  // make coordinate conversion usable before first netBuilder->compute()
134  }
135 
136  if (oc.getBool("ignore-errors")) {
138  }
139 
140  // check whether any errors occurred
141  if (MsgHandler::getErrorInstance()->wasInformed()) {
142  throw ProcessError();
143  } else {
144  net = new GNENet(netBuilder);
145  if (oc.getBool("lefthand")) {
146  // force initial geometry computation without volatile options because the net will look strange otherwise
147  net->computeAndUpdate(oc, false);
148  }
149  }
150  if (myFile == "") {
151  if (oc.isSet("configuration-file")) {
152  myFile = oc.getString("configuration-file");
153  } else if (oc.isSet("sumo-net-file")) {
154  myFile = oc.getString("sumo-net-file");
155  }
156  }
157 
158  } catch (ProcessError& e) {
159  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
160  WRITE_ERROR(e.what());
161  }
162  WRITE_ERROR("Failed to build network.");
163  delete net;
164  delete netBuilder;
165  net = nullptr;
166  } catch (std::exception& e) {
167  WRITE_ERROR(e.what());
168 #ifdef _DEBUG
169  throw;
170 #endif
171  delete net;
172  delete netBuilder;
173  net = nullptr;
174  }
175  }
176  // only a single setting file is supported
177  submitEndAndCleanup(net, oc.getString("gui-settings-file"), oc.getBool("registry-viewport"));
178  return 0;
179 }
180 
181 
182 
183 void
184 GNELoadThread::submitEndAndCleanup(GNENet* net, const std::string& guiSettingsFile, const bool viewportFromRegistry) {
185  // remove message callbacks
191  // inform parent about the process
192  GUIEvent* e = new GNEEvent_NetworkLoaded(net, myFile, guiSettingsFile, viewportFromRegistry);
195 }
196 
197 
198 void
200  oc.clear();
201  oc.addCallExample("--new", "start plain GUI with empty net");
202  oc.addCallExample("-s <SUMO_NET>", "edit SUMO network");
203  oc.addCallExample("-c <CONFIGURATION>", "edit net with options read from file");
204 
205  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
206  oc.addOptionSubTopic("Input");
207  oc.addOptionSubTopic("Output");
209  oc.addOptionSubTopic("Processing");
210  oc.addOptionSubTopic("Building Defaults");
211  oc.addOptionSubTopic("TLS Building");
212  oc.addOptionSubTopic("Ramp Guessing");
213  oc.addOptionSubTopic("Edge Removal");
214  oc.addOptionSubTopic("Unregulated Nodes");
215  oc.addOptionSubTopic("Junctions");
216  oc.addOptionSubTopic("Pedestrian");
217  oc.addOptionSubTopic("Bicycle");
218  oc.addOptionSubTopic("Railway");
219  oc.addOptionSubTopic("Formats");
220  oc.addOptionSubTopic("Netedit");
221  oc.addOptionSubTopic("Visualisation");
222  oc.addOptionSubTopic("Time");
223 
224  oc.doRegister("new", new Option_Bool(false)); // !!!
225  oc.addDescription("new", "Input", "Start with a new network");
226 
227  oc.doRegister("additional-files", 'a', new Option_FileName());
228  oc.addSynonyme("additional-files", "additional");
229  oc.addDescription("additional-files", "Netedit", "Load additional and shapes descriptions from FILE(s)");
230 
231  oc.doRegister("additionals-output", new Option_String());
232  oc.addDescription("additionals-output", "Netedit", "file in which additionals must be saved");
233 
234  oc.doRegister("route-files", 'r', new Option_FileName());
235  oc.addSynonyme("route-files", "routes");
236  oc.addDescription("route-files", "Netedit", "Load demand elements descriptions from FILE(s)");
237 
238  oc.doRegister("demandelements-output", new Option_String());
239  oc.addDescription("demandelements-output", "Netedit", "file in which demand elements must be saved");
240 
241  oc.doRegister("data-files", 'd', new Option_FileName());
242  oc.addSynonyme("data-files", "data");
243  oc.addDescription("data-files", "Netedit", "Load data elements descriptions from FILE(s)");
244 
245  oc.doRegister("dataelements-output", new Option_String());
246  oc.addDescription("dataelements-output", "Netedit", "file in which data elements must be saved");
247 
248  oc.doRegister("TLSPrograms-output", new Option_String());
249  oc.addDescription("TLSPrograms-output", "Netedit", "file in which TLS Programs must be saved");
250 
251  oc.doRegister("edgeTypes-output", new Option_String());
252  oc.addDescription("edgeTypes-output", "Netedit", "file in which edgeTypes must be saved");
253 
254  oc.doRegister("disable-laneIcons", new Option_Bool(false));
255  oc.addDescription("disable-laneIcons", "Visualisation", "Disable icons of special lanes");
256 
257  oc.doRegister("disable-textures", 'T', new Option_Bool(false)); // !!!
258  oc.addDescription("disable-textures", "Visualisation", "");
259 
260  oc.doRegister("gui-settings-file", 'g', new Option_FileName());
261  oc.addDescription("gui-settings-file", "Visualisation", "Load visualisation settings from FILE");
262 
263  oc.doRegister("registry-viewport", new Option_Bool(false));
264  oc.addDescription("registry-viewport", "Visualisation", "Load current viewport from registry");
265 
266  oc.doRegister("window-size", new Option_StringVector());
267  oc.addDescription("window-size", "Visualisation", "Create initial window with the given x,y size");
268 
269  oc.doRegister("window-pos", new Option_StringVector());
270  oc.addDescription("window-pos", "Visualisation", "Create initial window at the given x,y position");
271 
272  // testing
273 
274  oc.doRegister("gui-testing", new Option_Bool(false));
275  oc.addDescription("gui-testing", "Visualisation", "Enable overlay for screen recognition");
276 
277  oc.doRegister("gui-testing-debug", new Option_Bool(false));
278  oc.addDescription("gui-testing-debug", "Visualisation", "Enable output messages during GUI-Testing");
279 
280  oc.doRegister("gui-testing-debug-gl", new Option_Bool(false));
281  oc.addDescription("gui-testing-debug-gl", "Visualisation", "Enable output messages during GUI-Testing specific of gl functions");
282 
283  oc.doRegister("gui-testing.setting-output", new Option_FileName());
284  oc.addDescription("gui-testing.setting-output", "Visualisation", "Save gui settings in the given settings-output file");
285 
286  // register the simulation settings (needed for GNERouteHandler)
287  oc.doRegister("begin", new Option_String("0", "TIME"));
288  oc.addDescription("begin", "Time", "Defines the begin time in seconds; The simulation starts at this time");
289 
290  oc.doRegister("end", new Option_String("-1", "TIME"));
291  oc.addDescription("end", "Time", "Defines the end time in seconds; The simulation ends at this time");
292 
293  oc.doRegister("default.action-step-length", new Option_Float(0.0));
294  oc.addDescription("default.action-step-length", "Processing", "Length of the default interval length between action points for the car-following and lane-change models (in seconds). If not specified, the simulation step-length is used per default. Vehicle- or VType-specific settings override the default. Must be a multiple of the simulation step-length.");
295 
296  NIFrame::fillOptions(true);
297  NBFrame::fillOptions(false);
298  NWFrame::fillOptions(false);
300 }
301 
302 
303 void
305  oc.resetWritable();
306  oc.set("offset.disable-normalization", "true"); // preserve the given network as far as possible
307  oc.set("no-turnarounds", "true"); // otherwise it is impossible to manually removed turn-arounds
308 }
309 
310 
311 bool
314  // fill all optiones
315  fillOptions(oc);
316  // set manually the net file
317  if (myFile != "") {
318  if (myLoadNet) {
319  oc.set("sumo-net-file", myFile);
320  } else {
321  oc.set("configuration-file", myFile);
322  }
323  }
324  // set default options defined in GNELoadThread::setDefaultOptions(...)
325  setDefaultOptions(oc);
326  try {
327  // set all values writables, because certain attributes already setted can be updated throught console
328  oc.resetWritable();
329  // load options from console
331  // if output file wasn't defined in the command line manually, set value of "sumo-net-file"
332  if (!oc.isSet("output-file")) {
333  oc.set("output-file", oc.getString("sumo-net-file"));
334  }
335  return true;
336  } catch (ProcessError& e) {
337  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
338  WRITE_ERROR(e.what());
339  }
340  WRITE_ERROR("Failed to parse options.");
341  return false;
342  }
343 }
344 
345 
346 void
347 GNELoadThread::loadConfigOrNet(const std::string& file, bool isNet, bool useStartupOptions, bool newNet) {
348  myFile = file;
349  myLoadNet = isNet;
350  if (myFile != "" && !useStartupOptions) {
351  OptionsIO::setArgs(0, nullptr);
352  }
353  myNewNet = newNet;
354  start();
355 }
356 
357 
358 void
359 GNELoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
360  GUIEvent* e = new GUIEvent_Message(type, msg);
363 }
364 
365 
366 /****************************************************************************/
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:278
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
void push_back(T what)
Definition: FXSynchQue.h:114
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on.
Definition: GNELoadThread.h:90
void submitEndAndCleanup(GNENet *net, const std::string &guiSettingsFile="", const bool viewportFromRegistry=false)
Closes the loading process.
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
FXSynchQue< GUIEvent * > & myEventQue
event Queue
Definition: GNELoadThread.h:93
void loadConfigOrNet(const std::string &file, bool isNet, bool useStartupOptions, bool newNet=false)
begins the loading of a netconvert configuration or a a network
OutputDevice * myWarningRetriever
Definition: GNELoadThread.h:90
bool myNewNet
if true, a new net is created
OutputDevice * myMessageRetriever
Definition: GNELoadThread.h:90
virtual ~GNELoadThread()
destructor
OutputDevice * myGLDebugRetriever
Definition: GNELoadThread.h:90
std::string myFile
the path to load the network from
Definition: GNELoadThread.h:87
OutputDevice * myDebugRetriever
Definition: GNELoadThread.h:90
static void fillOptions(OptionsCont &oc)
clears and initializes the OptionsCont
bool initOptions()
init options
GNELoadThread(FXApp *app, MFXInterThreadEventClient *mw, FXSynchQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
bool myLoadNet
Information whether only the network shall be loaded.
Definition: GNELoadThread.h:99
static void setDefaultOptions(OptionsCont &oc)
sets required options for proper functioning
FXint run()
starts the thread. The thread ends after the net has been loaded
FXEX::FXThreadEvent & myEventThrow
event throw
Definition: GNELoadThread.h:96
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:40
void computeAndUpdate(OptionsCont &oc, bool volatileOptions)
recompute the network and update lane geometries
Definition: GNENet.cpp:3221
static void addProjectionOptions(OptionsCont &oc)
Adds projection options to the given container.
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
static void computeFinal(bool lefthand=false)
compute the location attributes which will be used for output based on the loaded location data,...
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:175
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:98
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:112
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:217
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:89
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:67
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:107
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:159
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:183
@ MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:45
@ MT_DEBUG
The message is an debug.
Definition: MsgHandler.h:51
@ MT_WARNING
The message is a warning.
Definition: MsgHandler.h:47
@ MT_GLDEBUG
The message is an debug.
Definition: MsgHandler.h:53
@ MT_ERROR
The message is an error.
Definition: MsgHandler.h:49
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.
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:647
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:47
Instance responsible for building networks.
Definition: NBNetBuilder.h:107
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool mayAddOrRemove=true)
Performs the network building steps.
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NIFrame.cpp:346
static void fillOptions(bool forNetedit=false)
Inserts options used by the network importer and network building modules.
Definition: NIFrame.cpp:47
Perfoms network import.
Definition: NILoader.h:48
void load(OptionsCont &oc)
Definition: NILoader.cpp:70
static void fillOptions(bool forNetgen)
Inserts options used by the network writer.
Definition: NWFrame.cpp:48
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:125
A storage for options typed value containers)
Definition: OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
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)
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:96
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.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
void resetWritable()
Resets all options to be writeable.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
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:79
static void initRandGlobal(std::mt19937 *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:76
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:44
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:38
static bool checkOptions()
checks shared options and sets StdDefs
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65