MSPersonControl.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Stores all persons in the net and handles their waiting for cars.
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 <vector>
00031 #include <algorithm>
00032 #include "MSCORN.h"
00033 #include "MSNet.h"
00034 #include "MSPerson.h"
00035 #include "MSVehicle.h"
00036 #include "MSPersonControl.h"
00037 #include <utils/iodevices/OutputDevice.h>
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // method definitions
00046 // ===========================================================================
00047 MSPersonControl::MSPersonControl() {}
00048 
00049 
00050 MSPersonControl::~MSPersonControl() {
00051     for (std::map<std::string, MSPerson*>::iterator i=myPersons.begin(); i!=myPersons.end(); ++i) {
00052         delete(*i).second;
00053     }
00054     myPersons.clear();
00055     myWaiting.clear();
00056 }
00057 
00058 
00059 bool
00060 MSPersonControl::add(const std::string &id, MSPerson *person) {
00061     if (myPersons.find(id) == myPersons.end()) {
00062         myPersons[id] = person;
00063         return true;
00064     }
00065     return false;
00066 }
00067 
00068 
00069 void
00070 MSPersonControl::erase(MSPerson *person) {
00071     const std::string &id = person->getID();
00072     if (MSCORN::wished(MSCORN::CORN_OUT_TRIPDURATIONS)) {
00073         OutputDevice& od = OutputDevice::getDeviceByOption("tripinfo-output");
00074         od.openTag("personinfo") << " id=\"" << id << "\" ";
00075         od << "depart=\"" << time2string(person->getDesiredDepart()) << "\">\n";
00076         person->tripInfoOutput(od);
00077         od.closeTag();
00078     }
00079     if (MSCORN::wished(MSCORN::CORN_OUT_VEHROUTES)) {
00080         OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
00081         od.openTag("person") << " id=\"" << id
00082         << "\" depart=\"" << time2string(person->getDesiredDepart())
00083         << "\" arrival=\"" << time2string(MSNet::getInstance()->getCurrentTimeStep())
00084         << "\">\n";
00085         od.closeTag();
00086         od << "\n";
00087     }
00088     if (myPersons.find(id) != myPersons.end()) {
00089         delete myPersons[id];
00090         myPersons.erase(id);
00091     }
00092 }
00093 
00094 void
00095 MSPersonControl::setArrival(const SUMOTime time, MSPerson *person) {
00096     const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
00097     if (myArrivals.find(step)==myArrivals.end()) {
00098         myArrivals[step] = PersonVector();
00099     }
00100     myArrivals[step].push_back(person);
00101 }
00102 
00103 
00104 bool
00105 MSPersonControl::hasArrivedPersons(SUMOTime time) const {
00106     return myArrivals.find(time)!=myArrivals.end();
00107 }
00108 
00109 
00110 const MSPersonControl::PersonVector
00111 MSPersonControl::popArrivedPersons(SUMOTime time) {
00112     MSPersonControl::PersonVector arrived = myArrivals[time];
00113     myArrivals.erase(time);
00114     return arrived;
00115 }
00116 
00117 
00118 void
00119 MSPersonControl::addWaiting(const MSEdge* const edge, MSPerson *person) throw() {
00120     if (myWaiting.find(edge) == myWaiting.end()) {
00121         myWaiting[edge] = std::vector<MSPerson*>();
00122     }
00123     myWaiting[edge].push_back(person);
00124 }
00125 
00126 
00127 void
00128 MSPersonControl::checkWaiting(const MSEdge* const edge, MSVehicle *vehicle) throw() {
00129     if (myWaiting.find(edge) != myWaiting.end()) {
00130         PersonVector &waitPersons = myWaiting[edge];
00131         for (PersonVector::iterator i=waitPersons.begin(); i!=waitPersons.end();) {
00132             const std::string &line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
00133             if ((*i)->isWaitingFor(line)) {
00134                 vehicle->addPerson(*i);
00135                 i = waitPersons.erase(i);
00136             } else {
00137                 ++i;
00138             }
00139         }
00140     }
00141 }
00142 
00143 
00144 /****************************************************************************/

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