This repository was archived by the owner on Dec 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.cpp
More file actions
252 lines (232 loc) · 6.67 KB
/
factory.cpp
File metadata and controls
252 lines (232 loc) · 6.67 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
#pragma once
using namespace std;
#include "job.h"
#include "machine.h"
class Factory
{
ItemManager im;
OrderManager om;
TaskManager tm;
vector<Job> jobs;
vector<Machine> machines;
public:
Factory(ItemManager&& imTwo, OrderManager&& omTwo, TaskManager&& tmTwo) :im(move(imTwo)), om(move(omTwo)), tm(move(tmTwo)) {
//for every task in taskmanager, put it into machine vector
cout << "Start creating machines!" << endl;
for (size_t i = 0; i < tm.getSize(); i++){
Machine temp(*tm.find(tm.getName(i)), tm);
machines.push_back(temp);
machines[i].print(cout);
}
//find source machine, look for a machine that is a source AND not a sink (size_t index) set index to where the source machine is
size_t sourceIndex = 1000000;
bool sourceError = false;
for (size_t j = 0; j < machines.size(); j++){
if (!machines[j].isSink()){
if (machines[j].isSource()){
if (sourceIndex == 1000000){
sourceIndex = j;
}
}
}
}
//throw error if more than 1 source machine
//throw error if can't find any source machine
if (sourceIndex == 1000000){
throw string("Can't find any source machine");
}
else if (sourceError){
throw string("There is more than one source machine");
}
else{
cout << "Source machine is at index " << sourceIndex << endl;
}
//go through all orders in order manager
//push each order into the source machine in order
cout << "Start pushing orders through!" << endl;
for (size_t k = 0; k < om.size(); k++){
Job temporary(*om.find(k));
jobs.push_back(temporary);
}
for (size_t w = 0; w < jobs.size(); w++){
machines[sourceIndex].push(move(jobs[w]));
machines[sourceIndex].increaseInput();
}
//run each job until all jobs are done
while (machines[sourceIndex].getNoInputs() != 0){
machines[sourceIndex].run(im, machines);
bool currentJobComplete = false;
size_t indexSink;
Job temp;
//run current tracking job until it reaches sink
while (!currentJobComplete){
for (size_t zq = 0; zq < machines.size(); zq++){
while (!machines[zq].empty() && zq!=sourceIndex){
machines[zq].run(im, machines);
if (machines[zq].isSink()){
indexSink = zq;
currentJobComplete = true;
}
}
}
}
temp = *machines[indexSink].returnJob();
//check if the job was complete at sink
if (temp.complete()){
machines[sourceIndex].decreaseinput();
}
//if it isn't complete, push it back into the source machine at the back of the queue
else{
machines[sourceIndex].push(move(temp));
}
}
//run each job at the next machine
//create infinite loop + counter
//flag = true;
cout << "All jobs are complete, factory will now shut down. Goodbye!" << endl;
//go through each machine , if queue empty, break; (empty());
//set to false if you have jobs left to do
//loop for all machines, execute run(im, machines)
}
};
int validate(ItemManager &im, OrderManager &om, TaskManager &tm){
int errors = 0;
//check order, that items in order exist in itemlist
for (size_t h = 0; h < om.size(); h++){
for (size_t k = 0; k < om.itemSize(h); k++){
string item = "";
item = om.itemListItem(h, k);
bool itemCheck = false;
//cout << "Check item " << item <<" for customer " << om.getCustomer(h);
for (size_t j = 0; j < im.getSize(); j++){
if (im.find(item)->getName() == item){
itemCheck = true;
}
}
if (itemCheck){
//cout << " | Passed!";
}
else{
//cout << " | Failed, item not in item list.";
errors++;
}
//cout << endl;
}
}
//check items, that the installer remover exist in task
for (size_t i = 0; i < im.getSize(); i++){
string installer = im.getInstaller(i);
string remover = im.getRemover(i);
bool installerCheck = false;
bool removerCheck = false;
//cout << "Check installer/remover " << installer << "/"<<remover <<" for item " << im.getName(i);
for (size_t z = 0; z < tm.getSize(); z++){
if (tm.getName(z) == installer){
installerCheck = true;
}
if (tm.getName(z) == remover){
removerCheck = true;
}
}
if (installerCheck){
}
else{
errors++;
}
if (removerCheck){
}
else{
errors++;
}
if (installerCheck && removerCheck){
//cout << " | Passed!";
}
//cout << endl;
}
// task checks for accept task
for (size_t a = 0; a < tm.getSize(); a++){
string taskAccept = "";
bool acceptCheck = false;
if (tm.getAccept(a) != ""){
taskAccept = tm.getAccept(a);
}
else{
acceptCheck = true;
}
//cout << "Check accept " << taskAccept << " for task " << tm.getName(a) << endl;
for (size_t b = 0; b < tm.getSize(); b++){
if (tm.getName(b) == taskAccept){
acceptCheck = true;
}
//cout << "Compare accept task " << taskAccept << " with " << tm.getName(b) << endl;
}
if (!acceptCheck){
errors++;
//cout << "Accept error" << endl;
}
}
//task checks for reject task
for (size_t c = 0; c < tm.getSize(); c++){
string taskReject = "";
bool rejectCheck = false;
if (tm.getReject(c) != ""){
taskReject = tm.getReject(c);
}
else{
rejectCheck = true;
}
//cout << "Check reject " << taskReject << " for task " << tm.getName(c) << endl;
for (size_t d = 0; d < tm.getSize(); d++){
if (tm.getName(d) == taskReject){
rejectCheck = true;
}
//cout << "Compare reject task " << taskReject << " with " << tm.getName(d) << endl;
}
if (!rejectCheck){
errors++;
//cout << "Reject error" << endl;
}
}
return errors;
}
int main(int argc, char* argv[]){
if (argc != 5){
cerr << "Usage : " << argv[0] << "Item-csv order-csv task-csv csv-seperator\n";
return 1;
}
string itemFile = argv[1];
string orderFile = argv[2];
string taskFile = argv[3];
char seperator = argv[4][0];
try{
string f;
vector<vector<string>> ItemCsvData;
vector<vector<string>> OrderCsvData;
vector<vector<string>> TaskCsvData;
csvread(itemFile, seperator, ItemCsvData);
csvread(orderFile, seperator, OrderCsvData);
csvread(taskFile, seperator, TaskCsvData);
TaskManager tm(TaskCsvData);
tm.print();
f = taskFile + string(".gv");
tm.graph(f);
ItemManager im(ItemCsvData);
im.print();
f = itemFile + string(".gv");
im.graph(f);
OrderManager om(OrderCsvData);
om.print();
f = orderFile + string(".gv");
om.graph(f);
int errors = validate(im, om, tm);
if (errors){
throw string("Bad data files - fix and resubmit");
}
Factory factory(move(im), move(om), move(tm));
}
catch (string& e){
cerr << argv[0] << " : ";
cerr << e << "\n";
}
return 0;
}