SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
14 // Copyright (C) 2001-2014 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 #ifdef HAVE_INTERNAL // catchall for internal stuff
59 #include <internal/RouteAggregator.h>
60 #endif // have HAVE_INTERNAL
61 
62 #ifdef CHECK_MEMORY_LEAKS
63 #include <foreign/nvwa/debug_new.h>
64 #endif // CHECK_MEMORY_LEAKS
65 
66 
67 // ===========================================================================
68 // method definitions
69 // ===========================================================================
70 // ---------------------------------------------------------------------------
71 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
72 // ---------------------------------------------------------------------------
73 void
75  SUMOReal val, SUMOReal beg, SUMOReal end) const {
76  ROEdge* e = myNet.getEdge(id);
77  if (e != 0) {
78  e->addTravelTime(val, beg, end);
79  } else {
80  if (id[0] != ':') {
81  if (OptionsCont::getOptions().getBool("ignore-errors")) {
82  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
83  } else {
84  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
85  }
86  }
87  }
88 }
89 
90 
91 // ---------------------------------------------------------------------------
92 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
93 // ---------------------------------------------------------------------------
94 void
96  SUMOReal val, SUMOReal beg, SUMOReal end) const {
97  ROEdge* e = myNet.getEdge(id);
98  if (e != 0) {
99  e->addEffort(val, beg, end);
100  } else {
101  if (id[0] != ':') {
102  if (OptionsCont::getOptions().getBool("ignore-errors")) {
103  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
104  } else {
105  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
106  }
107  }
108  }
109 }
110 
111 
112 // ---------------------------------------------------------------------------
113 // ROLoader - methods
114 // ---------------------------------------------------------------------------
115 ROLoader::ROLoader(OptionsCont& oc, const bool emptyDestinationsAllowed, const bool logSteps) :
116  myOptions(oc),
117  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
118  myLogSteps(logSteps),
119  myLoaders(oc.exists("unsorted-input") && oc.getBool("unsorted-input") ? 0 : DELTA_T)
120 {}
121 
122 
124 }
125 
126 
127 void
129  std::string file = myOptions.getString("net-file");
130  if (file == "") {
131  throw ProcessError("Missing definition of network to load!");
132  }
133  if (!FileHelpers::isReadable(file)) {
134  throw ProcessError("The network file '" + file + "' is not accessible.");
135  }
136  PROGRESS_BEGIN_MESSAGE("Loading net");
137  RONetHandler handler(toFill, eb);
138  handler.setFileName(file);
139  if (!XMLSubSys::runParser(handler, file, true)) {
141  throw ProcessError();
142  } else {
144  }
145  if (!deprecatedVehicleClassesSeen.empty()) {
146  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
148  }
149  if (myOptions.isSet("additional-files", false)) { // dfrouter does not register this option
150  std::vector<std::string> files = myOptions.getStringVector("additional-files");
151  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
152  if (!FileHelpers::isReadable(*fileIt)) {
153  throw ProcessError("The additional file '" + *fileIt + "' is not accessible.");
154  }
155  PROGRESS_BEGIN_MESSAGE("Loading additional file '" + *fileIt + "' ");
156  handler.setFileName(*fileIt);
157  if (!XMLSubSys::runParser(handler, *fileIt)) {
159  throw ProcessError();
160  } else {
162  }
163  }
164  }
165 }
166 
167 
168 void
170  // build loader
171  // load relevant elements from additinal file
172  bool ok = openTypedRoutes("additional-files", net);
173  // load sumo-routes when wished
174  ok &= openTypedRoutes("route-files", net);
175  // load the XML-trip definitions when wished
176  ok &= openTypedRoutes("trip-files", net);
177  // load the sumo-alternative file when wished
178  ok &= openTypedRoutes("alternative-files", net);
179  // load the amount definitions if wished
180  ok &= openTypedRoutes("flow-files", net);
181  // check
182  if (ok) {
185  throw ProcessError("No route input specified or all routes were invalid.");
186  }
187  // skip routes prior to the begin time
188  if (!myOptions.getBool("unsorted-input")) {
189  WRITE_MESSAGE("Skipped until: " + time2string(myLoaders.getFirstLoadTime()));
190  }
191  }
192 }
193 
194 
195 void
198  SUMOTime absNo = end - start;
199  // skip routes that begin before the simulation's begin
200  // loop till the end
201  bool endReached = false;
202  bool errorOccured = false;
203  const SUMOTime firstStep = myLoaders.getFirstLoadTime();
204  SUMOTime lastStep = firstStep;
205  for (SUMOTime time = firstStep; time < end && !errorOccured && !endReached; time += DELTA_T) {
206  writeStats(time, start, absNo);
207  myLoaders.loadNext(time);
208  net.saveAndRemoveRoutesUntil(myOptions, router, time);
209  endReached = !net.furtherStored();
210  lastStep = time;
211  errorOccured = MsgHandler::getErrorInstance()->wasInformed() && !myOptions.getBool("ignore-errors");
212  }
213  if (myLogSteps) {
214  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
215  }
216 }
217 
218 
219 #ifdef HAVE_INTERNAL // catchall for internal stuff
220 void
221 ROLoader::processAllRoutesWithBulkRouter(SUMOTime /* start */, SUMOTime end,
224  RouteAggregator::processAllRoutes(net, router);
225  net.saveAndRemoveRoutesUntil(myOptions, router, end);
226 }
227 #endif
228 
229 
230 bool
231 ROLoader::openTypedRoutes(const std::string& optionName,
232  RONet& net) {
233  // check whether the current loader is known
234  // (not all routers import all route formats)
235  if (!myOptions.exists(optionName)) {
236  return true;
237  }
238  // check whether the current loader is wished
239  // and the file(s) can be used
240  if (!myOptions.isUsableFileList(optionName)) {
241  return !myOptions.isSet(optionName);
242  }
243  bool ok = true;
244  std::vector<std::string> files = myOptions.getStringVector(optionName);
245  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
246  // build the instance when everything's all right
247  try {
248  myLoaders.add(new SUMORouteLoader(new RORouteHandler(net, *fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"))));
249  } catch (ProcessError& e) {
250  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
251  std::string reason = e.what();
252  if (reason != "Process Error" && reason != "") {
253  msg = msg + "\n Reason: " + reason + ".";
254  } else {
255  msg = msg + "\n (unknown reason).";
256  }
257  WRITE_ERROR(msg);
258  ok = false;
259  }
260  }
261  return ok;
262 }
263 
264 
265 bool
266 ROLoader::loadWeights(RONet& net, const std::string& optionName,
267  const std::string& measure, bool useLanes) {
268  // check whether the file exists
269  if (!myOptions.isUsableFileList(optionName)) {
270  return false;
271  }
272  // build and prepare the weights handler
273  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
274  // travel time, first (always used)
276  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
277  // the measure to use, then
279  if (measure != "traveltime") {
280  std::string umeasure = measure;
281  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
282  umeasure = measure + "_perVeh";
283  }
284  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
285  }
286  // set up handler
287  SAXWeightsHandler handler(retrieverDefs, "");
288  // go through files
289  std::vector<std::string> files = myOptions.getStringVector(optionName);
290  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
291  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
292  if (XMLSubSys::runParser(handler, *fileIt)) {
294  } else {
295  WRITE_MESSAGE("failed.");
296  return false;
297  }
298  }
299  // build edge-internal time lines
300  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
301  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
302  (*i).second->buildTimeLines(measure);
303  }
304  return true;
305 }
306 
307 
308 void
309 ROLoader::writeStats(SUMOTime time, SUMOTime start, int absNo) {
310  if (myLogSteps) {
311  const SUMOReal perc = (SUMOReal)(time - start) / (SUMOReal) absNo;
312  std::cout << "Reading time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
313  }
314 }
315 
316 
317 /****************************************************************************/
318 
void processRoutes(SUMOTime start, SUMOTime end, RONet &net, SUMOAbstractRouter< ROEdge, ROVehicle > &router)
Loads routes from all previously build route loaders.
Definition: ROLoader.cpp:196
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
RONet & myNet
The network edges shall be obtained from.
Definition: ROLoader.h:179
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:169
An XML-handler for network weights.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:102
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:266
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
const bool myLogSteps
Information whether the routing steps should be logged.
Definition: ROLoader.h:197
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:407
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
OptionsCont & myOptions
Options to use.
Definition: ROLoader.h:191
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:95
virtual ~ROLoader()
Destructor.
Definition: ROLoader.cpp:123
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:127
SUMORouteLoaderControl myLoaders
List of route loaders.
Definition: ROLoader.h:200
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:194
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:74
bool openTypedRoutes(const std::string &optionName, RONet &net)
Opens route handler of the given type.
Definition: ROLoader.cpp:231
virtual bool furtherStored()
Returns the information whether further vehicles are stored.
Definition: RONet.cpp:389
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
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:128
A basic edge for routing applications.
Definition: ROEdge.h:69
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:67
Obtains edge travel times from a weights handler and stores them within the edges.
Definition: ROLoader.h:158
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:215
#define DELTA_T
Definition: SUMOTime.h:50
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:335
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
ROLoader(OptionsCont &oc, const bool emptyDestinationsAllowed, const bool logSteps)
Constructor.
Definition: ROLoader.cpp:115
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.
void writeStats(SUMOTime time, SUMOTime start, int absNo)
Definition: ROLoader.cpp:309