-
Notifications
You must be signed in to change notification settings - Fork 0
/
f2lp_wrapper.cpp
480 lines (377 loc) · 10.9 KB
/
f2lp_wrapper.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*Pseudo code
1 Read file line by line //done
2 Every line that is read, split the line by delimiter(".") and put the result in a std::string array. //done
3 Match every std::string in array with one of the rules. If a particular rule matches, change output accordingly.
4 While matching the rules if the rule matched is the domain one, push the variable in domain in a struct which has 2 values(func, variable) and comment out that line.
5 While matching the rule, everywhere the domain variable appears, replace the domain variable with a local variable.
6 repeat till all lines are taken care of.
7 at max should require one parse throught he file.
compiling command : g++ -I /usr/include/boost f2lp_wrapper.cpp -o regex_test -L /usr/lib/x86_64-linux-gnu/ -lboost_regex
Go YOLO on monday.
*/
#include <fstream>
#include <streambuf>
#include <string>
#include <cerrno>
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/algorithm/string/replace.hpp>
// #define DEBUG 0
#define COMMENT "%"
#define PAREN_OPEN "("
#define PAREN_CLOSE ")"
#define COMMA ","
#define COLON_DASH ":-"
#define DOT "."
#define DOTCHAR '.'
#define WHITESPACE "\t \n"
#define HASHTAG "#"
#define NEWLINE "\n"
namespace io = boost::iostreams;
namespace ba = boost::algorithm;
std::vector< std::pair<std::string,std::string> > domain_list;
std::string remove_sum(const std::string& input){
}
std::set<std::string> search_domain_variables(const std::string& input)
{
std::set<std::string> domains;
// boost::regex expr("([A-Z]+)");
boost::regex expr("([A-Z_]+)([A-Za-z0-9_]*)");
boost::sregex_token_iterator iter(input.begin(), input.end(), expr, 0);
boost::sregex_token_iterator end;
for (; iter != end; ++iter)
{
domains.insert(std::string(*iter));
}
return domains;
}
int isDomain(const std::string& input)
{
boost::regex expr("(#domain)([ ]+)([a-z]+)([(]){1}([A-Z_]+)([A-Za-z0-9_]*)([)]){1}");
return regex_match(input,expr) ? 1 : 0 ;
}
std::pair<std::string,std::string> get_domain_variables(const std::string& input)
{
std::string::const_iterator start, end;
start = input.begin();
end = input.end();
boost::match_results<std::string::const_iterator> what;
boost::regex expr("(#domain)([ ]+)([a-z]+)([(]){1}([A-Z_]+[A-Za-z0-9_]*)([)]){1}");
boost::regex_match(start, end, what, expr);
std::string variable(what[what.size()-2]);
std::string identifier(what[what.size()-4]);
std::pair <std::string,std::string> domain (variable,identifier);
return domain;
}
/*
Matches all clauses that statrt with #domain.
*/
int match_domain_rule(std::string& output, const std::string& input)
{
//first check if the clause is a domain. If it is a domain clause, extract the identifier and variable from it and exit
if(isDomain(input))
{
// The sentence is a domain. Extract Domain identfier and domain variable and put it in a list
domain_list.push_back(get_domain_variables(input));
// Comment out the domain as it is no longer used
output.insert(0,COMMENT);
return 1;
}
else
{
return 0;
}
}
/*
Method removes domain variables from clauses.
Call this method for every sentence.
All senstences need to be checked for domain variables
*/
void remove_domain_variables(std::string& output,const std::string& input)
{
//if the line is a comment or begins with # ignore
if(input.compare(0,1,HASHTAG) == 0 || input.compare(0,1,COMMENT) == 0)
{
return;
}
else
{
std::set<std::string> domains = search_domain_variables(input);
output = input ;
if(!domains.empty())
{
/* Found a domain in this input.
strategy 1:
input types:
1-> {loc(X,Y)}.
Now although X and Y can both be a global
variables, there is a global variable associated
with X but not with Y. So we only process for X
and assume that Y is a local variable.
*/
/*For each global variable detected from a string and
append it to the end of the current clause. Match the
identifier of the global variable from the domain_list vector.*/
/*This is the generated string that needs to be appended to
original string */
std::string generated_string;
std::set<std::string>::iterator i;
int is_domain_added = 0;
for (i = domains.begin(); i != domains.end(); ++i)
{
for (int j = 0; j < domain_list.size(); ++j)
{
if ((*i).compare(domain_list.at(j).first) == 0)
{
generated_string.append(domain_list.at(j).second)
.append(PAREN_OPEN)
.append(domain_list.at(j).first)
.append(PAREN_CLOSE)
.append(COMMA);
is_domain_added = 1;
}
}
}
/* The last clause will have a trailing comma, remove that*/
ba::trim_right_if(generated_string,ba::is_any_of(COMMA));
if(is_domain_added == 1)
{
//detect if a string has ':-' in it
//If it doesn't then append a ':-' to the clause and
//then append the generated string
if(input.find(":-") == std::string::npos)
{
output.append(COLON_DASH)
.append(generated_string);
}
else
{
output.append(COMMA)
.append(generated_string);
}
}
}
else
{
return;
}
}
}
int match_normal_rule(std::string& output, const std::string& input)
{
boost::regex expr("([ a-zA-Z0-9\\(\\)]+)(:-){1}([ ,.a-zA-Z0-9\\(\\)]+)");
if(boost::regex_match(input,expr))
{
boost::regex comma_expr("(,)");
std::string semi_colon_expr(";");
boost::regex_replace(output,comma_expr,semi_colon_expr);
return 1;
}
else
return 0;
}
//need to optimize this
int match_counting_literal_rule(std::string& output, const std::string& input)
{
boost::regex expr("([0-9 ]*) ([{]){1} ([A-Za-z0-9\\(\\),:<>= ]+) ([}]){1} ([0-9 ]*)");
output = input;
if(boost::regex_match(output,expr))
{
//std::string new_string(s);
int count = 0;
int inside_paren = 0;//1 is true in c++, 0 is false
//2 loops
// first loop changes all the commas separating two choice rules to semi colons
//first loop
for (size_t i = 0; i < output.length() ; ++i)
{
if(output[i] == '(')
{
inside_paren = 1;
continue;
}
if(output[i] == ')')
{
inside_paren = 0;
continue;
}
if (output[i] == ',' && inside_paren == 0)
{
output[i] = ';';
continue;
}
}
//second loop changes all colons except the first colon to commas
// 1 { q(X,Y): p(X): p(Y): X < Y, q(X,X): p(X) } 1
for (size_t i = 0; i < output.length(); ++i)
{
if (output[i] == ':')
{
if(count != 0)
{
output[i] = ',' ;
}
count++ ;
continue;
}
if(output[i] == ';')
{
count = 0;
continue;
}
}
return 1;
}
else
return 0;
}
/*
Clingo 4 does not support #hide and #base clauses
*/
int match_hide_base_rule(std::string& output, const std::string& input)
{
output = input;
//c++ true = 1 false = 0
std::string hide("#hide");
std::string base("#base");
std::vector<std::string> clauses;
clauses.push_back(hide);
clauses.push_back(base);
for (int i = 0; i < clauses.size(); ++i)
{
if (output.find(clauses.at(i)) != std::string::npos)
{
output.insert(0, COMMENT);
return 1;
}
}
return 0;
}
//Every string will match one of these rules. If it wont match the string is written as it is.
//Every string after its transformation also needs to be checked for domain variables.
//If it contains domain variables then in that case those variables need to be removed.
int match_rule(std::string& output, const std::string& input)
{
if (match_hide_base_rule(output,input))
{
#ifdef DEBUG
std::cout<<"hide rule matched"<<std::endl;
#endif
return 1;
}
else if (match_counting_literal_rule(output,input))
{
#ifdef DEBUG
std::cout<<"counting rule matched"<<std::endl;
#endif
return 1;
}
else if (match_normal_rule(output,input))
{
#ifdef DEBUG
std::cout<<"normal rule matched"<<std::endl;
#endif
return 1;
}
else if (match_domain_rule(output,input))
{
#ifdef DEBUG
std::cout<<"domain rule matched"<<std::endl;
#endif
return 1;
}
else
{
#ifdef DEBUG
std::cout<<"No rule matched"<<std::endl;
#endif
return 0;
}
}
std::string get_file_contents(const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
return(std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()));
}
throw(errno);
}
int main(int argc, char const *argv[])
{
const char *input_file_name = argv[1];
const char *output_file_name = "output.l";
std::ifstream file(input_file_name, std::ios_base::in | std::ios_base::binary);
std::ofstream outfile(output_file_name);
if(!outfile.is_open())
{
std::cerr << "Couldn't open output.l \n";
return -1;
}
if(file)
{
try {
io::filtering_istream in;
in.push(file);
for(std::string str; std::getline(in, str); )
{
//str has all the lines
if(!str.empty())
{
//process str. Str can be a single line ex q(1,2).q(1,3). need to separate these commands process them individually
std::vector<std::string> ind_commands;
if(str.compare(0,1,COMMENT) == 0)
{
outfile << str.append(NEWLINE);
continue;
}
// First you need to check if it is a range statement.
// Example. number(1..8) should not be split at the innner dots.
// One way to do this is convert the first pair of dots to ",," do the split then reconvert it back to ".."
std::size_t found = str.find("..");
if(found != std::string::npos)
{
boost::replace_all(str, "..", ",,");
}
boost::split(ind_commands,str,boost::is_any_of(DOT));
for (int i = 0; i < ind_commands.size(); ++i)
{
/* code */
boost::trim_if(ind_commands[i], boost::is_any_of(WHITESPACE));
std::string input_temp(ind_commands[i]);
std::string output(input_temp);
match_rule(output,input_temp);
remove_domain_variables(output,output);
output.append(DOT).append(NEWLINE);
//Reversing the above operation
if(found != std::string::npos)
{
boost::replace_all(output, ",,", "..");
}
// Dirty hack to remove all outputs with just a dot. need to figure out why
if (output.size() > 2)
{
outfile << output;
#ifdef DEBUG
std::cout<<output<<std::endl;
#endif
}
}
}
}
}
catch(...) {
std::cout << "Bad File" << '\n';
}
}
else
{
std::cerr << "File could not be opened!\n";
std::cerr << "Usage: f2lp_test <input file>"<<"\n";
}
return 0;
}