Search in sources :

Example 46 with NodeSettings

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

the class CellSplitterCellFactory method createNewColumnTypes.

/**
 * Analyzes the values in the user selected column and tries to figure out
 * how many columns are needed to hold the splitted values and of which type
 * the new resulting column have to be. <br>
 * If the "output as list" or "output as set" flag IS set in the settings
 * object it returns one as column number, since only one collection cell
 * is needed to store the output.
 * If the "guess" flag in the settings object is NOT set, it returns the
 * column number entered by the user and string type for all columns.
 * Otherwise it runs once through the entire table, splits the value of the
 * selected column, stores the maximum number of parts received, and tries
 * to convert each part into an int (first), then into a double, and if both
 * fails it sets string type for the corresponding column.
 *
 * @param table the table with the column to examine (can be null, if no
 *            type guessing is required)
 * @param userSettings user settings
 * @param exec the execution context to set progress and check for cancel
 *            (can be null)
 * @return a settings object containing the same settings as the ones passed
 *         in and in addition the type (and number) of each column to add
 * @throws CanceledExecutionException if user cancels
 */
static CellSplitterSettings createNewColumnTypes(final BufferedDataTable table, final CellSplitterUserSettings userSettings, final ExecutionContext exec) throws CanceledExecutionException {
    // make sure we have settings we can deal with
    DataTableSpec spec = null;
    if (table != null) {
        spec = table.getDataTableSpec();
    }
    String msg = userSettings.getStatus(spec);
    if (msg != null) {
        // don't call this with invalid settings
        assert false;
        throw new IllegalStateException(msg);
    }
    // transfer the user settings into a new settings object (the result)
    CellSplitterSettings result;
    NodeSettings tmp = new NodeSettings("tmp");
    userSettings.saveSettingsTo(tmp);
    try {
        result = new CellSplitterSettings(tmp);
    } catch (InvalidSettingsException ise) {
        // the getStatus should have covered any invalidities
        throw new IllegalStateException(ise.getMessage());
    }
    /*
         * not guessing types: output as columns
         */
    if (!userSettings.isGuessNumOfCols() && userSettings.isOutputAsCols()) {
        // we are not supposed to analyze the file.
        for (int col = 0; col < userSettings.getNumOfCols(); col++) {
            // create as many string columns as the user set
            result.addColumnOfType(StringCell.TYPE);
        }
        return result;
    }
    /*
         * not guessing types: output as list or set
         */
    if (userSettings.isOutputAsList() || userSettings.isOutputAsSet()) {
        DataType colType = null;
        // list cell type
        if (userSettings.isOutputAsList()) {
            colType = ListCell.getCollectionType(StringCell.TYPE);
        // set cell type otherwise (there is no other option left)
        } else {
            colType = SetCell.getCollectionType(StringCell.TYPE);
        }
        result.addColumnOfType(colType);
        return result;
    }
    /*
         * analyze table
         */
    int colIdx = table.getDataTableSpec().findColumnIndex(userSettings.getColumnName());
    if (colIdx < 0) {
        // the status should have checked this!
        assert false;
        throw new IllegalStateException("Input table doesn't contain selected column");
    }
    TokenizerSettings tokenizerSettings = createTokenizerSettings(userSettings);
    if (tokenizerSettings == null) {
        throw new IllegalStateException("Incorrect user settings");
    }
    long rowCnt = 0;
    long numOfRows = table.size();
    for (DataRow row : table) {
        rowCnt++;
        String inputString = "";
        DataCell inputCell = row.getCell(colIdx);
        if (inputCell.isMissing()) {
            // missing cells don't help determining the target types
            continue;
        }
        if (inputCell instanceof StringValue) {
            inputString = ((StringValue) inputCell).getStringValue();
        } else {
            inputString = inputCell.toString();
        }
        // init the tokenizer
        StringReader inputReader = new StringReader(inputString);
        // the reader is no good if it doesn't support the mark operation
        assert inputReader.markSupported();
        Tokenizer tokenizer = new Tokenizer(inputReader);
        tokenizer.setSettings(tokenizerSettings);
        int addedColIdx = -1;
        // read tokens from the input, analyze the tokens and set the type
        while (true) {
            String token = tokenizer.nextToken();
            addedColIdx++;
            if (token == null) {
                // done with that input string from that row
                break;
            }
            token = token.trim();
            DataType colType = IntCell.TYPE;
            // if we already got that many columns, verify the type
            if (addedColIdx < result.getNumOfColsGuessed()) {
                colType = result.getTypeOfColumn(addedColIdx);
            } else {
                // otherwise init the type with int
                result.addColumnOfType(colType);
            }
            if (colType.equals(IntCell.TYPE)) {
                // try converting it to an integer
                try {
                    Integer.parseInt(token);
                } catch (NumberFormatException nfe) {
                    // that wasn't really an integer. Try double.
                    colType = DoubleCell.TYPE;
                }
            }
            if (colType.equals(DoubleCell.TYPE)) {
                // try converting it to a double
                try {
                    Double.parseDouble(token);
                } catch (NumberFormatException nfe) {
                    // that wasn't really a double. Use string.
                    colType = StringCell.TYPE;
                }
            }
            // write back the type
            result.replaceTypeOfColumn(addedColIdx, colType);
        }
        if (exec != null) {
            exec.checkCanceled();
            exec.setProgress((double) rowCnt / (double) numOfRows, "Analyzing row #" + rowCnt + " of " + numOfRows);
        }
    }
    /*
         * if the input table contained missing values only, we end up with no
         * column to add. Throw an exception.
         */
    if (result.getNumOfColsGuessed() < 1) {
        throw new IllegalStateException("Data analysis computed no " + "columns to add (happens if input table is empty or " + "has only missing values).\n" + "Please set the array size manually.");
    }
    return result;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataRow(org.knime.core.data.DataRow) NodeSettings(org.knime.core.node.NodeSettings) TokenizerSettings(org.knime.core.util.tokenizer.TokenizerSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringReader(java.io.StringReader) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) Tokenizer(org.knime.core.util.tokenizer.Tokenizer)

Example 47 with NodeSettings

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

the class DataValidatorNodeDialogPane method createApplyDataTableSpecPanel.

/**
 * @return
 */
private Component createApplyDataTableSpecPanel() {
    JPanel toReturn = new JPanel(new BorderLayout());
    JButton applyDataTablesSpecButon = new JButton("<html><center>Set Input Table <br>as Reference</html>");
    applyDataTablesSpecButon.setToolTipText("Sets the current input data table spec as the reference specification.");
    applyDataTablesSpecButon.setBorder(BorderFactory.createLineBorder(Color.orange));
    applyDataTablesSpecButon.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            NodeSettings nodeSettings = new NodeSettings("Tmp");
            try {
                // resetting the dialog is in principle the same procedure
                // done by #saveSettingsTo and #loadSettingsFrom.
                // As the m_dataTableSpec is saved in the configuration the only
                // thing we have to do is to set it to the given spec.
                m_referenceDataTableSpec = m_inputDataTableSpec;
                saveSettingsTo(nodeSettings);
                loadSettingsFrom(nodeSettings, new DataTableSpec[] { m_inputDataTableSpec });
            } catch (InvalidSettingsException | NotConfigurableException e1) {
                LOGGER.info("Problem while applying new configuration.", e1);
            }
        }
    });
    toReturn.add(applyDataTablesSpecButon, BorderLayout.CENTER);
    return toReturn;
}
Also used : JPanel(javax.swing.JPanel) NodeSettings(org.knime.core.node.NodeSettings) DataTableSpec(org.knime.core.data.DataTableSpec) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton)

Example 48 with NodeSettings

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

the class DNDSelectionConfiguration method loadSettingsFrom.

public void loadSettingsFrom(final NodeSettingsRO settings, final DataTableSpec[] specs) throws NotConfigurableException {
    m_manual.loadSettingsFrom(settings, specs);
    NodeSettings nsro;
    try {
        nsro = (NodeSettings) settings.getNodeSettings("typeSelectionSettings");
    } catch (InvalidSettingsException e) {
        nsro = new NodeSettings("typeSelectionSettings");
    }
    m_type.loadSettingsFrom(nsro, specs);
    m_radioButtons.loadSettingsFrom(settings, specs);
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 49 with NodeSettings

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

the class ManualSelectionConfiguration method saveSettings.

@Override
public void saveSettings(final NodeSettingsWO settings) {
    settings.addInt("numberOfPanels", m_panelList.size());
    List<DropPaneConfig> values = Arrays.asList(m_panelList.values().toArray(new DropPaneConfig[m_panelList.size()]));
    Collections.sort(values);
    for (int i = 0; i < values.size(); i++) {
        DropPaneConfig dpc = values.get(i);
        PaneConfigurationDialog p = dpc.getDialog();
        NodeSettings n = new NodeSettings("dialogSettings_" + i);
        p.saveSettings(n);
        settings.addNodeSettings(n);
        settings.addString("panelIndex_" + i, dpc.getSelectionAsString());
    // settings.addInt("panelIndex" + i, p.getIndex());
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) DropPaneConfig(org.knime.base.node.preproc.draganddroppanel.droppanes.DropPaneConfig)

Example 50 with NodeSettings

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

the class Joiner2NodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    NodeSettings internalSettings = new NodeSettings("joiner");
    NodeSettingsWO leftMapSet = internalSettings.addNodeSettings("leftHiliteMapping");
    ((DefaultHiLiteMapper) m_leftTranslator.getMapper()).save(leftMapSet);
    NodeSettingsWO rightMapSet = internalSettings.addNodeSettings("rightHiliteMapping");
    ((DefaultHiLiteMapper) m_rightTranslator.getMapper()).save(rightMapSet);
    File f = new File(nodeInternDir, "joinerInternalSettings");
    FileOutputStream out = new FileOutputStream(f);
    internalSettings.saveToXML(out);
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) FileOutputStream(java.io.FileOutputStream) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) 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