SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.sourceforge.net/
12 // Copyright (C) 2001-2012 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>
45 #include <router/ROFrame.h>
46 #include <router/ROLoader.h>
47 #include <router/RONet.h>
49 #include <utils/options/Option.h>
54 #include <utils/common/ToString.h>
57 #include <utils/xml/XMLSubSys.h>
58 #include "ROJTREdgeBuilder.h"
59 #include "ROJTRRouter.h"
60 #include "ROJTREdge.h"
61 #include "ROJTRTurnDefLoader.h"
62 #include "ROJTRFrame.h"
64 
65 #ifdef CHECK_MEMORY_LEAKS
66 #include <foreign/nvwa/debug_new.h>
67 #endif // CHECK_MEMORY_LEAKS
68 
69 
70 // ===========================================================================
71 // functions
72 // ===========================================================================
73 /* -------------------------------------------------------------------------
74  * data processing methods
75  * ----------------------------------------------------------------------- */
81 void
82 initNet(RONet& net, ROLoader& loader,
83  const std::vector<SUMOReal>& turnDefs) {
84  // load the net
85  ROJTREdgeBuilder builder;
86  loader.loadNet(net, builder);
87  // set the turn defaults
88  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
89  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
90  static_cast<ROJTREdge*>((*i).second)->setTurnDefaults(turnDefs);
91  }
92 }
93 
94 std::vector<SUMOReal>
96  std::vector<SUMOReal> ret;
97  std::vector<std::string> defs = oc.getStringVector("turn-defaults");
98  if (defs.size() < 2) {
99  throw ProcessError("The defaults for turnings must be a tuple of at least two numbers divided by ','.");
100  }
101  for (std::vector<std::string>::const_iterator i = defs.begin(); i != defs.end(); ++i) {
102  try {
103  SUMOReal val = TplConvert::_2SUMOReal((*i).c_str());
104  ret.push_back(val);
105  } catch (NumberFormatException&) {
106  throw ProcessError("A turn default is not numeric.");
107  }
108  }
109  return ret;
110 }
111 
112 
113 void
115  // load the turning definitions (and possible sink definition)
116  if (oc.isSet("turn-ratio-files")) {
117  ROJTRTurnDefLoader loader(net);
118  std::vector<std::string> ratio_files = oc.getStringVector("turn-ratio-files");
119  for (std::vector<std::string>::const_iterator i = ratio_files.begin(); i != ratio_files.end(); ++i) {
120  if (!XMLSubSys::runParser(loader, *i)) {
121  throw ProcessError();
122  }
123  }
124  }
125  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
127  }
128  // parse sink edges specified at the input/within the configuration
129  if (oc.isSet("sink-edges")) {
130  std::vector<std::string> edges = oc.getStringVector("sink-edges");
131  for (std::vector<std::string>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
132  ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
133  if (edge == 0) {
134  throw ProcessError("The edge '" + *i + "' declared as a sink is not known.");
135  }
136  edge->setType(ROEdge::ET_SINK);
137  }
138  }
139 }
140 
141 
145 void
147  // initialise the loader
148  loader.openRoutes(net);
149  // prepare the output
150  net.openOutput(oc.getString("output-file"), false, oc.getString("vtype-output"));
151  // build the router
152  ROJTRRouter router(net, oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
153  (int)(((SUMOReal) net.getEdgeNo()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
154  oc.getBool("ignore-vclasses"), oc.getBool("allow-loops"));
155  if (!oc.getBool("unsorted-input")) {
156  // the routes are sorted - process stepwise
157  loader.processRoutesStepWise(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, router);
158  } else {
159  // the routes are not sorted: load all and process
160  loader.processAllRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, router);
161  }
162  // end the processing
163  net.closeOutput();
164 }
165 
166 
167 /* -------------------------------------------------------------------------
168  * main
169  * ----------------------------------------------------------------------- */
170 int
171 main(int argc, char** argv) {
173  // give some application descriptions
174  oc.setApplicationDescription("Router for the microscopic road traffic simulation SUMO based on junction turning ratios.");
175  oc.setApplicationName("jtrrouter", "SUMO jtrrouter Version " + (std::string)VERSION_STRING);
176  int ret = 0;
177  RONet* net = 0;
178  try {
179  // initialise the application system (messaging, xml, options)
180  XMLSubSys::init();
182  OptionsIO::getOptions(true, argc, argv);
183  if (oc.processMetaOptions(argc < 2)) {
185  return 0;
186  }
187  XMLSubSys::setValidation(oc.getBool("xml-validation"));
189  if (!ROJTRFrame::checkOptions()) {
190  throw ProcessError();
191  }
193  std::vector<SUMOReal> defs = getTurningDefaults(oc);
194  // load data
195  ROLoader loader(oc, true);
196  net = new RONet();
197  initNet(*net, loader, defs);
198  try {
199  // parse and set the turn defaults first
200  loadJTRDefinitions(*net, oc);
201  // build routes
202  computeRoutes(*net, loader, oc);
203  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
204  WRITE_ERROR(toString(e.getLineNumber()));
205  ret = 1;
206  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
207  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
208  ret = 1;
209  }
210  if (MsgHandler::getErrorInstance()->wasInformed()) {
211  throw ProcessError();
212  }
213  } catch (const ProcessError& e) {
214  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
215  WRITE_ERROR(e.what());
216  }
217  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
218  ret = 1;
219 #ifndef _DEBUG
220  } catch (const std::exception& e) {
221  if (std::string(e.what()) != std::string("")) {
222  WRITE_ERROR(e.what());
223  }
224  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
225  ret = 1;
226  } catch (...) {
227  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
228  ret = 1;
229 #endif
230  }
231  delete net;
233  if (ret == 0) {
234  std::cout << "Success." << std::endl;
235  }
236  return ret;
237 }
238 
239 
240 
241 /****************************************************************************/