-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.h
More file actions
331 lines (288 loc) · 11.6 KB
/
Interpreter.h
File metadata and controls
331 lines (288 loc) · 11.6 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
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
324
325
326
327
328
329
330
331
//
// Created by bashirov on 7/23/24.
//
#ifndef PROJECT3_INTERPRETER_H
#define PROJECT3_INTERPRETER_H
#pragma once
#include "Database.h"
#include "Parser.h"
#include "Relation.h"
#include <iostream>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <string>
using namespace std;
class Interpreter{
private:
Database database;
Parser parser;
public:
Interpreter (Parser pip){
stringstream out;
this->parser=pip;
for ( auto &scheme: pip.getSchemes()){
string name1 = scheme.getId();
vector<Parameter> parameters = scheme.getParameters();
vector<string> attributes;
for (auto parameter: parameters){
string attribute = parameter.getParameter();
attributes.push_back(attribute);
}
Relation relation = Relation(name1, attributes);
for (auto &fact: pip.getFacts()){
string name2 = fact.getId();
vector<Parameter> t_parameters = fact.getParameters();
vector<string>tuples;
for (auto parameter: t_parameters){
string tuple = parameter.getParameter();
tuples.push_back(tuple);
}
if (name1 == name2){
Tuple new_tuple = Tuple(tuples);
relation.addTuple(new_tuple);
}
} database.Add(name1, relation);
}
}
static Graph makeGraph(const vector<Rule1>& rules) {
Graph graph(rules.size());
for (int i = 0; i < rules.size(); i++){
//cout << "from rule R" << to_string(i) << ": " << rules.at(i).toString() << endl;
for (int j = 0; j < rules.at(i).getRules().size();j++){
//cout << "from body predicate: " << rules.at(i).getRules().at(j).toString() << endl;
for (int k = 0; k < rules.size();k++){
//cout << "to rule R" << to_string(k) << ": " << rules.at(k).toString() << endl;
if (rules.at(k).getHeadPredicate().getId() == rules.at(i).getRules().at(j).getId()) {
//cout << "dependecy found: (R" << to_string(i) <<",R" << to_string(k) << ")" << endl;
graph.addEdge(i,k);
}
}
}
}
return graph;
}
static Graph reverseGraph(Graph graph) {
Graph new_graph = Graph(graph.getNodes().size());
for (auto &pair: graph.getNodes()) {
int index = pair.first;
Node nodes = pair.second;
for (auto &node : nodes.getAdjacentNodeIDs()) {
new_graph.addEdge(node, index);
}
}
return new_graph;
}
void evaluateRule(Rule1 Rule, Parser p){
string empty_string = "";
vector <string> empty_vector = vector<string>();
empty_vector.push_back(empty_string);
Scheme empty_scheme = Scheme(empty_vector);
Relation result = Relation(empty_string, empty_scheme);
vector <Relation> Relations = vector<Relation>();
for (auto &predicate : Rule.getRules()){
map<string, int> variables = map<string, int>();
vector <string> variable_order = vector <string>();
set<string> strings = set<string>();
vector <int> int_order = vector <int>();
string name = predicate.getId();
vector<Parameter> parameters = predicate.getParameters();
vector<string> attributes = vector<string>();
Relation relation = database.getRelation(name);
for (size_t i = 0; i < parameters.size();i++) {
string attribute = parameters[i].getParameter();
attributes.push_back(attribute);
bool isconst = parameters[i].isIsconst();
if (isconst) {
relation = relation.select1(i, attribute);
} else {
if (strings.find(attribute) != strings.end()) {
relation = relation.select2(i, variables[attribute]);
} else {
variables.insert({attribute, i});
strings.insert(attribute);
variable_order.push_back(attribute);
int_order.push_back(i);
}
}
}
vector<int> nums = vector<int>();
vector<string> names = vector<string>();
for (const auto &variable: variables) {
int num = variable.second;
string str = variable.first;
nums.push_back(num);
names.push_back(str);
}
relation = relation.project(int_order, attributes);
relation = relation.rename(variable_order);
Relations.push_back(relation);
}
if (Relations.size() > 1){
for (int i = 1; i < Relations.size(); i++){
Relations[0] = Relations[0].join(Relations[i]);
result = Relations[0];
}
} else {
for (auto &relation: Relations){
result = relation;
}
}
Predicate head = Rule.getHeadPredicate();
string name = head.getId();
vector <string> names = vector<string>();
vector <int> indexes = vector<int>();
for ( auto parameter : head.getParameters()){
names.push_back(parameter.getParameter());
}
for (auto &n : names){
for ( int i = 0; i < result.getScheme().size(); i++){
if (result.getScheme().at(i) == n){
indexes.push_back(i);
}
}
}
result = result.project(indexes, result.getScheme().getNames());
vector <string> new_names = vector<string>();
for ( auto &scheme : p.getSchemes()){
if (scheme.getId() == name) {
for (auto ¶meter: scheme.getParameters()) {
new_names.push_back(parameter.getParameter());
}
}
}
result = result.rename(new_names);
result = database.getRelation(name).unite(result);
database.Add2(name, result);
}
void rules_loop(Parser p){
bool isuptaded = true;
cout << "Rule Evaluation" << endl;
int n = 0;
map <string, bool> loop;
cout << "Dependency Graph" << endl;
Graph graph = Interpreter::makeGraph(p.getRules());
cout << graph.toString() << endl;
Graph copy_original = graph;
graph = Interpreter::reverseGraph(graph);
stack <int> post_order = graph.dfs_Forest(graph);
vector <vector<int>> SCCs = graph.dfs_Forest_scc(graph, post_order);
while (!isuptaded || n == 0){
isuptaded = true;
for (auto &SCC : SCCs) {
map <int, Node> copy = copy_original.getNodes();
int num = SCC[0];
bool depends_on_itself = false;
for (auto dependency :copy[num].getAdjacentNodeIDs()){
if (dependency == num){
depends_on_itself = true;
}
}
if (SCC.size() == 1 && !depends_on_itself){
cout << p.getRules()[num].toString() << endl;
evaluateRule(p.getRules()[num], p);
isuptaded = false;
}
else {
for (auto &index: SCC) {
Rule1 rule = p.getRules()[index];
string name = rule.getHeadPredicate().getId();
Relation copy_origin = database.getRelation(name);
cout << rule.toString() << endl;
evaluateRule(rule, p);
int begin = copy_origin.getTuples().size();
int end = database.getRelation(name).getTuples().size();
if (begin != end) {
isuptaded = false;
}
}
n = n + 1;
}
}
n = n + 1;
}
cout << endl;
cout << "Schemes populated after " << to_string(n) << " passes through the Rules." << endl;
cout << endl;
}
void evaluateQuery(Predicate p){
map<string, int> variables;
vector <string> variable_order;
set<string> strings;
vector <int> int_order;
string name = p.getId();
vector<Parameter> parameters = p.getParameters();
vector<string> attributes;
Relation relation = database.getRelation(name);
for (size_t i = 0; i < parameters.size();i++){
string attribute = parameters[i].getParameter();
attributes.push_back(attribute);
bool isconst = parameters[i].isIsconst();
if (isconst){
relation = relation.select1(i,attribute);
}
else {
if (strings.find(attribute) != strings.end()){
relation = relation.select2(i, variables[attribute]);
} else{
variables.insert({attribute, i});
strings.insert(attribute);
variable_order.push_back(attribute);
int_order.push_back(i);
}
// string empty = "empty";
// int integer = 100000000;
// variables.insert({integer,empty});
// if (variables.empty()){
// variables.insert({i, attribute});
// strings.insert(attribute);
// } else {
// for (const auto &variable: variables) {
// int num = variable.first;
// string str = variable.second;
// // auto it = variables.find(integer);
// // variables.erase(it);
// if (strings.find(attribute) != strings.end()) {
// relation = relation.select2(i, num);
// } else {
// variables.insert({i, attribute});
// strings.insert(attribute);
// }
// }
// }
}
}
vector<int> nums;
vector<string> names;
for (const auto &variable: variables) {
int num = variable.second;
string str = variable.first;
nums.push_back(num);
names.push_back(str);
}
stringstream out;
if (strings.empty() && !relation.getTuples().empty()){
string number = to_string(relation.getTuples().size());
out << p.toString() << "? Yes" << "(" << number << ")" << endl;
} else {
relation = relation.project(int_order, attributes);
relation = relation.rename(variable_order);
string result;
string num;
if (relation.getTuples().empty()) {
result = "No";
} else {
string number = to_string(relation.getTuples().size());
num = "(" + number + ")";
result = "Yes" + num;
}
out << p.toString() << "? " << result << endl;
for (auto &tuple: relation.getTuples()) {
out << " " << tuple.toString(relation.getScheme()) << endl;
}
}
cout << out.str();
}
};
#endif //PROJECT3_INTERPRETER_H