SUMO - Simulation of Urban MObility
MSPersonControl.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00010 // Stores all persons in the net and handles their waiting for cars.
00011 /****************************************************************************/
00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00014 /****************************************************************************/
00015 //
00016 //   This file is part of SUMO.
00017 //   SUMO is free software: you can redistribute it and/or modify
00018 //   it under the terms of the GNU General Public License as published by
00019 //   the Free Software Foundation, either version 3 of the License, or
00020 //   (at your option) any later version.
00021 //
00022 /****************************************************************************/
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #include <vector>
00035 #include <algorithm>
00036 #include "MSNet.h"
00037 #include "MSPerson.h"
00038 #include "MSVehicle.h"
00039 #include "MSPersonControl.h"
00040 #include <utils/iodevices/OutputDevice.h>
00041 #include <utils/options/OptionsCont.h>
00042 
00043 #ifdef CHECK_MEMORY_LEAKS
00044 #include <foreign/nvwa/debug_new.h>
00045 #endif // CHECK_MEMORY_LEAKS
00046 
00047 
00048 // ===========================================================================
00049 // method definitions
00050 // ===========================================================================
00051 MSPersonControl::MSPersonControl() {}
00052 
00053 
00054 MSPersonControl::~MSPersonControl() {
00055     for (std::map<std::string, MSPerson*>::iterator i = myPersons.begin(); i != myPersons.end(); ++i) {
00056         delete(*i).second;
00057     }
00058     myPersons.clear();
00059     myWaiting.clear();
00060 }
00061 
00062 
00063 bool
00064 MSPersonControl::add(const std::string& id, MSPerson* person) {
00065     if (myPersons.find(id) == myPersons.end()) {
00066         myPersons[id] = person;
00067         return true;
00068     }
00069     return false;
00070 }
00071 
00072 
00073 void
00074 MSPersonControl::erase(MSPerson* person) {
00075     const std::string& id = person->getID();
00076     if (OptionsCont::getOptions().isSet("tripinfo-output")) {
00077         OutputDevice& od = OutputDevice::getDeviceByOption("tripinfo-output");
00078         od.openTag("personinfo") << " id=\"" << id << "\" ";
00079         od << "depart=\"" << time2string(person->getDesiredDepart()) << "\">\n";
00080         person->tripInfoOutput(od);
00081         od.closeTag();
00082     }
00083     if (OptionsCont::getOptions().isSet("vehroute-output")) {
00084         OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
00085         od.openTag("person") << " id=\"" << id
00086                              << "\" depart=\"" << time2string(person->getDesiredDepart())
00087                              << "\" arrival=\"" << time2string(MSNet::getInstance()->getCurrentTimeStep())
00088                              << "\">\n";
00089         od.closeTag();
00090         od << "\n";
00091     }
00092     const std::map<std::string, MSPerson*>::iterator i = myPersons.find(id);
00093     if (i != myPersons.end()) {
00094         delete i->second;
00095         myPersons.erase(i);
00096     }
00097 }
00098 
00099 void
00100 MSPersonControl::setArrival(const SUMOTime time, MSPerson* person) {
00101     const SUMOTime step = time % DELTA_T == 0 ? time : (time / DELTA_T + 1) * DELTA_T;
00102     if (myArrivals.find(step) == myArrivals.end()) {
00103         myArrivals[step] = PersonVector();
00104     }
00105     myArrivals[step].push_back(person);
00106 }
00107 
00108 
00109 void
00110 MSPersonControl::checkArrivedPersons(MSNet* net, const SUMOTime time) {
00111     while (myArrivals.find(time) != myArrivals.end()) {
00112         const PersonVector& persons = myArrivals[time];
00113         // we cannot use an iterator here because there might be additions to the vector while proceeding
00114         for (size_t i = 0; i < persons.size(); ++i) {
00115             persons[i]->proceed(net, time);
00116         }
00117         myArrivals.erase(time);
00118     }
00119 }
00120 
00121 
00122 void
00123 MSPersonControl::addWaiting(const MSEdge* const edge, MSPerson* person) {
00124     if (myWaiting.find(edge) == myWaiting.end()) {
00125         myWaiting[edge] = std::vector<MSPerson*>();
00126     }
00127     myWaiting[edge].push_back(person);
00128 }
00129 
00130 
00131 bool
00132 MSPersonControl::boardAnyWaiting(const MSEdge* const edge, MSVehicle* vehicle) {
00133     bool ret = false;
00134     if (myWaiting.find(edge) != myWaiting.end()) {
00135         PersonVector& waitPersons = myWaiting[edge];
00136         for (PersonVector::iterator i = waitPersons.begin(); i != waitPersons.end();) {
00137             const std::string& line = vehicle->getParameter().line == "" ? vehicle->getParameter().id : vehicle->getParameter().line;
00138             if ((*i)->isWaitingFor(line)) {
00139                 vehicle->addPerson(*i);
00140                 i = waitPersons.erase(i);
00141                 ret = true;
00142             } else {
00143                 ++i;
00144             }
00145         }
00146     }
00147     return ret;
00148 }
00149 
00150 
00151 bool
00152 MSPersonControl::hasPersons() const {
00153     return !myPersons.empty();
00154 }
00155 
00156 
00157 bool
00158 MSPersonControl::hasPedestrians() const {
00159     return !myArrivals.empty();
00160 }
00161 
00162 
00163 void
00164 MSPersonControl::abortWaiting() {
00165     while (!myPersons.empty()) {
00166         std::map<std::string, MSPerson*>::iterator i = myPersons.begin();
00167         WRITE_WARNING("Person " + i->first + " aborted waiting for a ride that will never come.");
00168         erase(i->second);
00169     }
00170 }
00171 
00172 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines