Search in sources :

Example 1 with NodeSettingsRO

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

the class AnnotationData method load.

/**
 * loads new values.
 * @param config To load from
 * @param loadVersion Version to load
 * @throws InvalidSettingsException If fails
 */
public void load(final NodeSettingsRO config, final FileWorkflowPersistor.LoadVersion loadVersion) throws InvalidSettingsException {
    setText(config.getString("text"));
    setBgColor(config.getInt("bgcolor"));
    int x = config.getInt("x-coordinate");
    int y = config.getInt("y-coordinate");
    int width = config.getInt("width");
    int height = config.getInt("height");
    // default to 0 for backward compatibility
    int borderSize = config.getInt("borderSize", 0);
    // default for backward compatibility
    int borderColor = config.getInt("borderColor", 0);
    // default for backward compatibility
    int defFontSize = config.getInt("defFontSize", -1);
    // added in 3.0
    m_version = config.getInt("annotation-version", VERSION_OLD);
    TextAlignment alignment = TextAlignment.LEFT;
    if (loadVersion.ordinal() >= FileWorkflowPersistor.LoadVersion.V250.ordinal()) {
        String alignmentS = config.getString("alignment");
        try {
            alignment = TextAlignment.valueOf(alignmentS);
        } catch (Exception e) {
            throw new InvalidSettingsException("Invalid alignment: " + alignmentS, e);
        }
    }
    setDimension(x, y, width, height);
    setAlignment(alignment);
    setBorderSize(borderSize);
    setBorderColor(borderColor);
    setDefaultFontSize(defFontSize);
    NodeSettingsRO styleConfigs = config.getNodeSettings("styles");
    StyleRange[] styles = new StyleRange[styleConfigs.getChildCount()];
    int i = 0;
    for (String key : styleConfigs.keySet()) {
        NodeSettingsRO cur = styleConfigs.getNodeSettings(key);
        styles[i++] = StyleRange.load(cur);
    }
    setStyleRanges(styles);
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 2 with NodeSettingsRO

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

the class MetaNodeTemplateInformation method load.

/**
 * Load information from argument, throw {@link InvalidSettingsException}
 * if that fails.
 * @param settings To load from.
 * @param version The version this workflow is loading from
 * @return a new template loading from the argument settings.
 * @throws InvalidSettingsException If that fails.
 */
public static MetaNodeTemplateInformation load(final NodeSettingsRO settings, final FileWorkflowPersistor.LoadVersion version) throws InvalidSettingsException {
    if (!settings.containsKey(CFG_TEMPLATE_INFO)) {
        return NONE;
    }
    NodeSettingsRO nestedSettings = settings.getNodeSettings(CFG_TEMPLATE_INFO);
    String roleS = nestedSettings.getString("role");
    Role role;
    Date timestamp;
    URI sourceURI;
    TemplateType templateType;
    try {
        role = Role.valueOf(roleS);
    } catch (Exception e) {
        throw new InvalidSettingsException("Cannot parse template role \"" + roleS + "\": " + e.getMessage(), e);
    }
    switch(role) {
        case None:
            return NONE;
        case Template:
            sourceURI = null;
            timestamp = readTimestamp(nestedSettings);
            templateType = readTemplateType(nestedSettings, version);
            break;
        case Link:
            sourceURI = readURI(nestedSettings);
            templateType = null;
            timestamp = readTimestamp(nestedSettings);
            break;
        default:
            throw new InvalidSettingsException("Unsupported role: " + role);
    }
    return new MetaNodeTemplateInformation(role, templateType, sourceURI, timestamp);
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) URI(java.net.URI) Date(java.util.Date) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ParseException(java.text.ParseException)

Example 3 with NodeSettingsRO

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

the class FileNativeNodeContainerPersistor method guessPortTypesFromConnectedNodes.

/**
 * {@inheritDoc}
 */
@Override
public void guessPortTypesFromConnectedNodes(final NodeAndBundleInformation nodeInfo, final NodeSettingsRO additionalFactorySettings, final ArrayList<PersistorWithPortIndex> upstreamNodes, final ArrayList<List<PersistorWithPortIndex>> downstreamNodes) {
    if (m_node == null) {
        /* Input ports from the connection table. */
        // first is flow var port
        PortType[] inPortTypes = new PortType[Math.max(upstreamNodes.size() - 1, 0)];
        // default to BDT for unconnected ports
        Arrays.fill(inPortTypes, BufferedDataTable.TYPE);
        for (int i = 0; i < inPortTypes.length; i++) {
            // first is flow var port
            PersistorWithPortIndex p = upstreamNodes.get(i + 1);
            if (p != null) {
                PortType portTypeFromUpstreamNode = p.getPersistor().getUpstreamPortType(p.getPortIndex());
                if (portTypeFromUpstreamNode != null) {
                    // null if upstream is missing, too
                    inPortTypes[i] = portTypeFromUpstreamNode;
                }
            }
        }
        /* Output ports from node settings (saved ports) -- if possible (executed) */
        String nodeName = nodeInfo.getNodeNameNotNull();
        PortType[] outPortTypes;
        try {
            LoadResult guessLoadResult = new LoadResult("Port type guessing for missing node \"" + nodeName + "\"");
            NodeSettingsRO settingsForNode = loadSettingsForNode(guessLoadResult);
            FileNodePersistor nodePersistor = createNodePersistor(settingsForNode);
            outPortTypes = nodePersistor.guessOutputPortTypes(guessLoadResult, nodeName);
            if (guessLoadResult.hasErrors()) {
                getLogger().debug("Errors guessing port types for missing node \"" + nodeName + "\": " + guessLoadResult.getFilteredError("", LoadResultEntryType.Error));
            }
        } catch (Exception e) {
            getLogger().debug("Unable to guess port types for missing node \"" + nodeName + "\"", e);
            outPortTypes = null;
        }
        if (outPortTypes == null) {
            // couldn't guess port types from looking at node settings (e.g. not executed)
            // default to BDT for unconnected ports
            outPortTypes = new PortType[Math.max(downstreamNodes.size() - 1, 0)];
        }
        for (int i = 0; i < outPortTypes.length; i++) {
            PortType type = outPortTypes[i];
            // output types may be partially filled by settings guessing above, list may be empty or too short
            List<PersistorWithPortIndex> list = i < downstreamNodes.size() - 1 ? downstreamNodes.get(i + 1) : null;
            if (list != null) {
                assert !list.isEmpty();
                for (PersistorWithPortIndex p : list) {
                    PortType current = p.getPersistor().getDownstreamPortType(p.getPortIndex());
                    if (current == null) {
                    // ignore, downstream node is also missing
                    } else if (type == null) {
                        type = current;
                    } else if (type.equals(current)) {
                    // keep type
                    } else {
                        // this shouldn't really happen - someone changed port types between versions
                        type = PortObject.TYPE;
                    }
                }
                outPortTypes[i] = type;
            }
            if (outPortTypes[i] == null) {
                // might still be null if missing node is only connected to missing node, fallback: BDT
                outPortTypes[i] = BufferedDataTable.TYPE;
            }
        }
        MissingNodeFactory nodefactory = new MissingNodeFactory(nodeInfo, additionalFactorySettings, inPortTypes, outPortTypes);
        if (getLoadVersion().ordinal() < FileWorkflowPersistor.VERSION_LATEST.ordinal()) {
            nodefactory.setCopyInternDirForWorkflowVersionChange(true);
        }
        nodefactory.init();
        m_node = new Node((NodeFactory) nodefactory);
    }
}
Also used : Node(org.knime.core.node.Node) FileNodePersistor(org.knime.core.node.FileNodePersistor) LoadResult(org.knime.core.node.workflow.WorkflowPersistor.LoadResult) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) NodeFactoryUnknownException(org.knime.core.node.workflow.WorkflowPersistor.NodeFactoryUnknownException) IOException(java.io.IOException) NodeFactory(org.knime.core.node.NodeFactory) MissingNodeFactory(org.knime.core.node.missing.MissingNodeFactory) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) MissingNodeFactory(org.knime.core.node.missing.MissingNodeFactory) PortType(org.knime.core.node.port.PortType)

Example 4 with NodeSettingsRO

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

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

the class JavaSnippetTemplate method loadSettings.

/**
 * Loads parameters.
 * @param settings to load from
 */
@Override
public void loadSettings(final NodeSettingsRO settings) {
    try {
        String metaCategory = settings.getString(META_CATEGORY, null);
        m_metaCategory = getMetaCategoryClass(metaCategory);
        m_category = settings.getString(CATEGORY, "default");
        m_name = settings.getString(NAME, "?");
        m_description = settings.getString(DESCRIPTION, "");
        m_version = settings.getString(m_version, JavaSnippetTemplate.VERSION_1_X);
        NodeSettingsRO snippet = settings.getNodeSettings(SNIPPET);
        m_snippetSettings = new JavaSnippetSettings();
        m_snippetSettings.loadSettingsForDialog(snippet);
        m_uuid = m_snippetSettings.getTemplateUUID();
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    } catch (InvalidSettingsException e) {
        throw new IllegalStateException(e);
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) JavaSnippetSettings(org.knime.base.node.jsnippet.util.JavaSnippetSettings)

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