Search in sources :

Example 1 with ComboBoxItem

use of wekapar.ComboBoxItem in project wekapar by gems-uff.

the class PostprocessAssociationsPanel method loadRules.

/**
 * Loads rules into a JTable
 */
public static void loadRules() {
    /* Clears table model by setting a new empty one */
    table.setModel(new RulesTableModel());
    /* Disable components while running loading process */
    setEnabledAll(rulesPanel, false);
    /* Reset components values */
    comboFilter.removeAllItems();
    antecedentMap.removeAll();
    consequentMap.removeAll();
    spinDecimal.reset();
    /* Reset counters */
    lblTotalRulesValue.setText("0");
    lblFilteredRulesValue.setText("0");
    /* The association rules */
    List<AssociationRule> rulesList = associationRules.getRules();
    /* Create a new table model */
    final RulesTableModel tableModel = new RulesTableModel();
    /* Metric names */
    String[] metrics = rulesList.get(0).getMetricNamesForRule();
    /* Columns */
    tableModel.addColumn("Antecedent (X)");
    tableModel.addColumn("Consequent (Y)");
    tableModel.addColumn("Support");
    for (String m : metrics) {
        tableModel.addColumn(m);
    }
    /* Adds rules to table */
    String antecedent = "";
    String consequent = "";
    double support;
    double metricValue;
    for (AssociationRule r : rulesList) {
        /* Antecedent */
        antecedent = "";
        for (Item p : r.getPremise()) {
            antecedent += p + " ";
            antecedentMap.addAttribute(p.toString());
        }
        /* Consequent */
        consequent = "";
        for (Item c : r.getConsequence()) {
            consequent += c + " ";
            consequentMap.addAttribute(c.toString());
        }
        /* Support */
        support = ((double) r.getTotalSupport()) / r.getTotalTransactions();
        /* List containing each column's content to be added to table */
        List<Object> row = new ArrayList<Object>();
        /* Adds antecedent, consequent and support */
        row.add(antecedent.trim());
        row.add(consequent.trim());
        row.add(support);
        /* Adds metric values */
        for (String m : metrics) {
            try {
                metricValue = r.getNamedMetricValue(m);
                row.add(metricValue);
            } catch (Exception e) {
                log.statusMessage("FAILED! See log.");
                log.logMessage(e.getMessage());
            }
        }
        /* Adds a row with values to table */
        tableModel.addRow(row.toArray());
    }
    /* Set TableModel */
    table.setModel(tableModel);
    /* Get column model */
    final RulesTableColumnModel columnModel = (RulesTableColumnModel) table.getColumnModel();
    /* Set minimum widths for antecedent and consequent columns */
    columnModel.getColumn(0).setMinWidth(220);
    columnModel.getColumn(1).setMinWidth(120);
    /* Set custom renderer for antecedent and consequent cells */
    columnModel.getColumn(0).setCellRenderer(new RulesTableCellRenderer());
    columnModel.getColumn(1).setCellRenderer(new RulesTableCellRenderer());
    for (int i = 2; i < table.getColumnCount(); i++) {
        /* Set custom renderer for metric's cells */
        columnModel.getColumn(i).setCellRenderer(decimalCellRenderer);
        /* Set minimum size values for metric's columns */
        columnModel.getColumn(i).setPreferredWidth(105);
        columnModel.getColumn(i).setMinWidth(70);
        columnModel.getColumn(i).setMaxWidth(120);
    }
    /* Set table's RowSorter for sorting and filterig */
    sorter = new TableRowSorter<TableModel>(tableModel);
    table.setRowSorter(sorter);
    table.getRowSorter().addRowSorterListener(new RowSorterListener() {

        // TODO: Improve sorting performance.
        @Override
        public void sorterChanged(RowSorterEvent e) {
            if (e.getType() == Type.SORTED) {
                Enumeration<TableColumn> columns = columnModel.getColumns();
                TableColumn column;
                while (columns.hasMoreElements()) {
                    column = columns.nextElement();
                    if (column.getCellRenderer() instanceof ProgressCellRenderer) {
                        column.setCellRenderer(decimalCellRenderer);
                    }
                }
                List<?> sortKeys = e.getSource().getSortKeys();
                if (sortKeys.size() > 0) {
                    RowSorter.SortKey key = (SortKey) sortKeys.get(0);
                    int keyColumn = key.getColumn();
                    String columnName = tableModel.getColumnName(keyColumn);
                    if (columnModel.hasColumn(columnName)) {
                        int columnIndex = columnModel.getColumnIndex(columnName);
                        if (tableModel.getColumnClass(columnIndex) == Double.class) {
                            columnModel.getColumn(columnIndex).setCellRenderer(new ProgressCellRenderer(table, columnIndex));
                        }
                    }
                }
            }
        }
    });
    /* Get total values for results */
    lblTotalRulesValue.setText(String.valueOf(rulesList.size()));
    lblFilteredRulesValue.setText(String.valueOf(table.getRowCount()));
    /* Initializes attribute's component */
    comboRuleSide.removeAllItems();
    comboRuleSide.addItem(new ComboBoxItem(0, "Antecedent (X)", "X"));
    comboRuleSide.addItem(new ComboBoxItem(1, "Consequent (Y)", "Y"));
    /* Initializes filter parameters for metrics */
    initMetrics();
    setEnabledAll(rulesPanel, true);
    comboLogicalOperator.setEnabled(false);
    btnApply.setEnabled(false);
    btnClear.setEnabled(false);
    btnApplyMetric.setEnabled(false);
    btnResetMetric.setEnabled(false);
}
Also used : ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) ProgressCellRenderer(wekapar.table.ProgressCellRenderer) Item(weka.associations.Item) ComboBoxItem(wekapar.ComboBoxItem) JMenuItem(javax.swing.JMenuItem) RulesTableModel(wekapar.table.RulesTableModel) ArrayList(java.util.ArrayList) List(java.util.List) RulesTableColumnModel(wekapar.table.RulesTableColumnModel) RowSorterListener(javax.swing.event.RowSorterListener) Enumeration(java.util.Enumeration) RulesTableCellRenderer(wekapar.table.RulesTableCellRenderer) TableColumn(javax.swing.table.TableColumn) IOException(java.io.IOException) Point(java.awt.Point) AssociationRule(weka.associations.AssociationRule) ComboBoxItem(wekapar.ComboBoxItem) RowSorterEvent(javax.swing.event.RowSorterEvent) RulesTableModel(wekapar.table.RulesTableModel) TableModel(javax.swing.table.TableModel)

Aggregations

Point (java.awt.Point)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 JMenuItem (javax.swing.JMenuItem)1 SortKey (javax.swing.RowSorter.SortKey)1 RowSorterEvent (javax.swing.event.RowSorterEvent)1 RowSorterListener (javax.swing.event.RowSorterListener)1 TableColumn (javax.swing.table.TableColumn)1 TableModel (javax.swing.table.TableModel)1 AssociationRule (weka.associations.AssociationRule)1 Item (weka.associations.Item)1 ComboBoxItem (wekapar.ComboBoxItem)1 ProgressCellRenderer (wekapar.table.ProgressCellRenderer)1 RulesTableCellRenderer (wekapar.table.RulesTableCellRenderer)1 RulesTableColumnModel (wekapar.table.RulesTableColumnModel)1 RulesTableModel (wekapar.table.RulesTableModel)1