Search in sources :

Example 96 with NodeSettingsRO

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

the class MissingCellHandler method fromPMMLExtension.

/**
 * Creates a missing cell handler from an extension that is inside of a PMML derived field.
 * @param column the column this handler is used for
 * @param manager missing value factory manager
 * @param ext the extension containing the necessary information
 * @return a missing cell handler that was initialized from the extension
 * @throws InvalidSettingsException if the the factory from the extension is not applicable for the column
 */
protected static MissingCellHandler fromPMMLExtension(final DataColumnSpec column, final MissingCellHandlerFactoryManager manager, final Extension ext) throws InvalidSettingsException {
    String factoryID = ext.getValue();
    MissingCellHandlerFactory fac = manager.getFactoryByID(factoryID);
    if (fac == null) {
        LOGGER.error("Unknown missing cell handler " + factoryID + ".");
        final String[] parts = factoryID.split("\\.");
        throw new InvalidSettingsException("Unknown missing cell handler " + parts[parts.length - 1] + ".");
    }
    if (!fac.isApplicable(column.getType())) {
        throw new InvalidSettingsException("Missing cell handler " + fac.getDisplayName() + " is not applicable for columns of type " + column.getType().toString() + ".");
    }
    // Create document from empty node settings
    NodeSettings nodeSettings = new NodeSettings("");
    MissingCellHandler handler = fac.createHandler(column);
    // Without a panel, the handler has no settings and we can return it
    if (!fac.hasSettingsPanel()) {
        return handler;
    }
    // Load settings from the panel
    try {
        // Create an XML document from empty settings
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        nodeSettings.saveToXML(baos);
        Document doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(baos.toByteArray()));
        // Remove original settings
        doc.removeChild(doc.getFirstChild());
        // And plug in those from the PMML
        doc.appendChild(doc.importNode(ext.getDomNode().getFirstChild(), true));
        // Now write it to a stream and create new node settings
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(bos);
        transformer.transform(source, result);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        NodeSettingsRO settings = NodeSettings.loadFromXML(bis);
        handler.loadSettingsFrom(settings);
    } catch (Exception ex) {
        LOGGER.error("An error occurred while loading settings for a MissingCellHandler from PMML.\n" + ex.getMessage());
    }
    return handler;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettings(org.knime.core.node.NodeSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Example 97 with NodeSettingsRO

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

the class RenameNodeDialogPane method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final DataTableSpec[] specs) throws NotConfigurableException {
    DataTableSpec spec = specs[0];
    if (spec.getNumColumns() == 0) {
        throw new NotConfigurableException("No columns at input.");
    }
    m_orgTableSpec = specs[0];
    m_columnToSettings.clear();
    m_errornousColNames.clear();
    m_individualsPanel.removeAll();
    m_searchableListModifier = m_searchableListPanel.update(spec);
    NodeSettingsRO subSettings;
    try {
        // this node settings object must contain only entry of type
        // NodeSetting
        subSettings = settings.getNodeSettings(RenameNodeModel.CFG_SUB_CONFIG);
    } catch (InvalidSettingsException ise) {
        subSettings = null;
    }
    if (subSettings != null) {
        // process settings for individual column
        for (String id : subSettings) {
            NodeSettingsRO idSettings;
            String nameForSettings;
            try {
                // idSettigs address the settings for one particular column
                idSettings = subSettings.getNodeSettings(id);
                // the name of the column - must match
                nameForSettings = idSettings.getString(RenameColumnSetting.CFG_OLD_COLNAME);
            } catch (InvalidSettingsException is) {
                continue;
            }
            final DataColumnSpec orgSpec = m_orgTableSpec.getColumnSpec(nameForSettings);
            final RenameColumnSetting renameColumnSetting = orgSpec == null ? new RenameColumnSetting(nameForSettings) : new RenameColumnSetting(orgSpec);
            renameColumnSetting.loadSettingsFrom(idSettings);
            if (orgSpec == null) {
                DataColumnSpec invalidSpec = createInvalidSpec(nameForSettings);
                m_searchableListModifier.addAdditionalColumn(invalidSpec);
                m_columnToSettings.put(invalidSpec, renameColumnSetting);
            } else {
                m_columnToSettings.put(orgSpec, renameColumnSetting);
            }
        }
    }
    // add for each setting a panel in the individual panel
    for (Map.Entry<DataColumnSpec, RenameColumnSetting> entries : m_columnToSettings.entrySet()) {
        addToIndividualPanel(new RenameColumnPanel(entries.getValue(), entries.getKey(), m_hideColumnType));
    }
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 98 with NodeSettingsRO

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

the class RowFilterNodeModel method loadOrValidateSettingsFrom.

private void loadOrValidateSettingsFrom(final NodeSettingsRO settings, final boolean verifyOnly) throws InvalidSettingsException {
    IRowFilter tmpFilter = null;
    if (settings.containsKey(CFGFILTER)) {
        NodeSettingsRO filterCfg = settings.getNodeSettings(CFGFILTER);
        // because we don't know what type of filter is in the config we
        // must ask the factory to figure it out for us (actually the type
        // is also saved in a valid config). When we save row filters they
        // will (hopefully!!) add their type to the config.
        tmpFilter = RowFilterFactory.createRowFilter(filterCfg);
    } else {
        throw new InvalidSettingsException("Row Filter config contains no" + " row filter.");
    }
    // if we got so far settings are valid.
    if (verifyOnly) {
        return;
    }
    // take over settings
    m_rowFilter = tmpFilter;
    return;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) IRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter)

Example 99 with NodeSettingsRO

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

the class InteractiveHiLiteCollectorNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    this.reset();
    File f = new File(nodeInternDir, KEY_ANNOTATIONS);
    NodeSettingsRO sett = NodeSettings.loadFromXML(new FileInputStream(f));
    RowKey[] rowKeys = sett.getRowKeyArray("row_keys", (RowKey[]) null);
    if (rowKeys != null) {
        for (RowKey key : rowKeys) {
            try {
                NodeSettingsRO subSett = sett.getNodeSettings(key.toString());
                Map<Integer, String> map = new LinkedHashMap<Integer, String>();
                for (String i : subSett.keySet()) {
                    try {
                        int idx = Integer.parseInt(i);
                        m_lastIndex = (m_lastIndex == null ? idx : Math.max(m_lastIndex, idx));
                        map.put(idx, subSett.getString(i));
                    } catch (InvalidSettingsException ise) {
                    // ignored
                    }
                }
                m_annotationMap.put(key, map);
            } catch (InvalidSettingsException ise) {
            // ignored
            }
        }
    }
}
Also used : RowKey(org.knime.core.data.RowKey) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) File(java.io.File) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap)

Example 100 with NodeSettingsRO

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

the class AndRowFilter method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
public void loadSettingsFrom(final NodeSettingsRO cfg) throws InvalidSettingsException {
    NodeSettingsRO cfg1 = cfg.getNodeSettings(CFG_FILTER1);
    NodeSettingsRO cfg2 = cfg.getNodeSettings(CFG_FILTER2);
    m_in1 = RowFilterFactory.createRowFilter(cfg1);
    m_in2 = RowFilterFactory.createRowFilter(cfg2);
}
Also used : 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