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