diff options
author | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-06-14 17:48:22 +0200 |
---|---|---|
committer | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-06-14 17:48:22 +0200 |
commit | 7568f3782466531fe04fc14a40dc4d3a393c1fb9 (patch) | |
tree | 7778b598da5b698c35189234fe3a9dd58ae82913 /src/jrummikub/ai/fdsolver/constraint/SameConstraint.java | |
parent | 79767f762e39eac3a0247b9ee75b24258790e0de (diff) | |
download | JRummikub-7568f3782466531fe04fc14a40dc4d3a393c1fb9.tar JRummikub-7568f3782466531fe04fc14a40dc4d3a393c1fb9.zip |
Added TurnLogic with rule variables and constraints
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@436 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/ai/fdsolver/constraint/SameConstraint.java')
-rw-r--r-- | src/jrummikub/ai/fdsolver/constraint/SameConstraint.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/jrummikub/ai/fdsolver/constraint/SameConstraint.java b/src/jrummikub/ai/fdsolver/constraint/SameConstraint.java new file mode 100644 index 0000000..7fc8961 --- /dev/null +++ b/src/jrummikub/ai/fdsolver/constraint/SameConstraint.java @@ -0,0 +1,70 @@ +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<T> implements Constraint { + private Var<T> x, y; + Propagator propX, propY; + + public SameConstraint(Var<T> x, Var<T> y) { + this.x = x; + this.y = y; + propX = new SameProp<T>(x, y); + propY = new SameProp<T>(y, x); + } + + @Override + public Collection<Var<?>> getWatchedVars() { + return Arrays.<Var<?>> asList(x, y); + } + + private class SameProp<T> implements Propagator { + private Var<T> x, y; + public SameProp(Var<T> x, Var<T> y) { + this.x = x; + this.y = y; + } + + @Override + public Collection<Var<?>> getWatchedVars() { + return Arrays.<Var<?>>asList(y); + } + + @Override + public void propagate() { + for(Iterator<T> i = x.iterator(); i.hasNext();) { + if(!y.getRange().contains(i.next())) { + i.remove(); + } + } + } + } + + + @Override + public Collection<Propagator> getPropagators(boolean negate) { + return Arrays.asList(propX, propY); + } + + @Override + public Satisfiability getSatisfiability() { + HashSet<T> range = new HashSet<T>(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; + } + } + +} |