Constraint.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 constraint of a constraint solving problem.
  20.  */
  21. public abstract class Constraint<T> {

  22.     /** Name of the constraint. */
  23.     private String name_;

  24.     /** Enable the constraint to propagate. */
  25.     private boolean enablePropagate_;

  26.     /**
  27.      * Constructor.
  28.      * @param name
  29.      *            Name of the constraint
  30.      */
  31.     public Constraint(final String name) {
  32.         name_ = name;
  33.     }

  34.     /**
  35.      * Enable or disable the constraint propagation.
  36.      * @param enable
  37.      *            True if the constraint propagation is enabled, false otherwise
  38.      */
  39.     public void enablePropagate(final boolean enable) {
  40.         enablePropagate_ = enable;
  41.     }

  42.     /**
  43.      * Get the name of the constraint.
  44.      * @return The name of the constraint
  45.      */
  46.     public String getName() {
  47.         return name_;
  48.     }

  49.     /**
  50.      * Tests if the constraint is binary.
  51.      * @return True if the constraint is a binary constraint
  52.      */
  53.     public abstract boolean isBinary();

  54.     /**
  55.      * Tell if the propagation of the constraint is enabled.
  56.      * @return True if the constraint propagation is enabled
  57.      */
  58.     public boolean isPropagationEnabled() {
  59.         return enablePropagate_;
  60.     }

  61.     /**
  62.      * Tests if the constraint is ternary.
  63.      * @return True if the constraint is a ternary constraint
  64.      */
  65.     public abstract boolean isTernary();

  66.     /**
  67.      * Tests if the constraint is unary.
  68.      * @return True if the constraint is an unary constraint
  69.      */
  70.     public abstract boolean isUnary();

  71.     /**
  72.      * Tests if the constraint is unsized.
  73.      * @return True if the constraint is an unsized N-ary constraint
  74.      */
  75.     public abstract boolean isUnsized();

  76.     /**
  77.      * Indicates if the given variable is referenced by the constraint, ie if
  78.      * the constraint may reduce it.
  79.      * @param variable
  80.      *            Variable whose reference is checked
  81.      * @return True if the variable is referenced by the constraint
  82.      */
  83.     abstract boolean isVariableReferenced(Variable<T> variable);

  84.     /**
  85.      * Propagate the constraint (reduce the domain of each variable associated
  86.      * with the constraint).
  87.      */
  88.     public abstract void propagate();

  89.     /**
  90.      * Revise the constraint consistency.
  91.      * @return True if the constraint is consistent (the domain of each variable
  92.      *         associated with the constraint is not empty)
  93.      */
  94.     public abstract boolean revise();

  95.     /**
  96.      * {@inheritDoc}
  97.      */
  98.     @Override
  99.     public String toString() {
  100.         return name_;
  101.     }

  102. };