-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMain.java
127 lines (112 loc) · 5.59 KB
/
Main.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
package org.clafer.cli;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.clafer.compiler.ClaferSearchStrategy;
import org.clafer.javascript.Javascript;
import org.clafer.javascript.JavascriptFile;
public class Main
{
public static void main(String[] args) throws Exception {
OptionParser parser = new OptionParser() {
{
accepts( "dataFile", "Text file to store time (in seconds) to find each instance.").withRequiredArg()
.ofType( File.class ).describedAs("text file");
accepts( "file", "Input file in .cfr or .js format" ).withRequiredArg().ofType( File.class )
.describedAs( "Clafer model file (.cfr) or Clafer Javascript file (.js)." );
accepts( "help", "Show help.").forHelp();
accepts( "maxint", "Specify maximum integer value." ).withRequiredArg().ofType( Integer.class );
accepts( "minint", "Specify minimum integer value." ).withRequiredArg().ofType( Integer.class );
accepts( "moo", "Run in multi-objective optimization mode." );
accepts( "n", "Specify the maximum number of instances." ).withRequiredArg().ofType( Integer.class );
accepts( "noprint", "Don't print the instances to the console or a file");
accepts( "output", "Output instances to the given file." ).withRequiredArg().ofType( File.class ).describedAs( "text file" );
accepts( "prettify", "Use simple and pretty output format (not formal)." );
accepts( "repl", "Run in REPL (interactive) mode." );
accepts( "scope", "Override the default global scope value." ).withRequiredArg().ofType( Integer.class );
accepts( "search", "PreferSmallerInstances/PreferLargerInstances/Random" ).withRequiredArg().ofType( ClaferSearchStrategy.class );
accepts( "time", "Time how long it takes to find all instances (and print if it is turned on");
accepts( "v", "Run in validation mode; checks all assertions." );
accepts( "version", "Display the tool version" );
}
};
OptionSet options = parser.parse(args);
if (options.has("version")) {
Properties configFile = new Properties();
try {
configFile.load(Main.class.getClassLoader().getResourceAsStream("version.properties"));
String name = configFile.getProperty("name");
String releaseDate = configFile.getProperty("releasedate");
String version = configFile.getProperty("version");
System.out.println(name + " v" + version + "." + releaseDate);
} catch (IOException e) {
e.printStackTrace();
}
return;
}
if (options.has("help")) {
parser.printHelpOn(System.out);
return;
}
if (!options.has( "file" ))
throw new Exception("Use --file to provide the input file");
File inputFile = (File) options.valueOf("file");
if (!inputFile.exists())
throw new Exception("The provided input file does not exist: " + inputFile.getPath());
String fileName = inputFile.toString();
// If a .cfr file is given, compile it first and change the input file to the resulting .js file
if (fileName.endsWith(".cfr")) {
System.out.println("Compiling the Clafer model...");
// compile the file
try {
Process compilerProcess = Runtime.getRuntime().exec("clafer -k -m choco " + fileName);
compilerProcess.waitFor();
if (compilerProcess.exitValue() != 0) {
System.out.println("Clafer compilation error: make sure your model is correct. Aborting...");
System.exit(1);
}
} catch (Exception e) {
System.out.println("Abnormal Clafer compiler termination. Aborting...");
System.exit(1);
}
// replace the extension to .js
int extPos = fileName.lastIndexOf(".");
if(extPos != -1) {
fileName = fileName.substring(0, extPos) + ".js";
}
// change the inputFile to the resulting .js file
inputFile = new File(fileName);
}
PrintStream outStream = System.out;
if (options.has("output"))
{
File outputFile = (File) options.valueOf("output");
outStream = new PrintStream(outputFile);
}
if (!inputFile.exists())
{
throw new Exception("File does not exist: " + inputFile.getPath());
}
// run the different modes
JavascriptFile javascriptFile = null;
try {
if (options.has( "v"))
System.out.println("=========== Parsing+Typechecking " + fileName + " =============");
javascriptFile = Javascript.readModel(inputFile);
}
catch(Exception e) {
System.out.println("Unhandled compilation error occured. Please report this problem.");
System.out.println(e.getMessage());
return;
}
if (options.has("v"))
Validate.runValidate(inputFile, javascriptFile, options, outStream);
else if (options.has("repl"))
REPL.runREPL(inputFile, javascriptFile, options);
else
Normal.runNormal(javascriptFile, options, outStream);
}
}