package jrummikub.ai.fdsolver.constraint; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import jrummikub.ai.fdsolver.Constraint; import jrummikub.ai.fdsolver.Propagator; import jrummikub.ai.fdsolver.Satisfiability; import jrummikub.ai.fdsolver.Var; public class SameConstraint implements Constraint { private Var x, y; Propagator propX, propY; public SameConstraint(Var x, Var y) { this.x = x; this.y = y; propX = new SameProp(x, y); propY = new SameProp(y, x); } @Override public Collection> getWatchedVars() { return Arrays.> asList(x, y); } private class SameProp implements Propagator { private Var x, y; public SameProp(Var x, Var y) { this.x = x; this.y = y; } @Override public Collection> getWatchedVars() { return Arrays.>asList(y); } @Override public void propagate() { for(Iterator i = x.iterator(); i.hasNext();) { if(!y.getRange().contains(i.next())) { i.remove(); } } } } @Override public Collection getPropagators(boolean negate) { return Arrays.asList(propX, propY); } @Override public Satisfiability getSatisfiability() { HashSet range = new HashSet(x.getRange()); range.retainAll(y.getRange()); if (range.isEmpty()) { return Satisfiability.UNSAT; } else if (x.getRange().size() == 1 && y.getRange().size() == 1) { return Satisfiability.TAUT; } else { return Satisfiability.SAT; } } }