Constraint.java

/* Copyright 2015 Laurent COCAULT
 * Licensed to Laurent COCAULT under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership. Laurent COCAULT licenses this file to You
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License.  You may obtain a copy of the
 * License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.csp.constraint.model;

/**
 * Represents a constraint of a constraint solving problem.
 */
public abstract class Constraint<T> {

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

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

    /**
     * Constructor.
     * @param name
     *            Name of the constraint
     */
    public Constraint(final String name) {
        name_ = name;
    }

    /**
     * Enable or disable the constraint propagation.
     * @param enable
     *            True if the constraint propagation is enabled, false otherwise
     */
    public void enablePropagate(final boolean enable) {
        enablePropagate_ = enable;
    }

    /**
     * Get the name of the constraint.
     * @return The name of the constraint
     */
    public String getName() {
        return name_;
    }

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

    /**
     * Tell if the propagation of the constraint is enabled.
     * @return True if the constraint propagation is enabled
     */
    public boolean isPropagationEnabled() {
        return enablePropagate_;
    }

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

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

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

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

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

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

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return name_;
    }

};