forked from MiniZinc/libminizinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmzn2fzn.cpp
559 lines (527 loc) · 19.5 KB
/
mzn2fzn.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <[email protected]>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cerrno>
#include <minizinc/model.hh>
#include <minizinc/parser.hh>
#include <minizinc/prettyprinter.hh>
#include <minizinc/typecheck.hh>
#include <minizinc/astexception.hh>
#include <minizinc/flatten.hh>
#include <minizinc/optimize.hh>
#include <minizinc/builtins.hh>
#include <minizinc/file_utils.hh>
#include <minizinc/timer.hh>
using namespace MiniZinc;
using namespace std;
std::string stoptime(Timer& start) {
std::ostringstream oss;
oss << std::setprecision(0) << std::fixed << start.ms() << " ms";
start.reset();
return oss.str();
}
bool beginswith(string s, string t) {
return s.compare(0, t.length(), t)==0;
}
int main(int argc, char** argv) {
string filename;
vector<string> datafiles;
vector<string> includePaths;
bool flag_ignoreStdlib = false;
bool flag_typecheck = true;
bool flag_verbose = false;
bool flag_newfzn = false;
bool flag_optimize = true;
bool flag_werror = false;
bool flag_statistics = false;
bool flag_stdinInput = false;
Timer starttime;
Timer lasttime;
string std_lib_dir;
if (char* MZNSTDLIBDIR = getenv("MZN_STDLIB_DIR")) {
std_lib_dir = string(MZNSTDLIBDIR);
}
string globals_dir;
bool flag_no_output_ozn = false;
string flag_output_base;
string flag_output_fzn;
string flag_output_ozn;
bool flag_output_fzn_stdout = false;
bool flag_output_ozn_stdout = false;
bool flag_instance_check_only = false;
FlatteningOptions fopts;
if (argc < 2)
goto error;
for (int i=1; i<argc; i++) {
if (string(argv[i])==string("-h") || string(argv[i])==string("--help"))
goto error;
if (string(argv[i])==string("--version")) {
std::cout << "NICTA MiniZinc to FlatZinc converter, version "
<< MZN_VERSION_MAJOR << "." << MZN_VERSION_MINOR << "." << MZN_VERSION_PATCH << std::endl;
std::cout << "Copyright (C) 2014, 2015 Monash University and NICTA" << std::endl;
std::exit(EXIT_SUCCESS);
}
if (beginswith(string(argv[i]),"-I")) {
string include(argv[i]);
if (include.length() > 2) {
includePaths.push_back(include.substr(2)+string("/"));
} else {
i++;
if (i==argc) {
goto error;
}
includePaths.push_back(argv[i]+string("/"));
}
} else if (string(argv[i])==string("--ignore-stdlib")) {
flag_ignoreStdlib = true;
} else if (string(argv[i])==string("--no-typecheck")) {
flag_typecheck = false;
} else if (string(argv[i])==string("--instance-check-only")) {
flag_instance_check_only = true;
} else if (string(argv[i])==string("-v") || string(argv[i])==string("--verbose")) {
flag_verbose = true;
} else if (string(argv[i])==string("--newfzn")) {
flag_newfzn = true;
} else if (string(argv[i])==string("--no-optimize") || string(argv[i])==string("--no-optimise")) {
flag_optimize = false;
} else if (string(argv[i])==string("--no-output-ozn") ||
string(argv[i])==string("-O-")) {
flag_no_output_ozn = true;
} else if (string(argv[i])=="--output-base") {
i++;
if (i==argc)
goto error;
flag_output_base = argv[i];
} else if (beginswith(string(argv[i]),"-o")) {
string filename(argv[i]);
if (filename.length() > 2) {
flag_output_fzn = filename.substr(2);
} else {
i++;
if (i==argc) {
goto error;
}
flag_output_fzn = argv[i];
}
} else if (string(argv[i])=="--output-to-file" ||
string(argv[i])=="--output-fzn-to-file") {
i++;
if (i==argc)
goto error;
flag_output_fzn = argv[i];
} else if (string(argv[i])=="--output-ozn-to-file") {
i++;
if (i==argc)
goto error;
flag_output_ozn = argv[i];
} else if (string(argv[i])=="--output-to-stdout" ||
string(argv[i])=="--output-fzn-to-stdout") {
flag_output_fzn_stdout = true;
} else if (string(argv[i])=="--output-ozn-to-stdout") {
flag_output_ozn_stdout = true;
} else if (string(argv[i])=="-" || string(argv[i])=="--input-from-stdin") {
if (datafiles.size() > 0 || filename != "")
goto error;
flag_stdinInput = true;
} else if (beginswith(string(argv[i]),"-d")) {
if (flag_stdinInput)
goto error;
string filename(argv[i]);
string datafile;
if (filename.length() > 2) {
datafile = filename.substr(2);
} else {
i++;
if (i==argc) {
goto error;
}
datafile = argv[i];
}
if (datafile.length()<=4 ||
datafile.substr(datafile.length()-4,string::npos) != ".dzn")
goto error;
datafiles.push_back(datafile);
} else if (string(argv[i])=="--data") {
if (flag_stdinInput)
goto error;
i++;
if (i==argc) {
goto error;
}
string datafile = argv[i];
if (datafile.length()<=4 ||
datafile.substr(datafile.length()-4,string::npos) != ".dzn")
goto error;
datafiles.push_back(datafile);
} else if (string(argv[i])=="--stdlib-dir") {
i++;
if (i==argc)
goto error;
std_lib_dir = argv[i];
} else if (beginswith(string(argv[i]),"-G")) {
string filename(argv[i]);
if (filename.length() > 2) {
globals_dir = filename.substr(2);
} else {
i++;
if (i==argc) {
goto error;
}
globals_dir = argv[i];
}
} else if (beginswith(string(argv[i]),"-D")) {
if (flag_stdinInput)
goto error;
string cmddata(argv[i]);
if (cmddata.length() > 2) {
datafiles.push_back("cmd:/"+cmddata.substr(2));
} else {
i++;
if (i==argc) {
goto error;
}
datafiles.push_back("cmd:/"+string(argv[i]));
}
} else if (string(argv[i])=="--cmdline-data") {
if (flag_stdinInput)
goto error;
i++;
if (i==argc) {
goto error;
}
datafiles.push_back("cmd:/"+string(argv[i]));
} else if (string(argv[i])=="--globals-dir" ||
string(argv[i])=="--mzn-globals-dir") {
i++;
if (i==argc)
goto error;
globals_dir = argv[i];
} else if (string(argv[i])=="-Werror") {
flag_werror = true;
} else if (string(argv[i])=="-s" || string(argv[i])=="--statistics") {
flag_statistics = true;
} else {
if (flag_stdinInput)
goto error;
std::string input_file(argv[i]);
if (input_file.length()<=4) {
std::cerr << "Error: cannot handle file " << input_file << "." << std::endl;
goto error;
}
std::string extension = input_file.substr(input_file.length()-4,string::npos);
if (extension == ".mzn") {
if (filename=="") {
filename = input_file;
} else {
std::cerr << "Error: Multiple .mzn files given." << std::endl;
goto error;
}
} else if (extension == ".dzn") {
datafiles.push_back(input_file);
} else {
std::cerr << "Error: cannot handle file extension " << extension << "." << std::endl;
goto error;
}
}
}
if (filename=="" && !flag_stdinInput) {
std::cerr << "Error: no model file given." << std::endl;
goto error;
}
if (std_lib_dir=="") {
std::string mypath = FileUtils::progpath();
if (!mypath.empty()) {
if (FileUtils::file_exists(mypath+"/share/minizinc/std/builtins.mzn")) {
std_lib_dir = mypath+"/share/minizinc";
} else if (FileUtils::file_exists(mypath+"/../share/minizinc/std/builtins.mzn")) {
std_lib_dir = mypath+"/../share/minizinc";
} else if (FileUtils::file_exists(mypath+"/../../share/minizinc/std/builtins.mzn")) {
std_lib_dir = mypath+"/../../share/minizinc";
}
}
}
if (std_lib_dir=="") {
std::cerr << "Error: unknown minizinc standard library directory.\n"
<< "Specify --stdlib-dir on the command line or set the\n"
<< "MZN_STDLIB_DIR environment variable.\n";
std::exit(EXIT_FAILURE);
}
if (globals_dir!="") {
includePaths.push_back(std_lib_dir+"/"+globals_dir+"/");
}
includePaths.push_back(std_lib_dir+"/std/");
for (unsigned int i=0; i<includePaths.size(); i++) {
if (!FileUtils::directory_exists(includePaths[i])) {
std::cerr << "Cannot access include directory " << includePaths[i] << "\n";
std::exit(EXIT_FAILURE);
}
}
if (flag_output_base == "") {
if (flag_stdinInput) {
flag_output_base = "mznout";
} else {
flag_output_base = filename.substr(0,filename.length()-4);
}
}
if (flag_output_fzn == "") {
flag_output_fzn = flag_output_base+".fzn";
}
if (flag_output_ozn == "") {
flag_output_ozn = flag_output_base+".ozn";
}
{
std::stringstream errstream;
if (flag_verbose) {
if (flag_stdinInput) {
std::cerr << "Parsing standard input" << std::endl;
} else {
std::cerr << "Parsing '" << filename << "'" << std::endl;
}
}
Model* m;
if (flag_stdinInput) {
filename = "stdin";
std::string input = std::string(istreambuf_iterator<char>(std::cin), istreambuf_iterator<char>());
m = parseFromString(input, filename, includePaths, flag_ignoreStdlib, false, flag_verbose, errstream);
} else {
m = parse(filename, datafiles, includePaths, flag_ignoreStdlib, false, flag_verbose, errstream);
}
if (m) {
try {
if (flag_typecheck) {
Env env(m);
if (flag_verbose)
std::cerr << "Done parsing (" << stoptime(lasttime) << ")" << std::endl;
if (flag_verbose)
std::cerr << "Typechecking ...";
vector<TypeError> typeErrors;
MiniZinc::typecheck(env, m, typeErrors);
if (typeErrors.size() > 0) {
for (unsigned int i=0; i<typeErrors.size(); i++) {
if (flag_verbose)
std::cerr << std::endl;
std::cerr << typeErrors[i].loc() << ":" << std::endl;
std::cerr << typeErrors[i].what() << ": " << typeErrors[i].msg() << std::endl;
}
exit(EXIT_FAILURE);
}
MiniZinc::registerBuiltins(env,m);
if (flag_verbose)
std::cerr << " done (" << stoptime(lasttime) << ")" << std::endl;
if (!flag_instance_check_only) {
if (flag_verbose)
std::cerr << "Flattening ...";
try {
flatten(env,fopts);
} catch (LocationException& e) {
if (flag_verbose)
std::cerr << std::endl;
std::cerr << e.what() << ": " << std::endl;
env.dumpErrorStack(std::cerr);
std::cerr << " " << e.msg() << std::endl;
exit(EXIT_FAILURE);
}
for (unsigned int i=0; i<env.warnings().size(); i++) {
std::cerr << (flag_werror ? "Error: " : "Warning: ") << env.warnings()[i];
}
if (flag_werror && env.warnings().size() > 0) {
exit(EXIT_FAILURE);
}
env.clearWarnings();
Model* flat = env.flat();
if (flag_verbose)
std::cerr << " done (" << stoptime(lasttime) << ", max stack depth " << env.maxCallStack() << ")" << std::endl;
if (flag_optimize) {
if (flag_verbose)
std::cerr << "Optimizing ...";
optimize(env);
for (unsigned int i=0; i<env.warnings().size(); i++) {
std::cerr << (flag_werror ? "Error: " : "Warning: ") << env.warnings()[i];
}
if (flag_werror && env.warnings().size() > 0) {
exit(EXIT_FAILURE);
}
if (flag_verbose)
std::cerr << " done (" << stoptime(lasttime) << ")" << std::endl;
}
if (!flag_newfzn) {
if (flag_verbose)
std::cerr << "Converting to old FlatZinc ...";
oldflatzinc(env);
if (flag_verbose)
std::cerr << " done (" << stoptime(lasttime) << ")" << std::endl;
} else {
env.flat()->compact();
env.output()->compact();
}
if (flag_statistics) {
FlatModelStatistics stats = statistics(env);
std::cerr << "Generated FlatZinc statistics:\n";
std::cerr << "Variables: ";
bool had_one = false;
if (stats.n_bool_vars) {
had_one = true;
std::cerr << stats.n_bool_vars << " bool";
}
if (stats.n_int_vars) {
if (had_one) std::cerr << ", ";
had_one = true;
std::cerr << stats.n_int_vars << " int";
}
if (stats.n_float_vars) {
if (had_one) std::cerr << ", ";
had_one = true;
std::cerr << stats.n_float_vars << " float";
}
if (stats.n_set_vars) {
if (had_one) std::cerr << ", ";
had_one = true;
std::cerr << stats.n_set_vars << " int";
}
if (!had_one)
std::cerr << "none";
std::cerr << "\n";
std::cerr << "Constraints: ";
had_one = false;
if (stats.n_bool_ct) {
had_one = true;
std::cerr << stats.n_bool_ct << " bool";
}
if (stats.n_int_ct) {
if (had_one) std::cerr << ", ";
had_one = true;
std::cerr << stats.n_int_ct << " int";
}
if (stats.n_float_ct) {
if (had_one) std::cerr << ", ";
had_one = true;
std::cerr << stats.n_float_ct << " float";
}
if (stats.n_set_ct) {
if (had_one) std::cerr << ", ";
had_one = true;
std::cerr << stats.n_set_ct << " int";
}
if (!had_one)
std::cerr << "none";
std::cerr << "\n";
}
if (flag_verbose)
std::cerr << "Printing FlatZinc ...";
if (flag_output_fzn_stdout) {
Printer p(std::cout,0);
p.print(flat);
} else {
std::ofstream os;
os.open(flag_output_fzn.c_str(), ios::out);
if (!os.good()) {
if (flag_verbose)
std::cerr << std::endl;
std::cerr << "I/O error: cannot open fzn output file. " << strerror(errno) << "." << std::endl;
exit(EXIT_FAILURE);
}
Printer p(os,0);
p.print(flat);
os.close();
}
if (flag_verbose)
std::cerr << " done (" << stoptime(lasttime) << ")" << std::endl;
if (!flag_no_output_ozn) {
if (flag_verbose)
std::cerr << "Printing .ozn ...";
if (flag_output_ozn_stdout) {
Printer p(std::cout,0);
p.print(env.output());
} else {
std::ofstream os;
os.open(flag_output_ozn.c_str(), ios::out);
if (!os.good()) {
if (flag_verbose)
std::cerr << std::endl;
std::cerr << "I/O error: cannot open ozn output file. " << strerror(errno) << "." << std::endl;
exit(EXIT_FAILURE);
}
Printer p(os,0);
p.print(env.output());
os.close();
}
if (flag_verbose)
std::cerr << " done (" << stoptime(lasttime) << ")" << std::endl;
}
}
} else { // !flag_typecheck
Printer p(std::cout);
p.print(m);
}
} catch (LocationException& e) {
if (flag_verbose)
std::cerr << std::endl;
std::cerr << e.loc() << ":" << std::endl;
std::cerr << e.what() << ": " << e.msg() << std::endl;
exit(EXIT_FAILURE);
} catch (Exception& e) {
if (flag_verbose)
std::cerr << std::endl;
std::cerr << e.what() << ": " << e.msg() << std::endl;
exit(EXIT_FAILURE);
}
delete m;
} else {
if (flag_verbose)
std::cerr << std::endl;
std::copy(istreambuf_iterator<char>(errstream),istreambuf_iterator<char>(),ostreambuf_iterator<char>(std::cerr));
exit(EXIT_FAILURE);
}
}
if (flag_verbose) {
std::cerr << "Done (overall time " << stoptime(starttime) << ", ";
size_t mem = GC::maxMem();
if (mem < 1024)
std::cerr << "maximum memory " << mem << " bytes";
else if (mem < 1024*1024)
std::cerr << "maximum memory " << mem/1024 << " Kbytes";
else
std::cerr << "maximum memory " << mem/(1024*1024) << " Mbytes";
std::cerr << ")." << std::endl;
}
return 0;
error:
std::cerr << "Usage: "<< argv[0]
<< " [<options>] [-I <include path>] <model>.mzn [<data>.dzn ...]" << std::endl
<< std::endl
<< "Options:" << std::endl
<< " --help, -h\n Print this help message" << std::endl
<< " --version\n Print version information" << std::endl
<< " --ignore-stdlib\n Ignore the standard libraries stdlib.mzn and builtins.mzn" << std::endl
<< " -v, --verbose\n Print progress statements" << std::endl
<< " -s, --statistics\n Print statistics" << std::endl
<< " --instance-check-only\n Check the model instance (including data) for errors, but do not\n convert to FlatZinc." << std::endl
<< " --no-optimize\n Do not optimize the FlatZinc\n Currently does nothing (only available for compatibility with 1.6)" << std::endl
<< " -d <file>, --data <file>\n File named <file> contains data used by the model." << std::endl
<< " -D <data>, --cmdline-data <data>\n Include the given data in the model." << std::endl
<< " --stdlib-dir <dir>\n Path to MiniZinc standard library directory" << std::endl
<< " -G --globals-dir --mzn-globals-dir\n Search for included files in <stdlib>/<dir>." << std::endl
<< std::endl
<< "Input/Output options:" << std::endl
<< " -, --input-from-stdin\n Read model from standard input (no additional .mzn or .dzn files possible)" << std::endl
<< " --no-output-ozn, -O-\n Do not output ozn file" << std::endl
<< " --output-base <name>\n Base name for output files" << std::endl
<< " -o <file>, --output-to-file <file>, --output-fzn-to-file <file>\n Filename for generated FlatZinc output" << std::endl
<< " --output-ozn-to-file <file>\n Filename for model output specification" << std::endl
<< " --output-to-stdout, --output-fzn-to-stdout\n Print generated FlatZinc to standard output" << std::endl
<< " --output-ozn-to-stdout\n Print model output specification to standard output" << std::endl
<< " -Werror\n Turn warnings into errors" << std::endl
;
exit(EXIT_FAILURE);
}