Eclipse 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.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Main for JTRROUTER
21 /****************************************************************************/
22 #include <config.h>
23 
24 #ifdef HAVE_VERSION_H
25 #include <version.h>
26 #endif
27 
28 #include <iostream>
29 #include <string>
30 #include <limits.h>
31 #include <ctime>
32 #include <set>
33 #include <xercesc/sax/SAXException.hpp>
34 #include <xercesc/sax/SAXParseException.hpp>
39 #include <utils/common/ToString.h>
43 #include <utils/options/Option.h>
46 #include <utils/xml/XMLSubSys.h>
47 #include <router/ROFrame.h>
48 #include <router/ROLoader.h>
49 #include <router/RONet.h>
50 #include <router/RORouteDef.h>
51 #include "ROJTREdgeBuilder.h"
52 #include "ROJTRRouter.h"
53 #include "ROJTREdge.h"
54 #include "ROJTRTurnDefLoader.h"
55 #include "ROJTRFrame.h"
56 
57 // ===========================================================================
58 // method declaration
59 // ===========================================================================
60 
61 void initNet(RONet& net, ROLoader& loader, const std::vector<double>& turnDefs);
62 std::vector<double> getTurningDefaults(OptionsCont& oc);
63 void loadJTRDefinitions(RONet& net, OptionsCont& oc);
64 void computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc);
65 
66 // ===========================================================================
67 // method implementation
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(TL("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 = StringUtils::toDouble(*i);
99  ret.push_back(val);
100  } catch (NumberFormatException&) {
101  throw ProcessError(TL("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 (oc.getBool("sources-are-sinks") || oc.getBool("discount-sources")) {
121  // load all route-files and additional files to discover sink edges and flow discount values
122  ROJTRTurnDefLoader loader(net);
123  for (std::string fileOption : {
124  "route-files", "additional-files"
125  }) {
126  for (std::string file : oc.getStringVector(fileOption)) {
127  if (!XMLSubSys::runParser(loader, file)) {
128  throw ProcessError();
129  }
130  }
131  }
132  }
133 
134  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
136  }
137  // parse sink edges specified at the input/within the configuration
138  if (oc.isSet("sink-edges")) {
139  std::vector<std::string> edges = oc.getStringVector("sink-edges");
140  for (std::vector<std::string>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
141  ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
142  if (edge == nullptr) {
143  throw ProcessError(TLF("The edge '%' declared as a sink is not known.", *i));
144  }
145  edge->setSink();
146  }
147  }
148 }
149 
150 
154 void
156  // initialise the loader
157  loader.openRoutes(net);
158  // prepare the output
159  net.openOutput(oc);
160  // build the router
161  ROJTRRouter* router = new ROJTRRouter(oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
162  (int)(((double) net.getEdgeNumber()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
163  oc.getBool("ignore-vclasses"),
164  oc.getBool("allow-loops"),
165  oc.getBool("discount-sources"));
168  new ROIntermodalRouter(RONet::adaptIntermodalRouter, 0, 0, "dijkstra"), nullptr);
169  const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
170  loader.processRoutes(string2time(oc.getString("begin")), end,
171  string2time(oc.getString("route-steps")), net, provider);
172  net.cleanup();
173 }
174 
175 // -----------------------------------------------------------------------
176 // main
177 // -----------------------------------------------------------------------
178 
179 int
180 main(int argc, char** argv) {
182  oc.setApplicationDescription(TL("Router for the microscopic, multi-modal traffic simulation SUMO based on junction turning ratios."));
183  oc.setApplicationName("jtrrouter", "Eclipse SUMO jtrrouter Version " VERSION_STRING);
184  int ret = 0;
185  RONet* net = nullptr;
186  try {
187  // initialise the application system (messaging, xml, options)
188  XMLSubSys::init();
190  OptionsIO::setArgs(argc, argv);
192  if (oc.processMetaOptions(argc < 2)) {
194  return 0;
195  }
197  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
199  if (!ROJTRFrame::checkOptions()) {
200  throw ProcessError();
201  }
203  std::vector<double> defs = getTurningDefaults(oc);
204  // load data
205  ROLoader loader(oc, true, !oc.getBool("no-step-log"));
206  net = new RONet();
207  initNet(*net, loader, defs);
208  try {
209  // parse and set the turn defaults first
210  loadJTRDefinitions(*net, oc);
211  // build routes
212  computeRoutes(*net, loader, oc);
213  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
214  WRITE_ERROR(toString(e.getLineNumber()));
215  ret = 1;
216  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
217  WRITE_ERROR(StringUtils::transcode(e.getMessage()));
218  ret = 1;
219  }
220  if (MsgHandler::getErrorInstance()->wasInformed()) {
221  throw ProcessError();
222  }
223  } catch (const ProcessError& e) {
224  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
225  WRITE_ERROR(e.what());
226  }
227  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
228  ret = 1;
229 #ifndef _DEBUG
230  } catch (const std::exception& e) {
231  if (std::string(e.what()) != std::string("")) {
232  WRITE_ERROR(e.what());
233  }
234  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
235  ret = 1;
236  } catch (...) {
237  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
238  ret = 1;
239 #endif
240  }
241  delete net;
243  if (ret == 0) {
244  std::cout << "Success." << std::endl;
245  }
246  return ret;
247 }
248 
249 
250 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:41
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
#define SUMOTime_MAX
Definition: SUMOTime.h:34
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:92
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:154
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:316
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:213
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:74
void setSink(const bool isSink=true)
Sets whether the edge is a sink.
Definition: ROEdge.h:128
Interface for building instances of jtrrouter-edges.
An edge the jtr-router may route through.
Definition: ROJTREdge.h:48
static void fillOptions()
Inserts options used by jtrrouter into the OptionsCont-singleton.
Definition: ROJTRFrame.cpp:43
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within jtrrouter.
Definition: ROJTRFrame.cpp:96
Computes routes using junction turning percentages.
Definition: ROJTRRouter.h:43
Loader for the of turning percentages and source/sink definitions.
The data loader.
Definition: ROLoader.h:53
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:192
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:108
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:162
The router's network representation.
Definition: RONet.h:62
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:157
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary
Definition: RONet.cpp:340
void openOutput(const OptionsCont &options)
Opens the output for computed routes.
Definition: RONet.cpp:292
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:818
int getEdgeNumber() const
Returns the total number of edges the network contains including internal edges.
Definition: RONet.cpp:793
const NamedObjectCont< ROEdge * > & getEdgeMap() const
Definition: RONet.h:422
static void setUsingJTRR()
Definition: RORouteDef.h:142
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:87
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
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:146
static void close()
Closes all of an applications subsystems.
static bool checkOptions(OptionsCont &oc)
checks shared options and sets StdDefs
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:83
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:56
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:148
void initNet(RONet &net, ROLoader &loader, const std::vector< double > &turnDefs)
int main(int argc, char **argv)
void loadJTRDefinitions(RONet &net, OptionsCont &oc)
std::vector< double > getTurningDefaults(OptionsCont &oc)
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)