EqualToIntVar.java

  1. /* Copyright 2015 Laurent COCAULT
  2.  * Licensed to Laurent COCAULT under one or more contributor license agreements.
  3.  * See the NOTICE file distributed with this work for additional information
  4.  * regarding copyright ownership. Laurent COCAULT licenses this file to You
  5.  * under the Apache License, Version 2.0 (the "License"); you may not use this
  6.  * file except in compliance with the License.  You may obtain a copy of the
  7.  * License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.csp.constraint.model.integer;

  18. /**
  19.  * Constraint specifying that one specific integer variable is equal to another
  20.  * variable augmented by a constant delta.
  21.  */
  22. public class EqualToIntVar extends BinaryIntConstraint {

  23.     /**
  24.      * Constructor of the constraint "LEFT = RIGHT + DELTA".
  25.      * @param left
  26.      *            Left part of the comparison
  27.      * @param right
  28.      *            Right part of the comparison
  29.      * @param delta
  30.      *            Delta added to the right part
  31.      */
  32.     public EqualToIntVar(final IntVar left, final IntVar right,
  33.             final IntValue delta) {
  34.         super(left.getName() + "=" + right.getName() + "+" + delta, left,
  35.                 right, delta);
  36.     }

  37.     /**
  38.      * {@inheritDoc}
  39.      */
  40.     @Override
  41.     public void propagate() {

  42.         // Propagate on the left part
  43.         IntValue newMin = IntValue.sum(getRight().getMinValue(), getDelta());
  44.         IntValue newMax = IntValue.sum(getRight().getMaxValue(), getDelta());
  45.         final boolean leftChanged = getLeft().reduceWithMinValue(newMin) ||
  46.                 getLeft().reduceWithMaxValue(newMax);

  47.         // Propagate on the right part
  48.         newMin = IntValue.diff(getLeft().getMinValue(), getDelta());
  49.         newMax = IntValue.diff(getLeft().getMaxValue(), getDelta());
  50.         final boolean rightChanged = getRight().reduceWithMinValue(newMin) ||
  51.                 getRight().reduceWithMaxValue(newMax);

  52.         // Update the list of changed variables
  53.         if (leftChanged) {
  54.             addRecentChangedVariable(getLeft());
  55.         }
  56.         if (rightChanged) {
  57.             addRecentChangedVariable(getRight());
  58.         }
  59.     }

  60.     /**
  61.      * Get the left argument of the constraint.
  62.      * @return First argument
  63.      */
  64.     protected IntVar getLeft() {
  65.         return (IntVar) getFirstVariable();
  66.     }

  67.     /**
  68.      * Get the right argument of the constraint.
  69.      * @return Second argument
  70.      */
  71.     protected IntVar getRight() {
  72.         return (IntVar) getSecondVariable();
  73.     }

  74. }