/**
 *  Copyright (c) 1999-2011, Ecole des Mines de Nantes
 *  All rights reserved.
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of the Ecole des Mines de Nantes nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package parser.flatzinc;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import parser.flatzinc.parser.FZNParser;
import solver.Solver;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * <br/>
 *
 * @author Charles Prud'homme
 * @since 27/01/11
 */
public class ParseAndSolve {

    protected static final Logger LOGGER = LoggerFactory.getLogger("fzn");

    @Option(name = "-f", usage = "Flatzinc file.", required = true)
    private static String instance;

    @Option(name = "-tl", usage = "Time limit.", required = false)
    private static long tl = -1;


    public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
        new ParseAndSolve().doMain(args);
    }

    public void doMain(String[] args) throws IOException {
        CmdLineParser parser = new CmdLineParser(this);
        parser.setUsageWidth(160);
        try {
            parser.parseArgument(args);
        } catch (CmdLineException e) {
            System.err.println(e.getMessage());
            System.err.println("java SampleMain [options...]");
            parser.printUsage(System.err);
            System.err.println("\nCheck MiniZinc is correctly installed.");
            System.err.println();
            return;
        }
        parseandsolve();
    }

    private void parseandsolve() {
        final FZNParser parser = new FZNParser();
        LOGGER.info("% load file ...");
        parser.loadInstance(new File(instance));
        LOGGER.info("% parse instance...");
        parser.parse();
        LOGGER.info("% solve instance...");
        final Solver solver = parser.solver;
//        SearchMonitorFactory.statEveryXXms(solver, 1000);
        if (tl > -1) {
            solver.getSearchLoop().getLimitsBox().setTimeLimit(tl);
        }
        final boolean[] stop = {true};
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                if (stop[0]) {
                    LOGGER.info("% User interruption...");
                    parser.layout.beforeClose();
                }
            }
        });
        solver.solve();
        stop[0] = false;
    }

}

@@ -3567,32 +3567,34 @@
 (tl);%0A        %7D%0A
+//
         final bo
@@ -3616,16 +3616,18 @@
 %7Btrue%7D;%0A
+//
         
@@ -3674,24 +3674,26 @@
  Thread() %7B%0A
+//
             
@@ -3708,24 +3708,26 @@
 oid run() %7B%0A
+//
             
@@ -3741,24 +3741,26 @@
 (stop%5B0%5D) %7B%0A
+//
             
@@ -3802,24 +3802,26 @@
 ption...%22);%0A
+//
             
@@ -3853,24 +3853,26 @@
 oreClose();%0A
+//
             
@@ -3869,32 +3869,34 @@
               %7D%0A
+//
             %7D%0A  
@@ -3885,32 +3885,34 @@
 //            %7D%0A
+//
         %7D);%0A    
@@ -3927,24 +3927,26 @@
 er.solve();%0A
+//
         stop

comment shutdown hook