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 // ===========================================================================
65  FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
66  myEventThrow(ev) {
71 }
72 
73 
75  delete myErrorRetriever;
76  delete myMessageRetriever;
77  delete myWarningRetriever;
78 }
79 
80 
81 FXint
83  // register message callbacks
87 
88  GNENet* net = 0;
89 
90  // try to load the given configuration
92  oc.clear();
93  if (!initOptions()) {
95  return 0;
96  }
98  if (!(NIFrame::checkOptions() &&
101  // options are not valid
102  WRITE_ERROR("Invalid Options. Nothing loaded");
103  submitEndAndCleanup(net);
104  return 0;
105  }
109 
111  if (!GeoConvHelper::init(oc)) {
112  WRITE_ERROR("Could not build projection!");
113  submitEndAndCleanup(net);
114  return 0;
115  }
116  // this netbuilder instance becomes the responsibility of the GNENet
117  NBNetBuilder* netBuilder = new NBNetBuilder();
118 
119  netBuilder->applyOptions(oc);
120 
121  if (myNewNet) {
122  // create new network
123  net = new GNENet(netBuilder);
124  } else {
125  NILoader nl(*netBuilder);
126  try {
127  nl.load(oc);
128 
129  if (!myLoadNet) {
130  WRITE_MESSAGE("Performing initial computation ...\n");
131  // perform one-time processing (i.e. edge removal)
132  netBuilder->compute(oc);
133  // @todo remove one-time processing options!
134  } else {
135  // make coordinate conversion usable before first netBuilder->compute()
137  }
138 
139  if (oc.getBool("ignore-errors")) {
141  }
142  // check whether any errors occured
143  if (MsgHandler::getErrorInstance()->wasInformed()) {
144  throw ProcessError();
145  } else {
146  net = new GNENet(netBuilder);
147  }
148  } catch (ProcessError& e) {
149  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
150  WRITE_ERROR(e.what());
151  }
152  WRITE_ERROR("Failed to build network.");
153  delete net;
154  delete netBuilder;
155  net = 0;
156 #ifndef _DEBUG
157  } catch (std::exception& e) {
158  WRITE_ERROR(e.what());
159  delete net;
160  delete netBuilder;
161  net = 0;
162 #endif
163  }
164  }
165  // only a single setting file is supported
166  submitEndAndCleanup(net, oc.getString("gui-settings-file"), oc.getBool("registry-viewport"));
167  return 0;
168 }
169 
170 
171 
172 void
173 GNELoadThread::submitEndAndCleanup(GNENet* net, const std::string& guiSettingsFile, const bool viewportFromRegistry) {
174  // remove message callbacks
178  // inform parent about the process
179  GUIEvent* e = new GNEEvent_NetworkLoaded(net, myFile, guiSettingsFile, viewportFromRegistry);
180  myEventQue.add(e);
182 }
183 
184 
185 void
187  oc.clear();
188  oc.addCallExample("", "start plain GUI with empty net");
189  oc.addCallExample("-c <CONFIGURATION>", "edit net with options read from file");
190 
191  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
192  oc.addOptionSubTopic("Input");
193  oc.addOptionSubTopic("Output");
195  oc.addOptionSubTopic("TLS Building");
196  oc.addOptionSubTopic("Ramp Guessing");
197  oc.addOptionSubTopic("Edge Removal");
198  oc.addOptionSubTopic("Unregulated Nodes");
199  oc.addOptionSubTopic("Processing");
200  oc.addOptionSubTopic("Building Defaults");
201  oc.addOptionSubTopic("Visualisation");
202 
203  oc.doRegister("disable-textures", 'T', new Option_Bool(false)); // !!!
204  oc.addDescription("disable-textures", "Visualisation", "");
205 
206  oc.doRegister("gui-settings-file", new Option_FileName());
207  oc.addDescription("gui-settings-file", "Visualisation", "Load visualisation settings from FILE");
208 
209  oc.doRegister("registry-viewport", new Option_Bool(false));
210  oc.addDescription("registry-viewport", "Visualisation", "Load current viewport from registry");
211 
212  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
213 
215  NBFrame::fillOptions(false);
216  NWFrame::fillOptions(false);
218 }
219 
220 
221 void
223  oc.set("offset.disable-normalization", "true"); // preserve the given network as far as possible
224  oc.set("no-turnarounds", "true"); // otherwise it is impossible to manually removed turn-arounds
225 }
226 
227 
228 bool
231  fillOptions(oc);
232  if (myFile != "") {
233  if (myLoadNet) {
234  oc.set("sumo-net-file", myFile);
235  } else {
236  oc.set("configuration-file", myFile);
237  }
238  }
239  setDefaultOptions(oc);
240  try {
242  if (!oc.isSet("output-file")) {
243  oc.set("output-file", oc.getString("sumo-net-file"));
244  }
245  return true;
246  } catch (ProcessError& e) {
247  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
248  WRITE_ERROR(e.what());
249  }
250  WRITE_ERROR("Failed to parse options.");
251  }
252  return false;
253 }
254 
255 
256 void
257 GNELoadThread::loadConfigOrNet(const std::string& file, bool isNet, bool optionsReady, bool newNet) {
258  myFile = file;
259  myLoadNet = isNet;
260  if (myFile != "") {
261  OptionsIO::setArgs(0, 0);
262  }
263  myOptionsReady = optionsReady;
264  myNewNet = newNet;
265  start();
266 }
267 
268 
269 void
270 GNELoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
271  GUIEvent* e = new GUIEvent_Message(type, msg);
272  myEventQue.add(e);
274 }
275 
276 /****************************************************************************/
277 
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
event throw
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:419
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 initOptions()
init options
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:86
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.
FXint run()
starts the thread. The thread ends after the net has been loaded
#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
event Queue
Instance responsible for building networks.
Definition: NBNetBuilder.h:112
OutputDevice * myMessageRetriever
A storage for options typed value containers)
Definition: OptionsCont.h:108
std::string myFile
the path to load the simulation from
Definition: GNELoadThread.h:99
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