/*******************************************************************************
 * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU Lesser General Public License Version 2.1 or later (the
 * "LGPL"), in which case the provisions of the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of the LGPL, and not to allow others to use your version of
 * this file under the terms of the EPL, indicate your decision by deleting
 * the provisions above and replace them with the notice and other provisions
 * required by the LGPL. If you do not delete the provisions above, a recipient
 * may use your version of this file under the terms of the EPL or the LGPL.
 *
 * Based on the original MiniSat specification from:
 *
 * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
 * Sixth International Conference on Theory and Applications of Satisfiability
 * Testing, LNCS 2919, pp 502-518, 2003.
 *
 * See www.minisat.se for the original solver in C++.
 *
 * Contributors:
 *   CRIL - initial API and implementation
 *******************************************************************************/
package org.sat4j.tools;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.Test;
import org.sat4j.core.ConstrGroup;
import org.sat4j.core.VecInt;
import org.sat4j.minisat.SolverFactory;
import org.sat4j.specs.ContradictionException;
import org.sat4j.specs.IConstr;
import org.sat4j.specs.ISolver;
import org.sat4j.specs.IVecInt;
import org.sat4j.specs.TimeoutException;
import org.sat4j.tools.encoding.EncodingStrategy;
import org.sat4j.tools.encoding.Policy;

public class TestClausalCardinalitiesBinaryEncoding {

    private ISolver solver;
    private Policy policy;
    private final static boolean MODE_DEBUG = false;

    @Before
    public void setUp() {
        this.policy = new Policy();
        this.policy.setAtMostOneEncoding(EncodingStrategy.BINARY);
        this.policy.setAtMostKEncoding(EncodingStrategy.BINARY);
        this.policy.setAtLeastOneEncoding(EncodingStrategy.BINARY);
        this.policy.setAtLeastKEncoding(EncodingStrategy.BINARY);
        this.policy.setExactlyOneEncoding(EncodingStrategy.BINARY);
        this.policy.setExactlyKEncoding(EncodingStrategy.BINARY);

        this.solver = new ClausalCardinalitiesDecorator<ISolver>(
                SolverFactory.newDefault(), this.policy);
    }

    @Test
    public void testAtMostOne() throws ContradictionException, TimeoutException {

        this.solver.newVar(5);

        IVecInt clause = new VecInt();
        clause.push(1).push(2).push(3).push(4).push(5);

        IConstr constr = this.solver.addAtMost(clause, 1);
        assertNotNull(constr);

        if (MODE_DEBUG) {
            System.out.println();
            System.out.println("testBinaryAtMostOne");
            for (int i = 0; i < constr.size(); i++) {
                System.out.println(((ConstrGroup) constr).getConstr(i));
            }
        }

        ModelIterator iterator = new ModelIterator(this.solver);
        int[] model = null;
        int cpt = 0;

        if (MODE_DEBUG) {
            System.out.println("testBinaryAtMostOne models");
        }
        while (iterator.isSatisfiable()) {
            model = iterator.model();
            assertNotNull(model);
            if (MODE_DEBUG) {
                System.out.println(new VecInt(model));
            }
            cpt++;
        }
        assertEquals(6, cpt);
    }

    @Test
    public void testExactlyOne() throws ContradictionException,
            TimeoutException {
        this.solver.newVar(5);

        IVecInt clause = new VecInt();
        clause.push(1).push(2).push(3).push(4).push(5);

        IConstr constr = this.solver.addExactly(clause, 1);
        assertNotNull(constr);

        if (MODE_DEBUG) {
            System.out.println();
            System.out.println("testBinaryExactlyOne");
            for (int i = 0; i < constr.size(); i++) {
                if (constr instanceof ConstrGroup) {
                    System.out.println(((ConstrGroup) constr).getConstr(i));
                } else {
                    System.out.println(constr);
                }
            }
        }

        ModelIterator iterator = new ModelIterator(this.solver);
        int[] model = null;
        int cpt = 0;

        if (MODE_DEBUG) {
            System.out.println("testBinaryExactlyOne models");
        }
        while (iterator.isSatisfiable()) {
            model = iterator.model();
            assertNotNull(model);
            if (MODE_DEBUG) {
                System.out.println(new VecInt(model));
            }
            cpt++;
        }
        assertEquals(5, cpt);
    }

    @Test
    public void testAtLeastOne() throws ContradictionException,
            TimeoutException {
        this.solver.newVar(5);

        IVecInt clause = new VecInt();
        clause.push(1).push(2).push(3).push(4).push(5);

        IConstr constr = this.solver.addAtLeast(clause, 1);
        assertNotNull(constr);

        if (MODE_DEBUG) {
            System.out.println();
            System.out.println("testBinaryAtLeastOne");
            for (int i = 0; i < constr.size(); i++) {
                if (constr instanceof ConstrGroup) {
                    System.out.println(((ConstrGroup) constr).getConstr(i));
                } else {
                    System.out.println(constr);
                }
            }
        }

        ModelIterator iterator = new ModelIterator(this.solver);
        int[] model = null;
        int cpt = 0;

        if (MODE_DEBUG) {
            System.out.println("testBinaryAtLeastOne models");
        }
        while (iterator.isSatisfiable()) {
            model = iterator.model();
            assertNotNull(model);
            if (MODE_DEBUG) {
                System.out.println(new VecInt(model));
            }
            cpt++;
        }
        assertEquals(31, cpt);
    }

    @Test
    public void testAtMost2() throws ContradictionException, TimeoutException {

        this.solver.newVar(5);

        IVecInt clause = new VecInt();
        clause.push(1).push(2).push(3).push(4).push(5);

        IConstr constr = this.solver.addAtMost(clause, 2);
        assertNotNull(constr);

        if (MODE_DEBUG) {
            System.out.println();
            System.out.println("testBinaryAtMost2");
            for (int i = 0; i < constr.size(); i++) {
                System.out.println(((ConstrGroup) constr).getConstr(i));
            }
        }

        ModelIterator iterator = new ModelIterator(this.solver);
        int[] model = null;
        int cpt = 0;

        if (MODE_DEBUG) {
            System.out.println("testBinaryAtMost2 models");
        }
        while (iterator.isSatisfiable()) {
            model = iterator.model();
            assertNotNull(model);
            if (MODE_DEBUG) {
                System.out.println(new VecInt(model));
            }
            cpt++;
        }
        assertEquals(16, cpt);
    }

    @Test
    public void testAtLeast2() throws ContradictionException, TimeoutException {

        this.solver.newVar(5);

        IVecInt clause = new VecInt();
        clause.push(1).push(2).push(3).push(4).push(5);

        IConstr constr = this.solver.addAtLeast(clause, 2);
        assertNotNull(constr);

        if (MODE_DEBUG) {
            System.out.println();
            System.out.println("testBinaryAtLeast2");
            for (int i = 0; i < constr.size(); i++) {
                System.out.println(((ConstrGroup) constr).getConstr(i));
            }
        }

        ModelIterator iterator = new ModelIterator(this.solver);
        int[] model = null;
        int cpt = 0;

        if (MODE_DEBUG) {
            System.out.println("testBinaryAtLeast2 models");
        }
        while (iterator.isSatisfiable()) {
            model = iterator.model();
            assertNotNull(model);
            if (MODE_DEBUG) {
                System.out.println(new VecInt(model));
            }
            cpt++;
        }
        assertEquals(26, cpt);
    }

    @Test
    public void testExactly2() throws ContradictionException, TimeoutException {

        this.solver.newVar(5);

        IVecInt clause = new VecInt();
        clause.push(1).push(2).push(3).push(4).push(5);

        IConstr constr = this.solver.addExactly(clause, 2);
        assertNotNull(constr);

        if (MODE_DEBUG) {
            System.out.println();
            System.out.println("testBinaryExactly2");
            for (int i = 0; i < constr.size(); i++) {
                System.out.println(((ConstrGroup) constr).getConstr(i));
            }
        }

        ModelIterator iterator = new ModelIterator(this.solver);
        int[] model = null;
        int cpt = 0;

        if (MODE_DEBUG) {
            System.out.println("testBinaryExactly2 models");
        }
        while (iterator.isSatisfiable()) {
            model = iterator.model();
            assertNotNull(model);
            if (MODE_DEBUG) {
                System.out.println(new VecInt(model));
            }
            cpt++;
        }
        assertEquals(10, cpt);
    }

}
