UnsizedConstraint.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.Collection;
  20. import java.util.Iterator;
  21. import java.util.List;

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

  28.     /**
  29.      * List of constrained variables.
  30.      */
  31.     private List<Variable<T>> variables_;

  32.     /**
  33.      * Constructor of an unsized N-ary constraint.
  34.      * @param name
  35.      *            Name of the unsized N-ary constraint
  36.      * @param variables
  37.      *            List of variables to add to the constraint
  38.      */
  39.     public UnsizedConstraint(final String name,
  40.             final List<Variable<T>> variables) {
  41.         super(name);
  42.         variables_ = new ArrayList<Variable<T>>(variables);
  43.         for (Variable<T> variable : variables_) {
  44.             variable.addNAryConstraint(this);
  45.         }
  46.     }

  47.     /**
  48.      * Add a variable to the constraint.
  49.      * @param variable
  50.      *            Variable added to the constraint
  51.      */
  52.     protected void addVariable(final Variable<T> variable) {
  53.         variables_.add(variable);
  54.         variable.addNAryConstraint(this);
  55.     }

  56.     /**
  57.      * Add a collection of variables to the constraint.
  58.      * @param variables
  59.      *            Variables added to the constraint
  60.      */
  61.     protected void addVariables(final Collection<Variable<T>> variables) {
  62.         variables_.addAll(variables);
  63.         for (Variable<T> variable : variables) {
  64.             variable.addNAryConstraint(this);
  65.         }
  66.     }

  67.     /**
  68.      * Get the list of constrained variables.
  69.      * @return The list of constrained variables
  70.      */
  71.     protected final List<Variable<T>> getVariables() {
  72.         return variables_;
  73.     }

  74.     /**
  75.      * Get an iterator on the constrained variables.
  76.      * @return Iterator constrained variables
  77.      */
  78.     public Iterator<Variable<T>> getVariablesIterator() {
  79.         return variables_.iterator();
  80.     }

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

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

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

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

  109.     /**
  110.      * {@inheritDoc}
  111.      */
  112.     @Override
  113.     public boolean isVariableReferenced(final Variable<T> variable) {
  114.         return variables_.contains(variable);
  115.     }

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

  121.         try {

  122.             // Assumed consistency
  123.             boolean consistent = true;

  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.             // Check the constraint consistency
  131.             final Iterator<Variable<T>> variables = variables_.iterator();
  132.             while (consistent && variables.hasNext()) {
  133.                 consistent = !variables.next().getDomain().isEmpty();
  134.             }

  135.             return consistent;

  136.         } catch (EmptyDomainException e) {
  137.             // If the domain has been emptied, return false
  138.             return false;
  139.         }
  140.     }
  141. }