This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_reason_printer.cc
252 lines (227 loc) · 6.34 KB
/
simple_reason_printer.cc
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
// Copyright 2015-2018 RWTH Aachen University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "simple_reason_printer.hh"
#include<queue>
#include<iostream>
//#ifdef CONVERT_TO_DNF
//void SimpleReasonPrinter::print(bool result, vector<PolicyStackProcessorNode> reason, DNFConverterNode & dnf){
//#else
void SimpleReasonPrinter::print(bool result, vector<PolicyStackProcessorNode> reason){
//#endif
if (result){
queue<PolicyStackProcessorNode> workQueue;
workQueue.push(reason[1]);
while(!workQueue.empty()){
PolicyStackProcessorNode pspn = workQueue.front();
switch(pspn.type){
case PolicyStackProcessorNodeType::AND:
workQueue.push(reason[pspn.reason[0]]);
workQueue.push(reason[pspn.reason[1]]);
break;
case PolicyStackProcessorNodeType::OR:
workQueue.push(choose(reason, pspn));
break;
case PolicyStackProcessorNodeType::RELATION:
reasonList.push_back(pspn.reason[0]);
break;
}
workQueue.pop();
}
}
//#ifdef CONVERT_TO_DNF
//mDNF = dnf;
//#endif
}
//#ifdef CONVERT_TO_DNF
//void SimpleReasonPrinter::printDNF(){
//unsigned long i = 0;
//if (mDNF.size() == 0)
//return;
//while(true){
//std::cout<<"( ";
//set<uint64_t>::iterator it = mDNF[i].begin();
//while(true){
//std::cout<< _relationToString(*it);
//++it;
//if (it != mDNF[i].end())
//std::cout<<" && ";
//else
//break;
//}
//std::cout<<" )";
//++i;
//if (i != mDNF.size())
//std::cout<<" || ";
//else
//break;
//}
//std::cout<<std::endl;
//}
//#endif
string SimpleReasonPrinter::printReasonToString(){
string str = "";
for (uint64_t reasonId = 0; reasonId < reasonList.size(); ++reasonId){
vector<int64_t> variableIds = mRelationSet->getRelationVariableIds(reasonList[reasonId]);
Variable vLHS, vRHS;
vLHS = mVariableSet->getVariableById(variableIds[0]);
str += _variableToString(vLHS);
str += _relationTypeToString(mRelationSet->getRelationType(reasonList[reasonId]));
if (variableIds[1] >= 0){
vRHS = mVariableSet->getVariableById(variableIds[1]);
str += _variableToString(vRHS);
}
str += '\n';
}
return str;
}
string SimpleReasonPrinter::_relationToString(uint64_t relationId, string str_surrounding){
string str = "";
vector<int64_t> variableIds = mRelationSet->getRelationVariableIds(relationId);
Variable vLHS = mVariableSet->getVariableById(variableIds[0]);
RelationSetType type = mRelationSet->getRelationType(relationId);
if (vLHS.isFunction()){
if (type == RelationSetType::IS_TRUE){
str += _variableToString(vLHS, str_surrounding);
}
else if (type == RelationSetType::IS_FALSE){
str += "!" + _variableToString(vLHS, str_surrounding);
}
}
else{
str += _variableToString(vLHS, str_surrounding) + _relationTypeToString(type);
if (variableIds[1] >= 0){
Variable vRHS = mVariableSet->getVariableById(variableIds[1]);
str += _variableToString(vRHS, str_surrounding);
}
}
return str;
}
string SimpleReasonPrinter::_printReasonToJSON_AUX(uint64_t reasonId){
string str = "";
str += "\"";
str += _relationToString(reasonList[reasonId], "\\\"");
str += "\"";
return str;
}
void SimpleReasonPrinter::printReasonToJSON(string filename){
ofstream of(filename);
if (!of.is_open())
throw runtime_error("cannot open file" + filename);
of << "{\n" <<"\"reasons\":[\n";
if (reasonList.size() > 0){
of << _printReasonToJSON_AUX(0);
for (uint64_t reasonId = 1; reasonId < reasonList.size(); ++reasonId){
of << ",\n" << _printReasonToJSON_AUX(reasonId);
}
}
of << "\n] }";
of.close();
}
#ifdef PRINT_REASON_TO_CONSOLE
string SimpleReasonPrinter::printReasonToJSON(){
ostringstream out;
out << "{\n" <<"\"reasons\":[\n";
if (reasonList.size() > 0){
out << _printReasonToJSON_AUX(0);
for (uint64_t reasonId = 1; reasonId < reasonList.size(); ++reasonId){
out << ",\n" << _printReasonToJSON_AUX(reasonId);
}
}
out << "\n] }";
return out.str();
}
#endif//PRINT_REASON_TO_CONSOLE
string SimpleReasonPrinter::_relationTypeToString(RelationSetType t){
string str = "";
switch(t){
case RelationSetType::EQUAL:
str = "=";
break;
case RelationSetType::NEQ:
str = "!=";
break;
case RelationSetType::GEQ:
str = ">=";
break;
case RelationSetType::LEQ:
str = "<=";
break;
case RelationSetType::GREATER:
str = ">";
break;
case RelationSetType::LESS:
str = "<";
break;
case RelationSetType::IS_TRUE:
str = "=true";
break;
case RelationSetType::IS_FALSE:
str = "=false";
break;
case RelationSetType::END:
break;
};
return str;
}
string SimpleReasonPrinter::_variableToString(Variable v, string str_surrounding){
//string str;
ostringstream oss;
if (v.isReference()){
oss<< mPolicyDefinition->getIdName(v.idInPolicyDef);
if (v.isFunction()){
oss<< "( " << _variableToString(v.funcParams->at(0), str_surrounding);
for (vector<Variable>::iterator it = v.funcParams->begin() + 1; it != v.funcParams->end(); ++it){
oss << ", " << _variableToString(*it, str_surrounding);
}
oss<< " )";
}
}
else {
switch (v.type){
case VariableSetType::STRING:
oss<< str_surrounding <<*(v.value.asString)<< str_surrounding;
break;
case VariableSetType::BOOLEAN:
if (v.value.asBoolean)
oss<< "true";
else
oss<< "false";
break;
//ENUM_VALUE not used
case VariableSetType::INT8:
//uint8_t is defined as char. print directly will meet problem.
oss<< (int)(v.value.asInt8);
break;
case VariableSetType::INT16:
oss<< v.value.asInt16;
break;
case VariableSetType::INT32:
oss<< v.value.asInt32;
break;
case VariableSetType::UINT8:
//uint8_t is defined as char. print directly will meet problem.
oss<< (unsigned int)(v.value.asUInt8);
break;
case VariableSetType::UINT16:
oss<< v.value.asUInt16;
break;
case VariableSetType::UINT32:
oss<< v.value.asUInt32;
break;
default:
break;
}
}
return oss.str();
}