-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.h
252 lines (238 loc) · 10.7 KB
/
Interpreter.h
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
//
// Created by Matthew on 7/19/2022.
//
#ifndef CS236_INTERPRETER_H
#define CS236_INTERPRETER_H
#include "DatalogProgram.h"
#include "Database.h"
#include "Relation.h"
#include <iostream>
#include "Graph.h"
class Interpreter {
private:
DatalogProgram datalogProgram;
Database database;
public:
explicit Interpreter(const DatalogProgram &datalogProgram) : datalogProgram(datalogProgram) {
// make a relation for each scheme-predicate and put that relation in the database
// make a tuple for each fact-predicate and put that tuple in the appropriate relation in the database
// make a relation for each scheme and add it to the database
for (Predicate scheme: datalogProgram.getSchemes()) {
Relation relation;
relation.setName(scheme.getName());
Header header;
for (const Parameter ¶meter: scheme.getParameters()) {
header.push_back(parameter.getValue());
}
relation.setHeader(header);
database.addRelation(relation);
}
// make a tuple for each fact and add it to the database
for (Predicate fact: datalogProgram.getFacts()) {
Tuple tuple;
for (const Parameter ¶meter: fact.getParameters()) {
tuple.push_back(parameter.getValue());
}
database.addTuple(fact.getName(), tuple);
}
}
void run() {
//evalRules();
evalRulesOptimized();
cout << "\nQuery Evaluation\n";
evalQueries();
}
void evalRulesOptimized() {
/*****************************************************************
* Build the dependency graph and the reverse dependency graph
* Run DFS-Forest (in regular numeric order) on the reverse dependency graph to get the post-order
* Run DFS-Forest (in reverse post-order) on the forward dependency graph to find the strongly connected components
* Evaluate the rules in each component
****************************************************************/
Graph graph;
// make a node for each rule
for (unsigned int i = 0; i < datalogProgram.getRules().size(); i++)
graph.addNode();
/* for each rule in order:
* for each predicate p in the body of the rule
* find every rule r whose head matches p
* add the index r to the set of edges */
for (unsigned int rule = 0; rule < datalogProgram.getRules().size(); rule++) {
for (const Predicate& predicate: datalogProgram.getRules().at(rule).getBody()) {
for (unsigned int i = 0; i < datalogProgram.getRules().size(); i++) {
if (predicate.getName() == datalogProgram.getRules().at(i).getHead().getName())
graph.editNode(rule, i);
}
}
}
cout << "Dependency Graph\n";
cout << graph.toString();
Graph reverseGraph = graph;
reverseGraph.flipItAndReverseIt();
vector<int> postOrder = reverseGraph.dfsForest(reverseGraph);
graph.dfsForestSCC(postOrder);
vector<set<int>> SCCs = graph.getSCCs();
// determine if any rule is self dependent
for (unsigned int i = 0; i < graph.getEdges().size(); i++) {
for (unsigned int it:graph.getEdges().at(i)) {
// any of the things in j == i, the corresponding rule is self-dependent
// cout << "it = " << it << endl;
if (i == it) {
datalogProgram.getRules().at(it).setSelfDependent();
}
}
}
// make sets of SCCs
vector<vector<Rule>> setOfRules;
for (const auto& it:SCCs) {
vector<Rule> temp;
for (auto itt:it) {
temp.push_back(datalogProgram.getRules().at(itt));
}
setOfRules.push_back(temp);
temp.clear();
}
// evaluate rules
cout << "\nRule Evaluation\n";
int scc = 0;
for (auto & setOfRule : setOfRules) {
cout << "SCC: ";
string sep;
for (auto it: SCCs.at(scc)) {
cout << sep << "R" << it;
sep = ",";
}
cout << endl;
bool changesMade = true;
bool skip = false;
int n = 0;
while (changesMade && !skip) {
skip = false;
changesMade = false;
for (auto rule : setOfRule) {
if (setOfRule.size() == 1 && !setOfRule.at(0).isSelfDependent())
skip = true;
cout << rule.toString() << endl;
vector<Relation *> results;
// Evaluate the predicates on the right-hand side of the rule:
for (const Predicate &predicate: rule.getBody())
results.push_back(evalPredicate(predicate));
// Join the relations that result:
Relation *result = results[0];
if (results.size() > 1) { // nothing to join if there is only one result
for (unsigned int i = 0; i < results.size() - 1; i++)
result = result->naturalJoin(results[i + 1]);
}
// remove the columns that don't appear in the head of the rule from the result and reorder to match the head
vector<unsigned int> colsToKeep;
vector<string> names;
for (unsigned int i = 0; i < rule.getHead().getParameters().size(); i++) {
for (unsigned int j = 0; j < result->getHeader().size(); j++) {
if (rule.getHead().getParameters().at(i).getValue() == result->getHeader().at(j)) {
colsToKeep.push_back(j);
names.push_back(rule.getHead().getParameters().at(i).getValue());
}
}
}
result = result->project(colsToKeep);
result = result->rename(
database.getRelation(rule.getHead().getName())->getHeader().getAttributes());
pair<bool, set<Tuple>> foo;
foo = database.getRelation(rule.getHead().getName())->unionize(result);
if (foo.first)
changesMade = true;
if (!foo.second.empty()) {
for (const Tuple& t: foo.second)
cout << " " << t.toString(result->getHeader()) << endl;
}
}
n++;
} // while
cout << n << " passes: ";
sep = "";
for (auto it: SCCs.at(scc)) {
cout << sep << "R" << it;
sep = ",";
}
n = 0;
cout << endl;
scc++;
} // for each SCC
}
void evalRules() {
int n = 0;
bool changesMade = true;
while (changesMade) {
changesMade = false;
for (Rule rule: datalogProgram.getRules()) {
cout << rule.toString() << endl;
vector<Relation *> results;
// Evaluate the predicates on the right-hand side of the rule:
for (const Predicate& predicate: rule.getBody())
results.push_back(evalPredicate(predicate));
// Join the relations that result:
Relation *result = results[0];
if (results.size() > 1) { // nothing to join if there is only one result
for (unsigned int i = 0; i < results.size() - 1; i++)
result = result->naturalJoin(results[i + 1]);
}
// remove the columns that don't appear in the head of the rule from the result and reorder to match the head
vector<unsigned int> colsToKeep;
vector<string> names;
for (unsigned int i = 0; i < rule.getHead().getParameters().size(); i++) {
for (unsigned int j = 0; j < result->getHeader().size(); j++) {
if (rule.getHead().getParameters().at(i).getValue() == result->getHeader().at(j)) {
colsToKeep.push_back(j);
names.push_back(rule.getHead().getParameters().at(i).getValue());
}
}
}
result = result->project(colsToKeep);
result = result->rename(database.getRelation(rule.getHead().getName())->getHeader().getAttributes());
pair<bool, set<Tuple>> foo;
foo = database.getRelation(rule.getHead().getName())->unionize(result);
if (foo.first)
changesMade = true;
if (!foo.second.empty()) {
for (Tuple t: foo.second)
cout << " " << t.toString(result->getHeader()) << endl;
}
}
n++;
}
cout << endl << "Schemes populated after " << n << " passes through the Rules.\n";
}
void evalQueries() {
for (Predicate query: datalogProgram.getQueries()) {
Relation *result = evalPredicate(query);
int x = result->numTuples();
cout << query.toString() << "? "
<< (result->numTuples() > 0 ? "Yes(" + to_string(x) + ")\n" + result->toString() : "No\n");
}
}
Relation *evalPredicate(Predicate predicate) {
map<string, unsigned int> seenParameters;
vector<unsigned int> indexes;
vector<string> newHeader;
Relation *output = database.getRelation(predicate.getName());
unsigned int i = 0;
for (const Parameter ¶meter: predicate.getParameters()) {
if (parameter.isConst()) {
output = output->select(i, parameter.getValue());
} else {
if (seenParameters.find(parameter.getValue()) != seenParameters.end()) { // we've seen this parameter
output = output->select(seenParameters.at(parameter.getValue()), i);
} else { // we've not seen the parameter
seenParameters.insert(pair<string, unsigned int>(parameter.getValue(), i));
indexes.push_back(i);
newHeader.push_back(parameter.getValue());
}
}
i++;
}
output = output->project(indexes);
output = output->rename(newHeader);
return output;
}
};
#endif //CS236_INTERPRETER_H