SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AGAdult.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Person in working age: can be linked to a work position.
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2010-2014 DLR (http://www.dlr.de/) and contributors
14 // activitygen module
15 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include "AGAdult.h"
37 #include "AGWorkPosition.h"
39 #include <iostream>
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46 AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition>* wps) {
47  size_t wpsIndex = 0;
48 
49  // TODO: Could end up in an endless loop
50  do {
51  wpsIndex = RandHelper::rand(wps->size());
52  } while (wps->at(wpsIndex).isTaken());
53 
54  return &wps->at(wpsIndex);
55 }
56 
57 /****************************************************************************/
58 
60  : AGPerson(age), work(0) {}
61 
62 /****************************************************************************/
63 
64 void
65 AGAdult::print() const {
66  std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
67 }
68 
69 /****************************************************************************/
70 
71 void
72 AGAdult::tryToWork(SUMOReal rate, std::vector<AGWorkPosition>* wps) {
73  if (decide(rate)) {
74  // Select the new work position before giving up the current one.
75  // This avoids that the current one is the same as the new one.
76  AGWorkPosition* newWork = randomFreeWorkPosition(wps);
77 
78  if (work != 0) {
79  work->let();
80  }
81  work = newWork;
82  work->take(this);
83  } else {
84  if (work != 0) {
85  // Also sets work = 0 with the call back lostWorkPosition
86  work->let();
87  }
88  }
89 }
90 
91 /****************************************************************************/
92 
93 bool
95  return (work != 0);
96 }
97 
98 /****************************************************************************/
99 
100 void
102  work = 0;
103 }
104 
105 /****************************************************************************/
106 
107 void
109  if (work != 0) {
110  work->let();
111  }
112 }
113 
114 /****************************************************************************/
115 
116 const AGWorkPosition&
118  if (work != 0) {
119  return *work;
120  }
121 
122  else {
123  throw(std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed."));
124  }
125 }
126 
127 /****************************************************************************/