Search in sources :

Example 1 with NodeSettings

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

the class NodeContainerProperties method setPropertyValue.

/**
 * {@inheritDoc}
 */
@Override
public void setPropertyValue(final Object id, final Object value) {
    if ((id instanceof String) && (value instanceof String)) {
        String strVal = (String) value;
        String strID = (String) id;
        if (strID.startsWith(m_prefix)) {
            String[] hierarchy = strID.split(CONFIG_SEPARATOR);
            String key = hierarchy[hierarchy.length - 1];
            // apply it to the node's settings:
            NodeContainer node = getNode();
            if (node == null) {
                return;
            }
            WorkflowManager wfm = node.getParent();
            NodeSettings nodeSettings = new NodeSettings("Transfer");
            NodeSettings settings;
            try {
                wfm.saveNodeSettings(node.getID(), nodeSettings);
                // overwrite our config in the settings
                settings = nodeSettings.getNodeSettings("model");
                if (hierarchy.length > 1) {
                    for (int i = 0; i < hierarchy.length - 1; i++) {
                        settings = settings.getNodeSettings(hierarchy[i]);
                        if (settings == null) {
                            return;
                        }
                    }
                }
            } catch (InvalidSettingsException e) {
                // somehow node is not able to save its settings anymore
                return;
            }
            AbstractConfigEntry entry = settings.getEntry(key);
            if (entry == null || entry instanceof Config) {
                // settings are not complete or correct anymore
                return;
            }
            switch(entry.getType()) {
                case xboolean:
                    settings.addBoolean(key, Boolean.parseBoolean(strVal));
                    break;
                case xbyte:
                    settings.addByte(key, Byte.parseByte(strVal));
                    break;
                case xchar:
                    String decoded = TokenizerSettings.unescapeString(strVal);
                    settings.addChar(key, decoded.charAt(0));
                    break;
                case xdouble:
                    settings.addDouble(key, Double.parseDouble(strVal));
                    break;
                case xfloat:
                    settings.addFloat(key, Float.parseFloat(strVal));
                    break;
                case xint:
                    settings.addInt(key, Integer.parseInt(strVal));
                    break;
                case xlong:
                    settings.addLong(key, Long.parseLong(strVal));
                    break;
                case xshort:
                    settings.addShort(key, Short.parseShort(strVal));
                    break;
                case xstring:
                    String dec = TokenizerSettings.unescapeString(strVal);
                    settings.addString(key, dec);
                    break;
                default:
                    // ignore the new value
                    return;
            }
            try {
                wfm.loadNodeSettings(node.getID(), nodeSettings);
            } catch (Exception ex) {
                LOGGER.error("Invalid Value (" + strVal + "): " + ex.getMessage(), ex);
                return;
            }
            return;
        }
    }
}
Also used : AbstractConfigEntry(org.knime.core.node.config.base.AbstractConfigEntry) NodeSettings(org.knime.core.node.NodeSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Config(org.knime.core.node.config.Config) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) NodeContainer(org.knime.core.node.workflow.NodeContainer) SingleNodeContainer(org.knime.core.node.workflow.SingleNodeContainer) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 2 with NodeSettings

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

the class HistogramColumn method saveHistogramData.

/**
 * Saves the numeric histogram data to a file.
 *
 * @param histograms The numeric histogram models associated to the column indices.
 * @param histogramsFile The output file.
 * @throws IOException File write problem.
 */
public static void saveHistogramData(final Map<Integer, ?> histograms, final File histogramsFile) throws IOException {
    Config histogramData = new NodeSettings(HISTOGRAMS);
    final FileOutputStream os = new FileOutputStream(histogramsFile);
    final GZIPOutputStream dataOS = new GZIPOutputStream(os);
    List<Integer> colIndices = new ArrayList<Integer>(histograms.keySet());
    Collections.sort(colIndices);
    int[] numericColumnIndices = new int[colIndices.size()];
    for (int i = colIndices.size(); i-- > 0; ) {
        numericColumnIndices[i] = colIndices.get(i).intValue();
    }
    histogramData.addIntArray(NUMERIC_COLUMNS, numericColumnIndices);
    for (Integer colIdx : colIndices) {
        Object object = histograms.get(colIdx);
        if (object instanceof HistogramNumericModel) {
            HistogramNumericModel hd = (HistogramNumericModel) object;
            assert hd.getColIndex() == colIdx.intValue() : "colIdx: " + colIdx + ", but: " + hd.getColIndex();
            Config h = histogramData.addConfig(HISTOGRAM + colIdx);
            h.addDouble(MIN, hd.m_min);
            h.addDouble(MAX, hd.m_max);
            h.addDouble(WIDTH, hd.m_width);
            h.addInt(MAX_COUNT, hd.getMaxCount());
            h.addInt(ROW_COUNT, hd.getRowCount());
            h.addInt(COL_INDEX, hd.getColIndex());
            h.addString(COL_NAME, hd.getColName());
            double[] minValues = new double[hd.getBins().size()], maxValues = new double[hd.getBins().size()];
            int[] counts = new int[hd.getBins().size()];
            for (int c = 0; c < hd.getBins().size(); c++) {
                HistogramNumericModel.NumericBin bin = (HistogramNumericModel.NumericBin) hd.getBins().get(c);
                minValues[c] = bin.getDef().getFirst().doubleValue();
                maxValues[c] = bin.getDef().getSecond().doubleValue();
                counts[c] = bin.getCount();
            }
            h.addDoubleArray(BIN_MINS, minValues);
            h.addDoubleArray(BIN_MAXES, maxValues);
            h.addIntArray(BIN_COUNTS, counts);
        } else {
            throw new IllegalStateException("Illegal argument: " + colIdx + ": " + object.getClass() + "\n   " + object);
        }
    }
    histogramData.saveToXML(dataOS);
}
Also used : Config(org.knime.core.node.config.Config) ArrayList(java.util.ArrayList) NodeSettings(org.knime.core.node.NodeSettings) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream)

Example 3 with NodeSettings

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

the class NodeContainer method areDialogSettingsValid.

public boolean areDialogSettingsValid() {
    CheckUtils.checkState(hasDialog(), "Node \"%s\" has no dialog", getName());
    NodeSettings sett = new NodeSettings("node settings");
    NodeContext.pushContext(this);
    try {
        getDialogPane().finishEditingAndSaveSettingsTo(sett);
        validateSettings(sett);
        return true;
    } catch (InvalidSettingsException nce) {
        return false;
    } finally {
        NodeContext.removeLastContext();
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 4 with NodeSettings

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

the class NodeContainer method applySettingsFromDialog.

/**
 * Take settings from the node's dialog and apply them to the model. Throws
 * an exception if the apply fails.
 *
 * @throws InvalidSettingsException if settings are not applicable.
 */
public void applySettingsFromDialog() throws InvalidSettingsException {
    CheckUtils.checkState(hasDialog(), "Node \"%s\" has no dialog", getName());
    // TODO do we need to reset the node first??
    NodeSettings sett = new NodeSettings("node settings");
    NodeContext.pushContext(this);
    try {
        getDialogPane().finishEditingAndSaveSettingsTo(sett);
        m_parent.loadNodeSettings(getID(), sett);
    } finally {
        NodeContext.removeLastContext();
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings)

Example 5 with NodeSettings

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

the class FileNativeNodeContainerPersistor method loadNCAndWashModelSettings.

/**
 * {@inheritDoc}
 */
@Override
NodeSettingsRO loadNCAndWashModelSettings(final NodeSettingsRO settingsForNode, final NodeSettingsRO modelSettings, final Map<Integer, BufferedDataTable> tblRep, final ExecutionMonitor exec, final LoadResult result) throws InvalidSettingsException, CanceledExecutionException, IOException {
    final FileNodePersistor nodePersistor = createNodePersistor(settingsForNode);
    nodePersistor.preLoad(m_node, result);
    NodeSettingsRO washedModelSettings = modelSettings;
    try {
        if (modelSettings != null) {
            // null if the node never had settings - no reason to load them
            m_node.validateModelSettings(modelSettings);
            m_node.loadModelSettingsFrom(modelSettings);
            // previous versions of KNIME (2.7 and before) kept the model settings only in the node;
            // NodeModel#saveSettingsTo was always called before the dialog was opened (some dialog implementations
            // rely on the exact structure of the NodeSettings ... which may change between versions).
            // We wash the settings through the node so that the model settings are updated (they possibly
            // no longer map to the variable settings loaded further down below - if so, the inconsistency
            // is warned later during configuration)
            NodeSettings washedSettings = new NodeSettings("model");
            m_node.saveModelSettingsTo(washedSettings);
            washedModelSettings = washedSettings;
        }
    } catch (Exception e) {
        final String error;
        if (e instanceof InvalidSettingsException) {
            error = "Loading model settings failed: " + e.getMessage();
        } else {
            error = "Caught \"" + e.getClass().getSimpleName() + "\", " + "Loading model settings failed: " + e.getMessage();
        }
        final LoadNodeModelSettingsFailPolicy pol = getModelSettingsFailPolicy(getMetaPersistor().getState(), nodePersistor.isInactive());
        switch(pol) {
            case IGNORE:
                if (!(e instanceof InvalidSettingsException)) {
                    getLogger().coding(error, e);
                }
                break;
            case FAIL:
                result.addError(error);
                m_node.createErrorMessageAndNotify(error, e);
                setNeedsResetAfterLoad();
                break;
            case WARN:
                m_node.createWarningMessageAndNotify(error, e);
                result.addWarning(error);
                setDirtyAfterLoad();
                break;
        }
    }
    try {
        HashMap<Integer, ContainerTable> globalTableRepository = getGlobalTableRepository();
        WorkflowFileStoreHandlerRepository fileStoreHandlerRepository = getFileStoreHandlerRepository();
        nodePersistor.load(m_node, getParentPersistor(), exec, tblRep, globalTableRepository, fileStoreHandlerRepository, result);
    } catch (final Exception e) {
        String error = "Error loading node content: " + e.getMessage();
        getLogger().warn(error, e);
        needsResetAfterLoad();
        result.addError(error);
    }
    if (nodePersistor.isDirtyAfterLoad()) {
        setDirtyAfterLoad();
    }
    if (nodePersistor.needsResetAfterLoad()) {
        setNeedsResetAfterLoad();
    }
    return washedModelSettings;
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) LoadNodeModelSettingsFailPolicy(org.knime.core.node.NodePersistor.LoadNodeModelSettingsFailPolicy) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) FileNodePersistor(org.knime.core.node.FileNodePersistor) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) NodeFactoryUnknownException(org.knime.core.node.workflow.WorkflowPersistor.NodeFactoryUnknownException) IOException(java.io.IOException) ContainerTable(org.knime.core.data.container.ContainerTable)

Aggregations

NodeSettings (org.knime.core.node.NodeSettings)156 File (java.io.File)58 FileOutputStream (java.io.FileOutputStream)57 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)37 GZIPOutputStream (java.util.zip.GZIPOutputStream)27 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)17 Test (org.junit.Test)16 NodeSettingsWO (org.knime.core.node.NodeSettingsWO)16 DefaultHiLiteMapper (org.knime.core.node.property.hilite.DefaultHiLiteMapper)16 IOException (java.io.IOException)14 OutputStream (java.io.OutputStream)12 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)11 BufferedOutputStream (java.io.BufferedOutputStream)9 Map (java.util.Map)8 RowKey (org.knime.core.data.RowKey)8 Config (org.knime.core.node.config.Config)8 LinkedHashMap (java.util.LinkedHashMap)7 DataTableSpec (org.knime.core.data.DataTableSpec)7 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)6 ArrayList (java.util.ArrayList)5