BinaryConstraint.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;

  18. /**
  19.  * Represents a binary constraint in a constraint solving problem. To define a
  20.  * new binary constraint, inherit this class and define the propagate method
  21.  * that must fill the list _recentChangedVariables.
  22.  */
  23. public abstract class BinaryConstraint<T> extends NAryConstraint<T> {

  24.     /**
  25.      * A binary constraint is associated with two variables. This attribute is
  26.      * the first variable.
  27.      */
  28.     private Variable<T> firstVariable_;

  29.     /**
  30.      * A binary constraint is associated with two variables. This attribute is
  31.      * the second variable.
  32.      */
  33.     private Variable<T> secondVariable_;

  34.     /**
  35.      * Constructor of a binary constraint. It references two variables given as
  36.      * parameters.
  37.      * @param name
  38.      *            Name of the binary constraint
  39.      * @param first
  40.      *            Reference of the first variable
  41.      * @param second
  42.      *            Reference of the second variable
  43.      */
  44.     public BinaryConstraint(final String name, final Variable<T> first,
  45.             final Variable<T> second) {
  46.         super(name);
  47.         firstVariable_ = first;
  48.         secondVariable_ = second;
  49.         first.addNAryConstraint(this);
  50.         second.addNAryConstraint(this);
  51.     }

  52.     /**
  53.      * Get the first variable.
  54.      * @return First variable of the constraint
  55.      */
  56.     public Variable<T> getFirstVariable() {
  57.         return firstVariable_;
  58.     }

  59.     /**
  60.      * Get the second variable.
  61.      * @return Second variable of the constraint
  62.      */
  63.     public Variable<T> getSecondVariable() {
  64.         return secondVariable_;
  65.     }

  66.     /**
  67.      * {@inheritDoc}
  68.      */
  69.     @Override
  70.     public boolean isBinary() {
  71.         return true;
  72.     }

  73.     /**
  74.      * {@inheritDoc}
  75.      */
  76.     @Override
  77.     public boolean isTernary() {
  78.         return false;
  79.     }

  80.     /**
  81.      * {@inheritDoc}
  82.      */
  83.     @Override
  84.     public boolean isUnary() {
  85.         return false;
  86.     }

  87.     /**
  88.      * {@inheritDoc}
  89.      */
  90.     @Override
  91.     public boolean isUnsized() {
  92.         return false;
  93.     }

  94.     /**
  95.      * {@inheritDoc}
  96.      */
  97.     @Override
  98.     public boolean isVariableReferenced(final Variable<T> variable) {
  99.         return firstVariable_ == variable || secondVariable_ == variable;
  100.     }

  101.     /**
  102.      * {@inheritDoc}
  103.      */
  104.     @Override
  105.     public boolean revise() {

  106.         try {
  107.             // Initialize the list of reduced variables
  108.             clearRecentChangedVariables();
  109.             if (isPropagationEnabled()) {
  110.                 // This list must be updated by the propagation method
  111.                 propagate();
  112.             }
  113.             return !firstVariable_.getDomain().isEmpty() &&
  114.                     !secondVariable_.getDomain().isEmpty();

  115.         } catch (EmptyDomainException e) {
  116.             // If the domain has been emptied, return false
  117.             return false;
  118.         }
  119.     }

  120. }