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