UnaryConstraint.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 an unary constraint in a constraint solving problem. To define a
  20.  * new unary constraint, inherit this class and define the propagate method.
  21.  */
  22. public abstract class UnaryConstraint<T> extends Constraint<T> {

  23.     /**
  24.      * An unary constraint is associated with only one variable.
  25.      */
  26.     private Variable<T> variable_;

  27.     /**
  28.      * Constructor of an unary constraint. It references the variable given as
  29.      * parameters.
  30.      * @param name
  31.      *            Name of the binary constraint
  32.      * @param variable
  33.      *            Reference of the variable
  34.      */
  35.     public UnaryConstraint(final String name, final Variable<T> variable) {
  36.         super(name);
  37.         variable_ = variable;
  38.     }

  39.     /**
  40.      * Get the variable associated with the constraint.
  41.      * @return The reference of the variable associated with the constraint
  42.      */
  43.     public Variable<T> getVariable() {
  44.         return variable_;
  45.     }

  46.     /**
  47.      * {@inheritDoc}
  48.      */
  49.     @Override
  50.     public boolean isBinary() {
  51.         return false;
  52.     }

  53.     /**
  54.      * {@inheritDoc}
  55.      */
  56.     @Override
  57.     public boolean isTernary() {
  58.         return false;
  59.     }

  60.     /**
  61.      * {@inheritDoc}
  62.      */
  63.     @Override
  64.     public boolean isUnary() {
  65.         return true;
  66.     }

  67.     /**
  68.      * {@inheritDoc}
  69.      */
  70.     @Override
  71.     public boolean isUnsized() {
  72.         return false;
  73.     }

  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     @Override
  78.     public boolean isVariableReferenced(final Variable<T> variable) {
  79.         return variable_ == variable;
  80.     }

  81.     /**
  82.      * {@inheritDoc}
  83.      */
  84.     @Override
  85.     public boolean revise() {
  86.         try {
  87.             // Propagate and check if the domain is empty
  88.             if (isPropagationEnabled()) {
  89.                 propagate();
  90.             }
  91.             return !variable_.getDomain().isEmpty();
  92.         } catch (EmptyDomainException e) {
  93.             // If the domain has been emptied, return false
  94.             return false;
  95.         }
  96.     }

  97. }