Search in sources :

Example 66 with NodeSettingsRO

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

the class FileWorkflowPersistor method loadWorkflowAnnotations.

/**
 * Load annotations (added in v2.3).
 *
 * @param settings to load from
 * @return non-null list.
 * @throws InvalidSettingsException If this fails for any reason.
 */
List<WorkflowAnnotation> loadWorkflowAnnotations(final NodeSettingsRO settings) throws InvalidSettingsException {
    if (getLoadVersion().isOlderThan(LoadVersion.V230)) {
        // no credentials in v2.2 and before
        return Collections.emptyList();
    } else {
        if (!settings.containsKey("annotations")) {
            return Collections.emptyList();
        }
        NodeSettingsRO annoSettings = settings.getNodeSettings("annotations");
        List<WorkflowAnnotation> result = new ArrayList<WorkflowAnnotation>();
        for (String key : annoSettings.keySet()) {
            NodeSettingsRO child = annoSettings.getNodeSettings(key);
            WorkflowAnnotation anno = new WorkflowAnnotation();
            anno.load(child, getLoadVersion());
            result.add(anno);
        }
        return result;
    }
}
Also used : ArrayList(java.util.ArrayList) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Example 67 with NodeSettingsRO

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

the class FileWorkflowPersistor method preLoadNodeContainer.

/**
 * {@inheritDoc}
 */
@Override
public void preLoadNodeContainer(final WorkflowPersistor parentPersistor, final NodeSettingsRO parentSettings, final LoadResult loadResult) throws InvalidSettingsException, IOException {
    m_parentPersistor = parentPersistor;
    final ReferencedFile knimeFile = getWorkflowKNIMEFile();
    if (knimeFile == null || !knimeFile.getFile().isFile()) {
        setDirtyAfterLoad();
        String error = "Can't read workflow file \"" + knimeFile + "\"";
        throw new IOException(error);
    }
    // workflow.knime (or template.knime)
    File nodeFile = knimeFile.getFile();
    ReferencedFile parentRef = knimeFile.getParent();
    if (parentRef == null) {
        setDirtyAfterLoad();
        throw new IOException("Parent directory of file \"" + knimeFile + "\" is not represented by " + ReferencedFile.class.getSimpleName() + " object");
    }
    m_mustWarnOnDataLoadError = loadIfMustWarnOnDataLoadError(parentRef.getFile());
    NodeSettingsRO subWFSettings;
    try {
        InputStream in = new FileInputStream(nodeFile);
        if (m_parentPersistor != null) {
            // real metanode, not a project
            // the workflow.knime (or template.knime) file is not encrypted
            // with this metanode's cipher but possibly with a parent
            // cipher
            in = m_parentPersistor.decipherInput(in);
        }
        in = new BufferedInputStream(in);
        subWFSettings = NodeSettings.loadFromXML(in);
    } catch (IOException ioe) {
        setDirtyAfterLoad();
        throw ioe;
    }
    m_workflowSett = subWFSettings;
    try {
        if (m_nameOverwrite != null) {
            m_name = m_nameOverwrite;
        } else {
            m_name = loadWorkflowName(m_workflowSett);
        }
    } catch (InvalidSettingsException e) {
        String error = "Unable to load workflow name: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_name = null;
    }
    try {
        m_workflowCipher = loadWorkflowCipher(getLoadVersion(), m_workflowSett);
    } catch (InvalidSettingsException e) {
        String error = "Unable to load workflow cipher: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_workflowCipher = WorkflowCipher.NULL_CIPHER;
    }
    try {
        if (m_templateInformation != null) {
            // template information was set after construction (this node is a link created from a template)
            assert m_templateInformation.getRole() == Role.Link;
        } else {
            m_templateInformation = MetaNodeTemplateInformation.load(m_workflowSett, getLoadVersion());
            CheckUtils.checkSettingNotNull(m_templateInformation, "No template information");
        }
    } catch (InvalidSettingsException e) {
        String error = "Unable to load workflow template information: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_templateInformation = MetaNodeTemplateInformation.NONE;
    }
    try {
        m_authorInformation = loadAuthorInformation(m_workflowSett, loadResult);
    } catch (InvalidSettingsException e) {
        String error = "Unable to load workflow author information: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_authorInformation = AuthorInformation.UNKNOWN;
    }
    try {
        m_workflowVariables = loadWorkflowVariables(m_workflowSett);
    } catch (InvalidSettingsException e) {
        String error = "Unable to load workflow variables: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_workflowVariables = Collections.emptyList();
    }
    try {
        m_credentials = loadCredentials(m_workflowSett);
        // request to initialize credentials - if available
        if (m_credentials != null && !m_credentials.isEmpty()) {
            m_credentials = getLoadHelper().loadCredentialsPrefilled(m_credentials);
        }
    } catch (InvalidSettingsException e) {
        String error = "Unable to load credentials: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_credentials = Collections.emptyList();
    }
    try {
        m_tableBackendSettings = loadTableBackendSettings(m_workflowSett);
    } catch (InvalidSettingsException e) {
        String error = "Unable to table backend: " + e.getMessage();
        getLogger().debug(error, e);
        setNeedsResetAfterLoad();
        setDirtyAfterLoad();
        loadResult.addError(error, true);
        if (e instanceof TableBackendUnknownException) {
            // NOSONAR
            loadResult.addMissingTableFormat(((TableBackendUnknownException) e).getFormatInfo());
        }
        m_tableBackendSettings = isProject() ? new WorkflowTableBackendSettings() : null;
    }
    try {
        m_workflowAnnotations = loadWorkflowAnnotations(m_workflowSett);
    } catch (InvalidSettingsException e) {
        String error = "Unable to load workflow annotations: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_workflowAnnotations = Collections.emptyList();
    }
    try {
        m_wizardState = loadWizardState(m_workflowSett);
    } catch (InvalidSettingsException e) {
        String error = "Unable to load wizard state: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_wizardState = null;
    }
    NodeSettingsRO metaFlowParentSettings = new NodeSettings("fake_parent_settings");
    try {
        metaFlowParentSettings = readParentSettings();
    } catch (IOException e1) {
        String error = "Errors reading settings file: " + e1.getMessage();
        getLogger().warn(error, e1);
        setDirtyAfterLoad();
        loadResult.addError(error);
    }
    boolean isResetRequired = m_metaPersistor.load(subWFSettings, metaFlowParentSettings, loadResult);
    if (isResetRequired) {
        setNeedsResetAfterLoad();
    }
    if (m_metaPersistor.isDirtyAfterLoad()) {
        setDirtyAfterLoad();
    }
    /* read in and outports */
    NodeSettingsRO inPortsEnum = EMPTY_SETTINGS;
    try {
        NodeSettingsRO inPorts = loadInPortsSetting(m_workflowSett);
        if (inPorts != null) {
            inPortsEnum = loadInPortsSettingsEnum(inPorts);
        }
    } catch (InvalidSettingsException e) {
        String error = "Can't load workflow ports, config not found";
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        setNeedsResetAfterLoad();
    }
    int inPortCount = inPortsEnum.keySet().size();
    m_inPortTemplates = new WorkflowPortTemplate[inPortCount];
    for (String key : inPortsEnum.keySet()) {
        WorkflowPortTemplate p;
        try {
            NodeSettingsRO sub = inPortsEnum.getNodeSettings(key);
            p = loadInPortTemplate(sub);
        } catch (InvalidSettingsException e) {
            String error = "Can't load workflow inport (internal ID \"" + key + "\", skipping it: " + e.getMessage();
            getLogger().debug(error, e);
            setDirtyAfterLoad();
            loadResult.addError(error);
            setNeedsResetAfterLoad();
            continue;
        }
        int index = p.getPortIndex();
        if (index < 0 || index >= inPortCount) {
            setDirtyAfterLoad();
            loadResult.addError("Invalid inport index " + index);
            setNeedsResetAfterLoad();
            continue;
        }
        if (m_inPortTemplates[index] != null) {
            setDirtyAfterLoad();
            loadResult.addError("Duplicate inport definition for index: " + index);
        }
        m_inPortTemplates[index] = p;
    }
    for (int i = 0; i < m_inPortTemplates.length; i++) {
        if (m_inPortTemplates[i] == null) {
            setDirtyAfterLoad();
            loadResult.addError("Assigning fallback port type for " + "missing input port " + i);
            m_inPortTemplates[i] = new WorkflowPortTemplate(i, FALLBACK_PORTTYPE);
        }
    }
    NodeSettingsRO outPortsEnum = EMPTY_SETTINGS;
    try {
        NodeSettingsRO outPorts = loadOutPortsSetting(m_workflowSett);
        if (outPorts != null) {
            outPortsEnum = loadOutPortsSettingsEnum(outPorts);
        }
    } catch (InvalidSettingsException e) {
        String error = "Can't load workflow out ports, config not found: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
    }
    int outPortCount = outPortsEnum.keySet().size();
    m_outPortTemplates = new WorkflowPortTemplate[outPortCount];
    for (String key : outPortsEnum.keySet()) {
        WorkflowPortTemplate p;
        try {
            NodeSettingsRO sub = outPortsEnum.getNodeSettings(key);
            p = loadOutPortTemplate(sub);
        } catch (InvalidSettingsException e) {
            String error = "Can't load workflow outport (internal ID \"" + key + "\", skipping it: " + e.getMessage();
            getLogger().debug(error, e);
            setDirtyAfterLoad();
            loadResult.addError(error);
            setNeedsResetAfterLoad();
            continue;
        }
        int index = p.getPortIndex();
        if (index < 0 || index >= outPortCount) {
            setDirtyAfterLoad();
            loadResult.addError("Invalid inport index " + index);
            setNeedsResetAfterLoad();
            continue;
        }
        if (m_outPortTemplates[index] != null) {
            setDirtyAfterLoad();
            loadResult.addError("Duplicate outport definition for index: " + index);
        }
        m_outPortTemplates[index] = p;
    }
    for (int i = 0; i < m_outPortTemplates.length; i++) {
        if (m_outPortTemplates[i] == null) {
            setDirtyAfterLoad();
            loadResult.addError("Assigning fallback port type for " + "missing output port " + i);
            m_outPortTemplates[i] = new WorkflowPortTemplate(i, FALLBACK_PORTTYPE);
        }
    }
    boolean hasPorts = inPortCount > 0 || outPortCount > 0;
    if (hasPorts && m_isProject) {
        throw new InvalidSettingsException(String.format("Workflow \"%s\"" + " is not a project as it has ports (%d in, %d out)", nodeFile.getAbsoluteFile(), inPortCount, outPortCount));
    }
    NodeSettingsRO inPorts = EMPTY_SETTINGS;
    NodeUIInformation inPortsBarUIInfo = null;
    String uiInfoClassName = null;
    try {
        inPorts = loadInPortsSetting(m_workflowSett);
        if (inPorts != null) {
            uiInfoClassName = loadInPortsBarUIInfoClassName(inPorts);
        }
    } catch (InvalidSettingsException e) {
        String error = "Unable to load class name for inport bar's " + "UI information: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
    }
    if (uiInfoClassName != null) {
        try {
            if (!getLoadVersion().isOlderThan(LoadVersion.V200)) {
                inPortsBarUIInfo = loadNodeUIInformation(inPorts);
            }
        } catch (InvalidSettingsException e) {
            String error = "Unable to load inport bar's UI information: " + e.getMessage();
            getLogger().debug(error, e);
            setDirtyAfterLoad();
            loadResult.addError(error);
            inPortsBarUIInfo = null;
        }
    }
    NodeSettingsRO outPorts = null;
    m_inPortsBarUIInfo = inPortsBarUIInfo;
    NodeUIInformation outPortsBarUIInfo = null;
    uiInfoClassName = null;
    try {
        // TODO probably not necessary anymore to store the ui information class name (it's node ui information anyway)
        outPorts = loadOutPortsSetting(m_workflowSett);
        if (outPorts != null) {
            uiInfoClassName = loadOutPortsBarUIInfoClassName(outPorts);
        }
    } catch (InvalidSettingsException e) {
        String error = "Unable to load class name for outport bar's UI information" + ", no UI information available: " + e.getMessage();
        setDirtyAfterLoad();
        getLogger().debug(error, e);
        loadResult.addError(error);
    }
    if (uiInfoClassName != null) {
        try {
            if (!getLoadVersion().isOlderThan(LoadVersion.V200)) {
                outPortsBarUIInfo = loadNodeUIInformation(outPorts);
            }
        } catch (InvalidSettingsException e) {
            String error = "Unable to load outport bar's UI information: " + e.getMessage();
            getLogger().debug(error, e);
            setDirtyAfterLoad();
            loadResult.addError(error);
            outPortsBarUIInfo = null;
        }
    }
    m_outPortsBarUIInfo = outPortsBarUIInfo;
    try {
        m_editorUIInfo = loadEditorUIInformation(m_workflowSett);
    } catch (InvalidSettingsException e) {
        String error = "Unable to load editor UI information: " + e.getMessage();
        getLogger().debug(error, e);
        setDirtyAfterLoad();
        loadResult.addError(error);
        m_editorUIInfo = null;
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) FileInputStream(java.io.FileInputStream) NodeSettings(org.knime.core.node.NodeSettings) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) TableBackendUnknownException(org.knime.core.node.workflow.WorkflowTableBackendSettings.TableBackendUnknownException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 68 with NodeSettingsRO

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

the class FileWorkflowPersistor method loadAuthorInformation.

private AuthorInformation loadAuthorInformation(final NodeSettingsRO settings, final LoadResult loadResult) throws InvalidSettingsException {
    if (getLoadVersion().ordinal() >= LoadVersion.V280.ordinal() && settings.containsKey(CFG_AUTHOR_INFORMATION)) {
        final NodeSettingsRO sub = settings.getNodeSettings(CFG_AUTHOR_INFORMATION);
        final String author = sub.getString("authored-by");
        final String authorDateS = sub.getString("authored-when");
        OffsetDateTime authorDate;
        if (authorDateS == null) {
            authorDate = null;
        } else {
            try {
                authorDate = parseDate(authorDateS);
            } catch (DateTimeParseException e) {
                authorDate = OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
                loadResult.addWarning(String.format("Can't parse authored-when-date \"%s\". Replaced with \"%s\".", authorDateS, authorDate.toString()));
            }
        }
        final String editor = sub.getString("lastEdited-by");
        final String editDateS = sub.getString("lastEdited-when");
        OffsetDateTime editDate;
        if (editDateS == null) {
            editDate = null;
        } else {
            try {
                editDate = parseDate(editDateS);
            } catch (DateTimeParseException e) {
                editDate = OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
                loadResult.addWarning(String.format("Can't parse lastEdit-when-date \"%s\". Replaced with \"%s\".", editDateS, editDate.toString()));
            }
        }
        return new AuthorInformation(author, authorDate, editor, editDate);
    } else {
        return AuthorInformation.UNKNOWN;
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) OffsetDateTime(java.time.OffsetDateTime) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) AuthorInformation(org.knime.core.util.workflowalizer.AuthorInformation)

Example 69 with NodeSettingsRO

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

the class FileWorkflowPersistor method loadWorkflowVariables.

/**
 * Load workflow variables (not available in 1.3.x flows).
 *
 * @param settings To load from.
 * @return The variables in a list.
 * @throws InvalidSettingsException If any settings-related error occurs.
 */
List<FlowVariable> loadWorkflowVariables(final NodeSettingsRO settings) throws InvalidSettingsException {
    if (getLoadVersion().isOlderThan(LoadVersion.V200)) {
        return Collections.emptyList();
    } else {
        if (!settings.containsKey(CFG_WKF_VARIABLES)) {
            return Collections.emptyList();
        }
        NodeSettingsRO wfmVarSub = settings.getNodeSettings(CFG_WKF_VARIABLES);
        List<FlowVariable> result = new ArrayList<FlowVariable>();
        for (String key : wfmVarSub.keySet()) {
            result.add(FlowVariable.load(wfmVarSub.getNodeSettings(key)));
        }
        return result;
    }
}
Also used : ArrayList(java.util.ArrayList) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Example 70 with NodeSettingsRO

use of org.knime.core.node.NodeSettingsRO 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 {
    m_nodePersistor = createNodePersistor(settingsForNode);
    m_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(), m_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 {
        WorkflowDataRepository dataRepository = getWorkflowDataRepository();
        m_nodePersistor.load(m_node, getParentPersistor(), exec, tblRep, dataRepository, result);
    } catch (final Exception e) {
        String error = "Error loading node content: " + e.getMessage();
        getLogger().warn(error, e);
        needsResetAfterLoad();
        result.addError(error);
    }
    if (m_nodePersistor.isDirtyAfterLoad()) {
        setDirtyAfterLoad();
    }
    if (m_nodePersistor.needsResetAfterLoad()) {
        setNeedsResetAfterLoad();
    }
    return washedModelSettings;
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) LoadNodeModelSettingsFailPolicy(org.knime.core.node.NodePersistor.LoadNodeModelSettingsFailPolicy) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) InvalidNodeFactoryExtensionException(org.knime.core.node.extension.InvalidNodeFactoryExtensionException) NodeFactoryUnknownException(org.knime.core.node.workflow.WorkflowPersistor.NodeFactoryUnknownException) IOException(java.io.IOException)

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