This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
323 lines (259 loc) · 11.7 KB
/
main.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// #pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <boost/program_options.hpp>
// #include "rdf-types.h"
#include "rdf-io/reader.cpp"
#include "rdf-io/writer.cpp"
#include "buildingTriples2graph.cpp"
#include "djikstras.cpp"
#include "risk-calculation.cpp"
#include "RB-tree.cpp"
// #include <httplib.h>
using namespace std;
using namespace boost;
using namespace boost::program_options;
// This is the skolemizer id so that we
// can provide a unique identifier to each
// blank node that we are working with.
int skol_id = 0;
TriplesAndPrefixes ingestCliTriples(string file, variables_map vm)
{
if (vm.count(file))
{
try
{
return getTriples(vm[file].as<string>());
}
catch (int e)
{
throw error("Invalid file" + file + "; please fix");
};
}
else
{
throw error("Please specify the " + file + " in order to run this command");
};
}
int main(int argc, char *argv[])
{
// int t = stof("0.1");
// cout << t << endl;
// Declare the supported options.
options_description desc("Allowed options");
desc.add_options()("help,h", "produce help message")("building-file,b", value<string>(), "A .ttl file (to read) containing information about the architecture of the building")("person-file,p", value<string>(), "A .ttl file (to read) containing information the location of people in the building")("escape-file,e", value<string>(), "A .ttl file (to read/write) containing the escape paths to be taken by those in the building")("risk-file,s", value<string>(), "A .ttl file (to read/write) containing the escape paths to be taken by those in the building")("diagnostic-file,d", value<string>(), "A .txt file (to write) which contains diagonstics of running the algorithm")("calculate-paths,c", "Calculate the escape path to be taken")("calculate-risk,r", "Calculate the risk of catching covid")("run-diagnostics,i", "Run diagonstics on the program and output to the assigned .txt file");
variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
bool diagnostics = vm.count("run-diagnostics");
TriplesAndPrefixes building, person, escape, risk;
if (vm.count("help"))
{
cout << desc << endl;
return 1;
};
if (vm.count("unit-test"))
{
};
// Now we perform the first functionality of materialising
// the data - note that this operation is performed here as
cout << "Materializing building strucutre in memory (functionality 1) ..." << endl;
building = ingestCliTriples("building-file", vm);
GraphMatrix<string> graph = buildingsTriplesToGraph(building.triples);
cout << "Building triples materialized" << endl;
person = ingestCliTriples("person-file", vm);
cout << "Person triples materialized" << endl;
set<int> exits;
Map<string, RoomDimensions> dimensions;
for (Triple t : building.triples)
{
if (t[1] == "http://architecture#hasBuildingExit" && t[2] == "true")
{
exits.insert(graph.nameToId(t[0]));
}
else if (t[1] == "http://architecture#width") // Could just calc area directly
{
if (!dimensions.hasKey(t[0]))
{
dimensions.add(t[0], RoomDimensions(stoi(t[2]), 0));
}
else
{
dimensions.add(t[0], RoomDimensions(stoi(t[2]), dimensions.get(t[0]).length));
// RoomDimensions d = dimensions.get(t[0]);
// d.width = stof(t[2]);
};
}
else if (t[1] == "http://architecture#length")
{
if (!dimensions.hasKey(t[0]))
{
dimensions.add(t[0], RoomDimensions(0, stoi(t[2])));
}
else
{
// dimensions.get(t[0]).length = stof(t[0]);
// dimensions.get(t[0]).length = stof("0.1");
dimensions.add(t[0], RoomDimensions(dimensions.get(t[0]).width, stoi(t[2])));
// RoomDimensions d = dimensions.get(t[0]);
// d.length = stof(t[2]);
};
};
};
Map<int, int> path = shortestNeightbour(exits, graph);
// person = ingestCliTriples("person-file", vm);
// GraphMatrix<string> graph = buildingsTriplesToGraph(building.triples);
if (vm.count("calculate-paths"))
{
// building = ingestCliTriples("building-file", vm);
person = ingestCliTriples("person-file", vm);
// GraphMatrix<string> graph = buildingsTriplesToGraph(building.triples);
int skol_id = 0;
Map<string, int> locations;
set<string> people;
set<int> startIds;
Triples escapeTriples;
for (Triple p : person.triples)
{
if (p[1] == "http://architecture#located")
{
int id = graph.nameToId(p[2]);
locations.add(p[0], id);
startIds.insert(id);
people.insert(p[0]);
};
};
for (string p : people)
{
int location = locations.get(p);
if (path.hasKey(location))
{
escapeTriples.push_back({p,
"http://example.org/canEscape",
"\"true\""});
escapeTriples.push_back({p,
"http://example.org/escapeRoute",
"http://example.org/" + to_string(++skol_id)});
while (location != -1)
{
escapeTriples.push_back({"http://example.org/" + to_string(skol_id),
"http://example.org/path",
graph.idToName(location)});
escapeTriples.push_back({"http://example.org/" + to_string(skol_id),
"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
"http://example.org/" + to_string(++skol_id)});
location = path.get(location);
};
escapeTriples.push_back({"http://example.org/" + to_string(skol_id),
"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"});
}
else
{
escapeTriples.push_back({p,
"http://example.org/canEscape",
"\"false\""});
};
};
// Map<int, vector<int>> paths = multiStartMultiEnd<int>(startIds, exits, graph);
// for (string p : people)
// {
// int order = 0;
// vector<int> pathList = paths.get(locations.get(p));
// if (pathList.size() > 0)
// {
// escapeTriples.push_back({p,
// "http://example.org/canEscape",
// "true"});
// escapeTriples.push_back({p,
// "http://example.org/escapeRoute",
// "http://example.org/" + to_string(++skol_id)});
// for (int path : pathList)
// {
// escapeTriples.push_back({"http://example.org/" + to_string(skol_id),
// "http://example.org/path",
// graph.idToName(path)});
// escapeTriples.push_back({"http://example.org/" + to_string(skol_id),
// "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
// "http://example.org/" + to_string(++skol_id)});
// };
// escapeTriples.push_back({"http://example.org/" + to_string(skol_id),
// "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
// "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"});
// }
// else
// {
// escapeTriples.push_back({p,
// "http://example.org/canEscape",
// "'false'"});
// };
// };
if (!vm.count("escape-file"))
{
throw error("Please specify the escape-file in order to run this command");
};
string outFile = vm["escape-file"].as<string>();
// TODO: Change prefix map to our map
person.prefixes["rdfs"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
writeTriples(escapeTriples, person.prefixes, outFile);
// httplib::Client cli("http://www.ldf.fi/service/rdf-grapher");
// cli.Post("", "from=ttl", "to=png","rdf=")
//escape = ingestCliTriples("escape-file", vm);
};
// At this point we are able to assume that the escape file has been created by the
// previous functionality
// In additon we note that the shortest path mappings are also used in this functionality
if (vm.count("calculate-risk"))
{
Map<string, IndividualRiskDetails> individualRisks = personRisks(person.triples, graph);
cout << "individual risks have been generated" << endl;
individualRisks.print();
Map<int, RoomRisk> init = initRisk(graph, individualRisks);
Map<int, RoomDimensions> dims;
for (Pair<string, RoomDimensions> d : dimensions.getPairs())
{
dims.add(graph.nameToId(d.key), d.value);
};
Map<int, float> risks = generateCovidRisk(graph, init, path, dims);
RBtree<IndividualAndRisk> riskTree;
cout << "printing risks" << endl;
for (Pair<int, float> r : risks.getPairs())
{
cout << r.key << " " << r.value;
};
// Now we actually determine those most likely to be at risk of covid
for (Pair<string, IndividualRiskDetails> individual : individualRisks.getPairs())
{
cout << "inserting into risk tree" << endl;
cout << individual.key << " " << individual.value.risk << " " << individual.value.startLocation << " " << risks.hasKey(individual.value.startLocation) << endl;
cout << "the starting point risk is" << risks.get(individual.value.startLocation) << endl;
riskTree.insert(
IndividualAndRisk(
individual.key,
individual.value.risk + (1 - individual.value.risk) * risks.get(individual.value.startLocation)
));
};
vector<IndividualAndRisk> rs = riskTree.flat();
cout << "-------------------------------" << endl;
for (IndividualAndRisk r : rs)
{
cout << r.individual << " " << r.risk << endl;
};
cout << "--------------------------------" << endl;
for (IndividualAndRisk r : riskTree.getHighest(3))
{
cout << r.individual << " " << r.risk << endl;
};
// cout << riskTree.flat().size() << endl;
// Again we can argue that RB tree is better than just "live culling" as it allows
// us to generalise the software more easily
// We also need acknowledge that this process is just a heuristic
// and there are some slightly odd effects such as "reflecting" your
// own covid risk
cout << "inside calculate risk" << endl;
};
return 0;
};