SUMO - Simulation of Urban MObility

sumo_main.cpp

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