-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathREPL.java
538 lines (463 loc) · 24.4 KB
/
REPL.java
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
package org.clafer.cli;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import joptsimple.OptionSet;
import org.clafer.ast.AstClafer;
import org.clafer.ast.AstModel;
import static org.clafer.ast.Asts.global;
import static org.clafer.ast.Asts.sum;
import org.clafer.compiler.ClaferCompiler;
import org.clafer.compiler.ClaferOption;
import org.clafer.compiler.ClaferSearch;
import org.clafer.compiler.ClaferSearchStrategy;
import org.clafer.compiler.ClaferUnsat;
import org.clafer.instance.InstanceClafer;
import org.clafer.instance.InstanceModel;
import org.clafer.javascript.Javascript;
import org.clafer.javascript.JavascriptFile;
import org.clafer.objective.Objective;
import org.clafer.scope.Scope;
public class REPL {
private static int instanceID = 0; // id of an instance generated previously
private static String prompt(BufferedReader br) throws IOException {
System.out.print("\nClaferChocoIG> ");
return br.readLine().trim();
}
public static void printHelp() {
System.out.println("'h'elp Print the REPL commands.");
System.out.println("'n'ext Generate the next instance.");
System.out.println("<enter> Generate the next instance.");
System.out.println("'p'rettify Toggle prettify on/off.");
System.out.println("'r'eload Reload the model from the same <file-name.js> file.");
System.out.println("'u'nsatCore Compute the set of contradicting constraints if any.");
System.out.println("min'U'nsat Compute the minimal UnSAT core and a near-miss example.");
System.out.println("'S'etGlobalScope <value> Set the global scope to the <value> .");
System.out.println("'s'etScope <claferUID> <value> Set the scope of the given clafer to the <value>.");
System.out.println("'I'ncGlobalScope <value?> Increase the global scope by <value> or 1 by default.");
System.out.println("'i'ncScope <claferUID> <value?> Increase the scope of the given clafer by the <value> or 1 by default.");
System.out.println("sa'v'eScopes Save the currect scopes to a .cfr-scope file.");
System.out.println("'m'axInt <value> Set the largest allowed integer to <value>.");
System.out.println("ma`x`imize <claferUID> Find a solution where <claferUID>.dref is maximal.");
System.out.println("minimiz'e' <claferUID> Find a solution where <claferUID>.dref is minimal.");
System.out.println("sta't's Display statistics about the current search.");
System.out.println("'O'ptions Display the current solver options.");
System.out.println("strate'g'y <smaller|larger|random> Set search strategy to prefer smaller, prefer larger, or random.");
System.out.println("'o'ptimizations Toggle optimizations basic/full.");
System.out.println("symmetry'B'reaking Toggle symmetry breaking basic/full.");
System.out.println("'q'uit Exit the REPL sesssion.");
}
public static String commandHelpL = "help";
public static String commandHelpS = "h";
public static String commandNextL = "next";
public static String commandNextS = "n";
public static String commandNext = "";
public static String commandPrettifyL = "prettify";
public static String commandPrettifyS = "p";
public static String commandReloadL = "reload";
public static String commandReloadS = "r";
public static String commandUnsatCoreL = "unsatCore";
public static String commandUnsatCoreS = "u";
public static String commandMinUnsatL = "minUnsat";
public static String commandMinUnsatS = "U";
public static String commandSetGlobalScopeL = "SetGlobalScope";
public static String commandSetGlobalScopeS = "S";
public static String commandSetScopeL = "setScope";
public static String commandSetScopeS = "s";
public static String commandIncGlobalScopeL = "IncGlobalScope";
public static String commandIncGlobalScopeS = "I";
public static String commandIncScopeL = "incScope";
public static String commandIncScopeS = "i";
public static String commandSaveScopesL = "saveScopes";
public static String commandSaveScopesS = "v";
public static String commandMaxIntL = "maxInt";
public static String commandMaxIntS = "m";
public static String commandMaximizeL = "maximize";
public static String commandMaximizeS = "x";
public static String commandMinimizeL = "maximize";
public static String commandMinimizeS = "z";
public static String commandStatsL = "stats";
public static String commandStatsS = "t";
public static String commandOptionsL = "Options";
public static String commandOptionsS = "O";
public static String commandOptimizationsL = "optimizations";
public static String commandOptimizationsS = "o";
public static String commandSymmetryBreakingL = "symmetryBreaking";
public static String commandSymmetryBreakingS = "B";
public static String commandStrategyL = "strategy";
public static String commandStrategyS = "g";
public static String commandQuitL = "quit";
public static String commandQuitS = "q";
public static void runREPL(File inputFile, JavascriptFile javascriptFile, OptionSet options) throws Exception {
String scopesFile = inputFile.getAbsolutePath().substring(0, inputFile.getAbsolutePath().length() - 3) + ".cfr-scope";
Scope scope = Utils.resolveScopes(javascriptFile, options);
ClaferOption compilerOption = javascriptFile.getOption();
if (options.has("search"))
compilerOption = compilerOption.setStrategy((ClaferSearchStrategy) options.valueOf("search"));
AstModel model = javascriptFile.getModel();
Objective[] objectives = javascriptFile.getObjectives();
ClaferSearch solver = null;
if (objectives.length == 0)
solver = ClaferCompiler.compile(model, scope, compilerOption);
else
solver = ClaferCompiler.compile(model, scope, objectives, compilerOption);
boolean prettify = options.has("prettify");
System.out.println("Type 'help' for the list of available REPL commands");
if (solver != null)
nextInstance(solver, prettify);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = "";
/* ****************************************
| Main REPL loop |
**************************************** */
while(true) {
s = prompt(br);
if (s.equals(commandQuitS) || s.equals(commandQuitL))
break;
if (s.equals(commandPrettifyS) || s.equals(commandPrettifyL)) {
prettify = ! prettify;
System.out.println("Prettify: " + (prettify ? "On" : "Off"));
continue;
}
if (s.equals(commandStatsL)) {
System.out.println(solver.getInternalSolver().getMeasures().toString());
continue;
}
if (s.equals(commandOptionsS) || s.equals(commandOptionsL)) {
String strategy = (compilerOption.getStrategy() == ClaferSearchStrategy.PreferSmallerInstances)
? "smaller"
: ((compilerOption.getStrategy() == ClaferSearchStrategy.PreferLargerInstances)
? "larger" : "random"
);
System.out.println
( "Options:"
+ "\n strate'g'y: "
+ strategy
+ "\n symmetry'B'reaking: "
+ (compilerOption.isBasicSymmetryBreaking() ? "basic" : "")
+ (compilerOption.isFullSymmetryBreaking() ? "full" : "")
+ "\n 'o'ptimizations: "
+ (compilerOption.isBasicOptimizations() ? "basic" : "")
+ (compilerOption.isFullOptimizations() ? "full" : "")
+ "\n 'm'axInt "
+ (scope.getIntHigh())
+ "\n global'S'cope "
+ (scope.getDefaultScope())
);
continue;
}
if (s.equals(commandOptimizationsS) || s.equals(commandOptimizationsL)) {
if (compilerOption.isBasicOptimizations()) {
compilerOption = compilerOption.fullOptimizations();
System.out.println("Optimizations: full");
}
else {
compilerOption = compilerOption.basicOptimizations();
System.out.println("Optimizations: basic");
}
continue;
}
if (s.equals(commandSymmetryBreakingS) || s.equals(commandSymmetryBreakingL)) {
if (compilerOption.isBasicSymmetryBreaking()) {
compilerOption = compilerOption.fullSymmetryBreaking();
System.out.println("Symmetry breaking: full");
}
else {
compilerOption = compilerOption.basicSymmetryBreaking();
System.out.println("Symmetry breaking: basic");
}
continue;
}
String commandParts[] = s.split(" ");
if (s.equals(commandNext) || commandParts.length == 0) { // next instance
if (solver == null)
solver = compileModel(model, scope, objectives, compilerOption);
nextInstance(solver, prettify);
continue;
}
String command = commandParts[0];
if (command.equals(commandNextS) || command.equals(commandNextL)) // next instance
{
if (solver == null)
solver = compileModel(model, scope, objectives, compilerOption);
nextInstance(solver, prettify);
}
else if (command.equals(commandMinUnsatS) || command.equals(commandMinUnsatL)) { // unsat
System.out.println("Min UNSAT command:");
ClaferUnsat unsat = ClaferCompiler.compileUnsat(model, scope);
// Print the Min-Unsat and near-miss example.
System.out.println(unsat.minUnsat());
}
else if (command.equals(commandUnsatCoreS) || command.equals(commandUnsatCoreL)) {// min unsat
System.out.println("UNSAT Core command:");
ClaferUnsat unsat = ClaferCompiler.compileUnsat(model, scope);
// Print the Min-Unsat and near-miss example.
System.out.println(unsat.unsatCore());
}
else if (command.equals(commandReloadS) || command.equals(commandReloadL)) {// reloading
try {
javascriptFile = Javascript.readModel(inputFile);
}
catch(Exception e) {
System.out.println("Unhandled compilation error occured. Please report this problem.");
System.out.println(e.getMessage());
javascriptFile = null;
}
if (javascriptFile != null) {
model = javascriptFile.getModel();
scope = Utils.resolveScopes(javascriptFile, options);
objectives = javascriptFile.getObjectives();
solver = compileModel(model, scope, objectives, compilerOption);
if (solver == null)
System.out.println("Could not reload");
else
System.out.println("Reloaded");
}
else
System.out.println("Could not reload");
}
else if (command.equals(commandSetGlobalScopeS) || command.equals(commandSetGlobalScopeL)) {
System.out.println("Set global scope: " + s);
if (commandParts.length != 2) {
System.out.println("The format of the command is: '" + commandSetGlobalScopeS + " <integer>'");
System.out.println("Given: '" + s + "'");
continue;
}
int scopeValue;
try {
scopeValue = Integer.parseInt(commandParts[1]);
}
catch(Exception e) {
System.out.println("The scope has to be an integer number. Given '" + commandParts[1] + "'");
continue;
}
scope = scope.toBuilder().defaultScope(scopeValue).toScope();
solver = compileModel(model, scope, objectives, compilerOption);
if (solver != null)
System.out.println("Model is ready after the scope change");
}
else if (command.equals(commandIncGlobalScopeS) || command.equals(commandIncGlobalScopeL)) {
System.out.println("Increase global scope: " + s);
if (commandParts.length > 2) {
System.out.println("The format of the command is: '" + commandIncGlobalScopeS + " <integer?>'");
System.out.println("Given: '" + s + "'");
continue;
}
int scopeValue = 1; // default increase by 1
if (commandParts.length == 2) {
try {
scopeValue = Integer.parseInt(commandParts[1]);
}
catch(Exception e) {
System.out.println("The scope has to be an integer number. Given '" + commandParts[1] + "'");
continue;
}
}
scope = scope.toBuilder().adjustDefaultScope(scopeValue).toScope();
solver = compileModel(model, scope, objectives, compilerOption);
if (solver != null)
System.out.println("Model is ready after the scope change");
}
else if (command.equals(commandMaxIntS) || command.equals(commandMaxIntL)) {
System.out.println("Max Integer: " + s);
if (commandParts.length != 2) {
System.out.println("The format of the command is: '" + commandMaxIntS + " <integer>'");
System.out.println("Given: '" + s + "'");
continue;
}
int scopeHigh;
try {
scopeHigh = Integer.parseInt(commandParts[1]);
}
catch(Exception e) {
System.out.println("Expected integer numbers. Given '" + commandParts[1] + "' '" + commandParts[2] + "'");
continue;
}
int scopeLow = -(scopeHigh + 1);
scope = scope.toBuilder().intLow(scopeLow).intHigh(scopeHigh).toScope();
solver = compileModel(model, scope, objectives, compilerOption);
if (solver != null)
System.out.println("Model is ready after the scope change");
}
else if (command.equals(commandMaximizeS) || command.equals(commandMaximizeL)) {
System.out.println("Maximize: " + s);
if (commandParts.length != 2) {
System.out.println("The format of the command is: '" + commandMaximizeS + " <claferUID>'");
System.out.println("Given: '" + s + "'");
continue;
}
String claferName = commandParts[1];
AstClafer clafer = Utils.getModelChildByName(model, claferName);
if (clafer == null) {
System.out.println("The clafer is not found: '" + claferName + "'");
continue;
}
if (!clafer.hasRef()) {
System.out.println("Cannot maximize " + clafer + " because it is not a reference.");
continue;
}
solver = ClaferCompiler.compile(model, scope, Objective.maximize(sum(global(clafer))));
nextInstance(solver, prettify);
}
else if (command.equals(commandMinimizeS) || command.equals(commandMinimizeL)) {
System.out.println("Minimize: " + s);
if (commandParts.length != 2) {
System.out.println("The format of the command is: '" + commandMinimizeS + " <claferUID>'");
System.out.println("Given: '" + s + "'");
continue;
}
String claferName = commandParts[1];
AstClafer clafer = Utils.getModelChildByName(model, claferName);
if (clafer == null) {
System.out.println("The clafer is not found: '" + claferName + "'");
continue;
}
if (!clafer.hasRef()) {
System.out.println("Cannot maximize " + clafer + " because it is not a reference.");
continue;
}
solver = ClaferCompiler.compile(model, scope, Objective.minimize(sum(global(clafer))));
nextInstance(solver, prettify);
}
else if (command.equals(commandSaveScopesS) || command.equals(commandSaveScopesL)) { // getting list of scopes
List<ClaferNameScopePair> claferScopePairs = new ArrayList<ClaferNameScopePair>();
List<AstClafer> allClafers = Utils.getAllModelClafers(model);
for (AstClafer curClafer: allClafers) {
int curScope;
try {
curScope = scope.getScope(curClafer);
}
catch(Exception e) {
curScope = 0;
}
claferScopePairs.add(new ClaferNameScopePair(curClafer.getName(), curScope));
}
Collections.sort(claferScopePairs);
Utils.produceScopeFile(claferScopePairs, scopesFile);
}
else if (command.equals(commandSetScopeS) || command.equals(commandSetScopeL)) {
System.out.println("Set individual scope: " + s);
if (commandParts.length != 3) {
System.out.println("The format of the command is: '" + commandSetScopeS + " <claferUID> <integer>'");
System.out.println("Given: '" + s + "'");
continue;
}
int claferScopeValue;
try {
claferScopeValue = Integer.parseInt(commandParts[2]);
}
catch(Exception e) {
System.out.println("The scope has to be an integer number. Given '" + commandParts[2] + "'");
continue;
}
String claferName = commandParts[1];
AstClafer clafer = Utils.getModelChildByName(model, claferName);
if (clafer == null) {
System.out.println("The clafer is not found: '" + claferName + "'");
continue;
}
scope = scope.toBuilder().setScope(clafer, claferScopeValue).toScope();
solver = compileModel(model, scope, objectives, compilerOption);
if (solver != null)
System.out.println("Model is ready after the scope change");
}
else if (command.equals(commandIncScopeS) || command.equals(commandIncScopeL)) {
System.out.println("Increase individual scope: " + s);
if (commandParts.length < 2 || commandParts.length > 3) {
System.out.println("The format of the command is: '" + commandIncScopeS + " <claferUID> <integer?>'");
System.out.println("Given: '" + s + "'");
continue;
}
int claferScopeValue = 1; // Default increase by 1
if (commandParts.length == 3) {
try {
claferScopeValue = Integer.parseInt(commandParts[2]);
}
catch(Exception e) {
System.out.println("The scope has to be an integer number. Given '" + commandParts[2] + "'");
continue;
}
}
String claferName = commandParts[1];
AstClafer clafer = Utils.getModelChildByName(model, claferName);
if (clafer == null) {
System.out.println("The clafer is not found: '" + claferName + "'");
continue;
}
scope = scope.toBuilder().adjustScope(clafer, claferScopeValue).toScope();
solver = compileModel(model, scope, objectives, compilerOption);
if (solver != null)
System.out.println("Model is ready after the scope change");
}
else if (command.equals(commandStrategyS) || command.equals(commandStrategyL)) {
if (commandParts.length != 2 ||
(!"smaller".equals(commandParts[1]) &&
!"larger".equals(commandParts[1]) &&
!"random".equals(commandParts[1]))) {
System.out.println("The format of the command is: '" + commandStrategyS + " <smaller|larger|random>'");
System.out.println("Given: '" + s + "'");
continue;
}
switch (commandParts[1]) {
case "smaller":
compilerOption = compilerOption.setStrategy(ClaferSearchStrategy.PreferSmallerInstances);
System.out.println("Search strategy: smaller");
break;
case "larger":
compilerOption = compilerOption.setStrategy(ClaferSearchStrategy.PreferLargerInstances);
System.out.println("Search strategy: larger");
break;
case "random":
System.out.println("Search strategy: random");
compilerOption = compilerOption.setStrategy(ClaferSearchStrategy.Random);
break;
}
continue;
}
else if (command.equals(commandHelpS) || command.equals(commandHelpL)) {
printHelp();
}
else
System.out.println("Unhandled command: " + s);
}
}
private static void nextInstance(ClaferSearch solver, boolean prettify) throws IOException {
if (solver == null) {
System.out.println("Could not start the instantiation");
return;
}
if (solver.find()) {
instanceID++;
System.out.println("\n=== Instance " + instanceID + " Begin ===\n");
InstanceModel instance = solver.instance();
if (prettify)
instance.print(System.out);
else
for (InstanceClafer c : instance.getTopClafers())
Utils.printClafer(c, System.out);
System.out.println("\n--- Instance " + (instanceID) + " End ---\n");
}
else
System.out.println("No more instances found. Consider increasing scopes.");
}
private static ClaferSearch compileModel(AstModel model, Scope scope, Objective[] objectives, ClaferOption compilerOption) {
ClaferSearch solver;
instanceID = 0; // reset instance ID
try {
if (objectives.length == 0)
solver = ClaferCompiler.compile(model, scope, compilerOption);
else
solver = ClaferCompiler.compile(model, scope, objectives, compilerOption);
}
catch (Exception e) {
solver = null;
System.out.println("Message: " + e.getMessage());
e.printStackTrace();
}
return solver;
}
}