-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualization_simulator.cc
191 lines (162 loc) · 6.3 KB
/
visualization_simulator.cc
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
/**
* @file visualization_simulator.cc
*
* @copyright 2019 3081 Staff, All rights reserved.
*/
#include <iostream>
#include "src/visualization_simulator.h"
#include "src/bus.h"
#include "src/route.h"
#include "src/bus_factory.h"
VisualizationSimulator::VisualizationSimulator(WebInterface* webI,
ConfigManager* configM, std::ostream* out) {
webInterface_ = webI;
configManager_ = configM;
paused_ = false;
out_ = out;
}
VisualizationSimulator::~VisualizationSimulator() {
}
void VisualizationSimulator::TogglePause() {
std::cout << "Toggling Pause" << std::endl;
paused_ = !paused_;
}
void VisualizationSimulator::Start(const std::vector<int>& busStartTimings,
const int& numTimeSteps) {
busStartTimings_ = busStartTimings;
numTimeSteps_ = numTimeSteps;
timeSinceLastBus_.resize(busStartTimings_.size());
for (int i = 0; i < static_cast<int>(timeSinceLastBus_.size()); i++) {
timeSinceLastBus_[i] = 0;
}
simulationTimeElapsed_ = 0;
prototypeRoutes_ = configManager_->GetRoutes();
for (int i = 0; i < static_cast<int>(prototypeRoutes_.size()); i++) {
prototypeRoutes_[i]->Report(*out_);
prototypeRoutes_[i]->UpdateRouteData();
webInterface_->UpdateRoute(prototypeRoutes_[i]->GetRouteData());
}
}
bool VisualizationSimulator::Update() {
// Code called to update simulator. Will first check if
// we are in a state where we can update (e.g., not paused, not finished)
// then call ExecuteUpdate() to actually call update if possible
// return whether or not ExecuteUpdate() was called
bool can_update = CanUpdate();
if (can_update) {
ExecuteUpdate();
}
return can_update;
}
bool VisualizationSimulator::CanUpdate() {
// Check whether or not simulator can update
// maybe unable to update because paused, terminated, etcetra
// only cares about whether or not it is paused right now
return !paused_;
}
void VisualizationSimulator::ExecuteUpdate() {
// This function has the same text as what Update() used to have
// I added a gating mechanism for pause functionality
simulationTimeElapsed_++;
std::cout << "~~~~~~~~~~ The time is now " << simulationTimeElapsed_;
std::cout << "~~~~~~~~~~" << std::endl;
std::cout << "~~~~~~~~~~ Generating new busses if needed ";
std::cout << "~~~~~~~~~~" << std::endl;
// Check if we need to generate new busses
Route * my_outbound = prototypeRoutes_[0];
Route * my_inbound = prototypeRoutes_[1];
BusFactory* MyFactory = new BusFactory(std::to_string(busId),
my_outbound->Clone(),my_inbound->Clone(),30);
for (int i = 0; i < static_cast<int>(timeSinceLastBus_.size()); i++) {
// Check if we need to make a new bus
if (0 >= timeSinceLastBus_[i]) {
Route * outbound = prototypeRoutes_[2 * i];
Route * inbound = prototypeRoutes_[2 * i + 1];
// BusFactory* MyFactory = new BusFactory ("A_BUS", outbound,
// inbound, 30.0);
// busses_.push_back(new Bus(std::to_string(busId),
// outbound->Clone(), inbound->Clone(), 60, 1));
busses_.push_back(MyFactory->Generate(std::to_string(busId)));
// busses_.push_back(MyFactory->Generate("small"));
busId++;
timeSinceLastBus_[i] = busStartTimings_[i];
} else {
timeSinceLastBus_[i]--;
}
}
std::cout << "~~~~~~~~~ Updating busses ";
std::cout << "~~~~~~~~~" << std::endl;
// Update busses
for (int i = static_cast<int>(busses_.size()) - 1; i >= 0; i--) {
busses_[i]->Update();
if (busses_[i]->IsTripComplete()) {
webInterface_->UpdateBus(busses_[i]->GetBusData(), true);
busses_.erase(busses_.begin() + i);
continue;
}
webInterface_->UpdateBus(busses_[i]->GetBusData());
busses_[i]->Report(*out_);
busses_[i]->Report(mystream);
std::vector<std::string> data_vector = myUtil->processOutput(mystream);
FileWriter* other_writer = myWriter->getInstance();
other_writer->Write(bus_stats_file_name, data_vector);
}
std::cout << "~~~~~~~~~ Updating routes ";
std::cout << "~~~~~~~~~" << std::endl;
// Update routes
for (int i = 0; i < static_cast<int>(prototypeRoutes_.size()); i++) {
prototypeRoutes_[i]->Update();
webInterface_->UpdateRoute(prototypeRoutes_[i]->GetRouteData());
prototypeRoutes_[i]->Report(*out_);
}
/*for (int i = 0; i < static_cast<int>(prototypeRoutes_.size()); i++) {
std::list<Stop* > stop_list = prototypeRoutes_[i]->GetStops();
for(std::list<Stop *>::iterator iter2 = stop_list.begin();
iter2 != stop_list.end(); ++iter2){
(*iter2)->Update2();
webInterface_->Update
}
}*/
}
void VisualizationSimulator::ClearBusListeners() {
for (int i = 0; i < busses_.size(); i++){
busses_[i]->ClearObservers();
}
}
void VisualizationSimulator::AddBusListener(std::string * id,
IObserver<BusData*> * observer) {
// Find the Bus with the given ID and attach an Observer to it
for (int i = 0; i < busses_.size(); i++) {
if (busses_[i]->GetBusData().id.compare(*id)) {
std::cout <<"we attatched a listener" <<endl;
busses_[i]->RegisterObserver(observer);
return;
}
}
}
void VisualizationSimulator::ClearStopListeners() {
for (vector<Route*>::const_iterator iter = prototypeRoutes_.begin();
iter != prototypeRoutes_.end(); ++iter) {
std::list<Stop* > stop_list = (*iter)->GetStops();
for (std::list<Stop *>::iterator iter2 = stop_list.begin();
iter2 != stop_list.end(); ++iter2) {
(*iter2)->ClearObservers();
}
}
}
void VisualizationSimulator::AddStopListener(std::string * id,
IObserver<StopData*> * observer) {
// Find the Stop with the given ID and attach an Observer to it
for (vector<Route*>::const_iterator iter = prototypeRoutes_.begin();
iter != prototypeRoutes_.end(); ++iter) {
std::list<Stop* > stop_list = (*iter)->GetStops();
for (std::list<Stop *>::iterator iter2 = stop_list.begin();
iter2 != stop_list.end(); ++iter2) {
if ((*iter2)->GetStopData().id.compare(*id)) {
std::cout <<"we attatched a listener" << endl;
(*iter2)->RegisterObserver(observer);
return;
}
}
}
}