/* * @(#)MVDTableModel.java 0.0.1 99/08/06 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats; import java.util.*; import javax.swing.table.*; /** * A TableModel implementation of a multivariate distribution. The * independent variables are TextVariables and the dependent variables * are NumericVariables. *

* This class is in progress. Its methods are not yet stable. * * @version 0.0.1 08/06/99 * @author Willie Wheeler */ public class MVDTableModel extends DefaultTableModel { /** * Vector of independent variables. * * @serial */ protected Vector independents; /** * Vector of dependent variables. * * @serial */ protected Vector dependents; /** * Array for mapping combinations of independent variable values to row * indices. The row index for a given combination is the dot product of * that combination and the array of place values. * * @serial */ protected int[] placeValues; // // Constructors. // public MVDTableModel() { } public MVDTableModel(Vector independents, Vector dependents) { setVariables(independents, dependents); } public MVDTableModel(TextVariable[] independents, NumericVariable[] dependents) { setVariables(independents, dependents); } // // Accessing variables. // public void setVariables(Vector independents, Vector dependents) { this.independents = new VariablesVector(independents); this.dependents = new VariablesVector(dependents); int numIndependents = independents.size(); int numDependents = dependents.size(); // Set the column IDs. Vector columnIDs = new Vector(independents); columnIDs.addAll(dependents); setColumnIdentifiers(columnIDs); // Compute the place values. placeValues = new int[numIndependents]; placeValues[numIndependents-1] = 1; for (int i = numIndependents-2; i >= 0; i--) { placeValues[i] = getIndependent(i+1).getNumValues() * placeValues[i+1]; } int numRows = getIndependent(0).getNumValues() * placeValues[0]; // Set the table data. setNumRows(numRows); for (int i = 0; i < numRows; i++) { Vector rowData = (Vector)getDataVector().get(i); for (int j = 0; j < numIndependents; j++) { int numValues = getIndependent(j).getNumValues(); rowData.set(j, new Integer((i / placeValues[j]) % numValues)); } for (int j = 0; j < numDependents; j++) { Object clearValue = getDependent(j).getClearValue(); rowData.set(numIndependents + j, clearValue); } } } public void setVariables(TextVariable[] independents, NumericVariable[] dependents) { setVariables(convertToVector(independents), convertToVector(dependents)); } public int getNumIndependents() { return independents.size(); } public Vector getIndependents() { return independents; } public TextVariable getIndependent(int index) { return (TextVariable)independents.get(index); } public int getNumDependents() { return dependents.size(); } public Vector getDependents() { return dependents; } public NumericVariable getDependent(int index) { return (NumericVariable)dependents.get(index); } // // Accessing values and distributions. // public boolean isCellEditable(int rowIndex, int columnIndex) { return (columnIndex >= getNumIndependents()); } public void setValueAt(Object value, int rowIndex, int columnIndex) { if (! isCellEditable(rowIndex, columnIndex)) { String msg = "Cell at row " + rowIndex + ", column " + columnIndex + " cannot be edited."; throw new RuntimeException(msg); } Variable var = (Variable)super.columnIdentifiers.get(columnIndex); if (var.checkValue(value)) { super.setValueAt(var.filterValue(value), rowIndex, columnIndex); } } public double getValue(int dependentIndex, int[] totalState) { int rowIndex = rowIndex(totalState); int colIndex = getNumIndependents() + dependentIndex; return ((Number)getValueAt(rowIndex, colIndex)).doubleValue(); } public double[] getDistribution(int dependentIndex, int[][] totalStates) { int numValues = totalStates.length; double[] dist = new double[numValues]; for (int i = 0; i < numValues; i++) { dist[i] = getValue(dependentIndex, totalStates[i]); } return dist; } // // Converting between row indices and total states. // public int rowIndex(int[] totalState) { int rowIndex = 0; for (int i = 0; i < getNumIndependents(); i++) { rowIndex += totalState[i] * placeValues[i]; } return rowIndex; } public int[] totalState(int rowIndex) { int numIndependents = getNumIndependents(); int[] totalState = new int[numIndependents]; for (int i = 0; i < numIndependents; i++) { totalState[i] = (rowIndex / placeValues[i]) % getIndependent(i).getNumValues(); } return totalState; } // // Methods for obtaining string representations of variables, conditions, // and states. // public String getIndependentsAsString(int[] independentIndices) { VariablesVector vv = (VariablesVector)independents; return vv.getVariableNamesAsString(independentIndices); } public String getConditionAsString(int[] conditioningState) { VariablesVector vv = (VariablesVector)independents; return vv.getStateAsString(conditioningState, true, " & "); } public String getStateAsString(int[] state) { VariablesVector vv = (VariablesVector)independents; return vv.getStateAsString(state, false, ", "); } public String[] getStatesAsStrings(int[][] states) { VariablesVector vv = (VariablesVector)independents; return vv.getStatesAsStrings(states, false, ", "); } }