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-2017 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 
67 // ===========================================================================
68 // functions
69 // ===========================================================================
70 /* -------------------------------------------------------------------------
71  * data processing methods
72  * ----------------------------------------------------------------------- */
78 void
79 initNet(RONet& net, ROLoader& loader,
80  const std::vector<double>& turnDefs) {
81  // load the net
82  ROJTREdgeBuilder builder;
83  loader.loadNet(net, builder);
84  // set the turn defaults
85  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
86  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
87  static_cast<ROJTREdge*>((*i).second)->setTurnDefaults(turnDefs);
88  }
89 }
90 
91 std::vector<double>
93  std::vector<double> ret;
94  std::vector<std::string> defs = oc.getStringVector("turn-defaults");
95  if (defs.size() < 2) {
96  throw ProcessError("The defaults for turnings must be a tuple of at least two numbers divided by ','.");
97  }
98  for (std::vector<std::string>::const_iterator i = defs.begin(); i != defs.end(); ++i) {
99  try {
100  double val = TplConvert::_2double((*i).c_str());
101  ret.push_back(val);
102  } catch (NumberFormatException&) {
103  throw ProcessError("A turn default is not numeric.");
104  }
105  }
106  return ret;
107 }
108 
109 
110 void
112  // load the turning definitions (and possible sink definition)
113  if (oc.isSet("turn-ratio-files")) {
114  ROJTRTurnDefLoader loader(net);
115  std::vector<std::string> ratio_files = oc.getStringVector("turn-ratio-files");
116  for (std::vector<std::string>::const_iterator i = ratio_files.begin(); i != ratio_files.end(); ++i) {
117  if (!XMLSubSys::runParser(loader, *i)) {
118  throw ProcessError();
119  }
120  }
121  }
122  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
124  }
125  // parse sink edges specified at the input/within the configuration
126  if (oc.isSet("sink-edges")) {
127  std::vector<std::string> edges = oc.getStringVector("sink-edges");
128  for (std::vector<std::string>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
129  ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
130  if (edge == 0) {
131  throw ProcessError("The edge '" + *i + "' declared as a sink is not known.");
132  }
133  edge->setFunc(ROEdge::ET_SINK);
134  }
135  }
136 }
137 
138 
142 void
144  // initialise the loader
145  loader.openRoutes(net);
146  // prepare the output
147  net.openOutput(oc);
148  // build the router
149  ROJTRRouter* router = new ROJTRRouter(oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
150  (int)(((double) net.getEdgeNo()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
151  oc.getBool("ignore-vclasses"), oc.getBool("allow-loops"));
155  loader.processRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")),
156  string2time(oc.getString("route-steps")), net, provider);
157  net.cleanup();
158 }
159 
160 
161 /* -------------------------------------------------------------------------
162  * main
163  * ----------------------------------------------------------------------- */
164 int
165 main(int argc, char** argv) {
167  // give some application descriptions
168  oc.setApplicationDescription("Router for the microscopic road traffic simulation SUMO based on junction turning ratios.");
169  oc.setApplicationName("jtrrouter", "SUMO jtrrouter Version " VERSION_STRING);
170  int ret = 0;
171  RONet* net = 0;
172  try {
173  // initialise the application system (messaging, xml, options)
174  XMLSubSys::init();
176  OptionsIO::setArgs(argc, argv);
178  if (oc.processMetaOptions(argc < 2)) {
180  return 0;
181  }
182  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
184  if (!ROJTRFrame::checkOptions()) {
185  throw ProcessError();
186  }
188  std::vector<double> defs = getTurningDefaults(oc);
189  // load data
190  ROLoader loader(oc, true, !oc.getBool("no-step-log"));
191  net = new RONet();
192  initNet(*net, loader, defs);
193  try {
194  // parse and set the turn defaults first
195  loadJTRDefinitions(*net, oc);
196  // build routes
197  computeRoutes(*net, loader, oc);
198  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
199  WRITE_ERROR(toString(e.getLineNumber()));
200  ret = 1;
201  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
202  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
203  ret = 1;
204  }
205  if (MsgHandler::getErrorInstance()->wasInformed()) {
206  throw ProcessError();
207  }
208  } catch (const ProcessError& e) {
209  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
210  WRITE_ERROR(e.what());
211  }
212  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
213  ret = 1;
214 #ifndef _DEBUG
215  } catch (const std::exception& e) {
216  if (std::string(e.what()) != std::string("")) {
217  WRITE_ERROR(e.what());
218  }
219  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
220  ret = 1;
221  } catch (...) {
222  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
223  ret = 1;
224 #endif
225  }
226  delete net;
228  if (ret == 0) {
229  std::cout << "Success." << std::endl;
230  }
231  return ret;
232 }
233 
234 
235 /****************************************************************************/
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
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:82
Computes routes using junction turning percentages.
Definition: ROJTRRouter.h:53
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:161
int getEdgeNo() const
Returns the total number of edges the network contains including internal edges.
Definition: RONet.cpp:647
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:665
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
std::vector< double > getTurningDefaults(OptionsCont &oc)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
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:110
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:62
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:51
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
void openOutput(const OptionsCont &options, const std::string altFilename="")
Opens the output for computed routes.
Definition: RONet.cpp:237
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:64
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:257
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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.
The data loader.
Definition: ROLoader.h:63
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:47
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:58
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:120
int main(int argc, char **argv)
#define VERSION_STRING
Definition: config.h:210
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static void setUsingJTRR()
Definition: RORouteDef.h:150
void initNet(RONet &net, ROLoader &loader, const std::vector< double > &turnDefs)
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:52
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within jtrrouter...
Definition: ROJTRFrame.cpp:99
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:197
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
const std::map< std::string, ROEdge * > & getEdgeMap() const
Definition: RONet.cpp:659
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:165
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:145
static void initOutputOptions()
Definition: MsgHandler.cpp:193
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57
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)