SUMO - Simulation of Urban MObility

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 (C) 2001-2011 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 "MSNet.h"
00033 #include "MSPerson.h"
00034 #include "MSVehicle.h"
00035 #include "MSPersonControl.h"
00036 #include <utils/iodevices/OutputDevice.h>
00037 #include <utils/options/OptionsCont.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 (OptionsCont::getOptions().isSet("tripinfo-output")) {
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 (OptionsCont::getOptions().isSet("vehroute-output")) {
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     const std::map<std::string, MSPerson*>::iterator i = myPersons.find(id);
00089     if (i != myPersons.end()) {
00090         delete i->second;
00091         myPersons.erase(i);
00092     }
00093 }
00094 
00095 void
00096 MSPersonControl::setArrival(const SUMOTime time, MSPerson* person) {
00097     const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
00098     if (myArrivals.find(step)==myArrivals.end()) {
00099         myArrivals[step] = PersonVector();
00100     }
00101     myArrivals[step].push_back(person);
00102 }
00103 
00104 
00105 void
00106 MSPersonControl::checkArrivedPersons(MSNet* net, const SUMOTime time) {
00107     while (myArrivals.find(time)!=myArrivals.end()) {
00108         const PersonVector& persons = myArrivals[time];
00109         // we cannot use an iterator here because there might be additions to the vector while proceeding
00110         for (size_t i=0; i < persons.size(); ++i) {
00111             persons[i]->proceed(net, time);
00112         }
00113         myArrivals.erase(time);
00114     }
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 bool
00128 MSPersonControl::boardAnyWaiting(const MSEdge* const edge, MSVehicle* vehicle) throw() {
00129     bool ret = false;
00130     if (myWaiting.find(edge) != myWaiting.end()) {
00131         PersonVector& waitPersons = myWaiting[edge];
00132         for (PersonVector::iterator i=waitPersons.begin(); i!=waitPersons.end();) {
00133             const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
00134             if ((*i)->isWaitingFor(line)) {
00135                 vehicle->addPerson(*i);
00136                 i = waitPersons.erase(i);
00137                 ret = true;
00138             } else {
00139                 ++i;
00140             }
00141         }
00142     }
00143     return ret;
00144 }
00145 
00146 
00147 bool
00148 MSPersonControl::hasPersons() const throw() {
00149     return !myPersons.empty();
00150 }
00151 
00152 
00153 bool
00154 MSPersonControl::hasPedestrians() const throw() {
00155     return !myArrivals.empty();
00156 }
00157 
00158 
00159 void
00160 MSPersonControl::abortWaiting() throw() {
00161     while (!myPersons.empty()) {
00162         std::map<std::string, MSPerson*>::iterator i=myPersons.begin();
00163         WRITE_WARNING("Person " + i->first + " aborted waiting for a ride that will never come.");
00164         erase(i->second);
00165     }
00166 }
00167 
00168 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines