-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.cpp
More file actions
224 lines (172 loc) · 5.92 KB
/
Simulation.cpp
File metadata and controls
224 lines (172 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/************************************************************
* SimLib simulation library for event-based simulations *
* Author: Martin Ubl (A16N0026P) *
* ublm@students.zcu.cz *
************************************************************/
#include <iostream>
#include "Simulation.h"
std::random_device Simulation::m_randDev;
Simulation::Simulation(std::ostream& logOutput)
: m_guidNext(1), m_logger(logOutput), m_simMode(SimulationMode::CONTINUOUS), m_simulationTime(0)
{
//
}
Simulation::~Simulation()
{
//
}
SimulationPtr Simulation::Create(std::ostream& logOutput)
{
auto ptr = std::make_shared<Simulation>(logOutput);
return ptr;
}
void Simulation::SetSimulationMode(SimulationMode mode)
{
m_simMode = mode;
}
SimulationMode Simulation::GetSimulationMode() const
{
return m_simMode;
}
Logger& Simulation::GetLogger()
{
return m_logger;
}
unsigned int Simulation::GetTrueRandomNumber()
{
return m_randDev();
}
void Simulation::Setup(CalendarPtr mainCalendar)
{
m_calendarList.push_back(mainCalendar);
m_logger(GetSimulationTime()) << "Setting up simulation";
}
void Simulation::AddCalendar(CalendarPtr nextCalendar)
{
m_calendarList.push_back(nextCalendar);
}
CalendarPtr Simulation::GetMainCalendar() const
{
if (m_calendarList.empty())
return nullptr;
return *(m_calendarList.begin());
}
simtime_t Simulation::GetSimulationTime() const
{
return m_simulationTime;
}
bool Simulation::IsRunning() const
{
return m_running;
}
void Simulation::Terminate(int64_t exitCode, SimulationObjectPtr initiator)
{
if (!IsRunning())
return;
m_running = false;
m_exitCode = exitCode;
m_exitInitiator = initiator;
m_logger(GetSimulationTime()) << "Simulation termination requested with code " << m_exitCode;
}
SimulationObjectPtr Simulation::GetTerminateInitiator() const
{
return m_exitInitiator;
}
int64_t Simulation::Run()
{
// step counter
size_t step = 0;
// by default, assume that everything's ok
m_exitCode = SimulationExitCode_OK;
m_running = true;
while (!m_calendarList.all_empty() && m_running)
{
step++;
// if stepped simulation mode is selected, wait for key press every step
if (m_simMode == SimulationMode::STEPPED)
{
m_logger(GetSimulationTime()) << "Step: " << step;
std::cin.get();
}
// retrieve head of calendar queues (all of calendars)
auto obj = m_calendarList.top();
m_calendarList.pop();
// set current simulation time
m_simulationTime = obj->GetNextSimTime();
m_logger(GetSimulationTime()) << "Object " << obj->GetGUID() << " (type: " << static_cast<int>(obj->GetType()) << ", class: " << obj->GetObjectClass() << ") fired";
// run scope
{
CalendarPtr cal = obj->GetCurrentCalendar();
// at first, clear schedule info from object, so the object is no longer considered "scheduled"
obj->ClearScheduleInfo();
// fire Run method
obj->Run();
// if the object have periodic schedule plan, perform planning
if (obj->HasPeriodicSchedule())
obj->NextPeriodicSchedule(cal);
}
}
m_running = false;
return m_exitCode;
}
uint64_t Simulation::AddObject(SimulationObjectPtr object)
{
// assign GUID
object->SetGUID(m_guidNext);
// add object to all maps; from this point, the GUID, type and class are fixed and should not be changed!
m_objectGuidMap[m_guidNext] = object;
m_objectTypeMap[object->GetType()].push_back(object);
m_objectClassMap[object->GetObjectClass()].push_back(object);
m_logger(GetSimulationTime()) << "Adding object (GUID: " << m_guidNext << ", type: " << static_cast<int>(object->GetType()) << ", class: " << object->GetObjectClass() << ")";
return m_guidNext++;
}
void Simulation::RemoveObjectFromList(SimulationObjectList& simList, SimulationObjectPtr object)
{
for (auto itr = simList.begin(); itr != simList.end(); )
{
if ((*itr)->GetGUID() == object->GetGUID())
itr = simList.erase(itr);
else
++itr;
}
}
void Simulation::RemoveObject(SimulationObjectPtr object)
{
m_logger(GetSimulationTime()) << "Removing object (GUID: " << object->GetGUID() << ", type: " << static_cast<int>(object->GetType()) << ", class: " << object->GetObjectClass() << ")";
if (m_objectGuidMap.find(object->GetGUID()) != m_objectGuidMap.end())
m_objectGuidMap.erase(object->GetGUID());
auto itr = m_objectTypeMap.find(object->GetType());
if (itr != m_objectTypeMap.end())
RemoveObjectFromList(itr->second, object);
auto itr2 = m_objectClassMap.find(object->GetObjectClass());
if (itr2 != m_objectClassMap.end())
RemoveObjectFromList(itr2->second, object);
}
SimulationObjectPtr Simulation::GetObjectByGUID(uint64_t guid) const
{
auto itr = m_objectGuidMap.find(guid);
if (itr != m_objectGuidMap.end())
return itr->second;
return nullptr;
}
SimulationObjectList Simulation::GetObjectsByType(SimulationObjectType type) const
{
auto itr = m_objectTypeMap.find(type);
if (itr != m_objectTypeMap.end())
return itr->second;
return SimulationObjectList();
}
SimulationObjectList Simulation::GetObjectsByClass(uint32_t objectClass) const
{
auto itr = m_objectClassMap.find(objectClass);
if (itr != m_objectClassMap.end())
return itr->second;
return SimulationObjectList();
}
SimulationObjectList Simulation::GetAllObjects() const
{
SimulationObjectList lst;
for (auto& pr : m_objectGuidMap)
lst.push_back(pr.second);
return lst;
}