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