SUMO - Simulation of Urban MObility
AGAdult.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Person in working age: can be linked to a work position.
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 // activitygen module
00014 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
00015 /****************************************************************************/
00016 //
00017 //   This file is part of SUMO.
00018 //   SUMO is free software: you can redistribute it and/or modify
00019 //   it under the terms of the GNU General Public License as published by
00020 //   the Free Software Foundation, either version 3 of the License, or
00021 //   (at your option) any later version.
00022 //
00023 /****************************************************************************/
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include "AGAdult.h"
00036 #include "AGWorkPosition.h"
00037 #include <utils/common/RandHelper.h>
00038 #include <iostream>
00039 
00040 
00041 // ===========================================================================
00042 // method definitions
00043 // ===========================================================================
00044 AGWorkPosition*
00045 AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition> *wps) {
00046     size_t wpsIndex = 0;
00047 
00048     // TODO: Could end up in an endless loop
00049     do {
00050         wpsIndex = RandHelper::rand(wps->size());
00051     } while (wps->at(wpsIndex).isTaken());
00052 
00053     return &wps->at(wpsIndex);
00054 }
00055 
00056 /****************************************************************************/
00057 
00058 AGAdult::AGAdult(int age)
00059     : AGPerson(age), work(0) {}
00060 
00061 /****************************************************************************/
00062 
00063 void
00064 AGAdult::print() const {
00065     std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
00066 }
00067 
00068 /****************************************************************************/
00069 
00070 void
00071 AGAdult::tryToWork(SUMOReal rate, std::vector<AGWorkPosition>* wps) {
00072     if (decide(rate)) {
00073         // Select the new work position before giving up the current one.
00074         // This avoids that the current one is the same as the new one.
00075         AGWorkPosition* newWork = randomFreeWorkPosition(wps);
00076 
00077         if (work != 0) {
00078             work->let();
00079         }
00080         work = newWork;
00081         work->take(this);
00082     } else {
00083         if (work != 0) {
00084             // Also sets work = 0 with the call back lostWorkPosition
00085             work->let();
00086         }
00087     }
00088 }
00089 
00090 /****************************************************************************/
00091 
00092 bool
00093 AGAdult::isWorking() const {
00094     return (work != 0);
00095 }
00096 
00097 /****************************************************************************/
00098 
00099 void
00100 AGAdult::lostWorkPosition() {
00101     work = 0;
00102 }
00103 
00104 /****************************************************************************/
00105 
00106 void
00107 AGAdult::resignFromWorkPosition() {
00108     if (work != 0) {
00109         work->let();
00110     }
00111 }
00112 
00113 /****************************************************************************/
00114 
00115 const AGWorkPosition&
00116 AGAdult::getWorkPosition() const throw(std::runtime_error) {
00117     if (work != 0) {
00118         return *work;
00119     }
00120 
00121     else {
00122         throw(std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed."));
00123     }
00124 }
00125 
00126 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines