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>
34 #include <utils/xml/XMLSubSys.h>
44 #include <utils/options/Option.h>
47 #include <netbuild/NBFrame.h>
48 #include <netimport/NILoader.h>
49 #include <netimport/NIFrame.h>
50 #include <netwrite/NWFrame.h>
51 #include <netbuild/NBFrame.h>
52 
53 #include "GNELoadThread.h"
54 #include "GNENet.h"
55 #include "GNEEvent_NetworkLoaded.h"
56 
57 #ifdef CHECK_MEMORY_LEAKS
58 #include <foreign/nvwa/debug_new.h>
59 #endif // CHECK_MEMORY_LEAKS
60 
61 
62 // ===========================================================================
63 // member method definitions
64 // ===========================================================================
66  FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
67  myEventThrow(ev) {
72 }
73 
74 
76  delete myErrorRetriever;
77  delete myMessageRetriever;
78  delete myWarningRetriever;
79 }
80 
81 
82 FXint
84  // register message callbacks
88 
89  GNENet* net = 0;
90 
91  // try to load the given configuration
93  oc.clear();
94  if (!initOptions()) {
96  return 0;
97  }
99  if (!(NIFrame::checkOptions() &&
102  // options are not valid
103  WRITE_ERROR("Invalid Options. Nothing loaded");
104  submitEndAndCleanup(net);
105  return 0;
106  }
110 
112  if (!GeoConvHelper::init(oc)) {
113  WRITE_ERROR("Could not build projection!");
114  submitEndAndCleanup(net);
115  return 0;
116  }
117  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
118  // this netbuilder instance becomes the responsibility of the GNENet
119  NBNetBuilder* netBuilder = new NBNetBuilder();
120 
121  netBuilder->applyOptions(oc);
122 
123  if (myNewNet) {
124  // create new network
125  net = new GNENet(netBuilder);
126  } else {
127  NILoader nl(*netBuilder);
128  try {
129  nl.load(oc);
130 
131  if (!myLoadNet) {
132  WRITE_MESSAGE("Performing initial computation ...\n");
133  // perform one-time processing (i.e. edge removal)
134  netBuilder->compute(oc);
135  // @todo remove one-time processing options!
136  } else {
137  // make coordinate conversion usable before first netBuilder->compute()
139  }
140 
141  if (oc.getBool("ignore-errors")) {
143  }
144  // check whether any errors occured
145  if (MsgHandler::getErrorInstance()->wasInformed()) {
146  throw ProcessError();
147  } else {
148  net = new GNENet(netBuilder);
149  }
150  } catch (ProcessError& e) {
151  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
152  WRITE_ERROR(e.what());
153  }
154  WRITE_ERROR("Failed to build network.");
155  delete net;
156  delete netBuilder;
157  net = 0;
158 #ifndef _DEBUG
159  } catch (std::exception& e) {
160  WRITE_ERROR(e.what());
161  delete net;
162  delete netBuilder;
163  net = 0;
164 #endif
165  }
166  }
167  // only a single setting file is supported
168  submitEndAndCleanup(net, oc.getString("gui-settings-file"), oc.getBool("registry-viewport"));
169  return 0;
170 }
171 
172 
173 
174 void
175 GNELoadThread::submitEndAndCleanup(GNENet* net, const std::string& guiSettingsFile, const bool viewportFromRegistry) {
176  // remove message callbacks
180  // inform parent about the process
181  GUIEvent* e = new GNEEvent_NetworkLoaded(net, myFile, guiSettingsFile, viewportFromRegistry);
182  myEventQue.add(e);
184 }
185 
186 
187 void
189  oc.clear();
190  oc.addCallExample("", "start plain GUI with empty net");
191  oc.addCallExample("-c <CONFIGURATION>", "edit net with options read from file");
192 
193  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
194  oc.addOptionSubTopic("Input");
195  oc.addOptionSubTopic("Output");
197  oc.addOptionSubTopic("TLS Building");
198  oc.addOptionSubTopic("Ramp Guessing");
199  oc.addOptionSubTopic("Edge Removal");
200  oc.addOptionSubTopic("Unregulated Nodes");
201  oc.addOptionSubTopic("Processing");
202  oc.addOptionSubTopic("Building Defaults");
203  oc.addOptionSubTopic("Visualisation");
204 
205  oc.doRegister("disable-textures", 'T', new Option_Bool(false)); // !!!
206  oc.addDescription("disable-textures", "Visualisation", "");
207 
208  oc.doRegister("gui-settings-file", new Option_FileName());
209  oc.addDescription("gui-settings-file", "Visualisation", "Load visualisation settings from FILE");
210 
211  oc.doRegister("registry-viewport", new Option_Bool(false));
212  oc.addDescription("registry-viewport", "Visualisation", "Load current viewport from registry");
213 
214  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
215 
217  NBFrame::fillOptions(false);
218  NWFrame::fillOptions(false);
220 }
221 
222 
223 void
225  oc.set("offset.disable-normalization", "true"); // preserve the given network as far as possible
226  oc.set("no-turnarounds", "true"); // otherwise it is impossible to manually removed turn-arounds
227 }
228 
229 
230 bool
233  fillOptions(oc);
234  if (myFile != "") {
235  if (myLoadNet) {
236  oc.set("sumo-net-file", myFile);
237  } else {
238  oc.set("configuration-file", myFile);
239  }
240  }
241  setDefaultOptions(oc);
242  try {
244  if (!oc.isSet("output-file")) {
245  oc.set("output-file", oc.getString("sumo-net-file"));
246  }
247  return true;
248  } catch (ProcessError& e) {
249  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
250  WRITE_ERROR(e.what());
251  }
252  WRITE_ERROR("Failed to parse options.");
253  }
254  return false;
255 }
256 
257 
258 void
259 GNELoadThread::loadConfigOrNet(const std::string& file, bool isNet, bool optionsReady, bool newNet) {
260  myFile = file;
261  myLoadNet = isNet;
262  if (myFile != "") {
263  OptionsIO::setArgs(0, 0);
264  }
265  myOptionsReady = optionsReady;
266  myNewNet = newNet;
267  start();
268 }
269 
270 
271 void
272 GNELoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
273  GUIEvent* e = new GUIEvent_Message(type, msg);
274  myEventQue.add(e);
276 }
277 
278 /****************************************************************************/
279 
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 getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
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
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
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
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:277
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
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:99
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