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-2015 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 #include "RORouteAggregator.h"
58 
59 #ifdef CHECK_MEMORY_LEAKS
60 #include <foreign/nvwa/debug_new.h>
61 #endif // CHECK_MEMORY_LEAKS
62 
63 
64 // ===========================================================================
65 // method definitions
66 // ===========================================================================
67 // ---------------------------------------------------------------------------
68 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
69 // ---------------------------------------------------------------------------
70 void
72  SUMOReal val, SUMOReal beg, SUMOReal end) const {
73  ROEdge* e = myNet.getEdge(id);
74  if (e != 0) {
75  e->addTravelTime(val, beg, end);
76  } else {
77  if (id[0] != ':') {
78  if (OptionsCont::getOptions().getBool("ignore-errors")) {
79  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
80  } else {
81  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
82  }
83  }
84  }
85 }
86 
87 
88 // ---------------------------------------------------------------------------
89 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
90 // ---------------------------------------------------------------------------
91 void
93  SUMOReal val, SUMOReal beg, SUMOReal end) const {
94  ROEdge* e = myNet.getEdge(id);
95  if (e != 0) {
96  e->addEffort(val, beg, end);
97  } else {
98  if (id[0] != ':') {
99  if (OptionsCont::getOptions().getBool("ignore-errors")) {
100  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
101  } else {
102  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
103  }
104  }
105  }
106 }
107 
108 
109 // ---------------------------------------------------------------------------
110 // ROLoader - methods
111 // ---------------------------------------------------------------------------
112 ROLoader::ROLoader(OptionsCont& oc, const bool emptyDestinationsAllowed, const bool logSteps) :
113  myOptions(oc),
114  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
115  myLogSteps(logSteps),
116  myLoaders(oc.exists("unsorted-input") && oc.getBool("unsorted-input") ? 0 : DELTA_T)
117 {}
118 
119 
121 }
122 
123 
124 void
126  std::string file = myOptions.getString("net-file");
127  if (file == "") {
128  throw ProcessError("Missing definition of network to load!");
129  }
130  if (!FileHelpers::isReadable(file)) {
131  throw ProcessError("The network file '" + file + "' is not accessible.");
132  }
133  PROGRESS_BEGIN_MESSAGE("Loading net");
134  RONetHandler handler(toFill, eb);
135  handler.setFileName(file);
136  if (!XMLSubSys::runParser(handler, file, true)) {
138  throw ProcessError();
139  } else {
141  }
142  if (!deprecatedVehicleClassesSeen.empty()) {
143  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
145  }
146  if (myOptions.isSet("additional-files", false)) { // dfrouter does not register this option
147  std::vector<std::string> files = myOptions.getStringVector("additional-files");
148  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
149  if (!FileHelpers::isReadable(*fileIt)) {
150  throw ProcessError("The additional file '" + *fileIt + "' is not accessible.");
151  }
152  PROGRESS_BEGIN_MESSAGE("Loading additional file '" + *fileIt + "' ");
153  handler.setFileName(*fileIt);
154  if (!XMLSubSys::runParser(handler, *fileIt)) {
156  throw ProcessError();
157  } else {
159  }
160  }
161  }
162 }
163 
164 
165 void
167  // build loader
168  // load relevant elements from additinal file
169  bool ok = openTypedRoutes("additional-files", net);
170  // load sumo-routes when wished
171  ok &= openTypedRoutes("route-files", net);
172  // load the XML-trip definitions when wished
173  ok &= openTypedRoutes("trip-files", net);
174  // load the sumo-alternative file when wished
175  ok &= openTypedRoutes("alternative-files", net);
176  // load the amount definitions if wished
177  ok &= openTypedRoutes("flow-files", net);
178  // check
179  if (ok) {
181  if (!net.furtherStored()) {
183  throw ProcessError();
184  } else {
185  throw ProcessError("No route input specified or all routes were invalid.");
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,
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, router, 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 void
233  net.saveAndRemoveRoutesUntil(myOptions, router, end);
234 }
235 
236 
237 bool
238 ROLoader::openTypedRoutes(const std::string& optionName,
239  RONet& net) {
240  // check whether the current loader is known
241  // (not all routers import all route formats)
242  if (!myOptions.exists(optionName)) {
243  return true;
244  }
245  // check whether the current loader is wished
246  // and the file(s) can be used
247  if (!myOptions.isUsableFileList(optionName)) {
248  return !myOptions.isSet(optionName);
249  }
250  bool ok = true;
251  std::vector<std::string> files = myOptions.getStringVector(optionName);
252  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
253  // build the instance when everything's all right
254  try {
255  myLoaders.add(new SUMORouteLoader(new RORouteHandler(net, *fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"))));
256  } catch (ProcessError& e) {
257  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
258  std::string reason = e.what();
259  if (reason != "Process Error" && reason != "") {
260  msg = msg + "\n Reason: " + reason + ".";
261  } else {
262  msg = msg + "\n (unknown reason).";
263  }
264  WRITE_ERROR(msg);
265  ok = false;
266  }
267  }
268  return ok;
269 }
270 
271 
272 bool
273 ROLoader::loadWeights(RONet& net, const std::string& optionName,
274  const std::string& measure, bool useLanes) {
275  // check whether the file exists
276  if (!myOptions.isUsableFileList(optionName)) {
277  return false;
278  }
279  // build and prepare the weights handler
280  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
281  // travel time, first (always used)
283  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
284  // the measure to use, then
286  if (measure != "traveltime") {
287  std::string umeasure = measure;
288  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
289  umeasure = measure + "_perVeh";
290  }
291  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
292  }
293  // set up handler
294  SAXWeightsHandler handler(retrieverDefs, "");
295  // go through files
296  std::vector<std::string> files = myOptions.getStringVector(optionName);
297  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
298  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
299  if (XMLSubSys::runParser(handler, *fileIt)) {
301  } else {
302  WRITE_MESSAGE("failed.");
303  return false;
304  }
305  }
306  // build edge-internal time lines
307  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
308  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
309  (*i).second->buildTimeLines(measure);
310  }
311  return true;
312 }
313 
314 
315 void
316 ROLoader::writeStats(SUMOTime time, SUMOTime start, int absNo, bool endGiven) {
317  if (myLogSteps) {
318  if (endGiven) {
319  const SUMOReal perc = (SUMOReal)(time - start) / (SUMOReal) absNo;
320  std::cout << "Reading up to time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
321  } else {
322  std::cout << "Reading up to time step: " + time2string(time) + "\r";
323  }
324  }
325 }
326 
327 
328 /****************************************************************************/
329 
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:80
std::set< std::string > deprecatedVehicleClassesSeen
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:177
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
void writeStats(SUMOTime time, SUMOTime start, int absNo, bool endGiven)
Definition: ROLoader.cpp:316
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:166
An XML-handler for network weights.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:138
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool loadWeights(RONet &net, const std::string &optionName, const std::string &measure, bool useLanes)
Loads the net weights.
Definition: ROLoader.cpp:273
SUMOTime getFirstLoadTime() const
returns the timestamp of the first loaded vehicle or flow
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
void add(SUMORouteLoader *loader)
add another loader
static void processAllRoutes(RONet &net, SUMOAbstractRouter< ROEdge, ROVehicle > &router)
precomputes all routes grouped by their destination edge
const bool myLogSteps
Information whether the routing steps should be logged.
Definition: ROLoader.h:195
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:114
Interface for building instances of router-edges.
void addTravelTime(SUMOReal value, SUMOReal timeBegin, SUMOReal timeEnd)
Adds a travel time value.
Definition: ROEdge.cpp:119
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
const std::map< std::string, ROEdge * > & getEdgeMap() const
Definition: RONet.cpp:541
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
OptionsCont & myOptions
Options to use.
Definition: ROLoader.h:189
void addEdgeWeight(const std::string &id, SUMOReal val, SUMOReal beg, SUMOReal end) const
Adds an effort for a given edge and time period.
Definition: ROLoader.cpp:92
virtual ~ROLoader()
Destructor.
Definition: ROLoader.cpp:120
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:204
void setFileName(const std::string &name)
Sets the current file name.
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:125
SUMORouteLoaderControl myLoaders
List of route loaders.
Definition: ROLoader.h:198
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:192
void addEdgeWeight(const std::string &id, SUMOReal val, SUMOReal beg, SUMOReal end) const
Adds a travel time for a given edge and time period.
Definition: ROLoader.cpp:71
bool openTypedRoutes(const std::string &optionName, RONet &net)
Opens route handler of the given type.
Definition: ROLoader.cpp:238
virtual bool furtherStored()
Returns the information whether further vehicles, persons or containers are stored.
Definition: RONet.cpp:523
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
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:273
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:125
A basic edge for routing applications.
Definition: ROEdge.h:73
Complete definition about what shall be retrieved and where to store it.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
The router's network representation.
Definition: RONet.h:72
Obtains edge travel times from a weights handler and stores them within the edges.
Definition: ROLoader.h:156
void addEffort(SUMOReal value, SUMOReal timeBegin, SUMOReal timeEnd)
Adds a weight value.
Definition: ROEdge.cpp:112
#define SUMOTime_MAX
Definition: SUMOTime.h:44
A storage for options typed value containers)
Definition: OptionsCont.h:108
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.
int SUMOTime
Definition: SUMOTime.h:43
#define SUMOReal
Definition: config.h:218
#define DELTA_T
Definition: SUMOTime.h:50
void processAllRoutesWithBulkRouter(SUMOTime start, SUMOTime end, RONet &net, SUMOAbstractRouter< ROEdge, ROVehicle > &router)
Loads all routes and processes them with BulkStarRouter.
Definition: ROLoader.cpp:229
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
void processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment, RONet &net, SUMOAbstractRouter< ROEdge, ROVehicle > &router)
Loads routes from all previously build route loaders.
Definition: ROLoader.cpp:197
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:424
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
ROLoader(OptionsCont &oc, const bool emptyDestinationsAllowed, const bool logSteps)
Constructor.
Definition: ROLoader.cpp:112
bool haveAllLoaded() const
returns whether loading is completed
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.