forked from gsdlab/chocosolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormal.java
121 lines (106 loc) · 4.87 KB
/
Normal.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
package org.clafer.cli;
import java.io.File;
import java.io.PrintStream;
import joptsimple.OptionSet;
import org.clafer.compiler.ClaferCompiler;
import org.clafer.compiler.ClaferOption;
import org.clafer.compiler.ClaferSearch;
import org.clafer.compiler.ClaferSearchStrategy;
import org.clafer.instance.InstanceClafer;
import org.clafer.instance.InstanceModel;
import org.clafer.javascript.JavascriptFile;
import org.clafer.objective.Objective;
import org.clafer.scope.Scope;
import org.clafer.ast.AstModel;
import org.sysml.ast.SysmlProperty;
import org.sysml.ast.SysmlPropertyDef;
import org.sysml.compiler.AstSysmlCompiler;
import org.sysml.pprinter.SysmlPrinter;
public class Normal {
// Running the model itself(instantiating or optimizing)
public static void runNormal(JavascriptFile javascriptFile, OptionSet options, PrintStream outStream) throws Exception {
Objective[] objectives = javascriptFile.getObjectives();
if (objectives.length == 0)
System.out.println("Instantiating...");
else
System.out.println("Optimizing...");
// handle scopes
Scope scope = Utils.resolveScopes(javascriptFile, options);
// handle search strategy
ClaferOption compilerOption = javascriptFile.getOption();
if (options.has("search"))
compilerOption = compilerOption.setStrategy((ClaferSearchStrategy) options.valueOf("search"));
// pick the right solver
ClaferSearch solver = objectives.length == 0
? ClaferCompiler.compile(javascriptFile.getModel(), scope, compilerOption)
: ClaferCompiler.compile(javascriptFile.getModel(), scope, objectives, compilerOption);
int index = 0; // instance id
boolean prettify = options.has("prettify");
boolean sysml = options.has("sysml");
boolean printOff = options.has("noprint");
boolean dataTackingOn = options.has("dataFile");
boolean timeOn = options.has("time");
File dataFile;
PrintStream dataStream = null;
if (dataTackingOn) {
dataFile = (File) options.valueOf("dataFile");
dataStream = new PrintStream(dataFile);
}
double elapsedTime;
long startTime = System.nanoTime();
int n = 0;
if (options.has("n"))
n = (int)options.valueOf("n");
else
n = -1;
while (solver.find()) {
if (dataTackingOn) {
elapsedTime = (double) (System.nanoTime() - startTime) / 1000000000;
dataStream.println(elapsedTime + ", " + (index + 1));
}
if (n >= 0 && index == n)
break;
if (printOff) {
++index;
} else {
if (sysml) {
outStream.append("package Architecture {\n");
outStream.append(" import ScalarValues::*;\n");
AstModel top = javascriptFile.getModel();
SysmlPrinter pprinter = new SysmlPrinter(outStream);
AstSysmlCompiler compiler = new AstSysmlCompiler();
SysmlPropertyDef[] models = compiler.compile(top, top);
for (SysmlPropertyDef model: models){
pprinter.visit(model, " ");
}
InstanceModel instance = solver.instance();
instance.printSysml(outStream, " ");
outStream.append("}\n");
} else {
outStream.println("=== Instance " + (++index) + " Begin ===\n");
InstanceModel instance = solver.instance();
if (prettify)
instance.print(outStream);
else
for (InstanceClafer c : instance.getTopClafers())
Utils.printClafer(c, outStream);
outStream.println("\n--- Instance " + (index) + " End ---\n");
}
}
}
if (!sysml) {
if (timeOn) {
elapsedTime = (double) (System.nanoTime() - startTime) / 1000000000;
if (objectives.length == 0)
System.out.println("Generated " + index + " instance(s) within the scope in " + elapsedTime + " seconds\n");
else
System.out.println("Generated " + (n == -1 ? "all " : "") + index + " optimal instance(s) within the scope in " + elapsedTime + " secondse\n");
} else {
if (objectives.length == 0)
System.out.println("Generated " + index + " instance(s) within the scope\n");
else
System.out.println("Generated " + (n == -1 ? "all " : "") + index + " optimal instance(s) within the scope\n");
}
}
}
}