package org.chocosolver.solver.constraints.set;

import org.chocosolver.solver.Solver;
import org.chocosolver.solver.constraints.Constraint;
import org.chocosolver.solver.constraints.SatFactory;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.trace.Chatterbox;
import org.chocosolver.solver.variables.BoolVar;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.SetVar;
import org.chocosolver.solver.variables.VF;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * @author Jean-Guillaume Fages
 * @since 22/01/16
 * Created by IntelliJ IDEA.
 */
public class SetCstrsTest {

	@Test(groups = "1s")
	public static void testEq() {
		IntVar[] v1 = eqFilter("offset");
		IntVar[] v2 = eqFilter("all_equal");
		for(int i=0;i<v1.length;i++) {
			Assert.assertEquals(v1[i].getDomainSize(), v2[i].getDomainSize());
			for(int v=v1[i].getLB();v<=v1[i].getUB();v=v1[i].nextValue(v)){
				Assert.assertTrue(v2[i].contains(v));
			}
		}
		v1[0].getSolver().findAllSolutions();
		v2[0].getSolver().findAllSolutions();
		Assert.assertEquals(
				v1[0].getSolver().getMeasures().getSolutionCount(),
				v2[0].getSolver().getMeasures().getSolutionCount()
		);
	}

	public static IntVar[] eqFilter(String mode){
		Solver s = new Solver();
		IntVar x = VF.enumerated("x", 0, 10, s);
		IntVar y = VF.enumerated("y", 0, 10, s);
		// set view of A
		SetVar xset = VF.set("x as a set", 0, 10, s);
		SetVar yset = VF.set("y as a set", 0, 10, s);
		s.post(SCF.int_values_union(new IntVar[]{x},xset));
		s.post(SCF.int_values_union(new IntVar[]{y},yset));
		// X +9 <= Y or Y + 9 <= X
		SetVar Xleft = VF.set("", 0, 10, s);
		SetVar tmpLeft = VF.set("", 9, 19, s);
		s.post(SCF.offSet(Xleft,tmpLeft,9));
		SetVar Yleft = VF.set("", 0, 10, s);
		s.post(eq(tmpLeft, Yleft,mode));

		SetVar Yright = VF.set("",0, 10, s);
		SetVar tmpRight = VF.set("", 9, 19, s);
		s.post(SCF.offSet(Yright,tmpRight,9));
		SetVar Xright = VF.set("", 0, 10, s);
		s.post(eq(tmpRight, Xright,mode));

		//
		s.post(SCF.union(new SetVar[]{Xleft, Xright}, xset));
		s.post(SCF.union(new SetVar[]{Yleft,Yright},yset));
		// link to booleans
		BoolVar b1 = SCF.notEmpty(Yleft).reif();
		BoolVar b2 = SCF.notEmpty(Yright).reif();
		// ---
		SatFactory.addBoolOrArrayEqualTrue(new BoolVar[]{b1, b2});
		Chatterbox.showStatistics(s);
		Chatterbox.showSolutions(s);
		try {
			s.propagate();
		} catch (ContradictionException e) {
			e.printStackTrace();
		}
		System.out.println(mode);
		System.out.println(x);
		System.out.println(y);
		System.out.println("%%%%%%");
		return new IntVar[]{x,y};
	}

	public static Constraint eq(SetVar x, SetVar y, String mode){
		switch (mode){
			case "offset":return SCF.offSet(x, y, 0);
			default:
			case "all_equal":return SCF.all_equal(x, y);
		}
	}
}

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

add header