SUMO - Simulation of Urban MObility
jtrrouter_main.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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Main for JTRROUTER
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #ifdef HAVE_VERSION_H
33 #include <version.h>
34 #endif
35 
36 #include <xercesc/sax/SAXException.hpp>
37 #include <xercesc/sax/SAXParseException.hpp>
39 #include <iostream>
40 #include <string>
41 #include <limits.h>
42 #include <ctime>
43 #include <set>
47 #include <utils/common/ToString.h>
51 #include <utils/options/Option.h>
54 #include <utils/xml/XMLSubSys.h>
55 #include <router/ROFrame.h>
56 #include <router/ROLoader.h>
57 #include <router/RONet.h>
58 #include <router/RORouteDef.h>
59 #include "ROJTREdgeBuilder.h"
60 #include "ROJTRRouter.h"
61 #include "ROJTREdge.h"
62 #include "ROJTRTurnDefLoader.h"
63 #include "ROJTRFrame.h"
64 
65 
66 // ===========================================================================
67 // functions
68 // ===========================================================================
69 /* -------------------------------------------------------------------------
70  * data processing methods
71  * ----------------------------------------------------------------------- */
77 void
78 initNet(RONet& net, ROLoader& loader,
79  const std::vector<double>& turnDefs) {
80  // load the net
81  ROJTREdgeBuilder builder;
82  loader.loadNet(net, builder);
83  // set the turn defaults
84  for (const auto& i : net.getEdgeMap()) {
85  static_cast<ROJTREdge*>(i.second)->setTurnDefaults(turnDefs);
86  }
87 }
88 
89 std::vector<double>
91  std::vector<double> ret;
92  std::vector<std::string> defs = oc.getStringVector("turn-defaults");
93  if (defs.size() < 2) {
94  throw ProcessError("The defaults for turnings must be a tuple of at least two numbers divided by ','.");
95  }
96  for (std::vector<std::string>::const_iterator i = defs.begin(); i != defs.end(); ++i) {
97  try {
98  double val = TplConvert::_2double((*i).c_str());
99  ret.push_back(val);
100  } catch (NumberFormatException&) {
101  throw ProcessError("A turn default is not numeric.");
102  }
103  }
104  return ret;
105 }
106 
107 
108 void
110  // load the turning definitions (and possible sink definition)
111  if (oc.isSet("turn-ratio-files")) {
112  ROJTRTurnDefLoader loader(net);
113  std::vector<std::string> ratio_files = oc.getStringVector("turn-ratio-files");
114  for (std::vector<std::string>::const_iterator i = ratio_files.begin(); i != ratio_files.end(); ++i) {
115  if (!XMLSubSys::runParser(loader, *i)) {
116  throw ProcessError();
117  }
118  }
119  }
120  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
122  }
123  // parse sink edges specified at the input/within the configuration
124  if (oc.isSet("sink-edges")) {
125  std::vector<std::string> edges = oc.getStringVector("sink-edges");
126  for (std::vector<std::string>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
127  ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
128  if (edge == 0) {
129  throw ProcessError("The edge '" + *i + "' declared as a sink is not known.");
130  }
131  edge->setSink();
132  }
133  }
134 }
135 
136 
140 void
142  // initialise the loader
143  loader.openRoutes(net);
144  // prepare the output
145  net.openOutput(oc);
146  // build the router
147  ROJTRRouter* router = new ROJTRRouter(oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
148  (int)(((double) net.getEdgeNumber()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
149  oc.getBool("ignore-vclasses"), oc.getBool("allow-loops"));
153  loader.processRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")),
154  string2time(oc.getString("route-steps")), net, provider);
155  net.cleanup();
156 }
157 
158 
159 /* -------------------------------------------------------------------------
160  * main
161  * ----------------------------------------------------------------------- */
162 int
163 main(int argc, char** argv) {
165  // give some application descriptions
166  oc.setApplicationDescription("Router for the microscopic road traffic simulation SUMO based on junction turning ratios.");
167  oc.setApplicationName("jtrrouter", "SUMO jtrrouter Version " VERSION_STRING);
168  int ret = 0;
169  RONet* net = 0;
170  try {
171  // initialise the application system (messaging, xml, options)
172  XMLSubSys::init();
174  OptionsIO::setArgs(argc, argv);
176  if (oc.processMetaOptions(argc < 2)) {
178  return 0;
179  }
180  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
182  if (!ROJTRFrame::checkOptions()) {
183  throw ProcessError();
184  }
186  std::vector<double> defs = getTurningDefaults(oc);
187  // load data
188  ROLoader loader(oc, true, !oc.getBool("no-step-log"));
189  net = new RONet();
190  initNet(*net, loader, defs);
191  try {
192  // parse and set the turn defaults first
193  loadJTRDefinitions(*net, oc);
194  // build routes
195  computeRoutes(*net, loader, oc);
196  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
197  WRITE_ERROR(toString(e.getLineNumber()));
198  ret = 1;
199  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
200  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
201  ret = 1;
202  }
203  if (MsgHandler::getErrorInstance()->wasInformed()) {
204  throw ProcessError();
205  }
206  } catch (const ProcessError& e) {
207  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
208  WRITE_ERROR(e.what());
209  }
210  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
211  ret = 1;
212 #ifndef _DEBUG
213  } catch (const std::exception& e) {
214  if (std::string(e.what()) != std::string("")) {
215  WRITE_ERROR(e.what());
216  }
217  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
218  ret = 1;
219  } catch (...) {
220  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
221  ret = 1;
222 #endif
223  }
224  delete net;
226  if (ret == 0) {
227  std::cout << "Success." << std::endl;
228  }
229  return ret;
230 }
231 
232 
233 /****************************************************************************/
int getEdgeNumber() const
Returns the total number of edges the network contains including internal edges.
Definition: RONet.cpp:645
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:53
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:75
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
Computes routes using junction turning percentages.
Definition: ROJTRRouter.h:52
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:160
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:64
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:657
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
std::vector< double > getTurningDefaults(OptionsCont &oc)
void setSink(const bool isSink=true)
Sets whether the edge is a sink.
Definition: ROEdge.h:135
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
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:109
static void close()
Closes all of an applications subsystems.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:61
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:50
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
void openOutput(const OptionsCont &options)
Opens the output for computed routes.
Definition: RONet.cpp:214
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary ...
Definition: RONet.cpp:253
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
Loader for the of turning percentages and source/sink definitions.
The data loader.
Definition: ROLoader.h:62
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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
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) ...
An edge the jtr-router may route through.
Definition: ROJTREdge.h:57
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:119
int main(int argc, char **argv)
#define VERSION_STRING
Definition: config.h:210
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static void setUsingJTRR()
Definition: RORouteDef.h:149
void initNet(RONet &net, ROLoader &loader, const std::vector< double > &turnDefs)
The router&#39;s network representation.
Definition: RONet.h:74
const NamedObjectCont< ROEdge * > & getEdgeMap() const
Definition: RONet.h:400
static void fillOptions()
Inserts options used by jtrrouter into the OptionsCont-singleton.
Definition: ROJTRFrame.cpp:51
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within jtrrouter...
Definition: ROJTRFrame.cpp:98
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:190
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:84
A storage for options typed value containers)
Definition: OptionsCont.h:98
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:76
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:163
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:144
static void initOutputOptions()
Definition: MsgHandler.cpp:192
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:56
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)