Search in sources :

Example 6 with NodeSettings

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

the class FileNativeNodeContainerPersistor method loadSettingsForNode.

/**
 * {@inheritDoc}
 */
@Override
NodeSettingsRO loadSettingsForNode(final LoadResult loadResult) throws IOException {
    NodeSettingsRO nodeSettings = getNodeSettings();
    if (getLoadVersion().ordinal() < LoadVersion.V280.ordinal()) {
        FileNodeContainerMetaPersistor metaPersistor = getMetaPersistor();
        ReferencedFile nodeFile;
        try {
            nodeFile = loadNodeFile(nodeSettings);
        } catch (InvalidSettingsException e) {
            String error = "Unable to load node settings file for node with ID suffix " + metaPersistor.getNodeIDSuffix() + " (node \"" + m_node.getName() + "\"): " + e.getMessage();
            loadResult.addError(error);
            getLogger().debug(error, e);
            setDirtyAfterLoad();
            return new NodeSettings("empty");
        }
        File configFile = nodeFile.getFile();
        if (configFile == null || !configFile.isFile() || !configFile.canRead()) {
            String error = "Unable to read node settings file for node with ID suffix " + metaPersistor.getNodeIDSuffix() + " (node \"" + m_node.getName() + "\"), file \"" + configFile + "\"";
            loadResult.addError(error);
            // also implies dirty
            setNeedsResetAfterLoad();
            return new NodeSettings("empty");
        } else {
            InputStream in = new FileInputStream(configFile);
            try {
                in = getParentPersistor().decipherInput(in);
                return NodeSettings.loadFromXML(new BufferedInputStream(in));
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    getLogger().error("Failed to close input stream on \"" + configFile.getAbsolutePath() + "\"", e);
                }
            }
        }
    } else {
        return nodeSettings;
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with NodeSettings

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

the class FileSingleNodeContainerPersistor method save.

/**
 * @noreference This method is not intended to be referenced by clients.
 */
protected static String save(final SingleNodeContainer singleNC, final ReferencedFile rawNodeDirRef, final ExecutionMonitor exec, final WorkflowSaveHelper saveHelper) throws CanceledExecutionException, IOException, LockFailedException {
    String settingsDotXML = singleNC.getDirectNCParent().getCipherFileName(SETTINGS_FILE_NAME);
    ReferencedFile nodeDirRef = rawNodeDirRef;
    ReferencedFile sncWorkingDirRef = singleNC.getNodeContainerDirectory();
    ReferencedFile sncAutoSaveDirRef = singleNC.getAutoSaveDirectory();
    File nodeDir = nodeDirRef.getFile();
    boolean nodeDirExists = nodeDir.exists();
    // the if-checks below also update the nodeDirRef so that we can make changes on that object
    if (!saveHelper.isAutoSave() && nodeDirRef.equals(sncWorkingDirRef)) {
        if (!sncWorkingDirRef.isDirty() && nodeDirExists) {
            return settingsDotXML;
        } else {
            nodeDirRef = sncWorkingDirRef;
        }
    }
    if (saveHelper.isAutoSave() && nodeDirRef.equals(sncAutoSaveDirRef)) {
        if (!sncAutoSaveDirRef.isDirty() && nodeDirExists) {
            return settingsDotXML;
        } else {
            nodeDirRef = sncAutoSaveDirRef;
        }
    }
    boolean nodeDirDeleted = true;
    if (singleNC instanceof NativeNodeContainer) {
        nodeDirDeleted = deleteChildren(nodeDir, SingleNodeContainer.DROP_DIR_NAME);
    }
    nodeDir.mkdirs();
    if (!nodeDir.isDirectory() || !nodeDir.canWrite()) {
        throw new IOException("Unable to write or create directory \"" + nodeDirRef + "\"");
    }
    String debug;
    if (singleNC instanceof NativeNodeContainer && nodeDirExists) {
        if (nodeDirDeleted) {
            debug = "Replaced node directory \"" + nodeDirRef + "\"";
        } else {
            debug = "Failed to replace node directory \"" + nodeDirRef + "\" -- writing into existing directory";
        }
    } else {
        debug = "Created node directory \"" + nodeDirRef + "\"";
    }
    SAVE_LOGGER.debug(debug);
    // get drop directory in "home" (the designated working dir)
    ReferencedFile nodeDropDirInWDRef = sncWorkingDirRef == null ? null : new ReferencedFile(sncWorkingDirRef, SingleNodeContainer.DROP_DIR_NAME);
    ReferencedFile nodeDropDirRef = new ReferencedFile(nodeDirRef, SingleNodeContainer.DROP_DIR_NAME);
    // if node container directory is set and we write into a new location
    if (nodeDropDirInWDRef != null && !nodeDropDirRef.equals(nodeDropDirInWDRef)) {
        // this code is executed in either of the two cases:
        // - Node was copy&paste from node with drop folder
        // (its (freshly copied) drop folder is currently in /tmp)
        // - Node is saved into new location (saveAs) -- need to copy
        // the drop folder there (either from /tmp or from working dir)
        File dropInSource = nodeDropDirInWDRef.getFile();
        File dropInTarget = new File(nodeDir, SingleNodeContainer.DROP_DIR_NAME);
        if (dropInSource.exists()) {
            FileUtil.copyDir(dropInSource, dropInTarget);
        }
    }
    NodeSettings settings = new NodeSettings(SETTINGS_FILE_NAME);
    // only to allow 2.7- clients to load 2.8+ workflows
    saveNodeFileName(singleNC, settings, nodeDirRef);
    saveFlowObjectStack(settings, singleNC);
    saveSNCSettings(settings, singleNC);
    FileNodeContainerMetaPersistor.save(settings, singleNC, nodeDirRef);
    if (singleNC instanceof NativeNodeContainer) {
        NativeNodeContainer nativeNC = (NativeNodeContainer) singleNC;
        FileNativeNodeContainerPersistor.save(nativeNC, settings, exec, nodeDirRef, saveHelper.isSaveData() && singleNC.getInternalState().equals(InternalNodeContainerState.EXECUTED));
    } else {
        SubNodeContainer subnodeNC = (SubNodeContainer) singleNC;
        FileSubNodeContainerPersistor.save(subnodeNC, settings, exec, nodeDirRef, saveHelper);
    }
    File nodeSettingsXMLFile = new File(nodeDir, settingsDotXML);
    OutputStream os = new FileOutputStream(nodeSettingsXMLFile);
    os = singleNC.getDirectNCParent().cipherOutput(os);
    settings.saveToXML(os);
    if (saveHelper.isAutoSave() && sncAutoSaveDirRef == null) {
        sncAutoSaveDirRef = nodeDirRef;
        singleNC.setAutoSaveDirectory(sncAutoSaveDirRef);
    }
    if (!saveHelper.isAutoSave() && sncWorkingDirRef == null) {
        // set working dir so that we can unset the dirty flag
        sncWorkingDirRef = nodeDirRef;
        singleNC.setNodeContainerDirectory(sncWorkingDirRef);
    }
    nodeDirRef.setDirty(false);
    if (nodeDirRef.equals(sncWorkingDirRef)) {
        singleNC.unsetDirty();
    }
    exec.setProgress(1.0);
    return settingsDotXML;
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 8 with NodeSettings

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

the class FileTemplateRepository method addTemplate.

/**
 * Add a template to the default location.
 * @param template the template
 */
@Override
public void addTemplate(final T template) {
    if (m_readonly) {
        throw new RuntimeException("This repository is read only." + "Cannot add a template.");
    }
    try {
        File file = getFile(template);
        boolean isNew = file.createNewFile();
        if (isNew) {
            NodeSettings settings = new NodeSettings(file.getName());
            template.saveSettings(settings);
            settings.saveToXML(new FileOutputStream(file));
            // reload settings
            NodeSettingsRO settingsro = NodeSettings.loadFromXML(new FileInputStream(file));
            // set the reloaded settings so that all references to existing
            // objects are broken. This makes sure, that the template is not
            // changed from outside.
            template.loadSettings(settingsro);
            appendTemplates(Collections.singletonList(template));
        } else {
            throw new IOException("A file with this name does " + "already exist: " + file.getAbsolutePath());
        }
    } catch (IOException e1) {
        NodeLogger.getLogger(this.getClass()).error("Could not create template at the default location.", e1);
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) FileOutputStream(java.io.FileOutputStream) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 9 with NodeSettings

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

the class RandomForestClassificationLearnerNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File file;
    ExecutionMonitor sub;
    if (m_oldStyleEnsembleModel_deprecated != null) {
        // old workflow (<2.10) loaded and saved ...
        file = new File(nodeInternDir, INTERNAL_TREES_FILE);
        OutputStream out = new GZIPOutputStream(new FileOutputStream(file));
        sub = exec.createSubProgress(0.2);
        m_oldStyleEnsembleModel_deprecated.save(out, sub);
        out.close();
    }
    if (m_hiliteRowSample != null) {
        file = new File(nodeInternDir, INTERNAL_DATASAMPLE_FILE);
        sub = exec.createSubProgress(0.2);
        DataContainer.writeToZip(m_hiliteRowSample, file, sub);
    }
    if (m_viewMessage != null) {
        file = new File(nodeInternDir, INTERNAL_INFO_FILE);
        NodeSettings sets = new NodeSettings("ensembleData");
        sets.addString("view_warning", m_viewMessage);
        sets.saveToXML(new FileOutputStream(file));
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) File(java.io.File)

Example 10 with NodeSettings

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

the class RandomForestRegressionLearnerNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File file;
    ExecutionMonitor sub;
    if (m_oldStyleEnsembleModel_deprecated != null) {
        // old workflow (<2.10) loaded and saved ...
        file = new File(nodeInternDir, INTERNAL_TREES_FILE);
        OutputStream out = new GZIPOutputStream(new FileOutputStream(file));
        sub = exec.createSubProgress(0.2);
        m_oldStyleEnsembleModel_deprecated.save(out, sub);
        out.close();
    }
    if (m_hiliteRowSample != null) {
        file = new File(nodeInternDir, INTERNAL_DATASAMPLE_FILE);
        sub = exec.createSubProgress(0.2);
        DataContainer.writeToZip(m_hiliteRowSample, file, sub);
    }
    if (m_viewMessage != null) {
        file = new File(nodeInternDir, INTERNAL_INFO_FILE);
        NodeSettings sets = new NodeSettings("ensembleData");
        sets.addString("view_warning", m_viewMessage);
        sets.saveToXML(new FileOutputStream(file));
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) File(java.io.File)

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