Search in sources :

Example 41 with NodeSettingsRO

use of org.knime.core.node.NodeSettingsRO in project knime-core by knime.

the class DBWriterNodeModel method loadSettings.

private void loadSettings(final NodeSettingsRO settings, final boolean write) throws InvalidSettingsException {
    boolean append = settings.getBoolean(KEY_APPEND_DATA, true);
    final String table = settings.getString(KEY_TABLE_NAME);
    if (table == null || table.trim().isEmpty()) {
        throw new InvalidSettingsException("Configure node and enter a valid table name.");
    }
    // read and validate batch size
    final int batchSize = settings.getInt(KEY_BATCH_SIZE, m_batchSize);
    if (batchSize <= 0) {
        throw new InvalidSettingsException("Batch size must be greater than 0, is " + batchSize);
    }
    // write settings or skip it
    if (write) {
        m_tableName = table;
        m_append = append;
        // load SQL Types for each column
        m_types.clear();
        try {
            NodeSettingsRO typeSett = settings.getNodeSettings(CFG_SQL_TYPES);
            for (String key : typeSett.keySet()) {
                m_types.put(key, typeSett.getString(key));
            }
        } catch (InvalidSettingsException ise) {
        // ignore, will be determined during configure
        }
        // load batch size
        m_batchSize = batchSize;
    }
    // introduced in KNIME 2.11 default behavior before was inserting null
    m_insertNullForMissingCols = settings.getBoolean(KEY_INSERT_NULL_FOR_MISSING_COLS, true);
    // introduced in KNIME 3.3.1 legacy behavior is not failing e.g. false
    m_failOnError = settings.getBoolean(KEY_FAIL_ON_ERROR, false);
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Example 42 with NodeSettingsRO

use of org.knime.core.node.NodeSettingsRO in project knime-core by knime.

the class DBReaderNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException {
    File specFile = null;
    specFile = new File(nodeInternDir, "spec.xml");
    if (!specFile.exists()) {
        IOException ioe = new IOException("Spec file (\"" + specFile.getAbsolutePath() + "\") does not exist " + "(node may have been saved by an older version!)");
        throw ioe;
    }
    NodeSettingsRO specSett = NodeSettings.loadFromXML(new FileInputStream(specFile));
    try {
        setLastSpec(DataTableSpec.load(specSett));
    } catch (InvalidSettingsException ise) {
        IOException ioe = new IOException("Could not read output spec.");
        ioe.initCause(ise);
        throw ioe;
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 43 with NodeSettingsRO

use of org.knime.core.node.NodeSettingsRO in project knime-core by knime.

the class AbstractDBAggregationFunctionRow method loadFunction.

/**
 * @param tableSpec optional input {@link DataTableSpec}
 * @param functionProvider the {@link DBAggregationFunctionProvider}
 * @param cfg {@link NodeSettingsRO} to read from
 * @return the {@link DBAggregationFunction}
 * @throws InvalidSettingsException if the settings of the function are invalid
 */
public static DBAggregationFunction loadFunction(final DataTableSpec tableSpec, final DBAggregationFunctionProvider functionProvider, final NodeSettingsRO cfg) throws InvalidSettingsException {
    final String functionId = cfg.getString(CNFG_AGGR_COL_SECTION);
    DBAggregationFunction function = functionProvider.getFunction(functionId);
    if (function instanceof InvalidAggregationFunction) {
        final String errMsg = "Exception while loading database aggregation functions. " + ((InvalidAggregationFunction) function).getErrorMessage();
        LOGGER.warn(errMsg);
    } else {
        if (function.hasOptionalSettings()) {
            try {
                final NodeSettingsRO subSettings = cfg.getNodeSettings("functionSettings");
                if (tableSpec != null) {
                    // this method is called from the dialog
                    function.loadSettingsFrom(subSettings, tableSpec);
                } else {
                    // this method is called from the node model where we do not
                    // have the DataTableSpec
                    function.loadValidatedSettings(subSettings);
                }
            } catch (Exception e) {
                final String errMsg = "Exception while loading settings for aggreation function '" + function.getId() + "', reason: " + e.getMessage();
                function = new InvalidDBAggregationFunction(functionId, errMsg, null);
                LOGGER.error(errMsg);
            }
        }
    }
    return function;
}
Also used : InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) InvalidDBAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidDBAggregationFunction) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) InvalidDBAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidDBAggregationFunction)

Example 44 with NodeSettingsRO

use of org.knime.core.node.NodeSettingsRO 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 45 with NodeSettingsRO

use of org.knime.core.node.NodeSettingsRO in project knime-core by knime.

the class SettingsModelAggregationMethod method loadSettingsForModel.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsForModel(final NodeSettingsRO settings) throws InvalidSettingsException {
    try {
        final NodeSettingsRO subSettings = settings.getNodeSettings(m_configName);
        final String methodId = subSettings.getString(CFG_METHOD_ID);
        final AggregationOperator method = (AggregationOperator) AggregationMethods.getMethod4Id(methodId);
        final boolean includeMissing = subSettings.getBoolean(CFG_INCL_MISSING, method.inclMissingCells());
        // update the missing value option in the method based on the settings
        method.getOperatorColumnSettings().setInclMissing(includeMissing);
        if (method.hasOptionalSettings()) {
            final NodeSettingsRO methodSettings = subSettings.getNodeSettings(CFG_METHOD_SETTINGS);
            method.loadValidatedSettings(methodSettings);
        }
        // no default value, throw an exception instead
        setValues(method, subSettings.getString(CFG_VALUE_SEPARATOR), subSettings.getInt(CFG_MAX_UNIQUE_VALUES));
    } catch (final IllegalArgumentException iae) {
        throw new InvalidSettingsException(iae.getMessage());
    }
}
Also used : AggregationOperator(org.knime.base.data.aggregation.AggregationOperator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Aggregations

NodeSettingsRO (org.knime.core.node.NodeSettingsRO)208 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)125 File (java.io.File)49 FileInputStream (java.io.FileInputStream)47 IOException (java.io.IOException)43 InputStream (java.io.InputStream)22 LinkedHashMap (java.util.LinkedHashMap)20 NodeSettings (org.knime.core.node.NodeSettings)20 BufferedInputStream (java.io.BufferedInputStream)19 ArrayList (java.util.ArrayList)16 GZIPInputStream (java.util.zip.GZIPInputStream)15 DataTableSpec (org.knime.core.data.DataTableSpec)14 Map (java.util.Map)11 ReferencedFile (org.knime.core.internal.ReferencedFile)11 BufferedDataTable (org.knime.core.node.BufferedDataTable)10 HashMap (java.util.HashMap)9 DataColumnSpec (org.knime.core.data.DataColumnSpec)9 RowKey (org.knime.core.data.RowKey)9 DataType (org.knime.core.data.DataType)8 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)8