SUMO - Simulation of Urban MObility
ROLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // Loader for networks and route imports
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <iostream>
35 #include <string>
36 #include <iomanip>
37 #include <xercesc/parsers/SAXParser.hpp>
38 #include <xercesc/util/PlatformUtils.hpp>
39 #include <xercesc/util/TransService.hpp>
40 #include <xercesc/sax2/SAX2XMLReader.hpp>
42 #include <utils/common/ToString.h>
47 #include <utils/xml/XMLSubSys.h>
51 #include "RONet.h"
52 #include "RONetHandler.h"
53 #include "ROLoader.h"
54 #include "ROEdge.h"
55 #include "RORouteHandler.h"
56 
57 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
61 // ---------------------------------------------------------------------------
62 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
63 // ---------------------------------------------------------------------------
64 void
66  double val, double beg, double end) const {
67  ROEdge* e = myNet.getEdge(id);
68  if (e != 0) {
69  e->addTravelTime(val, beg, end);
70  } else {
71  if (id[0] != ':') {
72  if (OptionsCont::getOptions().getBool("ignore-errors")) {
73  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
74  } else {
75  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
76  }
77  }
78  }
79 }
80 
81 
82 // ---------------------------------------------------------------------------
83 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
84 // ---------------------------------------------------------------------------
85 void
87  double val, double beg, double end) const {
88  ROEdge* e = myNet.getEdge(id);
89  if (e != 0) {
90  e->addEffort(val, beg, end);
91  } else {
92  if (id[0] != ':') {
93  if (OptionsCont::getOptions().getBool("ignore-errors")) {
94  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
95  } else {
96  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
97  }
98  }
99  }
100 }
101 
102 
103 // ---------------------------------------------------------------------------
104 // ROLoader - methods
105 // ---------------------------------------------------------------------------
106 ROLoader::ROLoader(OptionsCont& oc, const bool emptyDestinationsAllowed, const bool logSteps) :
107  myOptions(oc),
108  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
109  myLogSteps(logSteps),
110  myLoaders(oc.exists("unsorted-input") && oc.getBool("unsorted-input") ? 0 : DELTA_T) {
111 }
112 
113 
115 }
116 
117 
118 void
120  std::string file = myOptions.getString("net-file");
121  if (file == "") {
122  throw ProcessError("Missing definition of network to load!");
123  }
124  if (!FileHelpers::isReadable(file)) {
125  throw ProcessError("The network file '" + file + "' is not accessible.");
126  }
127  PROGRESS_BEGIN_MESSAGE("Loading net");
128  RONetHandler handler(toFill, eb);
129  handler.setFileName(file);
130  if (!XMLSubSys::runParser(handler, file, true)) {
132  throw ProcessError();
133  } else {
135  }
136  if (!deprecatedVehicleClassesSeen.empty()) {
137  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
139  }
140  if (myOptions.isSet("additional-files", false)) { // dfrouter does not register this option
141  std::vector<std::string> files = myOptions.getStringVector("additional-files");
142  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
143  if (!FileHelpers::isReadable(*fileIt)) {
144  throw ProcessError("The additional file '" + *fileIt + "' is not accessible.");
145  }
146  PROGRESS_BEGIN_MESSAGE("Loading additional file '" + *fileIt + "' ");
147  handler.setFileName(*fileIt);
148  if (!XMLSubSys::runParser(handler, *fileIt)) {
150  throw ProcessError();
151  } else {
153  }
154  }
155  }
156 }
157 
158 
159 void
161  // build loader
162  // load relevant elements from additional file
163  bool ok = openTypedRoutes("additional-files", net, true);
164  // load sumo routes, trips, and flows
165  ok &= openTypedRoutes("route-files", net);
166  // check
167  if (ok) {
169  if (!net.furtherStored()) {
171  throw ProcessError();
172  } else {
173  const std::string error = "No route input specified or all routes were invalid.";
174  if (myOptions.getBool("ignore-errors")) {
175  WRITE_WARNING(error);
176  } else {
177  throw ProcessError(error);
178  }
179  }
180  }
181  // skip routes prior to the begin time
182  if (!myOptions.getBool("unsorted-input")) {
183  WRITE_MESSAGE("Skipped until: " + time2string(myLoaders.getFirstLoadTime()));
184  }
185  }
186 }
187 
188 
189 void
190 ROLoader::processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment,
191  RONet& net, const RORouterProvider& provider) {
192  const SUMOTime absNo = end - start;
193  const bool endGiven = !OptionsCont::getOptions().isDefault("end");
194  // skip routes that begin before the simulation's begin
195  // loop till the end
196  const SUMOTime firstStep = myLoaders.getFirstLoadTime();
197  SUMOTime lastStep = firstStep;
198  SUMOTime time = MIN2(firstStep, end);
199  while (time <= end) {
200  writeStats(time, start, absNo, endGiven);
201  myLoaders.loadNext(time);
203  break;
204  }
205  lastStep = net.saveAndRemoveRoutesUntil(myOptions, provider, time);
207  break;
208  }
209  if (time < end && time + increment > end) {
210  time = end;
211  } else {
212  time += increment;
213  }
214  }
215  if (myLogSteps) {
216  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
217  }
218 }
219 
220 
221 bool
222 ROLoader::openTypedRoutes(const std::string& optionName,
223  RONet& net, const bool readAll) {
224  // check whether the current loader is wished
225  // and the file(s) can be used
226  if (!myOptions.isUsableFileList(optionName)) {
227  return !myOptions.isSet(optionName);
228  }
229  for (const std::string& fileIt : myOptions.getStringVector(optionName)) {
230  try {
231  RORouteHandler* handler = new RORouteHandler(net, fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"));
232  if (readAll) {
233  if (!XMLSubSys::runParser(*handler, fileIt)) {
234  WRITE_ERROR("Loading of " + fileIt + " failed.");
235  return false;
236  }
237  delete handler;
238  } else {
239  myLoaders.add(new SUMORouteLoader(handler));
240  }
241  } catch (ProcessError& e) {
242  WRITE_ERROR("The loader for " + optionName + " from file '" + fileIt + "' could not be initialised (" + e.what() + ").");
243  return false;
244  }
245  }
246  return true;
247 }
248 
249 
250 bool
251 ROLoader::loadWeights(RONet& net, const std::string& optionName,
252  const std::string& measure, const bool useLanes, const bool boundariesOverride) {
253  // check whether the file exists
254  if (!myOptions.isUsableFileList(optionName)) {
255  return false;
256  }
257  // build and prepare the weights handler
258  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
259  // travel time, first (always used)
261  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
262  // the measure to use, then
264  if (measure != "traveltime") {
265  std::string umeasure = measure;
266  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
267  umeasure = measure + "_perVeh";
268  }
269  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
270  }
271  // set up handler
272  SAXWeightsHandler handler(retrieverDefs, "");
273  // go through files
274  std::vector<std::string> files = myOptions.getStringVector(optionName);
275  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
276  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
277  if (XMLSubSys::runParser(handler, *fileIt)) {
279  } else {
280  WRITE_MESSAGE("failed.");
281  return false;
282  }
283  }
284  // build edge-internal time lines
285  for (const auto& i : net.getEdgeMap()) {
286  i.second->buildTimeLines(measure, boundariesOverride);
287  }
288  return true;
289 }
290 
291 
292 void
293 ROLoader::writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven) {
294  if (myLogSteps) {
295  if (endGiven) {
296  const double perc = (double)(time - start) / (double) absNo;
297  std::cout << "Reading up to time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
298  } else {
299  std::cout << "Reading up to time step: " + time2string(time) + "\r";
300  }
301  }
302 }
303 
304 
305 /****************************************************************************/
306 
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:75
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:136
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:172
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:53
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:160
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:65
An XML-handler for network weights.
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:190
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:293
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:268
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:109
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:86
bool openTypedRoutes(const std::string &optionName, RONet &net, const bool readAll=false)
Opens route handler of the given type.
Definition: ROLoader.cpp:222
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
OptionsCont & myOptions
Options to use.
Definition: ROLoader.h:184
virtual ~ROLoader()
Destructor.
Definition: ROLoader.cpp:114
bool haveAllLoaded() const
returns whether loading is completed
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:204
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:55
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:120
SUMORouteLoaderControl myLoaders
List of route loaders.
Definition: ROLoader.h:193
void addEffort(double value, double timeBegin, double timeEnd)
Adds a weight value.
Definition: ROEdge.cpp:129
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:187
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
virtual bool furtherStored()
Returns the information whether further vehicles, persons or containers are stored.
Definition: RONet.cpp:639
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
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:67
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:201
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:119
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:205
The router&#39;s network representation.
Definition: RONet.h:74
Obtains edge travel times from a weights handler and stores them within the edges.
Definition: ROLoader.h:151
const NamedObjectCont< ROEdge * > & getEdgeMap() const
Definition: RONet.h:400
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:190
A storage for options typed value containers)
Definition: OptionsCont.h:98
The handler that parses a SUMO-network for its usage in a router.
Definition: RONetHandler.h:59
Parser and container for routes during their loading.
long long int SUMOTime
Definition: TraCIDefs.h:51
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:163
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:202
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:200
ROLoader(OptionsCont &oc, const bool emptyDestinationsAllowed, const bool logSteps)
Constructor.
Definition: ROLoader.cpp:106
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:251
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, const RORouterProvider &provider, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:533