Search in sources :

Example 1 with PatternAggregator

use of org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator in project knime-core by knime.

the class GroupByNodeModel method getAggregators.

/**
 * Creates a {@link List} with all {@link ColumnAggregator}s to use based on the given input settings.
 * Columns are only added once for the different aggregator types in the order they are added to the function
 * e.g. all column that are handled by one of the given {@link ColumnAggregator} are ignored by the
 * pattern and data type based aggregator all columns that are handled by one of the pattern based aggregators
 * is ignored by the data type based aggregators.
 * @param inputSpec the {@link DataTableSpec} of the input table
 * @param groupColumns the columns to group by
 * @param columnAggregators the manually added {@link ColumnAggregator}s
 * @param patternAggregators the {@link PatternAggregator}s
 * @param dataTypeAggregators the {@link DataTypeAggregator}s
 * @param invalidColAggrs empty {@link List} that is filled with the invalid column aggregators can be
 * <code>null</code>
 * @return the list of all {@link ColumnAggregator}s to use based on the given aggregator
 * @since 2.11
 */
public static List<ColumnAggregator> getAggregators(final DataTableSpec inputSpec, final Collection<String> groupColumns, final List<ColumnAggregator> columnAggregators, final Collection<PatternAggregator> patternAggregators, final Collection<DataTypeAggregator> dataTypeAggregators, final List<ColumnAggregator> invalidColAggrs) {
    final List<ColumnAggregator> columnAggregators2Use = new ArrayList<>(columnAggregators.size());
    final Set<String> usedColNames = new HashSet<>(inputSpec.getNumColumns());
    usedColNames.addAll(groupColumns);
    for (final ColumnAggregator colAggr : columnAggregators) {
        final String originalColName = colAggr.getOriginalColName();
        final DataColumnSpec colSpec = inputSpec.getColumnSpec(originalColName);
        if (colSpec != null && colAggr.getOriginalDataType().isASuperTypeOf(colSpec.getType())) {
            usedColNames.add(originalColName);
            columnAggregators2Use.add(colAggr);
        } else {
            if (invalidColAggrs != null) {
                invalidColAggrs.add(colAggr);
            }
        }
    }
    if (inputSpec.getNumColumns() > usedColNames.size() && !patternAggregators.isEmpty()) {
        for (final DataColumnSpec spec : inputSpec) {
            if (!usedColNames.contains(spec.getName())) {
                for (final PatternAggregator patternAggr : patternAggregators) {
                    Pattern pattern = patternAggr.getRegexPattern();
                    if (pattern != null && pattern.matcher(spec.getName()).matches() && patternAggr.isCompatible(spec)) {
                        final ColumnAggregator colAggregator = new ColumnAggregator(spec, patternAggr.getMethodTemplate(), patternAggr.inclMissingCells());
                        columnAggregators2Use.add(colAggregator);
                        usedColNames.add(spec.getName());
                    }
                }
            }
        }
    }
    // check if some columns are left
    if (inputSpec.getNumColumns() > usedColNames.size() && !dataTypeAggregators.isEmpty()) {
        for (final DataColumnSpec spec : inputSpec) {
            if (!usedColNames.contains(spec.getName())) {
                final DataType dataType = spec.getType();
                for (final DataTypeAggregator typeAggregator : dataTypeAggregators) {
                    if (typeAggregator.isCompatibleType(dataType)) {
                        final ColumnAggregator colAggregator = new ColumnAggregator(spec, typeAggregator.getMethodTemplate(), typeAggregator.inclMissingCells());
                        columnAggregators2Use.add(colAggregator);
                        usedColNames.add(spec.getName());
                    }
                }
            }
        }
    }
    return columnAggregators2Use;
}
Also used : PatternAggregator(org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator) Pattern(java.util.regex.Pattern) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) ArrayList(java.util.ArrayList) DataType(org.knime.core.data.DataType) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataTypeAggregator(org.knime.base.data.aggregation.dialogutil.type.DataTypeAggregator) HashSet(java.util.HashSet)

Example 2 with PatternAggregator

use of org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator in project knime-core by knime.

the class GroupByNodeModel method validateSettings.

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_groupByCols.validateSettings(settings);
    // FIX bug 5040: potential problem with clone settings method when in-/exclude list contain same elements
    final SettingsModelFilterString tmpSett = new SettingsModelFilterString(CFG_GROUP_BY_COLUMNS);
    tmpSett.loadSettingsFrom(settings);
    final List<String> groupByCols = tmpSett.getIncludeList();
    m_maxUniqueValues.validateSettings(settings);
    m_enableHilite.validateSettings(settings);
    // with Knime 2.0 as well as the naming policy
    try {
        final List<ColumnAggregator> aggregators = ColumnAggregator.loadColumnAggregators(settings);
        final List<DataTypeAggregator> typeAggregators = new LinkedList<>();
        final List<PatternAggregator> patternAggregators = new LinkedList<>();
        try {
            patternAggregators.addAll(PatternAggregator.loadAggregators(settings, CFG_PATTERN_AGGREGATORS));
            typeAggregators.addAll(DataTypeAggregator.loadAggregators(settings, CFG_DATA_TYPE_AGGREGATORS));
        } catch (InvalidSettingsException e) {
        // introduced in 2.11
        }
        if (groupByCols.isEmpty() && aggregators.isEmpty() && patternAggregators.isEmpty() && typeAggregators.isEmpty()) {
            throw new IllegalArgumentException("Please select at least one group column or aggregation option");
        }
        ColumnNamePolicy namePolicy;
        try {
            final String policyLabel = ((SettingsModelString) m_columnNamePolicy.createCloneWithValidatedValue(settings)).getStringValue();
            namePolicy = ColumnNamePolicy.getPolicy4Label(policyLabel);
        } catch (final InvalidSettingsException e) {
            namePolicy = compGetColumnNamePolicy(settings);
        }
        checkDuplicateAggregators(namePolicy, aggregators);
    } catch (final InvalidSettingsException e) {
    // these settings are prior Knime 2.0 and can't contain
    // a column several times
    } catch (final IllegalArgumentException e) {
        throw new InvalidSettingsException(e.getMessage());
    }
}
Also used : PatternAggregator(org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataTypeAggregator(org.knime.base.data.aggregation.dialogutil.type.DataTypeAggregator) LinkedList(java.util.LinkedList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 3 with PatternAggregator

use of org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator in project knime-core by knime.

the class MovingAggregationNodeModel method validateSettings.

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_winLength.validateSettings(settings);
    m_windowType.validateSettings(settings);
    m_cumulativeComputing.validateSettings(settings);
    m_handleMissings.validateSettings(settings);
    m_removeRetainedCols.validateSettings(settings);
    m_maxUniqueVals.validateSettings(settings);
    m_valueDelimiter.validateSettings(settings);
    final List<ColumnAggregator> aggregators = ColumnAggregator.loadColumnAggregators(settings);
    final List<DataTypeAggregator> typeAggregators = new LinkedList<>();
    final List<PatternAggregator> regexAggregators = new LinkedList<>();
    try {
        regexAggregators.addAll(PatternAggregator.loadAggregators(settings, CFG_PATTERN_AGGREGATORS));
        typeAggregators.addAll(DataTypeAggregator.loadAggregators(settings, CFG_DATA_TYPE_AGGREGATORS));
    } catch (InvalidSettingsException e) {
    // introduced in 2.11
    }
    if (aggregators.isEmpty() && regexAggregators.isEmpty() && typeAggregators.isEmpty()) {
        throw new IllegalArgumentException("Please select at least one aggregation option");
    }
    final String policyLabel = ((SettingsModelString) m_columnNamePolicy.createCloneWithValidatedValue(settings)).getStringValue();
    final ColumnNamePolicy namePolicy = ColumnNamePolicy.getPolicy4Label(policyLabel);
    try {
        GroupByNodeModel.checkDuplicateAggregators(namePolicy, aggregators);
    } catch (IllegalArgumentException e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    final boolean removeAggrCols = ((SettingsModelBoolean) m_removeAggregationCols.createCloneWithValidatedValue(settings)).getBooleanValue();
    if (ColumnNamePolicy.KEEP_ORIGINAL_NAME.equals(namePolicy) && !removeAggrCols) {
        throw new InvalidSettingsException("'" + ColumnNamePolicy.KEEP_ORIGINAL_NAME.getLabel() + "' option only valid if aggregation columns are filtered");
    }
}
Also used : PatternAggregator(org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataTypeAggregator(org.knime.base.data.aggregation.dialogutil.type.DataTypeAggregator) LinkedList(java.util.LinkedList) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) ColumnNamePolicy(org.knime.base.node.preproc.groupby.ColumnNamePolicy) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Aggregations

ColumnAggregator (org.knime.base.data.aggregation.ColumnAggregator)3 PatternAggregator (org.knime.base.data.aggregation.dialogutil.pattern.PatternAggregator)3 DataTypeAggregator (org.knime.base.data.aggregation.dialogutil.type.DataTypeAggregator)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 LinkedList (java.util.LinkedList)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1 ColumnNamePolicy (org.knime.base.node.preproc.groupby.ColumnNamePolicy)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataType (org.knime.core.data.DataType)1 SettingsModelBoolean (org.knime.core.node.defaultnodesettings.SettingsModelBoolean)1