SUMO - Simulation of Urban MObility
GNELoadThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // The thread that performs the loading of a Netedit-net (adapted from
8 // GUILoadThread)
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
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>
43 #include <utils/options/Option.h>
46 #include <netbuild/NBFrame.h>
47 #include <netimport/NILoader.h>
48 #include <netimport/NIFrame.h>
49 #include <netwrite/NWFrame.h>
50 #include <netbuild/NBFrame.h>
51 
52 #include "GNELoadThread.h"
53 #include "GNENet.h"
54 #include "GNEEvent_NetworkLoaded.h"
55 
56 #ifdef CHECK_MEMORY_LEAKS
57 #include <foreign/nvwa/debug_new.h>
58 #endif // CHECK_MEMORY_LEAKS
59 
60 
61 // ===========================================================================
62 // member method definitions
63 // ===========================================================================
66  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
67  myEventThrow(ev) {
75 }
76 
77 
79  delete myErrorRetriever;
80  delete myMessageRetriever;
81  delete myWarningRetriever;
82 }
83 
84 
85 FXint
87  GNENet* net = 0;
89 
90  // within gui-based applications, nothing is reported to the console
91  /*
92  MsgHandler::getErrorInstance()->report2cout(false);
93  MsgHandler::getErrorInstance()->report2cerr(false);
94  MsgHandler::getWarningInstance()->report2cout(false);
95  MsgHandler::getWarningInstance()->report2cerr(false);
96  MsgHandler::getMessageInstance()->report2cout(false);
97  MsgHandler::getMessageInstance()->report2cerr(false);
98  */
99  // register message callbacks
103 
104  // try to load the given configuration
105  if (!myOptionsReady && !initOptions()) {
106  // the options are not valid
107  submitEndAndCleanup(net);
108  return 0;
109  }
111  if (!(NIFrame::checkOptions() &&
114  // options are not valid
115  WRITE_ERROR("Invalid Options. Nothing loaded");
116  submitEndAndCleanup(net);
117  return 0;
118  }
122 
124  if (!GeoConvHelper::init(oc)) {
125  WRITE_ERROR("Could not build projection!");
126  submitEndAndCleanup(net);
127  return 0;
128  }
129  // this netbuilder instance becomes the responsibility of the GNENet
130  NBNetBuilder* netBuilder = new NBNetBuilder();
131  netBuilder->applyOptions(oc);
132 
133  if (myNewNet) {
134  // create new network
135  net = new GNENet(netBuilder);
136  } else {
137  NILoader nl(*netBuilder);
138  try {
139  nl.load(oc);
140  if (!myLoadNet) {
141  WRITE_MESSAGE("Performing initial computatation ...\n");
142  // perform one-time processing (i.e. edge removal)
143  netBuilder->compute(oc);
144  // @todo remove one-time processing options!
145  } else {
146  // make coordinate conversion usable before first netBuilder->compute()
148  }
149 
150  if (oc.getBool("ignore-errors")) {
152  }
153  // check whether any errors occured
154  if (MsgHandler::getErrorInstance()->wasInformed()) {
155  throw ProcessError();
156  } else {
157  net = new GNENet(netBuilder);
158  }
159  } catch (ProcessError& e) {
160  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
161  WRITE_ERROR(e.what());
162  }
163  WRITE_ERROR("Failed to build network.");
164  delete net;
165  delete netBuilder;
166  net = 0;
167 #ifndef _DEBUG
168  } catch (std::exception& e) {
169  WRITE_ERROR(e.what());
170  delete net;
171  delete netBuilder;
172  net = 0;
173 #endif
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
189  // inform parent about the process
190  GUIEvent* e = new GNEEvent_NetworkLoaded(net, myFile, guiSettingsFile, viewportFromRegistry);
191  myEventQue.add(e);
193 }
194 
195 
196 void
198  oc.clear();
199  oc.addCallExample("", "start plain GUI with empty net");
200  oc.addCallExample("-c <CONFIGURATION>", "edit net with options read from file");
201 
202  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
203  oc.addOptionSubTopic("Input");
204  oc.addOptionSubTopic("Output");
206  oc.addOptionSubTopic("TLS Building");
207  oc.addOptionSubTopic("Ramp Guessing");
208  oc.addOptionSubTopic("Edge Removal");
209  oc.addOptionSubTopic("Unregulated Nodes");
210  oc.addOptionSubTopic("Processing");
211  oc.addOptionSubTopic("Building Defaults");
212  oc.addOptionSubTopic("Visualisation");
213 
214  oc.doRegister("disable-textures", 'T', new Option_Bool(false)); // !!!
215  oc.addDescription("disable-textures", "Visualisation", "");
216  oc.doRegister("gui-settings-file", new Option_FileName());
217  oc.addDescription("gui-settings-file", "Visualisation", "Load visualisation settings from FILE");
218  oc.doRegister("registry-viewport", new Option_Bool(false));
219  oc.addDescription("registry-viewport", "Visualisation", "Load current viewport from registry");
220 
221  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
222 
224  NBFrame::fillOptions(false);
225  NWFrame::fillOptions(false);
227 }
228 
229 
230 void
232  oc.set("offset.disable-normalization", "true"); // preserve the given network as far as possible
233  oc.set("no-turnarounds", "true"); // otherwise it is impossible to manually removed turn-arounds
234 }
235 
236 
237 bool
240  fillOptions(oc);
241  if (myLoadNet) {
242  oc.set("sumo-net-file", myFile);
243  } else {
244  oc.set("configuration-file", myFile);
245  }
246  setDefaultOptions(oc);
247  OptionsIO::setArgs(0, 0);
248  try {
250  if (!oc.isSet("output-file")) {
251  oc.set("output-file", oc.getString("sumo-net-file"));
252  }
253  return true;
254  } catch (ProcessError& e) {
255  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
256  WRITE_ERROR(e.what());
257  }
258  WRITE_ERROR("Failed to parse options.");
259  }
260  return false;
261 }
262 
263 
264 void
265 GNELoadThread::loadConfigOrNet(const std::string& file, bool isNet, bool optionsReady, bool newNet) {
266  myFile = file;
267  myLoadNet = isNet;
268  myOptionsReady = optionsReady;
269  myNewNet = newNet;
270  start();
271 }
272 
273 
274 void
275 GNELoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
276  GUIEvent* e = new GUIEvent_Message(type, msg);
277  myEventQue.add(e);
279 }
280 
281 /****************************************************************************/
282 
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
The message is only something to show.
Definition: MsgHandler.h:62
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
FXEX::FXThreadEvent & myEventThrow
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:53
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:111
OutputDevice * myWarningRetriever
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:407
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:75
GNELoadThread(FXApp *app, MFXInterThreadEventClient *mw, MFXEventQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool removeElements=true)
Performs the network building steps.
void add(T what)
Definition: MFXEventQue.h:59
static void computeFinal(bool lefthand=false)
compute the location attributes which will be used for output based on the loaded location data...
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool myOptionsReady
if true, options will not be read from myFile
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on...
static void fillOptions()
Inserts options used by the network importer and network building modules.
Definition: NIFrame.cpp:59
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:77
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:161
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:50
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:65
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
Perfoms network import.
Definition: NILoader.h:59
static void setDefaultOptions(OptionsCont &oc)
sets required options for proper functioning
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool myNewNet
if true, a new net is created
bool myLoadNet
Information whether only the network shall be loaded.
void clear()
Removes all information from the container.
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:175
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
void load(OptionsCont &oc)
Definition: NILoader.cpp:86
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NIFrame.cpp:274
static void addProjectionOptions(OptionsCont &oc)
Adds projection options to the given container.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
The message is a warning.
Definition: MsgHandler.h:64
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
Encapsulates an object&#39;s method for using it as a message retriever.
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
MFXEventQue< GUIEvent * > & myEventQue
Instance responsible for building networks.
Definition: NBNetBuilder.h:113
OutputDevice * myMessageRetriever
A storage for options typed value containers)
Definition: OptionsCont.h:108
std::string myFile
the path to load the simulation from
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:61
void loadConfigOrNet(const std::string &file, bool isNet, bool optionsReady=false, bool newNet=false)
begins the loading of a netconvert configuration or a a network
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.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void fillOptions(bool forNetgen)
Inserts options used by the network writer.
Definition: NWFrame.cpp:63
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
static void initOutputOptions()
Definition: MsgHandler.cpp:197
static void fillOptions(OptionsCont &oc)
clears and initializes the OptionsCont
The message is an error.
Definition: MsgHandler.h:66
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
virtual ~GNELoadThread()
destructor