Search in sources :

Example 1 with RulesTableColumnModel

use of wekapar.table.RulesTableColumnModel 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)

Example 2 with RulesTableColumnModel

use of wekapar.table.RulesTableColumnModel in project wekapar by gems-uff.

the class PostprocessAssociationsPanel method buildMetricsFilter.

/**
 * Builds a {@link RowFilter} according to selected metrics minimum values.
 *
 * @return the row filter object
 */
private static RowFilter<? super TableModel, ? super Integer> buildMetricsFilter() {
    RowFilter<Object, Object> rowFilter = RowFilter.regexFilter(".*");
    List<RowFilter<Object, Object>> metricFilters = new ArrayList<RowFilter<Object, Object>>();
    List<RowFilter<Object, Object>> metricFilter;
    RowFilter<Object, Object> filterEqual;
    RowFilter<Object, Object> filterAfter;
    RulesTableColumnModel columnModel = (RulesTableColumnModel) table.getColumnModel();
    MetricSpinner spinner;
    String key;
    double value;
    int columnIndex;
    for (Map.Entry<String, JSpinner> entry : metricSpinnerMap.entrySet()) {
        metricFilter = new ArrayList<RowFilter<Object, Object>>();
        key = entry.getKey();
        spinner = (MetricSpinner) entry.getValue();
        value = (Double) spinner.getValue();
        if (columnModel.hasColumn(key)) {
            if (!spinner.isMinimumSelected()) {
                columnIndex = table.getColumnModel().getColumnIndex(key);
                filterEqual = RowFilter.numberFilter(ComparisonType.EQUAL, value, table.convertColumnIndexToModel(columnIndex));
                filterAfter = RowFilter.numberFilter(ComparisonType.AFTER, value, table.convertColumnIndexToModel(columnIndex));
                metricFilter.add(filterEqual);
                metricFilter.add(filterAfter);
                metricFilters.add(RowFilter.orFilter(metricFilter));
            }
        }
    }
    rowFilter = RowFilter.andFilter(metricFilters);
    return rowFilter;
}
Also used : ArrayList(java.util.ArrayList) Point(java.awt.Point) MetricSpinner(wekapar.MetricSpinner) RowFilter(javax.swing.RowFilter) JSpinner(javax.swing.JSpinner) RulesTableColumnModel(wekapar.table.RulesTableColumnModel) Map(java.util.Map) FilterMap(wekapar.FilterMap) HashMap(java.util.HashMap)

Example 3 with RulesTableColumnModel

use of wekapar.table.RulesTableColumnModel in project wekapar by gems-uff.

the class PostprocessAssociationsPanel method applyFilter.

/**
 * Apply filter for rules and metrics
 */
@SuppressWarnings("unchecked")
private static void applyFilter() {
    /* Lists for filters */
    List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();
    /* Rule's filter */
    RowFilter<Object, Object> rulesFilter = (RowFilter<Object, Object>) buildRulesFilter(comboFilterComponent.getText().trim());
    if (rulesFilter != null) {
        filters.add(rulesFilter);
    }
    /* Filter for metrics */
    RowFilter<Object, Object> metricsFilter = (RowFilter<Object, Object>) buildMetricsFilter();
    if (metricsFilter != null) {
        filters.add(metricsFilter);
    }
    /* Join rule and metric filters */
    RowFilter<Object, Object> andFilter = RowFilter.andFilter(filters);
    /* Apply filter to table */
    sorter.setRowFilter(andFilter);
    /* Update counting of filtered values */
    lblFilteredRulesValue.setText(String.valueOf(table.getRowCount()));
    /* Update max value reference for ProgressCellRenderer */
    List<?> sortKeys = sorter.getSortKeys();
    if (sortKeys.size() > 0) {
        RowSorter.SortKey key = (SortKey) sortKeys.get(0);
        TableModel tableModel = table.getModel();
        RulesTableColumnModel columnModel = table.getColumnModel();
        String columnName = tableModel.getColumnName(key.getColumn());
        if (columnModel.hasColumn(columnName)) {
            int columnIndex = columnModel.getColumnIndex(columnName);
            TableCellRenderer cellRenderer = columnModel.getColumn(columnIndex).getCellRenderer();
            if (cellRenderer instanceof ProgressCellRenderer) {
                ((ProgressCellRenderer) cellRenderer).updateMaxValue(table, columnIndex);
            }
        }
    }
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) RulesTableCellRenderer(wekapar.table.RulesTableCellRenderer) RowSorter(javax.swing.RowSorter) TableRowSorter(javax.swing.table.TableRowSorter) ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) SortKey(javax.swing.RowSorter.SortKey) ProgressCellRenderer(wekapar.table.ProgressCellRenderer) Point(java.awt.Point) RowFilter(javax.swing.RowFilter) RulesTableColumnModel(wekapar.table.RulesTableColumnModel) RulesTableModel(wekapar.table.RulesTableModel) TableModel(javax.swing.table.TableModel)

Aggregations

Point (java.awt.Point)3 ArrayList (java.util.ArrayList)3 RulesTableColumnModel (wekapar.table.RulesTableColumnModel)3 RowFilter (javax.swing.RowFilter)2 SortKey (javax.swing.RowSorter.SortKey)2 TableModel (javax.swing.table.TableModel)2 ProgressCellRenderer (wekapar.table.ProgressCellRenderer)2 RulesTableCellRenderer (wekapar.table.RulesTableCellRenderer)2 RulesTableModel (wekapar.table.RulesTableModel)2 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 JMenuItem (javax.swing.JMenuItem)1 JSpinner (javax.swing.JSpinner)1 RowSorter (javax.swing.RowSorter)1 RowSorterEvent (javax.swing.event.RowSorterEvent)1 RowSorterListener (javax.swing.event.RowSorterListener)1 TableCellRenderer (javax.swing.table.TableCellRenderer)1