TraCIHandler.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
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 // included modules
00022 // ===========================================================================
00023 #include "TraCIHandler.h"
00024 
00025 #ifndef NO_TRACI
00026 
00027 #include "utils/options/OptionsCont.h"
00028 #include "utils/common/StdDefs.h"
00029 
00030 #ifdef CHECK_MEMORY_LEAKS
00031 #include <foreign/nvwa/debug_new.h>
00032 #endif // CHECK_MEMORY_LEAKS
00033 
00034 
00035 // ===========================================================================
00036 // method definitions
00037 // ===========================================================================
00038 namespace traci {
00039 
00040 TraCIHandler::TraCIHandler(const std::string& file)
00041 throw()
00042         :SUMOSAXHandler(file),
00043         totalVehicleCount(0),
00044         currentVehCount(0) {}
00045 
00046 TraCIHandler::~TraCIHandler()
00047 throw() {}
00048 
00049 void
00050 TraCIHandler::myStartElement(SumoXMLTag element, const SUMOSAXAttributes& attributes)
00051 throw(ProcessError) {
00052     if (element == SUMO_TAG_VEHICLE) {
00053         openVehicleTag(attributes);
00054     }
00055 }
00056 
00057 void
00058 TraCIHandler::myEndElement(SumoXMLTag element)
00059 throw(ProcessError) {
00060     if (element == SUMO_TAG_VEHICLE) {
00061         totalVehicleCount += currentVehCount;
00062     }
00063 }
00064 
00065 void
00066 TraCIHandler::openVehicleTag(const SUMOSAXAttributes& attributes) {
00067     int repNo;
00068     int period;
00069     int depart;
00070     OptionsCont& optCont = OptionsCont::getOptions();
00071     SUMOTime simStart = string2time(optCont.getString("begin"));
00072     SUMOTime simEnd = string2time(optCont.getString("end"));
00073     if(simEnd<0) {
00074         simEnd = SUMOTime_MAX;
00075     }
00076     // every found vehicle tag counts for one vehicle
00077     currentVehCount = 1;
00078     // read value for emit period and number (if any)
00079     try {
00080         repNo = attributes.getInt(SUMO_ATTR_REPNUMBER);
00081         period = attributes.getInt(SUMO_ATTR_PERIOD);
00082     } catch (...) {
00083         repNo = 0;
00084         period = 0;
00085     }
00086 
00087     // read depart time
00088     try {
00089         depart = attributes.getInt(SUMO_ATTR_DEPART);
00090     } catch (...) {
00091         // no depart time: error, don't count vehicle
00092         currentVehCount = 0;
00093 //      std::cerr << "no depart time, vehicle = 0" << std::endl;
00094         return;
00095     }
00096 
00097     while ((depart < simStart) && (repNo >= 0)) {
00098         depart += period;
00099         repNo--;
00100 //      std::cerr << "removing 1 vehicle from repno (depart before sim start)" << std::endl;
00101     }
00102 
00103     // don't count vehicles that depart / are emitted after the sim ends
00104     while (((depart + (repNo * period)) > simEnd) && (repNo >= 0)) {
00105         repNo--;
00106 //      std::cerr << "removing 1 vehicle from repno (depart after sim end)" << std::endl;
00107 
00108     }
00109 
00110     // add number of vehicles, that will be emitted until sim end, to total count
00111     currentVehCount += repNo;
00112 //  std::cerr << "result: " << currentVehCount << " vehicles" << std::endl;
00113 }
00114 
00115 int
00116 TraCIHandler::getTotalVehicleCount() {
00117     return totalVehicleCount;
00118 }
00119 
00120 void
00121 TraCIHandler::resetTotalVehicleCount() {
00122     totalVehicleCount = 0;
00123 }
00124 
00125 }
00126 
00127 #endif

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