/*
 * @(#)SetBuilderExercise.java		0.0.0 99/08/04
 *
 * Copyright (c) 1999 by Willie Wheeler.  All rights reserved.
 */
package stats.exercises;
import java.io.*;
import java.util.*;
import exercise.*;
import stats.*;
/**
 * Specification for a SetBuilder exercise.  The student must use
 * the SetBuilder to build a data set satisfying instance-specific
 * constraints.
 *
 * @version     0.0.0 08/04/99
 * @author      Willie Wheeler
 */
public class SetBuilderExercise implements Exercise {
    protected VariablesVector	variables;
    protected Vector		atoms;
    protected Vector		constraints;
    public SetBuilderExercise(Vector variables, Vector atoms) {
	this.variables = new VariablesVector(variables);
	this.atoms = atoms;
    }
    public Vector getVariables() { return variables; }
    public Vector getAtoms() { return atoms; }
    public void setConstraints(Vector constraints) {
	this.constraints = constraints;
    }
    public Vector getConstraints() { return constraints; }
    /**
     * Specifies a constraint on the data set.  Each constraint maps a
     * (state, conditioning state) pair to a target ratio in the range 0.0 -
     * 1.0.
     */
    public class Constraint implements Serializable {
	protected int[]		state;
	protected int[]		conditioningState;
	protected double	target;
	public Constraint(int[] state, double target) {
	    this(state, null, target);
	}
	public Constraint(int[] state,
			  int[] conditioningState,
			  double target) {
	    this.state = state;
	    this.conditioningState = conditioningState;
	    this.target = target;
	}
	public int[] getState() { return state; }
	public int[] getConditioningState() { return conditioningState; }
	public double getTarget() { return target; }
	public String toString() {
	    // Get the state and conditioning states as strings.
	    String stateString =
		variables.getStateAsString(state, true, " & ");
	    String condString =
		variables.getStateAsString(conditioningState, true, " & ");
	    // Build the string representation of the constraint.
	    StringBuffer buffer = new StringBuffer("fr(" + stateString);
	    if (! condString.equals("")) { buffer.append(" | " + condString); }
	    buffer.append(") = " + target);
	    // And finally return it.
	    return buffer.toString();
	}
    }
}