NLBuilder.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // The main interface for loading a microsim
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 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 #include "NLBuilder.h"
00031 #include <microsim/MSNet.h>
00032 #include <microsim/MSEmitControl.h>
00033 #include <microsim/MSGlobals.h>
00034 #include <iostream>
00035 #include <vector>
00036 #include <xercesc/parsers/SAXParser.hpp>
00037 #include <xercesc/sax2/SAX2XMLReader.hpp>
00038 #include <string>
00039 #include <map>
00040 #include "NLHandler.h"
00041 #include "NLEdgeControlBuilder.h"
00042 #include "NLJunctionControlBuilder.h"
00043 #include "NLDetectorBuilder.h"
00044 #include "NLTriggerBuilder.h"
00045 #include <microsim/MSVehicleControl.h>
00046 #include <microsim/MSVehicleTransfer.h>
00047 #include <microsim/MSRouteLoaderControl.h>
00048 #include <microsim/MSRouteLoader.h>
00049 #include <utils/common/MsgHandler.h>
00050 #include <utils/common/StringTokenizer.h>
00051 #include <utils/options/Option.h>
00052 #include <utils/options/OptionsCont.h>
00053 #include <utils/common/TplConvert.h>
00054 #include <utils/common/FileHelpers.h>
00055 #include <utils/common/SysUtils.h>
00056 #include <utils/common/ToString.h>
00057 #include <utils/xml/XMLSubSys.h>
00058 #include <microsim/output/MSDetectorControl.h>
00059 #include <microsim/MSFrame.h>
00060 #include <microsim/MSEdgeWeightsStorage.h>
00061 #include <utils/iodevices/BinaryInputDevice.h>
00062 #include "NLGeomShapeBuilder.h"
00063 
00064 #ifdef CHECK_MEMORY_LEAKS
00065 #include <foreign/nvwa/debug_new.h>
00066 #endif // CHECK_MEMORY_LEAKS
00067 
00068 
00069 // ===========================================================================
00070 // method definitions
00071 // ===========================================================================
00072 // ---------------------------------------------------------------------------
00073 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeWeight - methods
00074 // ---------------------------------------------------------------------------
00075 void
00076 NLBuilder::EdgeFloatTimeLineRetriever_EdgeEffort::addEdgeWeight(const std::string &id,
00077         SUMOReal value, SUMOReal begTime, SUMOReal endTime) const throw() {
00078     MSEdge *edge = MSEdge::dictionary(id);
00079     if (edge!=0) {
00080         myNet.getWeightsStorage().addEffort(edge, begTime, endTime, value);
00081     } else {
00082         MsgHandler::getErrorInstance()->inform("Trying to set the effort for the unknown edge '" + id + "'.");
00083     }
00084 }
00085 
00086 
00087 // ---------------------------------------------------------------------------
00088 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
00089 // ---------------------------------------------------------------------------
00090 void
00091 NLBuilder::EdgeFloatTimeLineRetriever_EdgeTravelTime::addEdgeWeight(const std::string &id,
00092         SUMOReal value, SUMOReal begTime, SUMOReal endTime) const throw() {
00093     MSEdge *edge = MSEdge::dictionary(id);
00094     if (edge!=0) {
00095         myNet.getWeightsStorage().addTravelTime(edge, begTime, endTime, value);
00096     } else {
00097         MsgHandler::getErrorInstance()->inform("Trying to set the travel time for the unknown edge '" + id + "'.");
00098     }
00099 }
00100 
00101 
00102 // ---------------------------------------------------------------------------
00103 // NLBuilder - methods
00104 // ---------------------------------------------------------------------------
00105 NLBuilder::NLBuilder(OptionsCont &oc,
00106                      MSNet &net,
00107                      NLEdgeControlBuilder &eb,
00108                      NLJunctionControlBuilder &jb,
00109                      NLDetectorBuilder &db,
00110                      NLHandler &xmlHandler) throw()
00111         : myOptions(oc), myEdgeBuilder(eb), myJunctionBuilder(jb),
00112         myDetectorBuilder(db),
00113         myNet(net), myXMLHandler(xmlHandler) {}
00114 
00115 
00116 NLBuilder::~NLBuilder() throw() {}
00117 
00118 
00119 bool
00120 NLBuilder::build() throw(ProcessError) {
00121     // try to build the net
00122     if (!load("net-file")) {
00123         return false;
00124     }
00125     buildNet();
00126 #ifdef HAVE_MESOSIM
00127     // load the previous state if wished
00128     if (myOptions.isSet("load-state")) {
00129         long before = SysUtils::getCurrentMillis();
00130         BinaryInputDevice strm(myOptions.getString("load-state"));
00131         if (!strm.good()) {
00132             MsgHandler::getErrorInstance()->inform("Could not read state from '" + myOptions.getString("load-state") + "'!");
00133         } else {
00134             MsgHandler::getMessageInstance()->beginProcessMsg("Loading state from '" + myOptions.getString("load-state") + "'...");
00135             unsigned int step = myNet.loadState(strm);
00136             if (myOptions.isDefault("begin")) {
00137                 myOptions.set("begin", toString(step));
00138             }
00139             if (step != myOptions.getInt("begin")) {
00140                 WRITE_WARNING("State was written at a different time " + toString(step) + " than the begin time " + toString(myOptions.getInt("begin")) + "!");
00141             }
00142         }
00143         if (MsgHandler::getErrorInstance()->wasInformed()) {
00144             return false;
00145         }
00146         MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis()-before) + "ms).");
00147     }
00148 #endif
00149     // load weights if wished
00150     if (myOptions.isSet("weight-files")) {
00151         if (!myOptions.isUsableFileList("weight-files")) {
00152             return false;
00153         }
00154         // build and prepare the weights handler
00155         std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
00156         //  travel time, first (always used)
00157         EdgeFloatTimeLineRetriever_EdgeTravelTime ttRetriever(myNet);
00158         retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", true, ttRetriever));
00159         //  the measure to use, then
00160         EdgeFloatTimeLineRetriever_EdgeEffort eRetriever(myNet);
00161         std::string measure = myOptions.isSet("measure") ? myOptions.getString("measure") : "traveltime";
00162         if (measure!="traveltime") {
00163             std::string umeasure = measure;
00164             if (measure=="CO"||measure=="CO2"||measure=="HC"||measure=="PMx"||measure=="NOx"||measure=="fuel") {
00165                 umeasure = measure + "_perVeh";
00166             }
00167             retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, true, eRetriever));
00168         }
00169         //  set up handler
00170         SAXWeightsHandler handler(retrieverDefs, "");
00171         // start parsing; for each file in the list
00172         std::vector<std::string> files = myOptions.getStringVector("weight-files");
00173         for (std::vector<std::string>::iterator i=files.begin(); i!=files.end(); ++i) {
00174             // report about loading when wished
00175             WRITE_MESSAGE("Loading weights from '" + *i + "'...");
00176             // parse the file
00177             if (!XMLSubSys::runParser(handler, *i)) {
00178                 return false;
00179             }
00180         }
00181     }
00182     // load routes
00183     if (myOptions.isSet("route-files")&&myOptions.getInt("route-steps")<=0) {
00184         if (!load("route-files")) {
00185             return false;
00186         }
00187     }
00188     // load additional net elements (sources, detectors, ...)
00189     if (myOptions.isSet("additional-files")) {
00190         if (!load("additional-files")) {
00191             return false;
00192         }
00193     }
00194     WRITE_MESSAGE("Loading done.");
00195     return true;
00196 }
00197 
00198 
00199 void
00200 NLBuilder::buildNet() throw(ProcessError) {
00201     MSEdgeControl *edges = 0;
00202     MSJunctionControl *junctions = 0;
00203     MSRouteLoaderControl *routeLoaders = 0;
00204     MSTLLogicControl *tlc = 0;
00205     try {
00206         myJunctionBuilder.closeJunctions(myDetectorBuilder);
00207         edges = myEdgeBuilder.build();
00208         junctions = myJunctionBuilder.build();
00209         routeLoaders = buildRouteLoaderControl(myOptions);
00210         tlc = myJunctionBuilder.buildTLLogics();
00211         MSFrame::buildStreams();
00212         std::vector<SUMOTime> stateDumpTimes;
00213         std::vector<std::string> stateDumpFiles;
00214 #ifdef HAVE_MESOSIM
00215         const std::vector<int> times = myOptions.getIntVector("save-state.times");
00216         for (std::vector<int>::const_iterator i = times.begin(); i != times.end(); ++i) {
00217             stateDumpTimes.push_back(TIME2STEPS(*i));
00218         }
00219         if (!myOptions.isDefault("save-state.prefix")) {
00220             const std::string prefix = myOptions.getString("save-state.prefix");
00221             for (std::vector<SUMOTime>::iterator i = stateDumpTimes.begin(); i != stateDumpTimes.end(); ++i) {
00222                 stateDumpFiles.push_back(prefix + "_" + toString(*i) + ".bin");
00223             }
00224         } else {
00225             stateDumpFiles = StringTokenizer(myOptions.getString("save-state.files")).getVector() ;
00226         }
00227 #endif
00228         myNet.closeBuilding(edges, junctions, routeLoaders, tlc, stateDumpTimes, stateDumpFiles);
00229     } catch (IOError &e) {
00230         delete edges;
00231         delete junctions;
00232         delete routeLoaders;
00233         delete tlc;
00234         throw ProcessError(e.what());
00235     } catch (ProcessError &) {
00236         delete edges;
00237         delete junctions;
00238         delete routeLoaders;
00239         delete tlc;
00240         throw;
00241     }
00242 }
00243 
00244 
00245 bool
00246 NLBuilder::load(const std::string &mmlWhat) {
00247     if (!OptionsCont::getOptions().isUsableFileList(mmlWhat)) {
00248         return false;
00249     }
00250     std::vector<std::string> files = OptionsCont::getOptions().getStringVector(mmlWhat);
00251     for (std::vector<std::string>::const_iterator fileIt=files.begin(); fileIt!=files.end(); ++fileIt) {
00252         if (!gSuppressMessages) {
00253             MsgHandler::getMessageInstance()->beginProcessMsg("Loading " + mmlWhat + " from '" + *fileIt + "' ...");
00254         }
00255         long before = SysUtils::getCurrentMillis();
00256         if (!XMLSubSys::runParser(myXMLHandler, *fileIt)) {
00257             WRITE_MESSAGE("Loading of " + mmlWhat + " failed.");
00258             return false;
00259         }
00260         if (!gSuppressMessages) {
00261             MsgHandler::getMessageInstance()->endProcessMsg(" done (" + toString(SysUtils::getCurrentMillis()-before) + "ms).");
00262         }
00263     }
00264     return true;
00265 }
00266 
00267 
00268 MSRouteLoaderControl *
00269 NLBuilder::buildRouteLoaderControl(const OptionsCont &oc) throw(ProcessError) {
00270     // build the loaders
00271     MSRouteLoaderControl::LoaderVector loaders;
00272     // check whether a list is existing
00273     if (oc.isSet("route-files")&&oc.getInt("route-steps")>0) {
00274         std::vector<std::string> files = oc.getStringVector("route-files");
00275         for (std::vector<std::string>::const_iterator fileIt=files.begin(); fileIt!=files.end(); ++fileIt) {
00276             if (!FileHelpers::exists(*fileIt)) {
00277                 throw ProcessError("The route file '" + *fileIt + "' does not exist.");
00278             }
00279         }
00280         // open files for reading
00281         for (std::vector<std::string>::const_iterator fileIt=files.begin(); fileIt!=files.end(); ++fileIt) {
00282             loaders.push_back(new MSRouteLoader(myNet, new MSRouteHandler(*fileIt, false)));
00283         }
00284     }
00285     // build the route control
00286     return new MSRouteLoaderControl(myNet, oc.getInt("route-steps"), loaders);
00287 }
00288 
00289 
00290 /****************************************************************************/
00291 

Generated on Wed May 5 00:06:35 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6