SUMO - Simulation of Urban MObility
jtrrouter_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Main for JTRROUTER
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_VERSION_H
34 #include <version.h>
35 #endif
36 
37 #include <xercesc/sax/SAXException.hpp>
38 #include <xercesc/sax/SAXParseException.hpp>
40 #include <iostream>
41 #include <string>
42 #include <limits.h>
43 #include <ctime>
44 #include <set>
48 #include <utils/common/ToString.h>
52 #include <utils/options/Option.h>
55 #include <utils/xml/XMLSubSys.h>
56 #include <router/ROFrame.h>
57 #include <router/ROLoader.h>
58 #include <router/RONet.h>
59 #include <router/RORouteDef.h>
60 #include "ROJTREdgeBuilder.h"
61 #include "ROJTRRouter.h"
62 #include "ROJTREdge.h"
63 #include "ROJTRTurnDefLoader.h"
64 #include "ROJTRFrame.h"
65 
66 #ifdef CHECK_MEMORY_LEAKS
67 #include <foreign/nvwa/debug_new.h>
68 #endif // CHECK_MEMORY_LEAKS
69 
70 
71 // ===========================================================================
72 // functions
73 // ===========================================================================
74 /* -------------------------------------------------------------------------
75  * data processing methods
76  * ----------------------------------------------------------------------- */
82 void
83 initNet(RONet& net, ROLoader& loader,
84  const std::vector<SUMOReal>& turnDefs) {
85  // load the net
86  ROJTREdgeBuilder builder;
87  loader.loadNet(net, builder);
88  // set the turn defaults
89  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
90  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
91  static_cast<ROJTREdge*>((*i).second)->setTurnDefaults(turnDefs);
92  }
93 }
94 
95 std::vector<SUMOReal>
97  std::vector<SUMOReal> ret;
98  std::vector<std::string> defs = oc.getStringVector("turn-defaults");
99  if (defs.size() < 2) {
100  throw ProcessError("The defaults for turnings must be a tuple of at least two numbers divided by ','.");
101  }
102  for (std::vector<std::string>::const_iterator i = defs.begin(); i != defs.end(); ++i) {
103  try {
104  SUMOReal val = TplConvert::_2SUMOReal((*i).c_str());
105  ret.push_back(val);
106  } catch (NumberFormatException&) {
107  throw ProcessError("A turn default is not numeric.");
108  }
109  }
110  return ret;
111 }
112 
113 
114 void
116  // load the turning definitions (and possible sink definition)
117  if (oc.isSet("turn-ratio-files")) {
118  ROJTRTurnDefLoader loader(net);
119  std::vector<std::string> ratio_files = oc.getStringVector("turn-ratio-files");
120  for (std::vector<std::string>::const_iterator i = ratio_files.begin(); i != ratio_files.end(); ++i) {
121  if (!XMLSubSys::runParser(loader, *i)) {
122  throw ProcessError();
123  }
124  }
125  }
126  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
128  }
129  // parse sink edges specified at the input/within the configuration
130  if (oc.isSet("sink-edges")) {
131  std::vector<std::string> edges = oc.getStringVector("sink-edges");
132  for (std::vector<std::string>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
133  ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
134  if (edge == 0) {
135  throw ProcessError("The edge '" + *i + "' declared as a sink is not known.");
136  }
137  edge->setFunc(ROEdge::ET_SINK);
138  }
139  }
140 }
141 
142 
146 void
148  // initialise the loader
149  loader.openRoutes(net);
150  // prepare the output
151  net.openOutput(oc.getString("output-file"), "", oc.getString("vtype-output"));
152  // build the router
153  ROJTRRouter* router = new ROJTRRouter(oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
154  (int)(((SUMOReal) net.getEdgeNo()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
155  oc.getBool("ignore-vclasses"), oc.getBool("allow-loops"));
159  loader.processRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")),
160  string2time(oc.getString("route-steps")), net, provider);
161  net.cleanup();
162 }
163 
164 
165 /* -------------------------------------------------------------------------
166  * main
167  * ----------------------------------------------------------------------- */
168 int
169 main(int argc, char** argv) {
171  // give some application descriptions
172  oc.setApplicationDescription("Router for the microscopic road traffic simulation SUMO based on junction turning ratios.");
173  oc.setApplicationName("jtrrouter", "SUMO jtrrouter Version " VERSION_STRING);
174  int ret = 0;
175  RONet* net = 0;
176  try {
177  // initialise the application system (messaging, xml, options)
178  XMLSubSys::init();
180  OptionsIO::setArgs(argc, argv);
182  if (oc.processMetaOptions(argc < 2)) {
184  return 0;
185  }
186  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
188  if (!ROJTRFrame::checkOptions()) {
189  throw ProcessError();
190  }
192  std::vector<SUMOReal> defs = getTurningDefaults(oc);
193  // load data
194  ROLoader loader(oc, true, !oc.getBool("no-step-log"));
195  net = new RONet();
196  initNet(*net, loader, defs);
197  try {
198  // parse and set the turn defaults first
199  loadJTRDefinitions(*net, oc);
200  // build routes
201  computeRoutes(*net, loader, oc);
202  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
203  WRITE_ERROR(toString(e.getLineNumber()));
204  ret = 1;
205  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
206  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
207  ret = 1;
208  }
209  if (MsgHandler::getErrorInstance()->wasInformed()) {
210  throw ProcessError();
211  }
212  } catch (const ProcessError& e) {
213  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
214  WRITE_ERROR(e.what());
215  }
216  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
217  ret = 1;
218 #ifndef _DEBUG
219  } catch (const std::exception& e) {
220  if (std::string(e.what()) != std::string("")) {
221  WRITE_ERROR(e.what());
222  }
223  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
224  ret = 1;
225  } catch (...) {
226  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
227  ret = 1;
228 #endif
229  }
230  delete net;
232  if (ret == 0) {
233  std::cout << "Success." << std::endl;
234  }
235  return ret;
236 }
237 
238 
239 /****************************************************************************/
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 void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
Computes routes using junction turning percentages.
Definition: ROJTRRouter.h:53
size_t getEdgeNo() const
Returns the total number of edges the network contains including internal edges.
Definition: RONet.cpp:613
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:242
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:165
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:631
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:165
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
std::vector< SUMOReal > getTurningDefaults(OptionsCont &oc)
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
void setFunc(EdgeFunc func)
Sets the function of the edge.
Definition: ROEdge.h:141
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:114
static void close()
Closes all of an applications subsystems.
const std::map< std::string, ROEdge * > & getEdgeMap() const
Definition: RONet.cpp:625
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:65
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:51
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
void openOutput(const std::string &filename, const std::string altFilename, const std::string typeFilename)
Opens the output for computed routes.
Definition: RONet.cpp:212
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void initNet(RONet &net, ROLoader &loader, const std::vector< SUMOReal > &turnDefs)
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary ...
Definition: RONet.cpp:231
An edge where vehicles disappear (no vehicle may leave this edge)
Definition: ROEdge.h:91
Loader for the of turning percentages and source/sink definitions.
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
The data loader.
Definition: ROLoader.h:63
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
An edge the jtr-router may route through.
Definition: ROJTREdge.h:58
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:124
int main(int argc, char **argv)
#define VERSION_STRING
Definition: config.h:225
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static void setUsingJTRR()
Definition: RORouteDef.h:150
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
The router&#39;s network representation.
Definition: RONet.h:76
static void fillOptions()
Inserts options used by jtrrouter into the OptionsCont-singleton.
Definition: ROJTRFrame.cpp:56
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within jtrrouter...
Definition: ROJTRFrame.cpp:103
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:201
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
#define SUMOReal
Definition: config.h:213
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
static void initOutputOptions()
Definition: MsgHandler.cpp:197
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Interface for building instances of jtrrouter-edges.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
void loadJTRDefinitions(RONet &net, OptionsCont &oc)