NAryConstraint.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. import java.util.ArrayList;
  19. import java.util.List;

  20. /**
  21.  * Represents a N-ary constraint in a constraint solving problem To define a new
  22.  * N-ary constraint, inherit this class and define the revise and propagate
  23.  * methods.
  24.  */
  25. public abstract class NAryConstraint<T> extends Constraint<T> {

  26.     /**
  27.      * List of variables whose domain has been modified by the last call to
  28.      * revise.
  29.      */
  30.     private List<Variable<T>> recentChangedVariables_;

  31.     /**
  32.      * Constructor of a N-ary constraint.
  33.      * @param name
  34.      *            Name of the N-ary constraint
  35.      */
  36.     public NAryConstraint(final String name) {
  37.         super(name);
  38.         recentChangedVariables_ = new ArrayList<Variable<T>>();
  39.     }

  40.     /**
  41.      * Add a recently changed variable.
  42.      * @param changed
  43.      *            Variable recently changed
  44.      */
  45.     protected void addRecentChangedVariable(final Variable<T> changed) {
  46.         recentChangedVariables_.add(changed);
  47.     }

  48.     /**
  49.      * Clear the list of recently changed variables.
  50.      */
  51.     protected void clearRecentChangedVariables() {
  52.         recentChangedVariables_.clear();
  53.     }

  54.     /**
  55.      * Get the list of recently changed variables.
  56.      * @return The list of variables changed by the last propagation
  57.      */
  58.     public List<Variable<T>> getRecentChangedVariables() {
  59.         return recentChangedVariables_;
  60.     }

  61.     /**
  62.      * {@inheritDoc}
  63.      */
  64.     @Override
  65.     public boolean isVariableReferenced(final Variable<T> variable) {
  66.         return recentChangedVariables_.contains(variable);
  67.     }

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

  75. }