SUMO - Simulation of Urban MObility
ROLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Loader for networks and route imports
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <iostream>
36 #include <string>
37 #include <iomanip>
38 #include <xercesc/parsers/SAXParser.hpp>
39 #include <xercesc/util/PlatformUtils.hpp>
40 #include <xercesc/util/TransService.hpp>
41 #include <xercesc/sax2/SAX2XMLReader.hpp>
43 #include <utils/common/ToString.h>
48 #include <utils/xml/XMLSubSys.h>
52 #include "RONet.h"
53 #include "RONetHandler.h"
54 #include "ROLoader.h"
55 #include "ROEdge.h"
56 #include "RORouteHandler.h"
57 
58 #ifdef CHECK_MEMORY_LEAKS
59 #include <foreign/nvwa/debug_new.h>
60 #endif // CHECK_MEMORY_LEAKS
61 
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
66 // ---------------------------------------------------------------------------
67 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
68 // ---------------------------------------------------------------------------
69 void
71  SUMOReal val, SUMOReal beg, SUMOReal end) const {
72  ROEdge* e = myNet.getEdge(id);
73  if (e != 0) {
74  e->addTravelTime(val, beg, end);
75  } else {
76  if (id[0] != ':') {
77  if (OptionsCont::getOptions().getBool("ignore-errors")) {
78  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
79  } else {
80  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
81  }
82  }
83  }
84 }
85 
86 
87 // ---------------------------------------------------------------------------
88 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
89 // ---------------------------------------------------------------------------
90 void
92  SUMOReal val, SUMOReal beg, SUMOReal end) const {
93  ROEdge* e = myNet.getEdge(id);
94  if (e != 0) {
95  e->addEffort(val, beg, end);
96  } else {
97  if (id[0] != ':') {
98  if (OptionsCont::getOptions().getBool("ignore-errors")) {
99  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
100  } else {
101  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
102  }
103  }
104  }
105 }
106 
107 
108 // ---------------------------------------------------------------------------
109 // ROLoader - methods
110 // ---------------------------------------------------------------------------
111 ROLoader::ROLoader(OptionsCont& oc, const bool emptyDestinationsAllowed, const bool logSteps) :
112  myOptions(oc),
113  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
114  myLogSteps(logSteps),
115  myLoaders(oc.exists("unsorted-input") && oc.getBool("unsorted-input") ? 0 : DELTA_T) {
116 }
117 
118 
120 }
121 
122 
123 void
125  std::string file = myOptions.getString("net-file");
126  if (file == "") {
127  throw ProcessError("Missing definition of network to load!");
128  }
129  if (!FileHelpers::isReadable(file)) {
130  throw ProcessError("The network file '" + file + "' is not accessible.");
131  }
132  PROGRESS_BEGIN_MESSAGE("Loading net");
133  RONetHandler handler(toFill, eb);
134  handler.setFileName(file);
135  if (!XMLSubSys::runParser(handler, file, true)) {
137  throw ProcessError();
138  } else {
140  }
141  if (!deprecatedVehicleClassesSeen.empty()) {
142  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
144  }
145  if (myOptions.isSet("additional-files", false)) { // dfrouter does not register this option
146  std::vector<std::string> files = myOptions.getStringVector("additional-files");
147  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
148  if (!FileHelpers::isReadable(*fileIt)) {
149  throw ProcessError("The additional file '" + *fileIt + "' is not accessible.");
150  }
151  PROGRESS_BEGIN_MESSAGE("Loading additional file '" + *fileIt + "' ");
152  handler.setFileName(*fileIt);
153  if (!XMLSubSys::runParser(handler, *fileIt)) {
155  throw ProcessError();
156  } else {
158  }
159  }
160  }
161 }
162 
163 
164 void
166  // build loader
167  // load relevant elements from additinal file
168  bool ok = openTypedRoutes("additional-files", net);
169  // load sumo-routes when wished
170  ok &= openTypedRoutes("route-files", net);
171  // load the XML-trip definitions when wished
172  ok &= openTypedRoutes("trip-files", net);
173  // load the sumo-alternative file when wished
174  ok &= openTypedRoutes("alternative-files", net);
175  // load the amount definitions if wished
176  ok &= openTypedRoutes("flow-files", net);
177  // check
178  if (ok) {
180  if (!net.furtherStored()) {
182  throw ProcessError();
183  } else {
184  const std::string error = "No route input specified or all routes were invalid.";
185  if (myOptions.getBool("ignore-errors")) {
186  WRITE_WARNING(error);
187  } else {
188  throw ProcessError(error);
189  }
190  }
191  }
192  // skip routes prior to the begin time
193  if (!myOptions.getBool("unsorted-input")) {
194  WRITE_MESSAGE("Skipped until: " + time2string(myLoaders.getFirstLoadTime()));
195  }
196  }
197 }
198 
199 
200 void
201 ROLoader::processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment,
202  RONet& net, const RORouterProvider& provider) {
203  const SUMOTime absNo = end - start;
204  const bool endGiven = !OptionsCont::getOptions().isDefault("end");
205  // skip routes that begin before the simulation's begin
206  // loop till the end
207  const SUMOTime firstStep = myLoaders.getFirstLoadTime();
208  SUMOTime lastStep = firstStep;
209  SUMOTime time = MIN2(firstStep, end);
210  while (time <= end) {
211  writeStats(time, start, absNo, endGiven);
212  myLoaders.loadNext(time);
214  break;
215  }
216  lastStep = net.saveAndRemoveRoutesUntil(myOptions, provider, time);
218  break;
219  }
220  if (time < end && time + increment > end) {
221  time = end;
222  } else {
223  time += increment;
224  }
225  }
226  if (myLogSteps) {
227  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
228  }
229 }
230 
231 
232 bool
233 ROLoader::openTypedRoutes(const std::string& optionName,
234  RONet& net) {
235  // check whether the current loader is known
236  // (not all routers import all route formats)
237  if (!myOptions.exists(optionName)) {
238  return true;
239  }
240  // check whether the current loader is wished
241  // and the file(s) can be used
242  if (!myOptions.isUsableFileList(optionName)) {
243  return !myOptions.isSet(optionName);
244  }
245  bool ok = true;
246  std::vector<std::string> files = myOptions.getStringVector(optionName);
247  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
248  // build the instance when everything's all right
249  try {
250  myLoaders.add(new SUMORouteLoader(new RORouteHandler(net, *fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"))));
251  } catch (ProcessError& e) {
252  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
253  std::string reason = e.what();
254  if (reason != "Process Error" && reason != "") {
255  msg = msg + "\n Reason: " + reason + ".";
256  } else {
257  msg = msg + "\n (unknown reason).";
258  }
259  WRITE_ERROR(msg);
260  ok = false;
261  }
262  }
263  return ok;
264 }
265 
266 
267 bool
268 ROLoader::loadWeights(RONet& net, const std::string& optionName,
269  const std::string& measure, const bool useLanes, const bool boundariesOverride) {
270  // check whether the file exists
271  if (!myOptions.isUsableFileList(optionName)) {
272  return false;
273  }
274  // build and prepare the weights handler
275  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
276  // travel time, first (always used)
278  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
279  // the measure to use, then
281  if (measure != "traveltime") {
282  std::string umeasure = measure;
283  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
284  umeasure = measure + "_perVeh";
285  }
286  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
287  }
288  // set up handler
289  SAXWeightsHandler handler(retrieverDefs, "");
290  // go through files
291  std::vector<std::string> files = myOptions.getStringVector(optionName);
292  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
293  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
294  if (XMLSubSys::runParser(handler, *fileIt)) {
296  } else {
297  WRITE_MESSAGE("failed.");
298  return false;
299  }
300  }
301  // build edge-internal time lines
302  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
303  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
304  (*i).second->buildTimeLines(measure, boundariesOverride);
305  }
306  return true;
307 }
308 
309 
310 void
311 ROLoader::writeStats(SUMOTime time, SUMOTime start, int absNo, bool endGiven) {
312  if (myLogSteps) {
313  if (endGiven) {
314  const SUMOReal perc = (SUMOReal)(time - start) / (SUMOReal) absNo;
315  std::cout << "Reading up to time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
316  } else {
317  std::cout << "Reading up to time step: " + time2string(time) + "\r";
318  }
319  }
320 }
321 
322 
323 /****************************************************************************/
324 
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
long long int SUMOTime
Definition: SUMOTime.h:43
std::set< std::string > deprecatedVehicleClassesSeen
void loadNext(SUMOTime step)
loads the next routes up to and including the given time step
RONet & myNet
The network edges shall be obtained from.
Definition: ROLoader.h:173
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
void writeStats(SUMOTime time, SUMOTime start, int absNo, bool endGiven)
Definition: ROLoader.cpp:311
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:165
An XML-handler for network weights.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:165
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
SUMOTime getFirstLoadTime() const
returns the timestamp of the first loaded vehicle or flow
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
void add(SUMORouteLoader *loader)
add another loader
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
const bool myLogSteps
Information whether the routing steps should be logged.
Definition: ROLoader.h:191
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:114
Interface for building instances of router-edges.
void addTravelTime(SUMOReal value, SUMOReal timeBegin, SUMOReal timeEnd)
Adds a travel time value.
Definition: ROEdge.cpp:125
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
const std::map< std::string, ROEdge * > & getEdgeMap() const
Definition: RONet.cpp:625
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
OptionsCont & myOptions
Options to use.
Definition: ROLoader.h:185
void addEdgeWeight(const std::string &id, SUMOReal val, SUMOReal beg, SUMOReal end) const
Adds an effort for a given edge and time period.
Definition: ROLoader.cpp:91
virtual ~ROLoader()
Destructor.
Definition: ROLoader.cpp:119
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:205
void setFileName(const std::string &name)
Sets the current file name.
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file) ...
Obtains edge weights from a weights handler and stores them within the edges.
Definition: ROLoader.h:121
SUMORouteLoaderControl myLoaders
List of route loaders.
Definition: ROLoader.h:194
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:188
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: ROLoader.cpp:70
bool openTypedRoutes(const std::string &optionName, RONet &net)
Opens route handler of the given type.
Definition: ROLoader.cpp:233
virtual bool furtherStored()
Returns the information whether further vehicles, persons or containers are stored.
Definition: RONet.cpp:607
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
T MIN2(T a, T b)
Definition: StdDefs.h:69
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:273
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:124
A basic edge for routing applications.
Definition: ROEdge.h:77
Complete definition about what shall be retrieved and where to store it.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
The router&#39;s network representation.
Definition: RONet.h:76
Obtains edge travel times from a weights handler and stores them within the edges.
Definition: ROLoader.h:152
void addEffort(SUMOReal value, SUMOReal timeBegin, SUMOReal timeEnd)
Adds a weight value.
Definition: ROEdge.cpp:118
void processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment, RONet &net, const RORouterProvider &provider)
Loads routes from all previously build route loaders.
Definition: ROLoader.cpp:201
A storage for options typed value containers)
Definition: OptionsCont.h:108
The handler that parses a SUMO-network for its usage in a router.
Definition: RONetHandler.h:60
Parser and container for routes during their loading.
#define SUMOReal
Definition: config.h:213
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
ROLoader(OptionsCont &oc, const bool emptyDestinationsAllowed, const bool logSteps)
Constructor.
Definition: ROLoader.cpp:111
bool haveAllLoaded() const
returns whether loading is completed
bool loadWeights(RONet &net, const std::string &optionName, const std::string &measure, const bool useLanes, const bool boundariesOverride)
Loads the net weights.
Definition: ROLoader.cpp:268
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, const RORouterProvider &provider, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:507
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.