SUMO - Simulation of Urban MObility
jtrrouter_main.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Main for JTRROUTER
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #ifdef HAVE_VERSION_H
00034 #include <version.h>
00035 #endif
00036 
00037 #include <xercesc/sax/SAXException.hpp>
00038 #include <xercesc/sax/SAXParseException.hpp>
00039 #include <utils/common/TplConvert.h>
00040 #include <iostream>
00041 #include <string>
00042 #include <limits.h>
00043 #include <ctime>
00044 #include <set>
00045 #include <router/ROFrame.h>
00046 #include <router/ROLoader.h>
00047 #include <router/RONet.h>
00048 #include <utils/common/MsgHandler.h>
00049 #include <utils/options/Option.h>
00050 #include <utils/options/OptionsCont.h>
00051 #include <utils/options/OptionsIO.h>
00052 #include <utils/common/UtilExceptions.h>
00053 #include <utils/common/SystemFrame.h>
00054 #include <utils/common/ToString.h>
00055 #include <utils/common/RandHelper.h>
00056 #include <utils/common/StringTokenizer.h>
00057 #include <utils/xml/XMLSubSys.h>
00058 #include "ROJTREdgeBuilder.h"
00059 #include "ROJTRRouter.h"
00060 #include "ROJTREdge.h"
00061 #include "ROJTRTurnDefLoader.h"
00062 #include "ROJTRFrame.h"
00063 #include <utils/iodevices/OutputDevice.h>
00064 
00065 #ifdef CHECK_MEMORY_LEAKS
00066 #include <foreign/nvwa/debug_new.h>
00067 #endif // CHECK_MEMORY_LEAKS
00068 
00069 
00070 // ===========================================================================
00071 // functions
00072 // ===========================================================================
00073 /* -------------------------------------------------------------------------
00074  * data processing methods
00075  * ----------------------------------------------------------------------- */
00081 void
00082 initNet(RONet& net, ROLoader& loader, OptionsCont& oc,
00083         const std::vector<SUMOReal> &turnDefs) {
00084     // load the net
00085     ROJTREdgeBuilder builder;
00086     loader.loadNet(net, builder);
00087     // set the turn defaults
00088     const std::map<std::string, ROEdge*> &edges = net.getEdgeMap();
00089     for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
00090         static_cast<ROJTREdge*>((*i).second)->setTurnDefaults(turnDefs);
00091     }
00092 }
00093 
00094 std::vector<SUMOReal>
00095 getTurningDefaults(OptionsCont& oc) {
00096     std::vector<SUMOReal> ret;
00097     std::vector<std::string> defs = oc.getStringVector("turn-defaults");
00098     if (defs.size() < 2) {
00099         throw ProcessError("The defaults for turnings must be a tuple of at least two numbers divided by ','.");
00100     }
00101     for (std::vector<std::string>::const_iterator i = defs.begin(); i != defs.end(); ++i) {
00102         try {
00103             SUMOReal val = TplConvert<char>::_2SUMOReal((*i).c_str());
00104             ret.push_back(val);
00105         } catch (NumberFormatException&) {
00106             throw ProcessError("A turn default is not numeric.");
00107         }
00108     }
00109     return ret;
00110 }
00111 
00112 
00113 void
00114 loadJTRDefinitions(RONet& net, OptionsCont& oc) {
00115     // load the turning definitions (and possible sink definition)
00116     if (oc.isSet("turn-ratio-files")) {
00117         ROJTRTurnDefLoader loader(net);
00118         std::vector<std::string> ratio_files = oc.getStringVector("turn-ratio-files");
00119         for (std::vector<std::string>::const_iterator i = ratio_files.begin(); i != ratio_files.end(); ++i) {
00120             if (!XMLSubSys::runParser(loader, *i)) {
00121                 throw ProcessError();
00122             }
00123         }
00124     }
00125     if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
00126         MsgHandler::getErrorInstance()->clear();
00127     }
00128     // parse sink edges specified at the input/within the configuration
00129     if (oc.isSet("sink-edges")) {
00130         std::vector<std::string> edges = oc.getStringVector("sink-edges");
00131         for (std::vector<std::string>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
00132             ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
00133             if (edge == 0) {
00134                 throw ProcessError("The edge '" + *i + "' declared as a sink is not known.");
00135             }
00136             edge->setType(ROEdge::ET_SINK);
00137         }
00138     }
00139 }
00140 
00141 
00145 void
00146 computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
00147     // initialise the loader
00148     loader.openRoutes(net);
00149     // prepare the output
00150     net.openOutput(oc.getString("output-file"), false, oc.getString("vtype-output"));
00151     // build the router
00152     ROJTRRouter router(net, oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
00153                        (int)(((SUMOReal) net.getEdgeNo()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
00154                        oc.getBool("ignore-vclasses"), oc.getBool("allow-loops"));
00155     if (!oc.getBool("unsorted-input")) {
00156         // the routes are sorted - process stepwise
00157         loader.processRoutesStepWise(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, router);
00158     } else {
00159         // the routes are not sorted: load all and process
00160         loader.processAllRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, router);
00161     }
00162     // end the processing
00163     net.closeOutput();
00164 }
00165 
00166 
00167 /* -------------------------------------------------------------------------
00168  * main
00169  * ----------------------------------------------------------------------- */
00170 int
00171 main(int argc, char** argv) {
00172     OptionsCont& oc = OptionsCont::getOptions();
00173     // give some application descriptions
00174     oc.setApplicationDescription("Router for the microscopic road traffic simulation SUMO based on junction turning ratios.");
00175     oc.setApplicationName("jtrrouter", "SUMO jtrrouter Version " + (std::string)VERSION_STRING);
00176     int ret = 0;
00177     RONet* net = 0;
00178     try {
00179         // initialise the application system (messaging, xml, options)
00180         XMLSubSys::init(false);
00181         ROJTRFrame::fillOptions();
00182         OptionsIO::getOptions(true, argc, argv);
00183         if (oc.processMetaOptions(argc < 2)) {
00184             OutputDevice::closeAll();
00185             SystemFrame::close();
00186             return 0;
00187         }
00188         MsgHandler::initOutputOptions();
00189         if (!ROJTRFrame::checkOptions()) {
00190             throw ProcessError();
00191         }
00192         RandHelper::initRandGlobal();
00193         std::vector<SUMOReal> defs = getTurningDefaults(oc);
00194         // load data
00195         ROLoader loader(oc, true);
00196         net = new RONet();
00197         initNet(*net, loader, oc, defs);
00198         try {
00199             // parse and set the turn defaults first
00200             loadJTRDefinitions(*net, oc);
00201             // build routes
00202             computeRoutes(*net, loader, oc);
00203         } catch (SAXParseException& e) {
00204             WRITE_ERROR(toString(e.getLineNumber()));
00205             ret = 1;
00206         } catch (SAXException& e) {
00207             WRITE_ERROR(TplConvert<XMLCh>::_2str(e.getMessage()));
00208             ret = 1;
00209         }
00210         if (MsgHandler::getErrorInstance()->wasInformed()) {
00211             throw ProcessError();
00212         }
00213     } catch (ProcessError& e) {
00214         if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
00215             WRITE_ERROR(e.what());
00216         }
00217         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00218         ret = 1;
00219 #ifndef _DEBUG
00220     } catch (...) {
00221         MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
00222         ret = 1;
00223 #endif
00224     }
00225     delete net;
00226     OutputDevice::closeAll();
00227     SystemFrame::close();
00228     if (ret == 0) {
00229         std::cout << "Success." << std::endl;
00230     }
00231     return ret;
00232 }
00233 
00234 
00235 
00236 /****************************************************************************/
00237 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines