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/IfConstraint.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/IfConstraint.java')
-rw-r--r-- | src/jrummikub/ai/fdsolver/constraint/IfConstraint.java | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/jrummikub/ai/fdsolver/constraint/IfConstraint.java b/src/jrummikub/ai/fdsolver/constraint/IfConstraint.java new file mode 100644 index 0000000..802acc2 --- /dev/null +++ b/src/jrummikub/ai/fdsolver/constraint/IfConstraint.java @@ -0,0 +1,108 @@ +package jrummikub.ai.fdsolver.constraint; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.locks.Condition; + + +import jrummikub.ai.fdsolver.Constraint; +import jrummikub.ai.fdsolver.Propagator; +import jrummikub.ai.fdsolver.Satisfiability; +import jrummikub.ai.fdsolver.Var; + +public class IfConstraint implements Constraint { + Var<Boolean> condition; + Constraint child; + Collection<Var<?>> vars; + boolean negateCond; + + public IfConstraint(boolean negateCond, Var<Boolean> condition, Constraint child) { + this.condition = condition; + this.child = child; + this.negateCond = negateCond; + vars = new ArrayList<Var<?>>(); + vars.addAll(child.getWatchedVars()); + vars.add(condition); + } + + @Override + public Collection<Var<?>> getWatchedVars() { + return vars; + } + + private class IfPropagator implements Propagator { + Propagator child; + Collection<Var<?>> vars; + public IfPropagator(Propagator child) { + this.child = child; + vars = new ArrayList<Var<?>>(); + vars.addAll(child.getWatchedVars()); + vars.add(condition); + } + + @Override + public Collection<Var<?>> getWatchedVars() { + return vars; + } + + @Override + public void propagate() { + if(condition.getRange().contains(negateCond)) { + return; + } + child.propagate(); + } + } + + private class FailPropagator implements Propagator { + @Override + public Collection<Var<?>> getWatchedVars() { + return child.getWatchedVars(); + } + + @Override + public void propagate() { + if (child.getSatisfiability() == Satisfiability.UNSAT) { + condition.invalidate(!negateCond); + } + } + } + + @Override + public Collection<Propagator> getPropagators(boolean negate) { + List<Propagator> props = new ArrayList<Propagator>(); + if (negate) { + props.add(new FilterPropagator<Boolean>(new Filter<Boolean>() { + @Override + public boolean accept(Boolean value) { + return value ^ negateCond; + } + }, condition)); + props.addAll(child.getPropagators(true)); + } else { + for (Propagator p : child.getPropagators(false)) { + props.add(new IfPropagator(p)); + } + props.add(new FailPropagator()); + } + return props; + } + + @Override + public Satisfiability getSatisfiability() { + if (condition.getRange().contains(negateCond)) { + if (condition.getRange().size() == 1) { + return Satisfiability.TAUT; + } else { + if (child.getSatisfiability() == Satisfiability.TAUT) { + return Satisfiability.TAUT; + } else { + return Satisfiability.SAT; + } + } + } + return child.getSatisfiability(); + } + +} |