001package org.clafer.cli;
002
003import java.io.File;
004import java.io.PrintStream;
005import joptsimple.OptionSet;
006import org.clafer.compiler.ClaferCompiler;
007import org.clafer.compiler.ClaferOption;
008import org.clafer.compiler.ClaferSearch;
009import org.clafer.compiler.ClaferSearchStrategy;
010import org.clafer.instance.InstanceClafer;
011import org.clafer.instance.InstanceModel;
012import org.clafer.javascript.JavascriptFile;
013import org.clafer.objective.Objective;
014import org.clafer.scope.Scope;
015
016
017public class Normal {
018    // Running the model itself(instantiating or optimizing)
019    public static void runNormal(File inputFile, JavascriptFile  javascriptFile, OptionSet options, PrintStream outStream) throws Exception {
020
021        Objective[] objectives = javascriptFile.getObjectives();
022        if (objectives.length == 0)
023            System.out.println("Instantiating...");
024        else
025            System.out.println("Optimizing...");
026
027        // handle scopes
028        Scope scope = Utils.resolveScopes(inputFile, javascriptFile, options);
029
030        // handle search strategy
031        ClaferOption compilerOption = javascriptFile.getOption();
032        if (options.has("search"))
033            compilerOption = compilerOption.setStrategy((ClaferSearchStrategy) options.valueOf("search"));
034
035        // pick the right solver
036        ClaferSearch solver = objectives.length == 0
037            ? ClaferCompiler.compile(javascriptFile.getModel(), scope,             compilerOption)
038            : ClaferCompiler.compile(javascriptFile.getModel(), scope, objectives, compilerOption);
039
040        int index = 0; // instance id
041        boolean prettify = options.has("prettify");
042        boolean addTypes = options.has("addtypes");
043        boolean printOff = options.has("noprint");
044        boolean dataTackingOn = options.has("dataFile");
045        boolean timeOn = options.has("time");
046        File dataFile;
047        PrintStream dataStream = null;
048        if (dataTackingOn) {
049            dataFile = (File) options.valueOf("dataFile");
050            dataStream = new PrintStream(dataFile);
051        }
052
053        double elapsedTime;
054
055        long startTime = System.nanoTime();
056
057        int n = 0;
058        if (options.has("n"))
059            n = (int)options.valueOf("n");
060        else
061            n = -1;
062
063        // Stop at n instances before searching for another one: proving that there is
064        // no other instance can take much longer than finding the first (#46).
065        while ((n < 0 || index < n) && solver.find()) {
066            if (dataTackingOn) {
067                elapsedTime = (double) (System.nanoTime() - startTime) / 1000000000;
068                dataStream.println(elapsedTime + ", " + (index + 1));
069            }
070
071            if (printOff) {
072                ++index;
073            } else {
074                outStream.println("=== Instance " + (++index) + " Begin ===\n");
075                InstanceModel instance = solver.instance();
076                if (prettify)
077                    instance.print(outStream, addTypes);
078                else
079                    for (InstanceClafer c : instance.getTopClafers())
080                        Utils.printClafer(c, outStream);
081                outStream.println("\n--- Instance " + (index) + " End ---\n");
082            }
083        }
084        if (timeOn) {
085            elapsedTime = (double) (System.nanoTime() - startTime) / 1000000000;
086            if (objectives.length == 0)
087                System.out.println("Generated " +                           index + " instance(s) within the scope in " + elapsedTime + " seconds\n");
088            else
089                System.out.println("Generated " + (n == -1 ? "all " : "") + index + " optimal instance(s) within the scope in " + elapsedTime + " secondse\n");
090        } else {
091            if (objectives.length == 0)
092                System.out.println("Generated " +                           index + " instance(s) within the scope\n");
093            else
094                System.out.println("Generated " + (n == -1 ? "all " : "") + index + " optimal instance(s) within the scope\n");
095        }
096
097    }
098}