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-2018 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 /****************************************************************************/
17 // Main for JTRROUTER
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #ifdef HAVE_VERSION_H
27 #include <version.h>
28 #endif
29 
30 #include <iostream>
31 #include <string>
32 #include <limits.h>
33 #include <ctime>
34 #include <set>
35 #include <xercesc/sax/SAXException.hpp>
36 #include <xercesc/sax/SAXParseException.hpp>
41 #include <utils/common/ToString.h>
45 #include <utils/options/Option.h>
48 #include <utils/xml/XMLSubSys.h>
49 #include <router/ROFrame.h>
50 #include <router/ROLoader.h>
51 #include <router/RONet.h>
52 #include <router/RORouteDef.h>
53 #include "ROJTREdgeBuilder.h"
54 #include "ROJTRRouter.h"
55 #include "ROJTREdge.h"
56 #include "ROJTRTurnDefLoader.h"
57 #include "ROJTRFrame.h"
58 
59 
60 // ===========================================================================
61 // functions
62 // ===========================================================================
63 /* -------------------------------------------------------------------------
64  * data processing methods
65  * ----------------------------------------------------------------------- */
71 void
72 initNet(RONet& net, ROLoader& loader,
73  const std::vector<double>& turnDefs) {
74  // load the net
75  ROJTREdgeBuilder builder;
76  loader.loadNet(net, builder);
77  // set the turn defaults
78  for (const auto& i : net.getEdgeMap()) {
79  static_cast<ROJTREdge*>(i.second)->setTurnDefaults(turnDefs);
80  }
81 }
82 
83 std::vector<double>
85  std::vector<double> ret;
86  std::vector<std::string> defs = oc.getStringVector("turn-defaults");
87  if (defs.size() < 2) {
88  throw ProcessError("The defaults for turnings must be a tuple of at least two numbers divided by ','.");
89  }
90  for (std::vector<std::string>::const_iterator i = defs.begin(); i != defs.end(); ++i) {
91  try {
92  double val = StringUtils::toDouble(*i);
93  ret.push_back(val);
94  } catch (NumberFormatException&) {
95  throw ProcessError("A turn default is not numeric.");
96  }
97  }
98  return ret;
99 }
100 
101 
102 void
104  // load the turning definitions (and possible sink definition)
105  if (oc.isSet("turn-ratio-files")) {
106  ROJTRTurnDefLoader loader(net);
107  std::vector<std::string> ratio_files = oc.getStringVector("turn-ratio-files");
108  for (std::vector<std::string>::const_iterator i = ratio_files.begin(); i != ratio_files.end(); ++i) {
109  if (!XMLSubSys::runParser(loader, *i)) {
110  throw ProcessError();
111  }
112  }
113  }
114  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
116  }
117  // parse sink edges specified at the input/within the configuration
118  if (oc.isSet("sink-edges")) {
119  std::vector<std::string> edges = oc.getStringVector("sink-edges");
120  for (std::vector<std::string>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
121  ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
122  if (edge == nullptr) {
123  throw ProcessError("The edge '" + *i + "' declared as a sink is not known.");
124  }
125  edge->setSink();
126  }
127  }
128 }
129 
130 
134 void
136  // initialise the loader
137  loader.openRoutes(net);
138  // prepare the output
139  net.openOutput(oc);
140  // build the router
141  ROJTRRouter* router = new ROJTRRouter(oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
142  (int)(((double) net.getEdgeNumber()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
143  oc.getBool("ignore-vclasses"), oc.getBool("allow-loops"));
147  loader.processRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")),
148  string2time(oc.getString("route-steps")), net, provider);
149  net.cleanup();
150 }
151 
152 
153 /* -------------------------------------------------------------------------
154  * main
155  * ----------------------------------------------------------------------- */
156 int
157 main(int argc, char** argv) {
159  // give some application descriptions
160  oc.setApplicationDescription("Router for the microscopic, multi-modal traffic simulation SUMO based on junction turning ratios.");
161  oc.setApplicationName("jtrrouter", "Eclipse SUMO jtrrouter Version " VERSION_STRING);
162  int ret = 0;
163  RONet* net = nullptr;
164  try {
165  // initialise the application system (messaging, xml, options)
166  XMLSubSys::init();
168  OptionsIO::setArgs(argc, argv);
170  if (oc.processMetaOptions(argc < 2)) {
172  return 0;
173  }
174  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
177  throw ProcessError();
178  }
180  std::vector<double> defs = getTurningDefaults(oc);
181  // load data
182  ROLoader loader(oc, true, !oc.getBool("no-step-log"));
183  net = new RONet();
184  initNet(*net, loader, defs);
185  try {
186  // parse and set the turn defaults first
187  loadJTRDefinitions(*net, oc);
188  // build routes
189  computeRoutes(*net, loader, oc);
190  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
191  WRITE_ERROR(toString(e.getLineNumber()));
192  ret = 1;
193  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
194  WRITE_ERROR(StringUtils::transcode(e.getMessage()));
195  ret = 1;
196  }
197  if (MsgHandler::getErrorInstance()->wasInformed()) {
198  throw ProcessError();
199  }
200  } catch (const ProcessError& e) {
201  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
202  WRITE_ERROR(e.what());
203  }
204  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
205  ret = 1;
206 #ifndef _DEBUG
207  } catch (const std::exception& e) {
208  if (std::string(e.what()) != std::string("")) {
209  WRITE_ERROR(e.what());
210  }
211  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
212  ret = 1;
213  } catch (...) {
214  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
215  ret = 1;
216 #endif
217  }
218  delete net;
220  if (ret == 0) {
221  std::cout << "Success." << std::endl;
222  }
223  return ret;
224 }
225 
226 
227 /****************************************************************************/
int getEdgeNumber() const
Returns the total number of edges the network contains including internal edges.
Definition: RONet.cpp:647
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
Computes routes using junction turning percentages.
Definition: ROJTRRouter.h:46
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:154
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:659
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:130
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:113
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:55
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:44
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8...
Definition: StringUtils.h:133
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:49
Loader for the of turning percentages and source/sink definitions.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
The data loader.
Definition: ROLoader.h:56
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:42
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 bool checkOptions()
checks shared options and sets StdDefs
An edge the jtr-router may route through.
Definition: ROJTREdge.h:51
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:113
int main(int argc, char **argv)
#define VERSION_STRING
Definition: config.h:207
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:247
static void setUsingJTRR()
Definition: RORouteDef.h:143
void initNet(RONet &net, ROLoader &loader, const std::vector< double > &turnDefs)
The router&#39;s network representation.
Definition: RONet.h:68
const NamedObjectCont< ROEdge * > & getEdgeMap() const
Definition: RONet.h:397
static void fillOptions()
Inserts options used by jtrrouter into the OptionsCont-singleton.
Definition: ROJTRFrame.cpp:45
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within jtrrouter...
Definition: ROJTRFrame.cpp:92
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:184
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:113
A storage for options typed value containers)
Definition: OptionsCont.h:92
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:72
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:157
void clear()
Clears information whether an error occurred previously.
Definition: MsgHandler.cpp:173
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:239
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
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)