Eclipse 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-2019 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 /****************************************************************************/
18 // Loader for networks and route imports
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <iostream>
28 #include <string>
29 #include <iomanip>
30 #include <xercesc/parsers/SAXParser.hpp>
31 #include <xercesc/util/PlatformUtils.hpp>
32 #include <xercesc/util/TransService.hpp>
33 #include <xercesc/sax2/SAX2XMLReader.hpp>
35 #include <utils/common/ToString.h>
40 #include <utils/xml/XMLSubSys.h>
44 #include "RONet.h"
45 #include "RONetHandler.h"
46 #include "ROLoader.h"
47 #include "ROLane.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  myOptions.exists("weights.minor-penalty") ? myOptions.getFloat("weights.minor-penalty") : 0);
124  handler.setFileName(file);
125  if (!XMLSubSys::runParser(handler, file, true)) {
127  throw ProcessError();
128  } else {
130  }
131  if (myOptions.exists("restriction-params") && myOptions.isSet("restriction-params")) {
132  const std::vector<std::string> paramKeys = myOptions.getStringVector("restriction-params");
133  for (auto& edgeIt : toFill.getEdgeMap()) {
134  edgeIt.second->cacheParamRestrictions(paramKeys);
135  }
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, true);
165  // load sumo routes, trips, and flows
166  ok &= openTypedRoutes("route-files", net);
167  // check
168  if (ok) {
170  if (!net.furtherStored()) {
171  if (MsgHandler::getErrorInstance()->wasInformed()) {
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, const bool readAll) {
225  // check whether the current loader is wished
226  // and the file(s) can be used
227  if (!myOptions.isUsableFileList(optionName)) {
228  return !myOptions.isSet(optionName);
229  }
230  for (const std::string& fileIt : myOptions.getStringVector(optionName)) {
231  try {
232  RORouteHandler* handler = new RORouteHandler(net, fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"), !readAll);
233  if (readAll) {
234  if (!XMLSubSys::runParser(*handler, fileIt)) {
235  WRITE_ERROR("Loading of " + fileIt + " failed.");
236  return false;
237  }
238  delete handler;
239  } else {
240  myLoaders.add(new SUMORouteLoader(handler));
241  }
242  } catch (ProcessError& e) {
243  WRITE_ERROR("The loader for " + optionName + " from file '" + fileIt + "' could not be initialised (" + e.what() + ").");
244  return false;
245  }
246  }
247  return true;
248 }
249 
250 
251 bool
252 ROLoader::loadWeights(RONet& net, const std::string& optionName,
253  const std::string& measure, const bool useLanes, const bool boundariesOverride) {
254  // check whether the file exists
255  if (!myOptions.isUsableFileList(optionName)) {
256  return false;
257  }
258  // build and prepare the weights handler
259  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
260  // travel time, first (always used)
262  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
263  // the measure to use, then
265  if (measure != "traveltime") {
266  std::string umeasure = measure;
267  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
268  umeasure = measure + "_perVeh";
269  }
270  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
271  }
272  // set up handler
273  SAXWeightsHandler handler(retrieverDefs, "");
274  // go through files
275  std::vector<std::string> files = myOptions.getStringVector(optionName);
276  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
277  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
278  if (XMLSubSys::runParser(handler, *fileIt)) {
280  } else {
281  WRITE_MESSAGE("failed.");
282  return false;
283  }
284  }
285  // build edge-internal time lines
286  for (const auto& i : net.getEdgeMap()) {
287  i.second->buildTimeLines(measure, boundariesOverride);
288  }
289  return true;
290 }
291 
292 
293 void
294 ROLoader::writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven) {
295  if (myLogSteps) {
296  if (endGiven) {
297  const double perc = (double)(time - start) / (double) absNo;
298  std::cout << "Reading up to time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
299  } else {
300  std::cout << "Reading up to time step: " + time2string(time) + "\r";
301  }
302  }
303 }
304 
305 
306 /****************************************************************************/
307 
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
ToString.h
XMLSubSys::runParser
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:112
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:73
RONetHandler
The handler that parses a SUMO-network for its usage in a router.
Definition: RONetHandler.h:52
ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime::myNet
RONet & myNet
The network edges shall be obtained from.
Definition: ROLoader.h:165
SUMORouteLoader
Definition: SUMORouteLoader.h:42
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
ROLoader::openRoutes
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:161
RONetHandler.h
ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime::addEdgeWeight
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
MsgHandler::wasInformed
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:279
SUMORouteLoaderControl::getFirstLoadTime
SUMOTime getFirstLoadTime() const
returns the timestamp of the first loaded vehicle or flow
Definition: SUMORouteLoaderControl.h:64
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:36
RORouteHandler.h
SUMORouteLoaderControl::loadNext
void loadNext(SUMOTime step)
loads the next routes up to and including the given time step
Definition: SUMORouteLoaderControl.cpp:59
PROGRESS_FAILED_MESSAGE
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:282
OptionsCont.h
RONet::getEdge
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:152
MsgHandler.h
ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight::addEdgeWeight
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
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
FileHelpers.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
OptionsCont::exists
bool exists(const std::string &name) const
Returns the information whether the named option is known.
Definition: OptionsCont.cpp:129
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
ROLoader::myLogSteps
const bool myLogSteps
Information whether the routing steps should be logged.
Definition: ROLoader.h:183
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
SAXWeightsHandler.h
RONet
The router's network representation.
Definition: RONet.h:63
ROLoader::writeStats
void writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven)
Definition: ROLoader.cpp:294
ROLoader.h
SUMORouteLoaderControl::add
void add(SUMORouteLoader *loader)
add another loader
Definition: SUMORouteLoaderControl.cpp:53
ROLoader::myOptions
OptionsCont & myOptions
Options to use.
Definition: ROLoader.h:177
RORouteHandler
Parser and container for routes during their loading.
Definition: RORouteHandler.h:59
ROLoader::~ROLoader
virtual ~ROLoader()
Destructor.
Definition: ROLoader.cpp:108
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
ROLoader::openTypedRoutes
bool openTypedRoutes(const std::string &optionName, RONet &net, const bool readAll=false)
Opens route handler of the given type.
Definition: ROLoader.cpp:223
SAXWeightsHandler::ToRetrieveDefinition
Complete definition about what shall be retrieved and where to store it.
Definition: SAXWeightsHandler.h:100
OptionsCont::isUsableFileList
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file)
Definition: OptionsCont.cpp:356
ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime
Obtains edge travel times from a weights handler and stores them within the edges.
Definition: ROLoader.h:144
ROEdge::addTravelTime
void addTravelTime(double value, double timeBegin, double timeEnd)
Adds a travel time value.
Definition: ROEdge.cpp:140
SUMORouteLoaderControl::haveAllLoaded
bool haveAllLoaded() const
returns whether loading is completed
Definition: SUMORouteLoaderControl.h:69
ROEdge::addEffort
void addEffort(double value, double timeBegin, double timeEnd)
Adds a weight value.
Definition: ROEdge.cpp:133
RONet.h
ROLoader::myLoaders
SUMORouteLoaderControl myLoaders
List of route loaders.
Definition: ROLoader.h:186
ROLoader::myEmptyDestinationsAllowed
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:180
ProcessError
Definition: UtilExceptions.h:39
ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight
Obtains edge weights from a weights handler and stores them within the edges.
Definition: ROLoader.h:113
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:67
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
ROLoader::loadNet
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:113
RouterProvider
Definition: RouterProvider.h:37
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
OptionsCont::isDefault
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
Definition: OptionsCont.cpp:163
RONet::getEdgeMap
const NamedObjectCont< ROEdge * > & getEdgeMap() const
Definition: RONet.h:392
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
PROGRESS_BEGIN_MESSAGE
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:278
deprecatedVehicleClassesSeen
std::set< std::string > deprecatedVehicleClassesSeen
Definition: SUMOVehicleClass.cpp:84
SUMORouteLoaderControl.h
PROGRESS_DONE_MESSAGE
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:279
ROEdge
A basic edge for routing applications.
Definition: ROEdge.h:72
RONet::furtherStored
bool furtherStored()
Returns the information whether further vehicles, persons or containers are stored.
Definition: RONet.cpp:642
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
config.h
ROLane.h
ROLoader::processRoutes
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
StringTokenizer.h
RONet::saveAndRemoveRoutesUntil
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, const RORouterProvider &provider, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:534
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
SAXWeightsHandler
An XML-handler for network weights.
Definition: SAXWeightsHandler.h:68
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
ROEdge.h
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:277
ROAbstractEdgeBuilder
Interface for building instances of router-edges.
Definition: ROAbstractEdgeBuilder.h:53
SUMORouteLoader.h
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:68
XMLSubSys.h
ROLoader::ROLoader
ROLoader(OptionsCont &oc, const bool emptyDestinationsAllowed, const bool logSteps)
Constructor.
Definition: ROLoader.cpp:100
ROLoader::loadWeights
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:252