Search in sources :

Example 21 with ColumnAggregator

use of org.knime.base.data.aggregation.ColumnAggregator in project knime-core by knime.

the class AggregationColumnTableModel method updateMethod.

/**
 * @param row the row to update
 * @param method the {@link AggregationMethod} to use
 */
private void updateMethod(final int row, final AggregationMethod method) {
    final ColumnAggregator old = getRow(row);
    if (old.getMethodTemplate().getId().equals(method.getId())) {
        // check if the method has changed
        return;
    }
    // create a new operator each time it is updated to guarantee that
    // each column has its own operator instance
    AggregationMethod methodClone = AggregationMethods.getMethod4Id(method.getId());
    final ColumnAggregator newRow = new ColumnAggregator(old.getOriginalColSpec(), methodClone, old.inclMissingCells());
    newRow.setValid(old.isValid());
    updateRow(row, newRow);
}
Also used : AggregationMethod(org.knime.base.data.aggregation.AggregationMethod) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator)

Example 22 with ColumnAggregator

use of org.knime.base.data.aggregation.ColumnAggregator in project knime-core by knime.

the class AggregationColumnPanel method initialize.

/**
 * Initializes the panel.
 * @param spec the {@link DataTableSpec} of the input table
 * @param colAggrs the {@link List} of {@link ColumnAggregator}s that are
 * initially used
 */
public void initialize(final DataTableSpec spec, final List<ColumnAggregator> colAggrs) {
    m_avAggrColSpecs.clear();
    final List<DataColumnSpec> listElements = new LinkedList<>();
    for (final DataColumnSpec colSpec : spec) {
        m_avAggrColSpecs.add(colSpec);
        listElements.add(colSpec);
    }
    // remove all invalid column aggregator
    final List<ColumnAggregator> colAggrs2Use = new ArrayList<>(colAggrs.size());
    for (final ColumnAggregator colAggr : colAggrs) {
        final DataColumnSpec colSpec = spec.getColumnSpec(colAggr.getOriginalColName());
        final boolean valid;
        if (colSpec != null && colAggr.getOriginalDataType().isASuperTypeOf(colSpec.getType())) {
            valid = true;
        } else {
            valid = false;
        }
        colAggr.setValid(valid);
        colAggrs2Use.add(colAggr);
    }
    initialize(listElements, colAggrs2Use, spec);
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 23 with ColumnAggregator

use of org.knime.base.data.aggregation.ColumnAggregator in project knime-core by knime.

the class AggregationColumnPanel method getMethods4SelectedItems.

/**
 * @return a label list of all supported methods for the currently
 * selected rows
 */
protected List<Entry<String, List<AggregationMethod>>> getMethods4SelectedItems() {
    final int[] selectedColumns = getSelectedRows();
    final Set<DataType> types = new HashSet<>(selectedColumns.length);
    for (final int row : selectedColumns) {
        final ColumnAggregator aggregator = getTableModel().getRow(row);
        types.add(aggregator.getOriginalDataType());
    }
    final DataType superType = CollectionCellFactory.getElementType(types.toArray(new DataType[0]));
    final List<Entry<String, List<AggregationMethod>>> list = AggregationMethods.getCompatibleMethodGroupList(superType);
    return list;
}
Also used : AggregationMethod(org.knime.base.data.aggregation.AggregationMethod) Entry(java.util.Map.Entry) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) DataType(org.knime.core.data.DataType) HashSet(java.util.HashSet)

Example 24 with ColumnAggregator

use of org.knime.base.data.aggregation.ColumnAggregator in project knime-core by knime.

the class AggregationColumnPanel method adaptTableColumnModel.

/**
 * {@inheritDoc}
 */
@Override
protected void adaptTableColumnModel(final TableColumnModel columnModel) {
    columnModel.getColumn(0).setCellRenderer(new AggregationMethodDecoratorTableCellRenderer(new ValueRenderer() {

        @Override
        public void renderComponent(final DefaultTableCellRenderer c, final AggregationMethodDecorator method) {
            if (method instanceof ColumnAggregator) {
                final ColumnAggregator aggregator = (ColumnAggregator) method;
                final DataColumnSpec spec = aggregator.getOriginalColSpec();
                c.setText(spec.getName());
                c.setIcon(spec.getType().getIcon());
            }
        }
    }, true, "Double click to remove column. Right mouse click for context menu."));
    columnModel.getColumn(1).setCellEditor(new ColumnAggregatorTableCellEditor());
    columnModel.getColumn(1).setCellRenderer(new AggregationMethodDecoratorTableCellRenderer(new ValueRenderer() {

        @Override
        public void renderComponent(final DefaultTableCellRenderer c, final AggregationMethodDecorator method) {
            c.setText(method.getLabel());
        }
    }, false));
    columnModel.getColumn(0).setPreferredWidth(170);
    columnModel.getColumn(1).setPreferredWidth(150);
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) AggregationMethodDecorator(org.knime.base.data.aggregation.AggregationMethodDecorator) ColumnAggregatorTableCellEditor(org.knime.base.data.aggregation.dialogutil.column.ColumnAggregatorTableCellEditor) ValueRenderer(org.knime.base.data.aggregation.dialogutil.AggregationMethodDecoratorTableCellRenderer.ValueRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 25 with ColumnAggregator

use of org.knime.base.data.aggregation.ColumnAggregator in project knime-core by knime.

the class GroupByTable method createGroupByTableSpec.

/**
 * @param spec the original {@link DataTableSpec}
 * @param groupColNames the name of all columns to group by
 * @param columnAggregators the aggregation columns with the
 * aggregation method to use in the order the columns should be appear
 * in the result table
 * @param colNamePolicy the {@link ColumnNamePolicy} for the aggregation
 * columns
 * @return the result {@link DataTableSpec}
 */
public static final DataTableSpec createGroupByTableSpec(final DataTableSpec spec, final List<String> groupColNames, final ColumnAggregator[] columnAggregators, final ColumnNamePolicy colNamePolicy) {
    if (spec == null) {
        throw new NullPointerException("DataTableSpec must not be null");
    }
    if (groupColNames == null) {
        throw new IllegalArgumentException("groupColNames must not be null");
    }
    if (columnAggregators == null) {
        throw new NullPointerException("colMethods must not be null");
    }
    final int noOfCols = groupColNames.size() + columnAggregators.length;
    final DataColumnSpec[] colSpecs = new DataColumnSpec[noOfCols];
    int colIdx = 0;
    // add the group columns first
    final Map<String, MutableInteger> colNameCount = new HashMap<>(noOfCols);
    for (final String colName : groupColNames) {
        final DataColumnSpec colSpec = spec.getColumnSpec(colName);
        if (colSpec == null) {
            throw new IllegalArgumentException("No column spec found for name: " + colName);
        }
        colSpecs[colIdx++] = colSpec;
        colNameCount.put(colName, new MutableInteger(1));
    }
    // add the aggregation columns
    for (final ColumnAggregator aggrCol : columnAggregators) {
        final DataColumnSpec origSpec = spec.getColumnSpec(aggrCol.getOriginalColName());
        if (origSpec == null) {
            throw new IllegalArgumentException("No column spec found for name: " + aggrCol.getOriginalColName());
        }
        final String colName = colNamePolicy.createColumName(aggrCol);
        // since a column could be used several times create a unique name
        final String uniqueName;
        if (colNameCount.containsKey(colName)) {
            final MutableInteger counter = colNameCount.get(colName);
            uniqueName = colName + "_" + counter.intValue();
            counter.inc();
        } else {
            colNameCount.put(colName, new MutableInteger(1));
            uniqueName = colName;
        }
        final DataColumnSpec newSpec = aggrCol.getMethodTemplate().createColumnSpec(uniqueName, origSpec);
        colSpecs[colIdx++] = newSpec;
    }
    return new DataTableSpec(colSpecs);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) HashMap(java.util.HashMap) MutableInteger(org.knime.core.util.MutableInteger)

Aggregations

ColumnAggregator (org.knime.base.data.aggregation.ColumnAggregator)33 DataColumnSpec (org.knime.core.data.DataColumnSpec)14 HashSet (java.util.HashSet)9 LinkedList (java.util.LinkedList)9 ArrayList (java.util.ArrayList)8 AggregationMethod (org.knime.base.data.aggregation.AggregationMethod)8 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)5 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)5 DataCell (org.knime.core.data.DataCell)4 DataRow (org.knime.core.data.DataRow)4 DataTableSpec (org.knime.core.data.DataTableSpec)4 RowKey (org.knime.core.data.RowKey)4 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 GlobalSettings (org.knime.base.data.aggregation.GlobalSettings)3 PatternAggregator (org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator)3 DataTypeAggregator (org.knime.base.data.aggregation.dialogutil.type.DataTypeAggregator)3 GroupByTable (org.knime.base.node.preproc.groupby.GroupByTable)3 DataType (org.knime.core.data.DataType)3 DefaultRow (org.knime.core.data.def.DefaultRow)3 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)3