TernaryConstraint.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 ternary constraint in a constraint solving problem To define a
  20.  * new ternary constraint, inherit this class and define the propagate method
  21.  * that must fill the list _recentChangedVariables.
  22.  */
  23. public abstract class TernaryConstraint<T> extends NAryConstraint<T> {

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

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

  34.     /**
  35.      * A ternary constraint is associated with three variables. This attribute
  36.      * is the third variable.
  37.      */
  38.     private Variable<T> thirdVariable_;

  39.     /**
  40.      * Constructor of a ternary constraint. It references three variables given
  41.      * as parameters.
  42.      * @param name
  43.      *            Name of the ternary constraint
  44.      * @param first
  45.      *            Reference of the first variable
  46.      * @param second
  47.      *            Reference of the second variable
  48.      * @param third
  49.      *            Reference of the third variable
  50.      */
  51.     public TernaryConstraint(final String name, final Variable<T> first,
  52.             final Variable<T> second, final Variable<T> third) {
  53.         super(name);
  54.         firstVariable_ = first;
  55.         secondVariable_ = second;
  56.         thirdVariable_ = third;
  57.         first.addNAryConstraint(this);
  58.         second.addNAryConstraint(this);
  59.         third.addNAryConstraint(this);
  60.     }

  61.     /**
  62.      * Get the first variable.
  63.      * @return First variable of the constraint
  64.      */
  65.     public Variable<T> getFirstVariable() {
  66.         return firstVariable_;
  67.     }

  68.     /**
  69.      * Get the second variable.
  70.      * @return Second variable of the constraint
  71.      */
  72.     public Variable<T> getSecondVariable() {
  73.         return secondVariable_;
  74.     }

  75.     /**
  76.      * Get the third variable.
  77.      * @return Third variable of the constraint
  78.      */
  79.     public Variable<T> getThirdVariable() {
  80.         return thirdVariable_;
  81.     }

  82.     /**
  83.      * {@inheritDoc}
  84.      */
  85.     @Override
  86.     public boolean isBinary() {
  87.         return false;
  88.     }

  89.     /**
  90.      * {@inheritDoc}
  91.      */
  92.     @Override
  93.     public boolean isTernary() {
  94.         return true;
  95.     }

  96.     /**
  97.      * {@inheritDoc}
  98.      */
  99.     @Override
  100.     public boolean isUnary() {
  101.         return false;
  102.     }

  103.     /**
  104.      * {@inheritDoc}
  105.      */
  106.     @Override
  107.     public boolean isUnsized() {
  108.         return false;
  109.     }

  110.     /**
  111.      * {@inheritDoc}
  112.      */
  113.     @Override
  114.     public boolean isVariableReferenced(final Variable<T> variable) {
  115.         return firstVariable_ == variable || secondVariable_ == variable ||
  116.                 thirdVariable_ == variable;
  117.     }

  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     @Override
  122.     public boolean revise() {

  123.         try {
  124.             // Initialize the list of reduced variables
  125.             clearRecentChangedVariables();
  126.             if (isPropagationEnabled()) {
  127.                 // This list must be updated by the propagation method
  128.                 propagate();
  129.             }

  130.             return !firstVariable_.getDomain().isEmpty() &&
  131.                     !secondVariable_.getDomain().isEmpty() &&
  132.                     !thirdVariable_.getDomain().isEmpty();

  133.         } catch (EmptyDomainException e) {
  134.             // If the domain has been emptied, return false
  135.             return false;
  136.         }
  137.     }

  138. }