-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICLoader.cpp
More file actions
278 lines (226 loc) · 10.7 KB
/
ICLoader.cpp
File metadata and controls
278 lines (226 loc) · 10.7 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// Created by liam on 11/04/16.
//
#include <iostream>
#include "ICLoader.h"
#include "logic.h"
#include "yaml-cpp/yaml.h"
#include <algorithm>
void Component::set(std::string inputName, bool state) {
Input* input = inputs.at(inputName);
input->set(state);
update(input);
}
Output* Component::getOutput(std::string outputName) {
return outputs.at(outputName);
}
void Component::update(Gate *startNode) {
// std::cout << "------------UPDATE BEGIN---------------" << std::endl;
std::deque<Gate*> queue;
std::deque<Gate*> list;
startNode->setGray();
queue.push_back(startNode);
Gate* current;
while (!queue.empty()) {
current = queue.front();
// std::cout << "Update Loop: Loop for " << current << std::endl;
list.push_back(current);
for (Gate* g: current->outputs) {
if (!g->isGray()) {
//std::cout << "Update: Adding Output" << std::endl;
queue.push_back(g);
g->setGray();
}
}
queue.pop_front();
// std::cout << "New queue size: " << queue.size() << " Empty? " << queue.empty() << std::endl;
}
//std::cout << "BFS queue has finshed with " << list.size() << " items." << std::endl;
// std::cout << "Update Queue: [";
// for (Gate* g : list) {
// std::cout << g->getName() << " ";
// }
// std::cout << "]" <<std::endl;
while (!list.empty()) {
current = list.front();
list.pop_front();
if (current->isGray()) {
// std::cout << "Loop with Gray node" <<std::endl;
}
Component::BFSUpdate(current, &list);
}
}
void Component::BFSUpdate(Gate *node, std::deque<Gate *> *queue) {
if (node->isGray()) {
//std::cout << "BFS Update: " << node->getName() << std::endl;
if (node->areParentsUpdated()) {
node->update();
//std::cout << "node updated, Gray? " << node->isGray() << std::endl;
for (Gate *output : node->outputs) {
//std::cout << " ";
Component::BFSUpdate(output, queue);
}
}
else {
// std::cout << "[skipped, parents not updated]" << std::endl;
}
return;
}
else {
return;
}
}
Component::Component(std::string name, std::string filename) {
//std::cout << "---------GRAPH GEN START-----------" << std::endl;
//std::cout << "Loading filename: " << filename << std::endl;
YAML::Node config = YAML::LoadFile(filename);
this->name = name;
for (YAML::const_iterator it=config["inputs"].begin(); it!=config["inputs"].end(); ++it) {
std::string inputName = it->as<std::string>();
// std::cout << "Adding " << name << " Input: " << inputName << std::endl;
std::pair<std::string, Input*> inp = std::pair<std::string, Input*>(it->as<std::string>(), new Input(name + ":" + inputName));
std::pair<std::string, Gate*> gate = std::pair<std::string, Input*>(it->as<std::string>(), new Input(name + ":" + inputName));
inputs.insert(inp);
gates.insert(inp);
}
for (YAML::const_iterator it=config["outputs"].begin(); it!=config["outputs"].end(); ++it) {
std::string outputName = it->as<std::string>();
// std::cout << "Adding Output: " << outputName << std::endl;
std::pair<std::string,Output*> out = std::pair<std::string, Output*>(it->as<std::string>(), new Output(name + ":" + outputName));
outputs.insert(out);
}
// std::cout << "Finding components" << std::endl;
std::map<std::string, Component> components;
for (YAML::const_iterator it=config["components"].begin(); it!=config["components"].end(); ++it) {
std::string gateName = it->first.as<std::string>();
std::string type = it->second.as<std::string>();
if (Gate::isGate(type)) {
// std::cout << "Adding Gate: " << gateName << " (Type: " << type << ")" << std::endl;
gates.insert(std::pair<std::string, Gate *>(gateName, Gate::factory(name + ":" + gateName, type)));
}
else {
// std::cout << "Adding component: " << gateName << " (Type: " << type << ")" << std::endl;
Component subcomponent (gateName, type + ".component"); //TODO File exists check
//subcomponent.printGraph();
components.insert(std::pair<std::string, Component>(gateName, subcomponent));
}
}
// std::cout << "Disolving components into gates" << std::endl;
// std::cout << name << " inputs: [";
// for (std::pair<std::string, Input*> currentComponent : inputs) {
// std::cout << currentComponent.first << " ";
// }
// std::cout << "]" <<std::endl;
for (std::pair<std::string, Component> currentComponent : components) {
std::string componentName = currentComponent.first;
Component currentComponentObj = currentComponent.second;
//std::cout << "Looping " << componentName << "'s Outputs" << std::endl;
for (auto it=config[componentName].begin(); it!=config[componentName].end(); ++it) {
std::string outputName = it->first.as<std::string>();
YAML::Node recevierList = it->second;
for (YAML::const_iterator it2=recevierList.begin(); it2 != recevierList.end(); ++it2) {
std::string receiverName = it2->first.as<std::string>();
std::string receiverPlug = it2->second.as<std::string>();
// bool isGate = gates.find(receiverName) != gates.end();
bool isGate = gates.find(receiverPlug) != gates.end();
// std::cout << receiverName << ":" << receiverPlug << " is a gate? " << isGate << std::endl;
if (isGate) {
// ALGO for connecting component to gate (Output Removal)
//std::cout << "Adding a Gate to the end of a component" << std::endl;
Output* outputObj = currentComponentObj.outputs.at(outputName);
Gate* receivingGate = gates.at(receiverName);
Gate* sendingGate = outputObj->getInputs().at(0);
sendingGate->setOutput(receivingGate);
sendingGate->deleteInput(outputObj);
}
else if (receiverName == "OUT") {
//ALGO fo connecting component to output
//std::cout << "Appending new output obj" << std::endl;
Output* outputObj = currentComponentObj.outputs.at(outputName);
//std::cout << "Attempting to find output at " << receiverPlug << " in component " << name << std::endl;
Output* receivingOuput = outputs.at(receiverPlug);
Gate* sendingGate = outputObj->getInputs().at(0);
sendingGate->setOutput(receivingOuput);
//std::cout << "Is outputobj in sendingGate's outputs? " << (std::find(sendingGate->outputs.begin(), sendingGate->outputs.end(), outputObj) != sendingGate->outputs.end()) << std::endl;
sendingGate->deleteOutput(outputObj);
}
else {
//ALGO for connecting component to another component
//std::cout << "attempting to find output name: " << outputName << std::endl;
Output* outputObj = currentComponentObj.outputs.at(outputName);
//std::cout << "attemping to find input obj " << receiverName << ":" << receiverPlug << std::endl;
Input* inputObj = components.at(receiverName).inputs.at(receiverPlug);
Gate* sendingGate = outputObj->getInputs().at(0);
for (Gate* inputsOutput : inputObj->getOutputs()) {
sendingGate->setOutput(inputsOutput);
inputsOutput->deleteInput(inputObj);
}
sendingGate->deleteOutput(outputObj);
}
}
}
}
//std::cout << "Looping over Gates" << std::endl;
for (std::pair<std::string, Gate*> g : gates) {
std::string gateName = g.first;
Gate* gateObj = g.second;
for (YAML::const_iterator it=config[gateName].begin(); it!=config[gateName].end(); ++it) {
std::string receiverName = it->first.as<std::string>();
std::string receiverPlug = it->second.as<std::string>();
//std::cout << "Finding if " << receiverName << " is a gate. Searching : \n[";
// for (std::pair<std::string, Gate*> g2 : gates) {
// std::cout << g2.first << " ";
// }
//std::cout << "]" << std::endl;
bool isGate = gates.find(receiverName) != gates.end();
Gate* sendingGate = gateObj;
if (isGate) {
// Add Gate to another gate
//std::cout << receiverName << " is a Gate" << std::endl;
Gate* receivingGate = gates.at(receiverName);
sendingGate->setOutput(receivingGate);
}
else if (receiverName == "OUT") {
// Add output obj to Gates outputs
//std::cout << receiverName << " is a Out" << std::endl;
Output* receivingOutput = outputs.at(receiverPlug);
sendingGate->setOutput(receivingOutput);
}
else {
// Add gate into component (remove Input obj)
// std::cout << receiverName << " is a Component" << std::endl;
Input* receivingInput = components.at(receiverName).inputs.at(receiverPlug);
for (Gate* inputsOutput : receivingInput->getOutputs()) {
sendingGate->setOutput(inputsOutput);
inputsOutput->deleteInput(receivingInput);
}
}
}
}
}
std::ostream &operator<<(std::ostream &os, const Component &component) {
// for (std::pair<std::string, Gate*> gate : component.inputs) { // change to gates for all gate
// os << gate.first << ": " << gate.second->getState() << std::endl;
// }
// os << std::endl;
for (std::pair<std::string, Gate*> gate : component.outputs) {
os << gate.first << ": " << gate.second->getState() << std::endl;
}
return os;
}
void Component::printGraph() {
for (std::pair<std::string, Gate*> g : gates) {
//std::cout << this->name << ":" << g.first << " [";
for (Gate* h : g.second->outputs) {
std::cout << h->getName() << " ";
}
std::cout << "]" << std::endl;
}
}
std::map<std::string, bool> Component::getOutputs() {
std::map<std::string, bool> ret;
for (std::pair<std::string, Output*> o : outputs) {
ret[o.first] = o.second->getState();
}
return ret;
}