SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NLBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The main interface for loading a microsim
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "NLBuilder.h"
34 #include <microsim/MSNet.h>
35 #include <microsim/MSEdgeControl.h>
36 #include <microsim/MSGlobals.h>
37 #include <iostream>
38 #include <vector>
39 #include <xercesc/parsers/SAXParser.hpp>
40 #include <xercesc/sax2/SAX2XMLReader.hpp>
41 #include <string>
42 #include <map>
43 #include "NLHandler.h"
44 #include "NLEdgeControlBuilder.h"
46 #include "NLDetectorBuilder.h"
47 #include "NLTriggerBuilder.h"
54 #include <utils/options/Option.h>
58 #include <utils/common/SysUtils.h>
59 #include <utils/common/ToString.h>
60 #include <utils/xml/XMLSubSys.h>
62 #include <microsim/MSFrame.h>
66 #ifdef CHECK_MEMORY_LEAKS
67 #include <foreign/nvwa/debug_new.h>
68 #endif // CHECK_MEMORY_LEAKS
69 
70 
71 // ===========================================================================
72 // method definitions
73 // ===========================================================================
74 // ---------------------------------------------------------------------------
75 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeWeight - methods
76 // ---------------------------------------------------------------------------
77 void
79  SUMOReal value, SUMOReal begTime, SUMOReal endTime) const {
80  MSEdge* edge = MSEdge::dictionary(id);
81  if (edge != 0) {
82  myNet.getWeightsStorage().addEffort(edge, begTime, endTime, value);
83  } else {
84  WRITE_ERROR("Trying to set the effort for the unknown edge '" + id + "'.");
85  }
86 }
87 
88 
89 // ---------------------------------------------------------------------------
90 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
91 // ---------------------------------------------------------------------------
92 void
94  SUMOReal value, SUMOReal begTime, SUMOReal endTime) const {
95  MSEdge* edge = MSEdge::dictionary(id);
96  if (edge != 0) {
97  myNet.getWeightsStorage().addTravelTime(edge, begTime, endTime, value);
98  } else {
99  WRITE_ERROR("Trying to set the travel time for the unknown edge '" + id + "'.");
100  }
101 }
102 
103 
104 // ---------------------------------------------------------------------------
105 // NLBuilder - methods
106 // ---------------------------------------------------------------------------
108  MSNet& net,
111  NLDetectorBuilder& db,
112  NLHandler& xmlHandler)
113  : myOptions(oc), myEdgeBuilder(eb), myJunctionBuilder(jb),
114  myDetectorBuilder(db),
115  myNet(net), myXMLHandler(xmlHandler) {}
116 
117 
119 
120 
121 bool
123  // try to build the net
124  if (!load("net-file", true)) {
125  return false;
126  }
127  // check whether the loaded net agrees with the simulation options
128 #ifdef HAVE_INTERNAL_LANES
129  if (myOptions.getBool("no-internal-links") && myXMLHandler.haveSeenInternalEdge()) {
130  WRITE_WARNING("Network contains internal links but option --no-internal-links is set. Vehicles will 'jump' across junctions and thus underestimate route lengths and travel times.");
131  }
132 #endif
133  if (myOptions.getString("lanechange.duration") != "0" && myXMLHandler.haveSeenNeighs()) {
134  throw ProcessError("Network contains explicit neigh lanes which do not work together with option --lanechange.duration.");
135  }
136  buildNet();
137  // load the previous state if wished
138  if (myOptions.isSet("load-state")) {
139  long before = SysUtils::getCurrentMillis();
140  const std::string& f = myOptions.getString("load-state");
141  PROGRESS_BEGIN_MESSAGE("Loading state from '" + f + "'");
142  MSStateHandler h(f, string2time(OptionsCont::getOptions().getString("load-state.offset")));
143  XMLSubSys::runParser(h, f);
144  if (myOptions.isDefault("begin")) {
145  myOptions.set("begin", time2string(h.getTime()));
146  }
148  return false;
149  }
150  if (h.getTime() != string2time(myOptions.getString("begin"))) {
151  WRITE_WARNING("State was written at a different time " + time2string(h.getTime()) + " than the begin time " + myOptions.getString("begin") + "!");
152  }
153  PROGRESS_TIME_MESSAGE(before);
154  }
155  // load weights if wished
156  if (myOptions.isSet("weight-files")) {
157  if (!myOptions.isUsableFileList("weight-files")) {
158  return false;
159  }
160  // build and prepare the weights handler
161  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
162  // travel time, first (always used)
164  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", true, ttRetriever));
165  // the measure to use, then
167  std::string measure = myOptions.getString("weight-attribute");
168  if (!myOptions.isDefault("weight-attribute")) {
169  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
170  measure += "_perVeh";
171  }
172  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(measure, true, eRetriever));
173  }
174  // set up handler
175  SAXWeightsHandler handler(retrieverDefs, "");
176  // start parsing; for each file in the list
177  std::vector<std::string> files = myOptions.getStringVector("weight-files");
178  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
179  // report about loading when wished
180  WRITE_MESSAGE("Loading weights from '" + *i + "'...");
181  // parse the file
182  if (!XMLSubSys::runParser(handler, *i)) {
183  return false;
184  }
185  }
186  }
187  // load routes
188  if (myOptions.isSet("route-files") && string2time(myOptions.getString("route-steps")) <= 0) {
189  if (!load("route-files")) {
190  return false;
191  }
192  }
193  // load additional net elements (sources, detectors, ...)
194  if (myOptions.isSet("additional-files")) {
195  if (!load("additional-files")) {
196  return false;
197  }
198  // load shapes with separate handler
200  if (!ShapeHandler::loadFiles(myOptions.getStringVector("additional-files"), sh)) {
201  return false;
202  }
203  }
204  // optionally switch off traffic lights
205  if (myOptions.getBool("tls.all-off")) {
207  }
208  WRITE_MESSAGE("Loading done.");
209  return true;
210 }
211 
212 
213 void
215  MSEdgeControl* edges = 0;
216  MSJunctionControl* junctions = 0;
217  SUMORouteLoaderControl* routeLoaders = 0;
218  MSTLLogicControl* tlc = 0;
219  try {
220  edges = myEdgeBuilder.build();
221  junctions = myJunctionBuilder.build();
222  routeLoaders = buildRouteLoaderControl(myOptions);
225  std::vector<SUMOTime> stateDumpTimes;
226  std::vector<std::string> stateDumpFiles;
227  const std::vector<int> times = myOptions.getIntVector("save-state.times");
228  for (std::vector<int>::const_iterator i = times.begin(); i != times.end(); ++i) {
229  stateDumpTimes.push_back(TIME2STEPS(*i));
230  }
231  if (myOptions.isSet("save-state.files")) {
232  stateDumpFiles = myOptions.getStringVector("save-state.files");
233  if (stateDumpFiles.size() != stateDumpTimes.size()) {
234  WRITE_ERROR("Wrong number of state file names!");
235  }
236  } else {
237  const std::string prefix = myOptions.getString("save-state.prefix");
238  const std::string suffix = myOptions.getString("save-state.suffix");
239  for (std::vector<SUMOTime>::iterator i = stateDumpTimes.begin(); i != stateDumpTimes.end(); ++i) {
240  stateDumpFiles.push_back(prefix + "_" + time2string(*i) + suffix);
241  }
242  }
243  myNet.closeBuilding(myOptions, edges, junctions, routeLoaders, tlc, stateDumpTimes, stateDumpFiles,
248  } catch (IOError& e) {
249  delete edges;
250  delete junctions;
251  delete routeLoaders;
252  delete tlc;
253  throw ProcessError(e.what());
254  } catch (ProcessError&) {
255  delete edges;
256  delete junctions;
257  delete routeLoaders;
258  delete tlc;
259  throw;
260  }
261 }
262 
263 
264 bool
265 NLBuilder::load(const std::string& mmlWhat, const bool isNet) {
266  if (!OptionsCont::getOptions().isUsableFileList(mmlWhat)) {
267  return false;
268  }
269  std::vector<std::string> files = OptionsCont::getOptions().getStringVector(mmlWhat);
270  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
271  PROGRESS_BEGIN_MESSAGE("Loading " + mmlWhat + " from '" + *fileIt + "'");
272  long before = SysUtils::getCurrentMillis();
273  if (!XMLSubSys::runParser(myXMLHandler, *fileIt, isNet)) {
274  WRITE_MESSAGE("Loading of " + mmlWhat + " failed.");
275  return false;
276  }
277  PROGRESS_TIME_MESSAGE(before);
278  }
279  return true;
280 }
281 
282 
285  // build the loaders
286  SUMORouteLoaderControl* loaders = new SUMORouteLoaderControl(string2time(oc.getString("route-steps")));
287  // check whether a list is existing
288  if (oc.isSet("route-files") && string2time(oc.getString("route-steps")) > 0) {
289  std::vector<std::string> files = oc.getStringVector("route-files");
290  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
291  if (!FileHelpers::isReadable(*fileIt)) {
292  throw ProcessError("The route file '" + *fileIt + "' is not accessible.");
293  }
294  }
295  // open files for reading
296  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
297  loaders->add(new SUMORouteLoader(new MSRouteHandler(*fileIt, false)));
298  }
299  }
300  return loaders;
301 }
302 
303 
304 /****************************************************************************/
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) ...
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
Builds detectors for microsim.
void switchOffAll()
switch all logic variants to 'off'
SUMOReal networkVersion() const
Definition: NLHandler.h:120
Obtains edge efforts from a weights handler and stores them within the edges.
Definition: NLBuilder.h:176
void buildNet()
Closes the net building process.
Definition: NLBuilder.cpp:214
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
static void buildStreams()
Builds the streams used possibly by the simulation.
Definition: MSFrame.cpp:420
Parser and output filter for routes and vehicles state saving and loading.
void addEdgeWeight(const std::string &id, SUMOReal val, SUMOReal beg, SUMOReal end) const
Adds an effort for a given edge and time period.
Definition: NLBuilder.cpp:78
static bool loadFiles(const std::vector< std::string > &files, ShapeHandler &sh)
loads all of the given files
An XML-handler for network weights.
const IntVector & getIntVector(const std::string &name) const
Returns the list of integer-value of the named option (only for Option_IntVector) ...
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
MSNet & myNet
The network edges shall be obtained from.
Definition: NLBuilder.h:166
void add(SUMORouteLoader *loader)
add another loader
OptionsCont & myOptions
The options to get the names of the files to load and further information from.
Definition: NLBuilder.h:204
void addEdgeWeight(const std::string &id, SUMOReal val, SUMOReal beg, SUMOReal end) const
Adds a travel time for a given edge and time period.
Definition: NLBuilder.cpp:93
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary...
Definition: MSEdge.cpp:673
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:114
void addEffort(const MSEdge *const e, SUMOReal begin, SUMOReal end, SUMOReal value)
Adds an effort information for an edge and a time span.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The simulated network and simulation perfomer.
Definition: MSNet.h:93
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
Container for junctions; performs operations on all stored junctions.
void addTravelTime(const MSEdge *const e, SUMOReal begin, SUMOReal end, SUMOReal value)
Adds a travel time information for an edge and a time span.
A class that stores and controls tls and switching of their programs.
NLJunctionControlBuilder & myJunctionBuilder
The junction control builder to use.
Definition: NLBuilder.h:210
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
A road/street connecting two junctions.
Definition: MSEdge.h:80
#define PROGRESS_TIME_MESSAGE(before)
Definition: MsgHandler.h:204
MSEdgeControl * build()
builds the MSEdgeControl-class which holds all edges
bool haveSeenNeighs() const
Definition: NLHandler.h:112
void closeBuilding(const OptionsCont &oc, MSEdgeControl *edges, MSJunctionControl *junctions, SUMORouteLoaderControl *routeLoaders, MSTLLogicControl *tlc, std::vector< SUMOTime > stateDumpTimes, std::vector< std::string > stateDumpFiles, bool hasInternalLinks, bool hasNeighs, bool lefthand, SUMOReal version)
Closes the network's building process.
Definition: MSNet.cpp:213
Builder of microsim-junctions and tls.
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:430
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:380
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file) ...
Stores edges and lanes, performs moving of vehicle.
Definition: MSEdgeControl.h:74
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
SUMOTime getTime() const
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
MSNet & myNet
The net to fill.
Definition: NLBuilder.h:216
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:273
NLBuilder(OptionsCont &oc, MSNet &net, NLEdgeControlBuilder &eb, NLJunctionControlBuilder &jb, NLDetectorBuilder &db, NLHandler &xmlHandler)
Constructor.
Definition: NLBuilder.cpp:107
bool lefthand() const
Definition: NLHandler.h:116
The XML-Handler for network loading.
Definition: NLHandler.h:84
virtual bool build()
Builds and initialises the simulation.
Definition: NLBuilder.cpp:122
Complete definition about what shall be retrieved and where to store it.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
The XML-Handler for shapes loading network loading.
Definition: NLHandler.h:64
bool haveSeenInternalEdge() const
Definition: NLHandler.h:108
A storage for options typed value containers)
Definition: OptionsCont.h:99
SUMORouteLoaderControl * buildRouteLoaderControl(const OptionsCont &oc)
Builds the route loader control.
Definition: NLBuilder.cpp:284
bool load(const std::string &mmlWhat, const bool isNet=false)
Loads a described subpart form the given list of files.
Definition: NLBuilder.cpp:265
#define SUMOReal
Definition: config.h:214
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:50
MSJunctionControl * build() const
Builds the MSJunctionControl which holds all of the simulations junctions.
virtual ~NLBuilder()
Destructor.
Definition: NLBuilder.cpp:118
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
MSTLLogicControl * buildTLLogics()
Returns the built tls-logic control.
NLDetectorBuilder & myDetectorBuilder
The detector control builder to use.
Definition: NLBuilder.h:213
NLHandler & myXMLHandler
The handler used to parse the net.
Definition: NLBuilder.h:219
Parser and container for routes during their loading.
Interface for building edges.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
MSEdgeWeightsStorage & getWeightsStorage()
Returns the net's internal edge travel times/efforts container.
Definition: MSNet.cpp:716
NLEdgeControlBuilder & myEdgeBuilder
The edge control builder to use.
Definition: NLBuilder.h:207