SUMO - Simulation of Urban MObility
AGActivityGenHandler.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00011 // The handler for parsing the statistics file.
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 // activitygen module
00016 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
00017 /****************************************************************************/
00018 //
00019 //   This file is part of SUMO.
00020 //   SUMO is free software: you can redistribute it and/or modify
00021 //   it under the terms of the GNU General Public License as published by
00022 //   the Free Software Foundation, either version 3 of the License, or
00023 //   (at your option) any later version.
00024 //
00025 /****************************************************************************/
00026 
00027 
00028 // ===========================================================================
00029 // included modules
00030 // ===========================================================================
00031 #ifdef _MSC_VER
00032 #include <windows_config.h>
00033 #else
00034 #include <config.h>
00035 #endif
00036 
00037 #include "AGActivityGenHandler.h"
00038 #include <iostream>
00039 #include <utility>
00040 #include <map>
00041 #include <string>
00042 #include <utils/options/OptionsCont.h>
00043 #include <utils/common/MsgHandler.h>
00044 #include <utils/common/StringTokenizer.h>
00045 #include <utils/common/UtilExceptions.h>
00046 #include <utils/xml/SUMOSAXHandler.h>
00047 #include <utils/xml/SUMOXMLDefinitions.h>
00048 #include <router/RONet.h>
00049 #include "city/AGCity.h"
00050 #include "city/AGSchool.h"
00051 #include "city/AGPosition.h"
00052 #include "city/AGBusLine.h"
00053 
00054 #ifdef CHECK_MEMORY_LEAKS
00055 #include <foreign/nvwa/debug_new.h>
00056 #endif // CHECK_MEMORY_LEAKS
00057 
00058 using namespace std;
00059 
00060 // ===========================================================================
00061 // method definitions
00062 // ===========================================================================
00063 AGActivityGenHandler::AGActivityGenHandler(AGCity& city, RONet* net)
00064     : SUMOSAXHandler("sumo-stat"), net(net),
00065       myCity(city) {}
00066 
00067 
00068 AGActivityGenHandler::~AGActivityGenHandler() {}
00069 
00070 
00071 void
00072 AGActivityGenHandler::myStartElement(int element, const SUMOSAXAttributes& attrs)
00073 {
00074     try {
00075         switch (element) {
00076             case AGEN_TAG_GENERAL:
00077                 parseGeneralCityInfo(attrs);
00078                 break;
00079             case AGEN_TAG_STREET:
00080                 parseStreets(attrs);
00081                 break;
00082             case AGEN_TAG_WORKHOURS:
00083                 parseWorkHours();
00084                 break;
00085             case AGEN_TAG_OPENING:
00086                 parseOpeningHour(attrs);
00087                 break;
00088             case AGEN_TAG_CLOSING:
00089                 parseClosingHour(attrs);
00090                 break;
00091             case AGEN_TAG_SCHOOLS:
00092                 parseSchools();
00093                 break;
00094             case AGEN_TAG_SCHOOL:
00095                 parseSchool(attrs);
00096                 break;
00097             case AGEN_TAG_BUSSTATION:
00098                 parseBusStation(attrs);
00099                 break;
00100             case AGEN_TAG_BUSLINE:
00101                 parseBusLine(attrs);
00102                 break;
00103             case AGEN_TAG_STATIONS:
00104                 parseStations();
00105                 break;
00106             case AGEN_TAG_REV_STATIONS:
00107                 parseRevStations();
00108                 break;
00109             case AGEN_TAG_STATION:
00110                 parseStation(attrs);
00111                 break;
00112             case AGEN_TAG_FREQUENCY:
00113                 parseFrequency(attrs);
00114                 break;
00115             case AGEN_TAG_POPULATION:
00116                 parsePopulation();
00117                 break;
00118                 /*case AGEN_TAG_CHILD_ACOMP:
00119                     parseChildrenAccompaniment();
00120                     break;*/
00121             case AGEN_TAG_BRACKET:
00122                 parseBracket(attrs);
00123                 break;
00124             case AGEN_TAG_PARAM:
00125                 parseParameters(attrs);
00126                 break;
00127             case AGEN_TAG_ENTRANCE:
00128                 parseCityGates(attrs);
00129                 break;
00130             default:
00131                 break;
00132         }
00133     } catch (const exception& e) {
00134         throw ProcessError(e.what());
00135     }
00136 }
00137 
00138 
00139 void
00140 AGActivityGenHandler::parseGeneralCityInfo(const SUMOSAXAttributes& attrs) {
00141     try {
00142         myCity.statData.inhabitants = attrs.getInt(AGEN_ATTR_INHABITANTS);
00143         myCity.statData.households = attrs.getInt(AGEN_ATTR_HOUSEHOLDS);
00144         myCity.statData.limitAgeChildren = attrs.getIntSecure(AGEN_ATTR_CHILDREN, 18);
00145         myCity.statData.limitAgeRetirement = attrs.getIntSecure(AGEN_ATTR_RETIREMENT, 63);
00146         myCity.statData.carRate = attrs.getFloatSecure(AGEN_ATTR_CARS, 0.58);
00147         myCity.statData.unemployement = attrs.getFloatSecure(AGEN_ATTR_UNEMPLOYEMENT, 0.06);
00148         myCity.statData.maxFootDistance = attrs.getFloatSecure(AGEN_ATTR_MAX_FOOT_DIST, 300.0);
00149         myCity.statData.incomingTraffic = attrs.getIntSecure(AGEN_ATTR_IN_TRAFFIC, 0);
00150         myCity.statData.outgoingTraffic = attrs.getIntSecure(AGEN_ATTR_OUT_TRAFFIC, 0);
00151 
00152     } catch (const exception& e) {
00153         WRITE_ERROR("Error while parsing the element " +
00154                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_GENERAL) + ": " +
00155                     e.what());
00156         throw ProcessError();
00157     }
00158 }
00159 
00160 void
00161 AGActivityGenHandler::parseParameters(const SUMOSAXAttributes& attrs) {
00162     try {
00163         myCity.statData.carPreference = attrs.getFloatSecure(AGEN_ATTR_CARPREF, 0.0);
00164         myCity.statData.speedTimePerKm = attrs.getFloatSecure(AGEN_ATTR_CITYSPEED, 360.0);
00165         myCity.statData.freeTimeActivityRate = attrs.getFloatSecure(AGEN_ATTR_FREETIMERATE, 0.15);
00166         myCity.statData.uniformRandomTrafficRate = attrs.getFloatSecure(AGEN_ATTR_UNI_RAND_TRAFFIC, 0.0);
00167         myCity.statData.departureVariation = attrs.getFloatSecure(AGEN_ATTR_DEP_VARIATION, 0.0);
00168 
00169     } catch (const exception& e) {
00170         WRITE_ERROR("Error while parsing the element " +
00171                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_PARAM) + ": " +
00172                     e.what());
00173         throw ProcessError();
00174     }
00175 }
00176 
00177 void
00178 AGActivityGenHandler::parseStreets(const SUMOSAXAttributes& attrs) {
00179     try {
00180         SUMOReal pop = 0;
00181         SUMOReal work = 0;
00182 
00183         if (attrs.hasAttribute(AGEN_ATTR_POPULATION)) {
00184             pop = attrs.getFloat(AGEN_ATTR_POPULATION);
00185         }
00186         if (attrs.hasAttribute(AGEN_ATTR_OUT_WORKPOSITION)) {
00187             work = attrs.getFloat(AGEN_ATTR_OUT_WORKPOSITION);
00188         }
00189 
00190         AGStreet str(net->getEdge(attrs.getString(SUMO_ATTR_EDGE)), pop, work);
00191         myCity.streets.push_back(str);
00192 
00193     } catch (const exception& e) {
00194         WRITE_ERROR("Error while parsing the element " +
00195                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_STREET) + ": " +
00196                     e.what());
00197         throw ProcessError();
00198     }
00199 }
00200 
00201 void
00202 AGActivityGenHandler::parseCityGates(const SUMOSAXAttributes& attrs) {
00203     try {
00204         string edge = attrs.getString(SUMO_ATTR_EDGE);
00205         SUMOReal positionOnEdge = attrs.getFloat(SUMO_ATTR_POSITION);
00206         AGPosition posi(myCity.getStreet(edge), positionOnEdge);
00207         myCity.statData.incoming[(int)myCity.cityGates.size()] = attrs.getFloat(AGEN_ATTR_INCOMING);
00208         myCity.statData.outgoing[(int)myCity.cityGates.size()] = attrs.getFloat(AGEN_ATTR_OUTGOING);
00209         myCity.cityGates.push_back(posi);
00210 
00211     } catch (const exception& e) {
00212         WRITE_ERROR("Error while parsing the element " +
00213                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_CITYGATES) + ": " +
00214                     e.what());
00215         throw ProcessError();
00216     }
00217 }
00218 
00219 void
00220 AGActivityGenHandler::parseWorkHours() {
00221     myCurrentObject = "workHours";
00222 }
00223 
00224 void
00225 AGActivityGenHandler::parseOpeningHour(const SUMOSAXAttributes& attrs) {
00226     if (myCurrentObject == "workHours") {
00227         try {
00228             myCity.statData.beginWorkHours[attrs.getInt(AGEN_ATTR_HOUR)] = attrs.getFloat(AGEN_ATTR_PROP);
00229 
00230         } catch (const exception& e) {
00231             WRITE_ERROR("Error while parsing the element " +
00232                         SUMOXMLDefinitions::Tags.getString(AGEN_TAG_OPENING) + ": "
00233                         + e.what());
00234             throw ProcessError();
00235         }
00236     }
00237 }
00238 
00239 void
00240 AGActivityGenHandler::parseClosingHour(const SUMOSAXAttributes& attrs) {
00241     if (myCurrentObject == "workHours") {
00242         try {
00243             myCity.statData.endWorkHours[attrs.getInt(AGEN_ATTR_HOUR)] = attrs.getFloat(AGEN_ATTR_PROP);
00244 
00245         } catch (const exception& e) {
00246             WRITE_ERROR("Error while parsing the element " +
00247                         SUMOXMLDefinitions::Tags.getString(AGEN_TAG_CLOSING) + ": "
00248                         + e.what());
00249             throw ProcessError();
00250         }
00251     }
00252 }
00253 
00254 void
00255 AGActivityGenHandler::parseSchools() {
00256     myCurrentObject = "schools";
00257 }
00258 
00259 void
00260 AGActivityGenHandler::parseSchool(const SUMOSAXAttributes& attrs) {
00261     try {
00262         string edge = attrs.getString(SUMO_ATTR_EDGE);
00263         SUMOReal positionOnEdge = attrs.getFloat(SUMO_ATTR_POSITION);
00264         AGPosition posi(myCity.getStreet(edge), positionOnEdge);
00265         int beginAge = attrs.getInt(AGEN_ATTR_BEGINAGE);
00266         int endAge = attrs.getInt(AGEN_ATTR_ENDAGE);
00267         int capacity = attrs.getInt(AGEN_ATTR_CAPACITY);
00268         int openingHour = attrs.getInt(AGEN_ATTR_OPENING);
00269         int closingHour = attrs.getInt(AGEN_ATTR_CLOSING);
00270         AGSchool sch(capacity, posi, beginAge, endAge, openingHour, closingHour);
00271         myCity.schools.push_back(sch);
00272 
00273     } catch (const exception& e) {
00274         WRITE_ERROR("Error while parsing the element " +
00275                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_SCHOOL) + ": " +
00276                     e.what());
00277         throw ProcessError();
00278     }
00279 }
00280 
00281 void
00282 AGActivityGenHandler::parseBusStation(const SUMOSAXAttributes& attrs) {
00283     try {
00284         string edge = attrs.getString(SUMO_ATTR_EDGE);
00285         SUMOReal positionOnEdge = attrs.getFloat(SUMO_ATTR_POSITION);
00286         int id = attrs.getInt(SUMO_ATTR_ID);
00287         AGPosition posi(myCity.getStreet(edge), positionOnEdge);
00288         myCity.statData.busStations.insert(std::pair<int, AGPosition>(id, posi));
00289 
00290     } catch (const exception& e) {
00291         WRITE_ERROR("Error while parsing the element " +
00292                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_BUSSTATION) + ": " +
00293                     e.what());
00294         throw ProcessError();
00295     }
00296 }
00297 
00298 void
00299 AGActivityGenHandler::parseBusLine(const SUMOSAXAttributes& attrs) {
00300     try {
00301         myCurrentObject = "busLine";
00302         AGBusLine busL(attrs.getString(SUMO_ATTR_ID));
00303         busL.setMaxTripTime(attrs.getInt(AGEN_ATTR_MAX_TRIP_DURATION));
00304         myCity.busLines.push_front(busL);
00305         currentBusLine = &*myCity.busLines.begin();
00306 
00307     } catch (const exception& e) {
00308         WRITE_ERROR("Error while parsing the element " +
00309                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_BUSLINE) + ": " +
00310                     e.what());
00311         throw ProcessError();
00312     }
00313 }
00314 
00315 void
00316 AGActivityGenHandler::parseStations() {
00317     isRevStation = false;
00318 }
00319 
00320 void
00321 AGActivityGenHandler::parseRevStations() {
00322     isRevStation = true;
00323 }
00324 
00325 void
00326 AGActivityGenHandler::parseStation(const SUMOSAXAttributes& attrs) {
00327     if (myCurrentObject != "busLine") {
00328         return;
00329     }
00330 
00331     try {
00332         bool ok = true;
00333         int refID = attrs.hasAttribute(SUMO_ATTR_REFID)
00334                     ? attrs.getIntReporting(SUMO_ATTR_REFID, myCurrentObject.c_str(), ok)
00335                     : attrs.getIntReporting(SUMO_ATTR_REFID__DEPRECATED, myCurrentObject.c_str(), ok);
00336         if (!ok) {
00337             throw ProcessError();
00338         }
00339 
00340         if (!isRevStation) {
00341             currentBusLine->locateStation(myCity.statData.busStations.find(refID)->second);
00342         } else {
00343             currentBusLine->locateRevStation(myCity.statData.busStations.find(refID)->second);
00344         }
00345 
00346     } catch (const exception& e) {
00347         WRITE_ERROR("Error while parsing the element " +
00348                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_STATION) + ": " +
00349                     e.what());
00350         throw ProcessError();
00351     }
00352 }
00353 
00354 void
00355 AGActivityGenHandler::parseFrequency(const SUMOSAXAttributes& attrs) {
00356     if (myCurrentObject != "busLine") {
00357         return;
00358     }
00359 
00360     try {
00361         int beginB = attrs.getInt(SUMO_ATTR_BEGIN);
00362         int endB = attrs.getInt(SUMO_ATTR_END);
00363         int rateB = attrs.getInt(AGEN_ATTR_RATE);
00364         currentBusLine->generateBuses(beginB, endB, rateB);
00365 
00366     } catch (const exception& e) {
00367         WRITE_ERROR("Error while parsing the element " +
00368                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_FREQUENCY) + ": " +
00369                     e.what());
00370         throw ProcessError();
00371     }
00372 }
00373 
00374 void
00375 AGActivityGenHandler::parsePopulation() {
00376     myCurrentObject = "population";
00377 }
00378 
00379 void
00380 AGActivityGenHandler::parseBracket(const SUMOSAXAttributes& attrs) {
00381     try {
00382         int beginAge = attrs.getInt(AGEN_ATTR_BEGINAGE); //included in the bracket
00383         int endAge = attrs.getInt(AGEN_ATTR_ENDAGE); //NOT included in the bracket
00384         if (myCurrentObject == "population") {
00385             myCity.statData.population[endAge] = attrs.getInt(AGEN_ATTR_PEOPLENBR);
00386         }
00387 
00388     } catch (const exception& e) {
00389         WRITE_ERROR("Error while parsing the element " +
00390                     SUMOXMLDefinitions::Tags.getString(AGEN_TAG_BRACKET) + ": " +
00391                     e.what());
00392         throw ProcessError();
00393     }
00394 }
00395 
00396 /****************************************************************************/
00397 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines