/**
 *  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.ast.constraints;

import parser.flatzinc.FZNException;
import parser.flatzinc.ast.Datas;
import parser.flatzinc.ast.expression.EAnnotation;
import parser.flatzinc.ast.expression.Expression;
import solver.Cause;
import solver.Solver;
import solver.constraints.Constraint;
import solver.constraints.ICF;
import solver.exception.ContradictionException;
import solver.variables.BoolVar;
import solver.variables.IntVar;

import java.util.List;

/**
 * (a = b)
 * <br/>
 *
 * @author Charles Prud'homme
 * @since 26/01/11
 */
public class Bool2IntBuilder implements IBuilder {

    private static Constraint[] NO_CSTR = new Constraint[0];

    @Override
    public Constraint[] build(Solver solver, String name, List<Expression> exps, List<EAnnotation> annotations, Datas datas) {
        BoolVar a = exps.get(0).boolVarValue(solver);
        IntVar b = exps.get(1).intVarValue(solver);
        String vname = b.getName();
        if (b.getNbProps() > 0) {
            //throw new FZNException("unable to remove " + b + ": it already exists in propagator(s).");
            return new Constraint[]{ICF.arithm(a, "=", b)};
        }
        solver.unassociates(b);
        datas.register(vname, a);
        if (b.instantiated()) {
            try {
                a.instantiateTo(b.getValue(), Cause.Null);
            } catch (ContradictionException e) {
                throw new FZNException("unable to instantiate " + a + " to " + b.getValue() + ".");
            }
        }
        return NO_CSTR;
    }
}

@@ -1670,45 +1670,8 @@
 s;%0A%0A
-import parser.flatzinc.FZNException;%0A
 impo
@@ -1805,29 +1805,8 @@
 on;%0A
-import solver.Cause;%0A
 impo
@@ -1896,56 +1896,8 @@
 CF;%0A
-import solver.exception.ContradictionException;%0A
 impo
@@ -2114,16 +2114,18 @@
 lder %7B%0A%0A
+//
     priv
@@ -2461,16 +2461,101 @@
 Name();%0A
+        return new Constraint%5B%5D%7BICF.arithm(a, %22=%22, b)%7D;%0A        /*%0A        TODO: fix%0A
         
@@ -2697,133 +2697,303 @@
     
-    return new Constraint%5B%5D%7BICF.arithm(a, %22=%22, b)%7D;%0A        %7D%0A        solver.unassociates(b);%0A        datas.register(vname, a
+%7D%0A        LoggerFactory.getLogger(%22solver%22).info(%22%7B%7D -%3E %7B%7D // %7B%7D: %7B%7D%22, new Object%5B%5D%7Ba, b, vname, datas.get(vname)%7D);%0A        solver.unassociates(b);%0A        datas.register(vname, a);%0A        LoggerFactory.getLogger(%22solver%22).info(%22%7B%7D -%3E %7B%7D // %7B%7D: %7B%7D%22, new Object%5B%5D%7Ba, b, vname, datas.get(vname)%7D
 );%0A 
@@ -3296,16 +3296,18 @@
 NO_CSTR;
+*/
 %0A    %7D%0A%7D

fix bool2Int