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-2017 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 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 // ---------------------------------------------------------------------------
63 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
64 // ---------------------------------------------------------------------------
65 void
67  double val, double beg, double end) const {
68  ROEdge* e = myNet.getEdge(id);
69  if (e != 0) {
70  e->addTravelTime(val, beg, end);
71  } else {
72  if (id[0] != ':') {
73  if (OptionsCont::getOptions().getBool("ignore-errors")) {
74  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
75  } else {
76  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
77  }
78  }
79  }
80 }
81 
82 
83 // ---------------------------------------------------------------------------
84 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
85 // ---------------------------------------------------------------------------
86 void
88  double val, double beg, double end) const {
89  ROEdge* e = myNet.getEdge(id);
90  if (e != 0) {
91  e->addEffort(val, beg, end);
92  } else {
93  if (id[0] != ':') {
94  if (OptionsCont::getOptions().getBool("ignore-errors")) {
95  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
96  } else {
97  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
98  }
99  }
100  }
101 }
102 
103 
104 // ---------------------------------------------------------------------------
105 // ROLoader - methods
106 // ---------------------------------------------------------------------------
107 ROLoader::ROLoader(OptionsCont& oc, const bool emptyDestinationsAllowed, const bool logSteps) :
108  myOptions(oc),
109  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
110  myLogSteps(logSteps),
111  myLoaders(oc.exists("unsorted-input") && oc.getBool("unsorted-input") ? 0 : DELTA_T) {
112 }
113 
114 
116 }
117 
118 
119 void
121  std::string file = myOptions.getString("net-file");
122  if (file == "") {
123  throw ProcessError("Missing definition of network to load!");
124  }
125  if (!FileHelpers::isReadable(file)) {
126  throw ProcessError("The network file '" + file + "' is not accessible.");
127  }
128  PROGRESS_BEGIN_MESSAGE("Loading net");
129  RONetHandler handler(toFill, eb);
130  handler.setFileName(file);
131  if (!XMLSubSys::runParser(handler, file, true)) {
133  throw ProcessError();
134  } else {
136  }
137  if (!deprecatedVehicleClassesSeen.empty()) {
138  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
140  }
141  if (myOptions.isSet("additional-files", false)) { // dfrouter does not register this option
142  std::vector<std::string> files = myOptions.getStringVector("additional-files");
143  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
144  if (!FileHelpers::isReadable(*fileIt)) {
145  throw ProcessError("The additional file '" + *fileIt + "' is not accessible.");
146  }
147  PROGRESS_BEGIN_MESSAGE("Loading additional file '" + *fileIt + "' ");
148  handler.setFileName(*fileIt);
149  if (!XMLSubSys::runParser(handler, *fileIt)) {
151  throw ProcessError();
152  } else {
154  }
155  }
156  }
157 }
158 
159 
160 void
162  // build loader
163  // load relevant elements from additinal file
164  bool ok = openTypedRoutes("additional-files", net);
165  // load sumo-routes when wished
166  ok &= openTypedRoutes("route-files", net);
167  // load the XML-trip definitions when wished
168  ok &= openTypedRoutes("trip-files", net);
169  // load the sumo-alternative file when wished
170  ok &= openTypedRoutes("alternative-files", net);
171  // load the amount definitions if wished
172  ok &= openTypedRoutes("flow-files", net);
173  // check
174  if (ok) {
176  if (!net.furtherStored()) {
178  throw ProcessError();
179  } else {
180  const std::string error = "No route input specified or all routes were invalid.";
181  if (myOptions.getBool("ignore-errors")) {
182  WRITE_WARNING(error);
183  } else {
184  throw ProcessError(error);
185  }
186  }
187  }
188  // skip routes prior to the begin time
189  if (!myOptions.getBool("unsorted-input")) {
190  WRITE_MESSAGE("Skipped until: " + time2string(myLoaders.getFirstLoadTime()));
191  }
192  }
193 }
194 
195 
196 void
197 ROLoader::processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment,
198  RONet& net, const RORouterProvider& provider) {
199  const SUMOTime absNo = end - start;
200  const bool endGiven = !OptionsCont::getOptions().isDefault("end");
201  // skip routes that begin before the simulation's begin
202  // loop till the end
203  const SUMOTime firstStep = myLoaders.getFirstLoadTime();
204  SUMOTime lastStep = firstStep;
205  SUMOTime time = MIN2(firstStep, end);
206  while (time <= end) {
207  writeStats(time, start, absNo, endGiven);
208  myLoaders.loadNext(time);
210  break;
211  }
212  lastStep = net.saveAndRemoveRoutesUntil(myOptions, provider, time);
214  break;
215  }
216  if (time < end && time + increment > end) {
217  time = end;
218  } else {
219  time += increment;
220  }
221  }
222  if (myLogSteps) {
223  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
224  }
225 }
226 
227 
228 bool
229 ROLoader::openTypedRoutes(const std::string& optionName,
230  RONet& net) {
231  // check whether the current loader is known
232  // (not all routers import all route formats)
233  if (!myOptions.exists(optionName)) {
234  return true;
235  }
236  // check whether the current loader is wished
237  // and the file(s) can be used
238  if (!myOptions.isUsableFileList(optionName)) {
239  return !myOptions.isSet(optionName);
240  }
241  bool ok = true;
242  std::vector<std::string> files = myOptions.getStringVector(optionName);
243  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
244  // build the instance when everything's all right
245  try {
246  myLoaders.add(new SUMORouteLoader(new RORouteHandler(net, *fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"))));
247  } catch (ProcessError& e) {
248  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
249  std::string reason = e.what();
250  if (reason != "Process Error" && reason != "") {
251  msg = msg + "\n Reason: " + reason + ".";
252  } else {
253  msg = msg + "\n (unknown reason).";
254  }
255  WRITE_ERROR(msg);
256  ok = false;
257  }
258  }
259  return ok;
260 }
261 
262 
263 bool
264 ROLoader::loadWeights(RONet& net, const std::string& optionName,
265  const std::string& measure, const bool useLanes, const bool boundariesOverride) {
266  // check whether the file exists
267  if (!myOptions.isUsableFileList(optionName)) {
268  return false;
269  }
270  // build and prepare the weights handler
271  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
272  // travel time, first (always used)
274  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
275  // the measure to use, then
277  if (measure != "traveltime") {
278  std::string umeasure = measure;
279  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
280  umeasure = measure + "_perVeh";
281  }
282  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
283  }
284  // set up handler
285  SAXWeightsHandler handler(retrieverDefs, "");
286  // go through files
287  std::vector<std::string> files = myOptions.getStringVector(optionName);
288  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
289  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
290  if (XMLSubSys::runParser(handler, *fileIt)) {
292  } else {
293  WRITE_MESSAGE("failed.");
294  return false;
295  }
296  }
297  // build edge-internal time lines
298  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
299  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
300  (*i).second->buildTimeLines(measure, boundariesOverride);
301  }
302  return true;
303 }
304 
305 
306 void
307 ROLoader::writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven) {
308  if (myLogSteps) {
309  if (endGiven) {
310  const double perc = (double)(time - start) / (double) absNo;
311  std::cout << "Reading up to time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
312  } else {
313  std::cout << "Reading up to time step: " + time2string(time) + "\r";
314  }
315  }
316 }
317 
318 
319 /****************************************************************************/
320 
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
SUMOTime getFirstLoadTime() const
returns the timestamp of the first loaded vehicle or flow
std::set< std::string > deprecatedVehicleClassesSeen
void addTravelTime(double value, double timeBegin, double timeEnd)
Adds a travel time value.
Definition: ROEdge.cpp:121
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:54
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:161
void addEdgeWeight(const std::string &id, double val, double beg, double end) const
Adds a travel time for a given edge and time period.
Definition: ROLoader.cpp:66
An XML-handler for network weights.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
void add(SUMORouteLoader *loader)
add another loader
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
const bool myLogSteps
Information whether the routing steps should be logged.
Definition: ROLoader.h:191
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven)
Definition: ROLoader.cpp:307
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:269
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:110
Interface for building instances of router-edges.
void addEdgeWeight(const std::string &id, double val, double beg, double end) const
Adds an effort for a given edge and time period.
Definition: ROLoader.cpp:87
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
OptionsCont & myOptions
Options to use.
Definition: ROLoader.h:185
virtual ~ROLoader()
Destructor.
Definition: ROLoader.cpp:115
bool haveAllLoaded() const
returns whether loading is completed
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:205
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void setFileName(const std::string &name)
Sets the current file name.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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
void addEffort(double value, double timeBegin, double timeEnd)
Adds a weight value.
Definition: ROEdge.cpp:114
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:188
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool openTypedRoutes(const std::string &optionName, RONet &net)
Opens route handler of the given type.
Definition: ROLoader.cpp:229
virtual bool furtherStored()
Returns the information whether further vehicles, persons or containers are stored.
Definition: RONet.cpp:641
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:47
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) ...
T MIN2(T a, T b)
Definition: StdDefs.h:64
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:120
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 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:197
A storage for options typed value containers)
Definition: OptionsCont.h:99
The handler that parses a SUMO-network for its usage in a router.
Definition: RONetHandler.h:60
const std::map< std::string, ROEdge * > & getEdgeMap() const
Definition: RONet.cpp:659
Parser and container for routes during their loading.
long long int SUMOTime
Definition: TraCIDefs.h:52
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:165
#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:107
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:264
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, const RORouterProvider &provider, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:533