package edu.neu.ccs.evergreen.model; /** * A constraint which is a relation number and its variables. * */ public class Constraint { /** * The maximum rank of variables supported by the system */ public static final int MAX_RANK = 3; /** * The unsatisfied constraint relation number. */ public static final int UNSATISFIED = 0; /** * The satisfied constraint relation number. */ public static final int SATISFIED = 255; /** * Can this relation be reduced at all? * * @param relationNumber * Relation number to check * @return True if it can not be reduced */ public static boolean reducable(int relationNumber) { return ((relationNumber != UNSATISFIED) && (relationNumber != SATISFIED)); } private int id; private int relationNumber; private int weight; private int variables[]; /** * Create a new Relation Number. * * @param id * ID of the constraint * @param relationNumber * Relation number for the constraint * @param variables * Variables in the constraint * @param weight * Weight of the constraint */ public Constraint(int id, int relationNumber, int variables[], int weight) { this.id = id; this.relationNumber = relationNumber; verify(variables); this.variables = variables; this.weight = weight; } /** * Sanity check that the variables are valid. * * @param variables * Variables */ private void verify(int variables[]) { for (int i = 0; i < variables.length; i++) { for (int j = i + 1; j < variables.length; j++) { if (variables[i] == variables[j]) { throw new IllegalArgumentException("Variable " + variables[i] + " is equal to " + variables[j]); } } } } /** * Return the ID of this constraint. * * @return ID */ public int getID() { return id; } /** * Return the position of a variable in the constraint. * * @param variable * Variable * @return Position */ public int getPosition(int variable) { int position = 0; while (variables[position] != variable) { position++; } return position; } /** * The relation number of this constraint. * * @return Relation number */ public int getRelationNumber() { return relationNumber; } /** * The weight of this constraint. * * @return Relation weight */ public int getWeight() { return weight; } /** * Rank of constraint. * * @return Rank */ public int getRank() { return variables.length; } /** * Return the variable at the given position. * * @param position * Position * @return Variable */ public int getVariable(int position) { return variables[position]; } /** * @see java.lang.Object#toString() */ @Override public String toString() { StringBuffer result = new StringBuffer(); result.append(relationNumber); result.append(" "); result.append(weight); result.append(":"); for (int i = variables.length - 1; i >= 0; i--) { result.append(variables[i]); if (i != 0) { result.append(" "); } } return result.toString(); } }