package edu.neu.ccs.evergreen.model; import java.util.Set; /** * Used to capture how a value was assigned to a variable. */ public class Assignment { private int variable; private int value; private boolean decision; private Set forcedBy; private Constraint forcingConstraint; /** * Create a simple assignment that attaches a value to a variable. * * @param variable * Variable * @param value * Value */ public Assignment(int variable, int value) { this(variable, value, true, null, null); } /** * Create a complex assignment that tracks what variables forced the * variable to the set and the constraint the caused it to be forced. * * @param variable * Variable * @param value * Value * @param forcedBy * Variables that forced it * @param forcingConstraint * Constraint that did the forcing */ public Assignment(int variable, int value, Set forcedBy, Constraint forcingConstraint) { this(variable, value, false, forcedBy, forcingConstraint); } /** * An assignment of a value to a variable. This also indicates if the * assignment was forced or was a decision variable. * * @param variable * Variable * @param value * Value * @param decision * True if decided */ private Assignment(int variable, int value, boolean decision, Set forcedBy, Constraint forcingConstraint) { if (variable < 0) { throw new IllegalArgumentException("Variable must be positive"); } this.variable = variable; this.value = value; this.decision = decision; this.forcedBy = forcedBy; this.forcingConstraint = forcingConstraint; } /** * Return the variable for use in creating a super resolvent. This will * reverse its value if it was a decision variable otherwise it returns it * as is. * * @return Signed negated variable */ public int getForcedVariable() { if (!decision) { return variable; } if (value == 0) { return variable; } return -variable; } /** * Raw variable. * * @return Variable */ public int getVariable() { return variable; } /** * Value assigned to the variable. * * @return Value */ public int getValue() { return value; } /** * Was this a decision variable. * * @return True if decision; false otherwise */ public boolean getDecision() { return decision; } /** * Set if this was a decision variable. * * @param decision * True if a decision variable */ public void setDecision(boolean decision) { this.decision = decision; } /** * Return the variables that forced this variable to be set. * * @return Forcing variables */ public Set getForcedBy() { return forcedBy; } /** * Return the constraint that caused the variable to be forced. * * @return Constraint */ public Constraint getForcingConstraint() { return forcingConstraint; } /** * Add a set of variables to those that caused the forcing. * * @param forcedBy * Forced by */ public void addForcedBy(Set forcedBy) { this.forcedBy.addAll(forcedBy); } /** * @see java.lang.Object#toString() */ @Override public String toString() { StringBuffer result = new StringBuffer(); result.append(variable); if (decision) { result.append("*"); } result.append("="); result.append(value); return result.toString(); } }