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);
}
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);
}
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;
}
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);
}
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);
}
Aggregations