-
Notifications
You must be signed in to change notification settings - Fork 28
/
config.cpp
332 lines (307 loc) · 13.8 KB
/
config.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
324
325
326
327
328
329
330
331
332
#include "config.h"
#include "gl_const.h"
#include "tinyxml2.h"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <math.h>
Config::Config()
{
LogParams = nullptr;
SearchParams = nullptr;
}
Config::~Config()
{
if (SearchParams) delete[] SearchParams;
if (LogParams) delete[] LogParams;
}
bool Config::getConfig(const char *FileName)
{
std::string value;
std::stringstream stream;
tinyxml2::XMLElement *root = 0, *algorithm = 0, *element = 0, *options = 0;
tinyxml2::XMLDocument doc;
if (doc.LoadFile(FileName) != tinyxml2::XMLError::XML_SUCCESS) {
std::cout << "Error opening XML file!" << std::endl;
return false;
}
root = doc.FirstChildElement(CNS_TAG_ROOT);
if (!root) {
std::cout << "Error! No '" << CNS_TAG_ROOT << "' element found in XML file!" << std::endl;
return false;
}
algorithm = root->FirstChildElement(CNS_TAG_ALG);
if (!algorithm) {
std::cout << "Error! No '" << CNS_TAG_ALG << "' tag found in XML file!" << std::endl;
return false;
}
element = algorithm->FirstChildElement(CNS_TAG_ST);
if (!element) {
std::cout << "Error! No '" << CNS_TAG_ST << "' tag found in XML file!" << std::endl;
return false;
}
if (element->GetText())
value = element->GetText();
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
if (value == CNS_SP_ST_BFS) {
N = 8;
SearchParams = new double[N];
SearchParams[CN_SP_ST] = CN_SP_ST_BFS;
}
else if (value == CNS_SP_ST_DIJK) {
N = 8;
SearchParams = new double[N];
SearchParams[CN_SP_ST] = CN_SP_ST_DIJK;
}
else if (value == CNS_SP_ST_ASTAR || value == CNS_SP_ST_JP_SEARCH || value == CNS_SP_ST_TH) {
N = 8;
SearchParams = new double[N];
SearchParams[CN_SP_ST] = CN_SP_ST_ASTAR;
if (value == CNS_SP_ST_JP_SEARCH)
SearchParams[CN_SP_ST] = CN_SP_ST_JP_SEARCH;
else if (value == CNS_SP_ST_TH)
SearchParams[CN_SP_ST] = CN_SP_ST_TH;
element = algorithm->FirstChildElement(CNS_TAG_HW);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_HW << "' tag found in algorithm section." << std::endl;
std::cout << "Value of '" << CNS_TAG_HW << "' was defined to 1." << std::endl;
SearchParams[CN_SP_HW] = 1;
}
else {
stream << element->GetText();
stream >> SearchParams[CN_SP_HW];
stream.str("");
stream.clear();
if (SearchParams[CN_SP_HW] < 1) {
std::cout << "Warning! Value of '" << CNS_TAG_HW << "' tag is not correctly specified. Should be >= 1."
<< std::endl;
std::cout << "Value of '" << CNS_TAG_HW << "' was defined to 1." << std::endl;
SearchParams[CN_SP_HW] = 1;
}
}
element = algorithm->FirstChildElement(CNS_TAG_MT);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_MT << "' tag found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_MT << "' was defined to 'euclidean'." << std::endl;
SearchParams[CN_SP_MT] = CN_SP_MT_EUCL;
}
else {
if (element->GetText())
value = element->GetText();
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
if (value == CNS_SP_MT_MANH) SearchParams[CN_SP_MT] = CN_SP_MT_MANH;
else if (value == CNS_SP_MT_EUCL) SearchParams[CN_SP_MT] = CN_SP_MT_EUCL;
else if (value == CNS_SP_MT_DIAG) SearchParams[CN_SP_MT] = CN_SP_MT_DIAG;
else if (value == CNS_SP_MT_CHEB) SearchParams[CN_SP_MT] = CN_SP_MT_CHEB;
else {
std::cout << "Warning! Value of'" << CNS_TAG_MT << "' is not correctly specified." << std::endl;
std::cout << "Value of '" << CNS_TAG_MT << "' was defined to 'euclidean'" << std::endl;
SearchParams[CN_SP_MT] = CN_SP_MT_EUCL;
}
if (SearchParams[CN_SP_ST] == CN_SP_ST_TH && SearchParams[CN_SP_MT] != CN_SP_MT_EUCL) {
std::cout << "Warning! This type of metric is not admissible for Theta*!" << std::endl;
}
}
element = algorithm->FirstChildElement(CNS_TAG_BT);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_BT << "' tag found in XML file" << std::endl;
std::cout << "Value of '" << CNS_TAG_BT << "' was defined to 'g-max'" << std::endl;
SearchParams[CN_SP_BT] = CN_SP_BT_GMAX;
}
else {
value = element->GetText();
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
if (value == CNS_SP_BT_GMIN) SearchParams[CN_SP_BT] = CN_SP_BT_GMIN;
else if (value == CNS_SP_BT_GMAX) SearchParams[CN_SP_BT] = CN_SP_BT_GMAX;
else {
std::cout << "Warning! Value of '" << CNS_TAG_BT << "' is not correctly specified." << std::endl;
std::cout << "Value of '" << CNS_TAG_BT << "' was defined to 'g-max'" << std::endl;
SearchParams[CN_SP_BT] = CN_SP_BT_GMAX;
}
}
}
else {
std::cout << "Error! Value of '" << CNS_TAG_ST << "' tag (algorithm name) is not correctly specified."
<< std::endl;
std::cout << "Supported algorithm's names are: '" << CNS_SP_ST_BFS << "', '" << CNS_SP_ST_DIJK << "', '"
<< CNS_SP_ST_ASTAR << "', '" << CNS_SP_ST_TH << "', '" << CNS_SP_ST_JP_SEARCH << "'." << std::endl;
return false;
}
element = algorithm->FirstChildElement(CNS_TAG_AD);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_AD << "' element found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_AD << "' was defined to default - true" << std::endl;
SearchParams[CN_SP_AD] = 1;
}
else {
std::string check;
stream << element->GetText();
stream >> check;
stream.clear();
stream.str("");
if (check != "1" && check != "true" && check != "0" && check != "false") {
std::cout << "Warning! Value of '" << CNS_TAG_AD << "' is not correctly specified." << std::endl;
std::cout << "Value of '" << CNS_TAG_AD << "' was defined to default - true " << std::endl;
SearchParams[CN_SP_AD] = 1;
}
else if (check == "1" || check == "true")
SearchParams[CN_SP_AD] = 1;
else
SearchParams[CN_SP_AD] = 0;
}
if (SearchParams[CN_SP_AD] == 0) {
SearchParams[CN_SP_CC] = 0;
SearchParams[CN_SP_AS] = 0;
}
else {
element = algorithm->FirstChildElement(CNS_TAG_CC);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_CC << "' element found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_CC << "' was defined to default - false" << std::endl;
SearchParams[CN_SP_CC] = 0;
}
else {
std::string check;
stream << element->GetText();
stream >> check;
stream.clear();
stream.str("");
if (check != "1" && check != "true" && check != "0" && check != "false") {
std::cout << "Warning! Value of '" << CNS_TAG_CC << "' is not correctly specified." << std::endl;
std::cout << "Value of '" << CNS_TAG_CC << "' was defined to default - false" << std::endl;
SearchParams[CN_SP_CC] = 0;
}
else {
if (check == "1" || check == "true")
SearchParams[CN_SP_CC] = 1;
else
SearchParams[CN_SP_CC] = 0;
}
}
if (SearchParams[CN_SP_CC] == 0) {
SearchParams[CN_SP_AS] = 0;
}
else {
element = algorithm->FirstChildElement(CNS_TAG_AS);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_AS << "' element found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_AS << "' was defined to default - false." << std::endl;
SearchParams[CN_SP_AS] = 0;
}
else {
std::string check;
stream << element->GetText();
stream >> check;
stream.clear();
stream.str("");
if (check != "1" && check != "true" && check != "0" && check != "false") {
std::cout << "Warning! Value of '" << CNS_TAG_AS << "' is not correctly specified." << std::endl;
std::cout << "Value of '" << CNS_TAG_AS << "' was defined to default - false." << std::endl;
SearchParams[CN_SP_AS] = 0;
}
else {
if (check == "1" || check == "true")
SearchParams[CN_SP_AS] = 1;
else
SearchParams[CN_SP_AS] = 0;
}
}
}
}
element = algorithm->FirstChildElement(CNS_TAG_PS);
if (!element)
{
std::cout << "Warning! No '" << CNS_TAG_PS << "' element found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_PS << "' was defined to default - false" << std::endl;
SearchParams[CN_SP_PS] = 0;
}
else
{
SearchParams[CN_SP_PS] = 0;
if(SearchParams[CN_SP_ST] != CN_SP_ST_TH)
{
std::string check;
stream << element->GetText();
stream >> check;
stream.clear();
stream.str("");
if (check != "1" && check != "true" && check != "0" && check != "false") {
std::cout << "Warning! Value of '" << CNS_TAG_PS << "' is not correctly specified." << std::endl;
std::cout << "Value of '" << CNS_TAG_PS << "' was defined to default - false " << std::endl;
SearchParams[CN_SP_PS] = 0;
}
else if (check == "1" || check == "true")
SearchParams[CN_SP_PS] = 1;
else
SearchParams[CN_SP_PS] = 0;
}
}
options = root->FirstChildElement(CNS_TAG_OPT);
LogParams = new std::string[3];
LogParams[CN_LP_PATH] = "";
LogParams[CN_LP_NAME] = "";
if (!options) {
std::cout << "Warning! No '" << CNS_TAG_OPT << "' tag found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_LOGLVL << "' tag was defined to 'short log' (1)." << std::endl;
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_SHORT_WORD;
}
else {
element = options->FirstChildElement(CNS_TAG_LOGLVL);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_LOGLVL << "' tag found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_LOGLVL << "' tag was defined to 'short log' (1)." << std::endl;
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_SHORT_WORD;
}
else {
stream << element->GetText();
stream >> value;
stream.str("");
stream.clear();
//std::transform(value.begin(), value.end(), value.begin(), ::tolower);
if (value == CN_LP_LEVEL_NOPE_WORD || value == CN_LP_LEVEL_NOPE_VALUE)
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_NOPE_WORD;
else if (value == CN_LP_LEVEL_TINY_WORD || value == CN_LP_LEVEL_TINY_VALUE)
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_TINY_WORD;
else if (value == CN_LP_LEVEL_SHORT_WORD || value == CN_LP_LEVEL_SHORT_VALUE)
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_SHORT_WORD;
else if (value == CN_LP_LEVEL_MEDIUM_WORD || value == CN_LP_LEVEL_MEDIUM_VALUE)
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_MEDIUM_WORD;
else if (value == CN_LP_LEVEL_FULL_WORD || value == CN_LP_LEVEL_FULL_VALUE)
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_FULL_WORD;
else {
std::cout << "'" << CNS_TAG_LOGLVL << "' is not correctly specified" << std::endl;
std::cout << "Value of '" << CNS_TAG_LOGLVL << "' tag was defined to 'short log' (1)." << std::endl;
LogParams[CN_LP_LEVEL] = CN_LP_LEVEL_SHORT_WORD;
}
std::cout << "LogLevel: " << LogParams[CN_LP_LEVEL] << std::endl;
}
element = options->FirstChildElement(CNS_TAG_LOGPATH);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_LOGPATH << "' tag found in XML file." << std::endl;
std::cout << "Value of '" << CNS_TAG_LOGPATH << "' tag was defined to 'current directory'." << std::endl;
}
else if (!element->GetText()) {
std::cout << "Warning! Value of '" << CNS_TAG_LOGPATH << "' tag is missing!" << std::endl;
std::cout << "Value of '" << CNS_TAG_LOGPATH << "' tag was defined to 'current directory'." << std::endl;
}
else {
LogParams[CN_LP_PATH] = element->GetText();
}
element = options->FirstChildElement(CNS_TAG_LOGFN);
if (!element) {
std::cout << "Warning! No '" << CNS_TAG_LOGFN << "' tag found in XML file!" << std::endl;
std::cout << "Value of '" << CNS_TAG_LOGFN
<< "' tag was defined to default (original filename +'_log' + original file extension."
<< std::endl;
}
else if (!element->GetText()) {
std::cout << "Warning! Value of '" << CNS_TAG_LOGFN << "' tag is missing." << std::endl;
std::cout << "Value of '" << CNS_TAG_LOGFN
<< "' tag was defined to default (original filename +'_log' + original file extension."
<< std::endl;
}
else
LogParams[CN_LP_NAME] = element->GetText();
}
return true;
}