SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-sim.org/
12 // Copyright (C) 2001-2013 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")) {
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 lenghts and travel times");
131  }
132 #endif
133  buildNet();
134  // load the previous state if wished
135  if (myOptions.isSet("load-state")) {
136  long before = SysUtils::getCurrentMillis();
137  const std::string& f = myOptions.getString("load-state");
138  PROGRESS_BEGIN_MESSAGE("Loading state from '" + f + "'");
139  MSStateHandler h(f, string2time(OptionsCont::getOptions().getString("load-state.offset")));
140  XMLSubSys::runParser(h, f);
141  if (myOptions.isDefault("begin")) {
142  myOptions.set("begin", time2string(h.getTime()));
143  }
144  if (h.getTime() != string2time(myOptions.getString("begin"))) {
145  WRITE_WARNING("State was written at a different time " + time2string(h.getTime()) + " than the begin time " + myOptions.getString("begin") + "!");
146  }
148  return false;
149  }
151  }
152  // load weights if wished
153  if (myOptions.isSet("weight-files")) {
154  if (!myOptions.isUsableFileList("weight-files")) {
155  return false;
156  }
157  // build and prepare the weights handler
158  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
159  // travel time, first (always used)
161  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", true, ttRetriever));
162  // the measure to use, then
164  std::string measure = myOptions.getString("weight-attribute");
165  if (measure != "traveltime") {
166  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
167  measure += "_perVeh";
168  }
169  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(measure, true, eRetriever));
170  }
171  // set up handler
172  SAXWeightsHandler handler(retrieverDefs, "");
173  // start parsing; for each file in the list
174  std::vector<std::string> files = myOptions.getStringVector("weight-files");
175  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
176  // report about loading when wished
177  WRITE_MESSAGE("Loading weights from '" + *i + "'...");
178  // parse the file
179  if (!XMLSubSys::runParser(handler, *i)) {
180  return false;
181  }
182  }
183  }
184  // load routes
185  if (myOptions.isSet("route-files") && string2time(myOptions.getString("route-steps")) <= 0) {
186  if (!load("route-files")) {
187  return false;
188  }
189  }
190  // load additional net elements (sources, detectors, ...)
191  if (myOptions.isSet("additional-files")) {
192  if (!load("additional-files")) {
193  return false;
194  }
195  }
196  WRITE_MESSAGE("Loading done.");
197  return true;
198 }
199 
200 
201 void
203  MSEdgeControl* edges = 0;
204  MSJunctionControl* junctions = 0;
205  SUMORouteLoaderControl* routeLoaders = 0;
206  MSTLLogicControl* tlc = 0;
207  try {
208  edges = myEdgeBuilder.build();
209  junctions = myJunctionBuilder.build();
210  routeLoaders = buildRouteLoaderControl(myOptions);
213  std::vector<SUMOTime> stateDumpTimes;
214  std::vector<std::string> stateDumpFiles;
215  const std::vector<int> times = myOptions.getIntVector("save-state.times");
216  for (std::vector<int>::const_iterator i = times.begin(); i != times.end(); ++i) {
217  stateDumpTimes.push_back(TIME2STEPS(*i));
218  }
219  if (myOptions.isSet("save-state.files")) {
220  stateDumpFiles = myOptions.getStringVector("save-state.files");
221  if (stateDumpFiles.size() != stateDumpTimes.size()) {
222  WRITE_ERROR("Wrong number of state file names!");
223  }
224  } else {
225  const std::string prefix = myOptions.getString("save-state.prefix");
226  for (std::vector<SUMOTime>::iterator i = stateDumpTimes.begin(); i != stateDumpTimes.end(); ++i) {
227  stateDumpFiles.push_back(prefix + "_" + time2string(*i) + ".sbx");
228  }
229  }
230  myNet.closeBuilding(edges, junctions, routeLoaders, tlc, stateDumpTimes, stateDumpFiles);
231  } catch (IOError& e) {
232  delete edges;
233  delete junctions;
234  delete routeLoaders;
235  delete tlc;
236  throw ProcessError(e.what());
237  } catch (ProcessError&) {
238  delete edges;
239  delete junctions;
240  delete routeLoaders;
241  delete tlc;
242  throw;
243  }
244 }
245 
246 
247 bool
248 NLBuilder::load(const std::string& mmlWhat) {
249  if (!OptionsCont::getOptions().isUsableFileList(mmlWhat)) {
250  return false;
251  }
252  std::vector<std::string> files = OptionsCont::getOptions().getStringVector(mmlWhat);
253  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
254  PROGRESS_BEGIN_MESSAGE("Loading " + mmlWhat + " from '" + *fileIt + "'");
255  long before = SysUtils::getCurrentMillis();
256  if (!XMLSubSys::runParser(myXMLHandler, *fileIt)) {
257  WRITE_MESSAGE("Loading of " + mmlWhat + " failed.");
258  return false;
259  }
261  }
262  return true;
263 }
264 
265 
268  // build the loaders
269  SUMORouteLoaderControl* loaders = new SUMORouteLoaderControl(string2time(oc.getString("route-steps")));
270  // check whether a list is existing
271  if (oc.isSet("route-files") && string2time(oc.getString("route-steps")) > 0) {
272  std::vector<std::string> files = oc.getStringVector("route-files");
273  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
274  if (!FileHelpers::exists(*fileIt)) {
275  throw ProcessError("The route file '" + *fileIt + "' does not exist.");
276  }
277  }
278  // open files for reading
279  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
280  loaders->add(new SUMORouteLoader(new MSRouteHandler(*fileIt, false)));
281  }
282  }
283  return loaders;
284 }
285 
286 
287 /****************************************************************************/
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.
Obtains edge efforts from a weights handler and stores them within the edges.
Definition: NLBuilder.h:175
void buildNet()
Closes the net building process.
Definition: NLBuilder.cpp:202
static void buildStreams()
Builds the streams used possibly by the simulation.
Definition: MSFrame.cpp:333
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
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:61
MSNet & myNet
The network edges shall be obtained from.
Definition: NLBuilder.h:165
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:203
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&#39;t already in the dictionary...
Definition: MSEdge.cpp:465
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
static bool runParser(GenericSAXHandler &handler, const std::string &file)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:96
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:196
The simulated network and simulation perfomer.
Definition: MSNet.h:89
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
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:209
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:73
MSEdgeControl * build()
builds the MSEdgeControl-class which holds all edges
Builder of microsim-junctions and tls.
bool load(const std::string &mmlWhat)
Loads a described subpart form the given list of files.
Definition: NLBuilder.cpp:248
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:73
static bool exists(std::string path)
Checks whether the given file exists.
Definition: FileHelpers.cpp:57
MSTLLogicControl * buildTLLogics() const
Returns the built tls-logic control.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:198
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
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:215
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:269
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
NLBuilder(OptionsCont &oc, MSNet &net, NLEdgeControlBuilder &eb, NLJunctionControlBuilder &jb, NLDetectorBuilder &db, NLHandler &xmlHandler)
Constructor.
Definition: NLBuilder.cpp:107
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:201
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
bool haveSeenInternalEdge() const
Definition: NLHandler.h:95
A storage for options typed value containers)
Definition: OptionsCont.h:108
SUMORouteLoaderControl * buildRouteLoaderControl(const OptionsCont &oc)
Builds the route loader control.
Definition: NLBuilder.cpp:267
#define SUMOReal
Definition: config.h:215
void closeBuilding(MSEdgeControl *edges, MSJunctionControl *junctions, SUMORouteLoaderControl *routeLoaders, MSTLLogicControl *tlc, std::vector< SUMOTime > stateDumpTimes, std::vector< std::string > stateDumpFiles)
Closes the network&#39;s building process.
Definition: MSNet.cpp:202
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:48
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:197
NLDetectorBuilder & myDetectorBuilder
The detector control builder to use.
Definition: NLBuilder.h:212
NLHandler & myXMLHandler
The handler used to parse the net.
Definition: NLBuilder.h:218
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&#39;s internal edge travel times/efforts container.
Definition: MSNet.cpp:611
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:131
NLEdgeControlBuilder & myEdgeBuilder
The edge control builder to use.
Definition: NLBuilder.h:206