SUMO - Simulation of Urban MObility

sumo_main.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00011 // Main for SUMO
00012 /****************************************************************************/
00013 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00014 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00015 /****************************************************************************/
00016 //
00017 //   This file is part of SUMO.
00018 //   SUMO is free software: you can redistribute it and/or modify
00019 //   it under the terms of the GNU General Public License as published by
00020 //   the Free Software Foundation, either version 3 of the License, or
00021 //   (at your option) any later version.
00022 //
00023 /****************************************************************************/
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #ifdef HAVE_VERSION_H
00036 #include <version.h>
00037 #endif
00038 
00039 #include <ctime>
00040 #include <string>
00041 #include <iostream>
00042 #include <microsim/MSNet.h>
00043 #include <microsim/MSRoute.h>
00044 #include <microsim/MSVehicleControl.h>
00045 #include <netload/NLBuilder.h>
00046 #include <netload/NLHandler.h>
00047 #include <netload/NLTriggerBuilder.h>
00048 #include <netload/NLEdgeControlBuilder.h>
00049 #include <netload/NLJunctionControlBuilder.h>
00050 #include <netload/NLDetectorBuilder.h>
00051 #include <utils/options/OptionsCont.h>
00052 #include <utils/options/OptionsIO.h>
00053 #include <utils/common/MsgHandler.h>
00054 #include <utils/common/SystemFrame.h>
00055 #include <utils/common/UtilExceptions.h>
00056 #include <utils/common/FileHelpers.h>
00057 #include <utils/common/StringTokenizer.h>
00058 #include <utils/common/ToString.h>
00059 #include <utils/xml/XMLSubSys.h>
00060 #include <microsim/MSFrame.h>
00061 #include <microsim/output/MSDetectorControl.h>
00062 #include <utils/iodevices/OutputDevice.h>
00063 
00064 #ifdef HAVE_MESOSIM
00065 #include <mesosim/MEVehicleControl.h>
00066 #endif
00067 
00068 #ifdef CHECK_MEMORY_LEAKS
00069 #include <foreign/nvwa/debug_new.h>
00070 #endif
00071 
00072 // ===========================================================================
00073 // functions
00074 // ===========================================================================
00075 /* -------------------------------------------------------------------------
00076  * data processing methods
00077  * ----------------------------------------------------------------------- */
00081 MSNet*
00082 load(OptionsCont& oc) {
00083     MSFrame::setMSGlobals(oc);
00084     MSVehicleControl* vc = 0;
00085 #ifdef HAVE_MESOSIM
00086     if (MSGlobals::gUseMesoSim) {
00087         vc = new MEVehicleControl();
00088     } else {
00089 #endif
00090         vc = new MSVehicleControl();
00091 #ifdef HAVE_MESOSIM
00092     }
00093 #endif
00094     MSNet* net = new MSNet(vc, new MSEventControl(),
00095                            new MSEventControl(), new MSEventControl());
00096     NLEdgeControlBuilder eb;
00097     NLDetectorBuilder db(*net);
00098     NLJunctionControlBuilder jb(*net, db);
00099     NLTriggerBuilder tb;
00100     NLHandler handler("", *net, db, tb, eb, jb);
00101     tb.setHandler(&handler);
00102     NLBuilder builder(oc, *net, eb, jb, db, handler);
00103     if (!builder.build()) {
00104         delete net;
00105         throw ProcessError();
00106     }
00107     return net;
00108 }
00109 
00110 
00111 /* -------------------------------------------------------------------------
00112  * main
00113  * ----------------------------------------------------------------------- */
00114 int
00115 main(int argc, char** argv) {
00116     OptionsCont& oc = OptionsCont::getOptions();
00117     // give some application descriptions
00118     oc.setApplicationDescription("A microscopic road traffic simulation.");
00119     oc.setApplicationName("sumo", "SUMO sumo Version " + (std::string)VERSION_STRING);
00120     int ret = 0;
00121     MSNet* net = 0;
00122     try {
00123         // initialise subsystems
00124         XMLSubSys::init(false);
00125         MSFrame::fillOptions();
00126         OptionsIO::getOptions(true, argc, argv);
00127         if (oc.processMetaOptions(argc < 2)) {
00128             SystemFrame::close();
00129             return 0;
00130         }
00131         MsgHandler::initOutputOptions();
00132         if (!MSFrame::checkOptions()) {
00133             throw ProcessError();
00134         }
00135         RandHelper::initRandGlobal();
00136         // load the net
00137         net = load(oc);
00138         if (net != 0) {
00139             SUMOTime begin = string2time(oc.getString("begin"));
00140             SUMOTime end = string2time(oc.getString("end"));
00141             // report the begin when wished
00142             WRITE_MESSAGE("Simulation started with time: " + time2string(begin));
00143             // simulate
00144             ret = net->simulate(begin, end);
00145             // report the end when wished
00146             WRITE_MESSAGE("Simulation ended at time: " + time2string(net->getCurrentTimeStep()));
00147         }
00148     } catch (ProcessError& e) {
00149         if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
00150             WRITE_ERROR(e.what());
00151         }
00152         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00153         ret = 1;
00154 #ifndef _DEBUG
00155     } catch (...) {
00156         MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
00157         ret = 1;
00158 #endif
00159     }
00160     delete net;
00161     OutputDevice::closeAll();
00162     SystemFrame::close();
00163     return ret;
00164 }
00165 
00166 
00167 
00168 /****************************************************************************/
00169 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines