/* * @(#)Tabulator.java 0.0.1 99/07/30 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats; import java.util.*; import javax.swing.event.*; import lang.*; /** * ... *

06/02/99: We need to optimize tableChanged() so that we don't * retabulate from scratch when INSERT events come in. * * @version 0.0.1 07/30/99 * @author Willie Wheeler */ public class Tabulator implements TableModelListener { static boolean debug = false; /** @serial */ protected DataTableModel dataIn; /** @serial */ protected int[] varIndices; /** @serial */ protected ContingencyTableModel dataOut; /** * Builds a tabulator to compute the joint frequency and probability * distributions for all variables in the specified data table. * This assumes that all of the variables are TextVariables. * * @param dataIn data table over which to compute the joint * frequency and probability distributions */ public Tabulator(DataTableModel dataIn) { setDataIn(dataIn); } /** * Builds a tabulator to compute the joint frequency and probability * distributions for a restricted list of variables in the specified data * table. * * @param dataIn data table over which to compute the joint * frequency and probability distributions * @param variableIndices array of variable indices */ public Tabulator(DataTableModel dataIn, int[] variableIndices) { setDataIn(dataIn, variableIndices); } public void setDataIn(DataTableModel dataIn) { int numVars = dataIn.getColumnCount(); varIndices = new int[numVars]; for (int i = 0; i < numVars; i++) { varIndices[i] = i; } setDataIn(dataIn, varIndices); } public void setDataIn(DataTableModel dataIn, int[] variableIndices) { // Install the new dataIn, etc. if (this.dataIn != null) { this.dataIn.removeTableModelListener(this); } this.dataIn = dataIn; dataIn.addTableModelListener(this); this.varIndices = variableIndices; // Build and install a new dataOut. Vector factors = new Vector(); for (int i = 0; i < varIndices.length; i++) { factors.add((TextVariable)dataIn.getVariable(i)); } dataOut = new ContingencyTableModel(factors); tabulate(); } public void tabulate() { int numFactors = varIndices.length; int numData = dataIn.getNumData(); int numTotalStates = dataOut.getRowCount(); // Build and fill an array of frequencies. int[] freqs = new int[numTotalStates]; for (int i = 0; i < numData; i++) { // Build the current total state. int[] totalState = new int[numFactors]; for (int j = 0; j < numFactors; j++) { Object value = dataIn.getValueAt(i, varIndices[j]); totalState[j] = ((Integer)value).intValue(); } // Increment the state's frequency. freqs[dataOut.rowIndex(totalState)]++; } // Put frequencies and estimates into dataOut. Vector tableData = dataOut.getDataVector(); for (int i = 0; i < numTotalStates; i++) { Vector rowData = (Vector)tableData.get(i); rowData.set(dataOut.getFrequencyColumnIndex(), new Integer(freqs[i])); } // Update the estimates. dataOut.updateEstimates(); } public ContingencyTableModel getDataOut() { return dataOut; } public void tableChanged(TableModelEvent e) { tabulate(); } }