All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metacsp.framework.Constraint Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-2013 Federico Pecora 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
package org.metacsp.framework;

import java.awt.Color;
import java.awt.Paint;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.HashSet;
import java.util.logging.Logger;

import org.metacsp.utility.logging.MetaCSPLogging;

/**
 * This class is used to represent n-ary constraints in the MetaCSP framework.
 * The scope of a constraint is the set of all {@link Variable}s involved in the constraint.
 * An important subclass is {@link BinaryConstraint}, which provides support for constraints
 * whose scope has size two. 
 * 
 * @author Federico Pecora
 *
 */
public abstract class Constraint implements Cloneable, Serializable {
	
	private static final long serialVersionUID = 8278654163054138810L;
	private HashSet solversToSkip = null;

	/**
	 * Progressive ID of a constraint
	 */
	public static int numIDs = 0;
	
	protected boolean masked = false;
	
	protected int id;
	
	protected Variable[] scope;
	
	protected Object annotation;
	
	protected boolean autoRemovable = false;
	
	protected Paint color= Color.black;
	
	protected transient Logger logger = MetaCSPLogging.getLogger(this.getClass());
	
	/**
	 * Provide a list of solvers that should not process this constraint.
	 * @param solvers Solvers that should not process this constraint.
	 */
	public void skipSolver(ConstraintSolver ... solvers) {
		if (solversToSkip == null) solversToSkip = new HashSet();
		for (ConstraintSolver solver : solvers) solversToSkip.add(solver);
	}
	
	/**
	 * Returns true iff this {@link Constraint} is invisible to the solver (masked).
	 * @return true iff this {@link Constraint} is invisible to the solver (masked).
	 */
	public boolean isMasked() { return masked; }
	
	/**
	 * Makes this {@link Constraint} invisible to the solver (masked).
	 */
	public void mask() { this.masked = true; }

	/**
	 * Makes this {@link Constraint} invisible to the solver (masked).
	 */
	public void unmask() { this.masked = false; }

	/**
	 * Returns true iff this {@link Constraint}'s scope only refers to one {@link Variable}. 
	 * @return true iff this {@link Constraint}'s scope only refers to one {@link Variable}.
	 */
	public boolean isUnary() {
		Variable oneVar = this.scope[0];
		for (int i = 1; i < this.scope.length; i++) {
			if (!this.scope[i].equals(oneVar)) return false;
		}
		return true;
	}
	
	/**
	 * Assess whether a given solver is allowed to process this constraint.
	 * @param cs The {@link ConstraintSolver} to check.
	 * @return true iff the given solver will process this constraint.
	 */
	public boolean isSkippableSolver(ConstraintSolver cs) {
		return solversToSkip != null && solversToSkip.contains(cs);
	}
	
	/**
	 * Get this {@link Constraint}'s annotation (can be any {@link Object} and used in any way).
	 * @return This {@link Constraint}'s annotation.
	 */
	public Object getAnnotation() { return annotation; }

	/**
	 * Set this {@link Constraint}'s annotation (can be any {@link Object} and used in any way).
	 * @param o This {@link Constraint}'s annotation.
	 */
	public void setAnnotation(Object o) { annotation = o; }
	
	/**
	 * Returns the ID of this {@link Constraint}.
	 * @return The ID of this {@link Constraint}.
	 */
	public int getID() {return this.id;}
	
	/**
	 * Every {@link Constraint} should implement a toString method (used by the
	 * {@link ConstraintNetwork} rendering methods).
	 * @return the {@link String} representation of this {@link Constraint}. 
	 */
	public abstract String toString();
	
	/**
	 * Every {@link Constraint} should implement this method - its value is
	 * what is drawn by the {@link ConstraintNetwork} rendering methods.
	 * @return a {@link String} representation of the constraint to be used for
	 * {@link ConstraintNetwork} rendering.
	 */
	public abstract String getEdgeLabel();

	/**
	 * Returns the scope of this {@link Constraint}.
	 * @return the scope of this {@link Constraint} (an array of size 2 if this is a
	 * {@link BinaryConstraint}).
	 */
	public Variable[] getScope() {
		return scope;
	}
	
	/**
	 * Set the scope of this {@link Constraint} to a given array of {@link Variable}s.
	 * @param scope The desired new scope of this {@link Constraint}. 
	 */
	public void setScope(Variable[] scope) {
		this.scope = scope;
	}
	
	@Override
	public abstract Object clone();
	
	/**
	 * Method for assessing the "equivalence" between two constraints.  This is not used
	 * for lookups nor comparisons (hence, it is not the equals method).  
	 * @param c The {@link Constraint} to compare against.
	 * @return true iff the two constraints are considered to be equivalent.
	 */
	public abstract boolean isEquivalent(Constraint c);
	
	/**
	 * Get the description of this {@link Constraint}'s type.
	 * @return The description of this {@link Constraint}'s type.
	 */
	public String getDescription() {
		return this.getClass().getSimpleName();
	}

	public boolean isAutoRemovable() {
		return autoRemovable;
	}

	public void setAutoRemovable(boolean autoRemovable) {
		this.autoRemovable = autoRemovable;
	}

	public Paint getColor() {
		return color;
	}

	public void setColor(Paint color) {
		this.color = color;
	}
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		in.defaultReadObject();
		logger = MetaCSPLogging.getLogger(this.getClass());
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy