Search in sources :

Example 11 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class UniqueConcatenateWithCountOperator method computeInternal.

/**
 * {@inheritDoc}
 */
@Override
protected boolean computeInternal(final DataCell cell) {
    if (cell.isMissing()) {
        return false;
    }
    final String val = cell.toString();
    final MutableInteger counter = m_vals.get(val);
    if (counter != null) {
        counter.inc();
        return false;
    }
    // before adding a new value
    if (m_vals.size() >= getMaxUniqueValues()) {
        return true;
    }
    m_vals.put(val, new MutableInteger(1));
    return false;
}
Also used : MutableInteger(org.knime.core.util.MutableInteger)

Example 12 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class ColumnAggregator method loadColumnAggregators.

/**
 * Creates a {@link List} with all {@link ColumnAggregator}s that were
 * stored in the settings.
 *
 * @param settings the settings object to read from
 * @param key the unique settings key or <code>null</code> if the default should be used
 * @param spec {@link DataTableSpec} of the input table if available
 * @return {@link List} with the {@link ColumnAggregator}s
 * @throws InvalidSettingsException if the settings are invalid
 * @since 2.11
 */
public static List<ColumnAggregator> loadColumnAggregators(final NodeSettingsRO settings, final String key, final DataTableSpec spec) throws InvalidSettingsException {
    final Config cnfg;
    if (key == null || key.isEmpty()) {
        cnfg = settings.getConfig(CNFG_AGGR_COL_SECTION);
    } else {
        cnfg = settings.getConfig(key);
    }
    final String[] colNames = cnfg.getStringArray(CNFG_COL_NAMES);
    final DataType[] colTypes = cnfg.getDataTypeArray(CNFG_COL_TYPES);
    final String[] aggrMethods = cnfg.getStringArray(CNFG_AGGR_METHODS);
    boolean[] inclMissingVals = null;
    try {
        inclMissingVals = cnfg.getBooleanArray(CNFG_INCL_MISSING_VALS);
    } catch (final InvalidSettingsException e) {
    // be compatible to version 2.3 and earlier
    }
    final List<ColumnAggregator> colAggrList = new LinkedList<>();
    if (aggrMethods.length != colNames.length) {
        throw new InvalidSettingsException("Column name array and aggregation method array should be of equal size");
    }
    final NodeSettingsRO operatorSettings;
    if (settings.containsKey(CNFG_OPERATOR_SETTINGS)) {
        operatorSettings = settings.getNodeSettings(CNFG_OPERATOR_SETTINGS);
    } else {
        operatorSettings = settings;
    }
    final Map<String, MutableInteger> idMap = new HashMap<>();
    for (int i = 0, length = aggrMethods.length; i < length; i++) {
        final AggregationMethod method = AggregationMethods.getMethod4Id(aggrMethods[i]);
        final boolean inclMissingVal;
        if (inclMissingVals != null) {
            inclMissingVal = inclMissingVals[i];
        } else {
            // get the default behavior of the method
            inclMissingVal = method.inclMissingCells();
        }
        final DataColumnSpec resultSpec = new DataColumnSpecCreator(colNames[i], colTypes[i]).createSpec();
        final ColumnAggregator aggrCol = new ColumnAggregator(resultSpec, method, inclMissingVal);
        if (aggrCol.hasOptionalSettings()) {
            try {
                // TK_TODO: We should use a settings key but a running number to allow the user to change the columns
                // to aggregate via flow variables. However we cannot do this now because of backward compatibility
                NodeSettingsRO operatorSetting = operatorSettings.getNodeSettings(createSettingsKey(idMap, aggrCol));
                if (spec != null) {
                    // this method is called from the dialog
                    aggrCol.loadSettingsFrom(operatorSetting, spec);
                } else {
                    // this method is called from the node model where we do not
                    // have the DataTableSpec
                    aggrCol.loadValidatedSettings(operatorSetting);
                }
            } catch (Exception e) {
                LOGGER.error("Exception while loading settings for aggreation operator '" + aggrCol.getId() + "', reason: " + e.getMessage());
            }
        }
        colAggrList.add(aggrCol);
    }
    return colAggrList;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) HashMap(java.util.HashMap) Config(org.knime.core.node.config.Config) MutableInteger(org.knime.core.util.MutableInteger) LinkedList(java.util.LinkedList) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Example 13 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class UniqueConcatenateWithCountOperator method computeInternal.

/**
 * {@inheritDoc}
 */
@Override
protected boolean computeInternal(final DataCell cell) {
    final MutableInteger counter = m_vals.get(cell);
    if (counter != null) {
        counter.inc();
        return false;
    }
    // before adding a new value
    if (m_vals.size() >= getMaxUniqueValues()) {
        setSkipMessage("Group contains too many unique values");
        return true;
    }
    m_vals.put(cell, new MutableInteger(1));
    return false;
}
Also used : MutableInteger(org.knime.core.util.MutableInteger)

Example 14 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class DatabaseLoopingNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable inputTable = (BufferedDataTable) inData[0];
    final long rowCount = inputTable.size();
    final String column = m_columnModel.getStringValue();
    final DataTableSpec spec = inputTable.getDataTableSpec();
    final int colIdx = spec.findColumnIndex(column);
    if (colIdx < 0) {
        throw new InvalidSettingsException("Column " + column + " not found in input table.");
    }
    final Set<DataCell> values = new HashSet<>();
    BufferedDataContainer buf = null;
    final String oQuery = getQuery();
    final Collection<DataCell> curSet = new LinkedHashSet<>();
    final DBReader load = loadConnectionSettings(inData[getNrInPorts() - 1]);
    try {
        final int noValues = m_noValues.getIntValue();
        MutableInteger rowCnt = new MutableInteger(0);
        for (Iterator<DataRow> it = inputTable.iterator(); it.hasNext(); ) {
            exec.checkCanceled();
            DataCell cell = it.next().getCell(colIdx);
            if (values.contains(cell) && !it.hasNext() && curSet.isEmpty()) {
                continue;
            }
            values.add(cell);
            curSet.add(cell);
            if (curSet.size() == noValues || !it.hasNext()) {
                StringBuilder queryValues = new StringBuilder();
                for (DataCell v : curSet) {
                    if (queryValues.length() > 0) {
                        queryValues.append("','");
                    }
                    queryValues.append(v.toString());
                }
                String newQuery = parseQuery(oQuery.replaceAll(IN_PLACE_HOLDER, queryValues.toString()));
                load.updateQuery(newQuery);
                exec.setProgress(values.size() * (double) noValues / rowCount, "Selecting all values \"" + queryValues + "\"...");
                final BufferedDataTable table = getResultTable(exec, inData, load);
                if (buf == null) {
                    DataTableSpec resSpec = table.getDataTableSpec();
                    buf = exec.createDataContainer(createSpec(resSpec, spec.getColumnSpec(column)));
                }
                if (m_aggByRow.getBooleanValue()) {
                    aggregate(table, rowCnt, buf, CollectionCellFactory.createListCell(curSet));
                } else {
                    notAggregate(table, rowCnt, buf, CollectionCellFactory.createListCell(curSet));
                }
                curSet.clear();
            }
        }
        if (buf == null) {
            // create empty dummy container with spec generated during #configure
            final PortObjectSpec[] inSpec;
            if ((inData.length > 1) && (inData[1] instanceof DatabaseConnectionPortObject)) {
                DatabaseConnectionPortObject dbPort = (DatabaseConnectionPortObject) inData[1];
                inSpec = new PortObjectSpec[] { inputTable.getSpec(), dbPort.getSpec() };
            } else {
                inSpec = new PortObjectSpec[] { inputTable.getSpec() };
            }
            final String newQuery = createDummyValueQuery(spec, colIdx, oQuery);
            setQuery(newQuery);
            final DataTableSpec resultSpec = getResultSpec(inSpec);
            final DataTableSpec outSpec = createSpec(resultSpec, spec.getColumnSpec(column));
            buf = exec.createDataContainer(outSpec);
        }
        buf.close();
    } catch (CanceledExecutionException cee) {
        throw cee;
    } catch (Exception e) {
        setLastSpec(null);
        throw e;
    } finally {
        // reset query to original
        setQuery(oQuery);
    }
    final BufferedDataTable resultTable = buf.getTable();
    setLastSpec(resultTable.getDataTableSpec());
    return new BufferedDataTable[] { resultTable };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) MutableInteger(org.knime.core.util.MutableInteger) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) SQLException(java.sql.SQLException) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DBReader(org.knime.core.node.port.database.reader.DBReader) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec) DataCell(org.knime.core.data.DataCell) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 15 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class NominalValue method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    int index = 0;
    for (int i : getIndices()) {
        MutableInteger mutableInt = m_nominalValues[index].get(dataRow.getCell(i));
        if (mutableInt == null) {
            if (m_nominalValues[index].size() < m_maxAmountOfCountedValues) {
                m_nominalValues[index].put(dataRow.getCell(i), new MutableInteger(1));
            } else {
                if (m_exceededColumns == null) {
                    m_exceededColumns = new HashSet<String>(5);
                }
                m_exceededColumns.add(getColumns()[index]);
            }
        } else {
            mutableInt.inc();
        }
        index++;
    }
}
Also used : MutableInteger(org.knime.core.util.MutableInteger)

Aggregations

MutableInteger (org.knime.core.util.MutableInteger)32 DataCell (org.knime.core.data.DataCell)12 HashMap (java.util.HashMap)11 DataRow (org.knime.core.data.DataRow)8 RowKey (org.knime.core.data.RowKey)7 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)6 HashSet (java.util.HashSet)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 DefaultRow (org.knime.core.data.def.DefaultRow)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 Set (java.util.Set)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 DoubleCell (org.knime.core.data.def.DoubleCell)3 StringCell (org.knime.core.data.def.StringCell)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2