SUMO - Simulation of Urban MObility

AGAdult.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Person in working age: can be linked to a work position.
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 "AGAdult.h"
00033 #include "AGWorkPosition.h"
00034 #include <utils/common/RandHelper.h>
00035 #include <iostream>
00036 
00037 
00038 // ===========================================================================
00039 // method definitions
00040 // ===========================================================================
00041 AGWorkPosition*
00042 AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition> *wps) throw() {
00043     size_t wpsIndex = 0;
00044 
00045     // TODO: Could end up in an endless loop
00046     do {
00047         wpsIndex = RandHelper::rand(wps->size());
00048     } while (wps->at(wpsIndex).isTaken());
00049 
00050     return &wps->at(wpsIndex);
00051 }
00052 
00053 /****************************************************************************/
00054 
00055 AGAdult::AGAdult(int age) throw()
00056     : AGPerson(age), work(0) {}
00057 
00058 /****************************************************************************/
00059 
00060 void
00061 AGAdult::print() const throw() {
00062     std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
00063 }
00064 
00065 /****************************************************************************/
00066 
00067 void
00068 AGAdult::tryToWork(SUMOReal rate, std::vector<AGWorkPosition>* wps) throw() {
00069     if (decide(rate)) {
00070         // Select the new work position before giving up the current one.
00071         // This avoids that the current one is the same as the new one.
00072         AGWorkPosition* newWork = randomFreeWorkPosition(wps);
00073 
00074         if (work != 0) {
00075             work->let();
00076         }
00077         work = newWork;
00078         work->take(this);
00079     } else {
00080         if (work != 0) {
00081             // Also sets work = 0 with the call back lostWorkPosition
00082             work->let();
00083         }
00084     }
00085 }
00086 
00087 /****************************************************************************/
00088 
00089 bool
00090 AGAdult::isWorking() const throw() {
00091     return (work != 0);
00092 }
00093 
00094 /****************************************************************************/
00095 
00096 void
00097 AGAdult::lostWorkPosition() throw() {
00098     work = 0;
00099 }
00100 
00101 /****************************************************************************/
00102 
00103 void
00104 AGAdult::resignFromWorkPosition() throw() {
00105     if (work != 0) {
00106         work->let();
00107     }
00108 }
00109 
00110 /****************************************************************************/
00111 
00112 const AGWorkPosition&
00113 AGAdult::getWorkPosition() const throw(std::runtime_error) {
00114     if (work != 0) {
00115         return *work;
00116     }
00117 
00118     else {
00119         throw(std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed."));
00120     }
00121 }
00122 
00123 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines