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 additional file
164  bool ok = openTypedRoutes("additional-files", net);
165  // load sumo routes, trips, and flows
166  ok &= openTypedRoutes("route-files", net);
167  // check
168  if (ok) {
170  if (!net.furtherStored()) {
172  throw ProcessError();
173  } else {
174  const std::string error = "No route input specified or all routes were invalid.";
175  if (myOptions.getBool("ignore-errors")) {
176  WRITE_WARNING(error);
177  } else {
178  throw ProcessError(error);
179  }
180  }
181  }
182  // skip routes prior to the begin time
183  if (!myOptions.getBool("unsorted-input")) {
184  WRITE_MESSAGE("Skipped until: " + time2string(myLoaders.getFirstLoadTime()));
185  }
186  }
187 }
188 
189 
190 void
191 ROLoader::processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment,
192  RONet& net, const RORouterProvider& provider) {
193  const SUMOTime absNo = end - start;
194  const bool endGiven = !OptionsCont::getOptions().isDefault("end");
195  // skip routes that begin before the simulation's begin
196  // loop till the end
197  const SUMOTime firstStep = myLoaders.getFirstLoadTime();
198  SUMOTime lastStep = firstStep;
199  SUMOTime time = MIN2(firstStep, end);
200  while (time <= end) {
201  writeStats(time, start, absNo, endGiven);
202  myLoaders.loadNext(time);
204  break;
205  }
206  lastStep = net.saveAndRemoveRoutesUntil(myOptions, provider, time);
208  break;
209  }
210  if (time < end && time + increment > end) {
211  time = end;
212  } else {
213  time += increment;
214  }
215  }
216  if (myLogSteps) {
217  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
218  }
219 }
220 
221 
222 bool
223 ROLoader::openTypedRoutes(const std::string& optionName,
224  RONet& net) {
225  // check whether the current loader is known
226  // (not all routers import all route formats)
227  if (!myOptions.exists(optionName)) {
228  return true;
229  }
230  // check whether the current loader is wished
231  // and the file(s) can be used
232  if (!myOptions.isUsableFileList(optionName)) {
233  return !myOptions.isSet(optionName);
234  }
235  bool ok = true;
236  std::vector<std::string> files = myOptions.getStringVector(optionName);
237  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
238  // build the instance when everything's all right
239  try {
240  myLoaders.add(new SUMORouteLoader(new RORouteHandler(net, *fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"))));
241  } catch (ProcessError& e) {
242  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
243  std::string reason = e.what();
244  if (reason != "Process Error" && reason != "") {
245  msg = msg + "\n Reason: " + reason + ".";
246  } else {
247  msg = msg + "\n (unknown reason).";
248  }
249  WRITE_ERROR(msg);
250  ok = false;
251  }
252  }
253  return ok;
254 }
255 
256 
257 bool
258 ROLoader::loadWeights(RONet& net, const std::string& optionName,
259  const std::string& measure, const bool useLanes, const bool boundariesOverride) {
260  // check whether the file exists
261  if (!myOptions.isUsableFileList(optionName)) {
262  return false;
263  }
264  // build and prepare the weights handler
265  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
266  // travel time, first (always used)
268  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
269  // the measure to use, then
271  if (measure != "traveltime") {
272  std::string umeasure = measure;
273  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
274  umeasure = measure + "_perVeh";
275  }
276  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
277  }
278  // set up handler
279  SAXWeightsHandler handler(retrieverDefs, "");
280  // go through files
281  std::vector<std::string> files = myOptions.getStringVector(optionName);
282  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
283  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
284  if (XMLSubSys::runParser(handler, *fileIt)) {
286  } else {
287  WRITE_MESSAGE("failed.");
288  return false;
289  }
290  }
291  // build edge-internal time lines
292  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
293  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
294  (*i).second->buildTimeLines(measure, boundariesOverride);
295  }
296  return true;
297 }
298 
299 
300 void
301 ROLoader::writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven) {
302  if (myLogSteps) {
303  if (endGiven) {
304  const double perc = (double)(time - start) / (double) absNo;
305  std::cout << "Reading up to time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
306  } else {
307  std::cout << "Reading up to time step: " + time2string(time) + "\r";
308  }
309  }
310 }
311 
312 
313 /****************************************************************************/
314 
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:76
std::set< std::string > deprecatedVehicleClassesSeen
void addTravelTime(double value, double timeBegin, double timeEnd)
Adds a travel time value.
Definition: ROEdge.cpp:137
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
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:60
void add(SUMORouteLoader *loader)
add another loader
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
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
const bool myLogSteps
Information whether the routing steps should be logged.
Definition: ROLoader.h:191
void writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven)
Definition: ROLoader.cpp:301
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.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
const std::map< std::string, ROEdge * > & getEdgeMap() const
Definition: RONet.cpp:659
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
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.
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:130
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:188
bool openTypedRoutes(const std::string &optionName, RONet &net)
Opens route handler of the given type.
Definition: ROLoader.cpp:223
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
T MIN2(T a, T b)
Definition: StdDefs.h:68
#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:269
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:120
A basic edge for routing applications.
Definition: ROEdge.h:78
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_ERROR(msg)
Definition: MsgHandler.h:206
Complete definition about what shall be retrieved and where to store it.
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:191
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
Parser and container for routes during their loading.
long long int SUMOTime
Definition: TraCIDefs.h:52
#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 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:258
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, const RORouterProvider &provider, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:533
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.