-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
151 lines (102 loc) · 3.38 KB
/
Copy pathInterpreter.cpp
File metadata and controls
151 lines (102 loc) · 3.38 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
//
// Interpreter.cpp
// Project3
//
// Created by Michael Bird on 7/21/14.
// Copyright (c) 2014 Michael Bird. All rights reserved.
//
#include "Interpreter.h"
Interpreter::Interpreter() {};
Interpreter::~Interpreter() {};
void Interpreter::interpret(DataLog obj_) {
DL_obj = obj_;
queries_ = DL_obj.get_queries();
add_relation();
Relations_ = DAT.get_relations();
}
void Interpreter::add_relation() { // add a relation
schemes_ = DL_obj.get_schemes();
for (auto scheme: schemes_) {
Scheme SC;
for (auto parameter: scheme.get_parameters()) {
for (auto value: parameter) {
stringstream ss; ss << value;
SC.push_back(ss.str());
}
}
vector<Tuple> tuplevec = add_facts(scheme.get_id());
DAT.create_new_relation(scheme.get_id(), SC, tuplevec);
tuple_clear();
}
}
vector<Tuple> Interpreter::add_facts(string id) {
facts_ = DL_obj.get_facts(); // checks for tuples in the relation
for (auto fact: facts_) {
Tuple TU;
if(id == fact.get_id()) {
for (auto parameter: fact.get_parameters()) {
TU.push_back(parameter);
}
tuple_vector.push_back(TU);
}
}
return tuple_vector;
}
void Interpreter::tuple_clear() {
tuple_vector.clear();
}
void Interpreter::eval_query(char* filename) {
ofstream outputFile;
outputFile.open(filename);
for (auto query: queries_) {
Relation RelTemp;
RelTemp = Relations_[query.get_id()];
stringstream ss;
ss << query.get_id() << "(";
vector<string> parameters = query.get_parameters();
for (unsigned int j=0; j < parameters.size(); j++) {
ss << parameters[j];
if (is_string(parameters[j]) == true) {
RelTemp = RelTemp.select(j,parameters[j]);
}
else {
for (unsigned int k = (j+1); k < parameters.size(); k++) {
if (parameters[k] == parameters[j]) {
RelTemp = RelTemp.select(k, j);
}
}
variable_pos.push_back(j);
variable_name.push_back(parameters[j]);
}
if (j < (parameters.size()-1)) {
ss << ",";
}
}
ss << ")?";
set<Tuple> tupleset = RelTemp.get_tuples();
ss << yes_no(tupleset);
if(variable_pos.size() > 0)
ss << RelTemp.project(variable_name, variable_pos);
outputFile << ss.str();
//clear vector tables
variable_pos.clear();
variable_name.clear();
}
outputFile.close();
}
string Interpreter::yes_no(set<Tuple> set) {
stringstream ss;
if(set.size() >= 1)
ss << " YES(" << set.size() <<")\n";
else
ss << " No\n";
return ss.str();
}
bool Interpreter::is_string(string param) {
bool bvalue = false;
if(isalpha(param.at(0)))
bvalue = false;
else
bvalue = true;
return bvalue;
}