Simpact Cyan
Population based event driven simulation using mNRM
populationevent.h
Go to the documentation of this file.
1 #ifndef POPULATIONEVENT_H
2 
3 #define POPULATIONEVENT_H
4 
9 #include "eventbase.h"
10 #include "population.h"
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <string>
14 #include <iostream>
15 
16 #define POPULATIONEVENT_MAXPERSONS 2
17 
18 class PersonBase;
19 
58 class PopulationEvent : public EventBase
59 {
60 public:
62 
64  PopulationEvent(PersonBase *pPerson);
65 
67  PopulationEvent(PersonBase *pPerson1, PersonBase *pPerson2);
68  ~PopulationEvent();
69 
70  void setEventID(int64_t id) { assert(m_eventID < 0); assert(id >= 0); m_eventID = id; }
71  int64_t getEventID() const { return m_eventID; }
72 
75  int getNumberOfPersons() const { assert(m_numPersons >= 0 && m_numPersons <= POPULATIONEVENT_MAXPERSONS); return (int)m_numPersons; }
76 
79  PersonBase *getPerson(int idx) const;
80 
83  virtual int getNumberOfOtherAffectedPersons() const { return 0; }
84 
87 
90  virtual PersonBase *getNextOtherAffectedPerson() { return 0; }
91 
94  virtual std::string getDescription(double tNow) const { return std::string("No description given"); }
95 
96  // TODO: shield these from the user somehow? These functions are used internally
97  // by the algorithm and should not be called directly by the user. */
98  void setEventIndex(PersonBase *pPerson, int idx);
99  int getEventIndex(PersonBase *pPerson) const;
100 
101  void setScheduledForRemoval() { m_scheduledForRemoval = true; }
102  bool isScheduledForRemoval() const { return m_scheduledForRemoval; }
103 
104  bool isNoLongerUseful();
105 protected:
110  virtual bool isUseless() { return false; }
111 private:
112  void commonConstructor();
113 
114  PersonBase *m_pPersons[POPULATIONEVENT_MAXPERSONS];
115  int m_eventIndex[POPULATIONEVENT_MAXPERSONS];
116  int8_t m_numPersons;
117 
118  bool m_scheduledForRemoval;
119  int64_t m_eventID;
120 };
121 
122 inline void PopulationEvent::setEventIndex(PersonBase *pPerson, int idx)
123 {
124  assert(pPerson != 0);
125  int num = m_numPersons;
126 
127  for (int i = 0 ; i < num ; i++)
128  {
129  assert(m_pPersons[i] != 0);
130 
131  if (pPerson == m_pPersons[i])
132  {
133  m_eventIndex[i] = idx;
134  return;
135  }
136  }
137 
138  std::cerr << "PopulationEvent::setEventIndex: Consistency error: invalid Person object in setEventIndex" << std::endl;
139  exit(-1);
140 }
141 
142 inline int PopulationEvent::getEventIndex(PersonBase *pPerson) const
143 {
144  assert(pPerson != 0);
145  int num = m_numPersons;
146 
147  for (int i = 0 ; i < num ; i++)
148  {
149  assert(m_pPersons[i] != 0);
150 
151  if (pPerson == m_pPersons[i])
152  return m_eventIndex[i];
153  }
154 
155  std::cerr << "PopulationEvent::getEventIndex: Consistency error: invalid Person object in getEventIndex" << std::endl;
156  exit(-1);
157 }
158 
159 #ifdef NDEBUG
160 inline PersonBase *PopulationEvent::getPerson(int idx) const
161 {
162  return m_pPersons[idx];
163 }
164 #endif
165 
166 #endif // POPULATIONEVENT_H
virtual PersonBase * getNextOtherAffectedPerson()
Retrieves an affected person and advaces the iteration (should return 0 if no more persons are affect...
Definition: populationevent.h:90
virtual bool isUseless()
This function can be used to inform the algorithm that an event is no longer of any use and should be...
Definition: populationevent.h:110
virtual void startOtherAffectedPersonIteration()
Starts the iteration of the other affected persons.
Definition: populationevent.h:86
int getNumberOfPersons() const
Returns the number of people specified during the creation of the event.
Definition: populationevent.h:75
PersonBase * getPerson(int idx) const
Returns a person that was specified when the event was constructed, where idx can range from 0 to Pop...
Definition: populationevent.cpp:74
This is the base class for events in population-based simulations which use the Population class...
Definition: populationevent.h:58
This is the base class for a person in a population-based simulation that used the Population class...
Definition: personbase.h:23
virtual std::string getDescription(double tNow) const
Returns a short description of the event, can be useful for logging/debugging purposes.
Definition: populationevent.h:94
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:60
virtual int getNumberOfOtherAffectedPersons() const
Returns the number of persons that are also affected by the firing of the event (see the detailed des...
Definition: populationevent.h:83