Search in sources :

Example 6 with SettingsStatus

use of org.knime.core.util.tokenizer.SettingsStatus in project knime-core by knime.

the class VariableFileReaderNodeModel method readSettingsFromConfiguration.

/*
     * validates the settings object, or reads its settings from it. Depending
     * on the specified value of the 'validateOnly' parameter.
     */
private void readSettingsFromConfiguration(final NodeSettingsRO settings, final boolean validateOnly) throws InvalidSettingsException {
    if (settings == null) {
        throw new NullPointerException("Can't read filereader node settings" + " from null config object");
    }
    // will puke and die if config is not readable.
    VariableFileReaderNodeSettings newSettings = new VariableFileReaderNodeSettings(settings);
    // check consistency of settings.
    SettingsStatus status = newSettings.getStatusOfSettings();
    if (status.getNumOfErrors() > 0) {
        throw new InvalidSettingsException(status.getAllErrorMessages(0));
    }
    if (!validateOnly) {
        // everything looks good - take over the new settings.
        m_frSettings = newSettings;
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SettingsStatus(org.knime.core.util.tokenizer.SettingsStatus)

Example 7 with SettingsStatus

use of org.knime.core.util.tokenizer.SettingsStatus in project knime-core by knime.

the class CSVReaderNodeModel method createFileTable.

protected FileTable createFileTable(final ExecutionContext exec) throws Exception {
    // prepare the settings for the file analyzer
    FileReaderNodeSettings settings = new FileReaderNodeSettings();
    CheckUtils.checkSourceFile(m_config.getLocation());
    URL url = FileUtil.toURL(m_config.getLocation());
    settings.setDataFileLocationAndUpdateTableName(url);
    String colDel = m_config.getColDelimiter();
    if (colDel != null && !colDel.isEmpty()) {
        settings.addDelimiterPattern(colDel, false, false, false);
    }
    settings.setDelimiterUserSet(true);
    String rowDel = m_config.getRowDelimiter();
    if (rowDel != null && !rowDel.isEmpty()) {
        settings.addRowDelimiter(rowDel, true);
    }
    String quote = m_config.getQuoteString();
    if (quote != null && !quote.isEmpty()) {
        settings.addQuotePattern(quote, quote);
    }
    settings.setQuoteUserSet(true);
    String commentStart = m_config.getCommentStart();
    if (commentStart != null && !commentStart.isEmpty()) {
        settings.addSingleLineCommentPattern(commentStart, false, false);
    }
    settings.setCommentUserSet(true);
    boolean hasColHeader = m_config.hasColHeader();
    settings.setFileHasColumnHeaders(hasColHeader);
    settings.setFileHasColumnHeadersUserSet(true);
    boolean hasRowHeader = m_config.hasRowHeader();
    settings.setFileHasRowHeaders(hasRowHeader);
    settings.setFileHasRowHeadersUserSet(true);
    settings.setWhiteSpaceUserSet(true);
    boolean supportShortLines = m_config.isSupportShortLines();
    settings.setSupportShortLines(supportShortLines);
    int skipFirstLinesCount = m_config.getSkipFirstLinesCount();
    settings.setSkipFirstLines(skipFirstLinesCount);
    final long limitRowsCount = m_config.getLimitRowsCount();
    settings.setMaximumNumberOfRowsToRead(limitRowsCount);
    settings.setCharsetName(m_config.getCharSetName());
    settings.setCharsetUserSet(true);
    settings.setConnectTimeout(m_config.getConnectTimeout());
    final int limitAnalysisCount = m_config.getLimitAnalysisCount();
    final ExecutionMonitor analyseExec = exec.createSubProgress(0.5);
    final ExecutionContext readExec = exec.createSubExecutionContext(0.5);
    exec.setMessage("Analyzing file");
    if (limitAnalysisCount >= 0) {
        final FileReaderExecutionMonitor fileReaderExec = new FileReaderExecutionMonitor();
        fileReaderExec.getProgressMonitor().addProgressListener(new NodeProgressListener() {

            @Override
            public void progressChanged(final NodeProgressEvent pe) {
                try {
                    // if the node was canceled, cancel (interrupt) the analysis
                    analyseExec.checkCanceled();
                    // otherwise update the node progress
                    NodeProgress nodeProgress = pe.getNodeProgress();
                    analyseExec.setProgress(nodeProgress.getProgress(), nodeProgress.getMessage());
                } catch (CanceledExecutionException e) {
                    fileReaderExec.setExecuteInterrupted();
                }
            }
        });
        fileReaderExec.setShortCutLines(limitAnalysisCount);
        fileReaderExec.setExecuteCanceled();
        settings = FileAnalyzer.analyze(settings, fileReaderExec);
    } else {
        settings = FileAnalyzer.analyze(settings, analyseExec);
    }
    SettingsStatus status = settings.getStatusOfSettings();
    if (status.getNumOfErrors() > 0) {
        throw new IllegalStateException(status.getErrorMessage(0));
    }
    final DataTableSpec tableSpec = settings.createDataTableSpec();
    if (tableSpec == null) {
        final SettingsStatus status2 = settings.getStatusOfSettings(true, null);
        if (status2.getNumOfErrors() > 0) {
            throw new IllegalStateException(status2.getErrorMessage(0));
        } else {
            throw new IllegalStateException("Unknown error during file analysis.");
        }
    }
    exec.setMessage("Buffering file");
    return new FileTable(tableSpec, settings, readExec);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) NodeProgressListener(org.knime.core.node.workflow.NodeProgressListener) NodeProgress(org.knime.core.node.workflow.NodeProgress) FileTable(org.knime.base.node.io.filereader.FileTable) SettingsStatus(org.knime.core.util.tokenizer.SettingsStatus) URL(java.net.URL) FileReaderNodeSettings(org.knime.base.node.io.filereader.FileReaderNodeSettings) ExecutionContext(org.knime.core.node.ExecutionContext) NodeProgressEvent(org.knime.core.node.workflow.NodeProgressEvent) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) FileReaderExecutionMonitor(org.knime.base.node.io.filereader.FileReaderExecutionMonitor) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) FileReaderExecutionMonitor(org.knime.base.node.io.filereader.FileReaderExecutionMonitor)

Example 8 with SettingsStatus

use of org.knime.core.util.tokenizer.SettingsStatus in project knime-core by knime.

the class FileReaderNodeDialog method updatePreview.

/*
     * from the current settings it creates a data table and displays it in the
     * preview pane. Will not analyze the file. Will not change things.
     */
private void updatePreview() {
    // the settings changed - clear error message first
    setErrorLabelText("");
    // update preview
    if (m_frSettings.getDataFileLocation() == null) {
        // if there is no data file specified display empty table
        setPreviewTable(null);
        showPreviewTable();
        return;
    }
    FileReaderNodeSettings previewSettings = createPreviewSettings(m_frSettings);
    SettingsStatus status = previewSettings.getStatusOfSettings(true, null);
    if (status.getNumOfErrors() > 0) {
        setErrorLabelText(status.getErrorMessage(0));
        setPreviewTable(null);
        showPreviewTable();
        return;
    }
    DataTableSpec tSpec = previewSettings.createDataTableSpec();
    FileReaderPreviewTable newTable = new FileReaderPreviewTable(tSpec, previewSettings, null);
    setPreviewTable(newTable);
    showPreviewTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) SettingsStatus(org.knime.core.util.tokenizer.SettingsStatus)

Example 9 with SettingsStatus

use of org.knime.core.util.tokenizer.SettingsStatus in project knime-core by knime.

the class FileReaderNodeModel method readSettingsFromConfiguration.

/*
     * validates the settings object, or reads its settings from it. Depending
     * on the specified value of the 'validateOnly' parameter.
     */
private void readSettingsFromConfiguration(final NodeSettingsRO settings, final boolean validateOnly) throws InvalidSettingsException {
    if (settings == null) {
        throw new NullPointerException("Can't read filereader node settings" + " from null config object");
    }
    // will puke and die if config is not readable.
    FileReaderNodeSettings newSettings = new FileReaderNodeSettings(settings);
    // check consistency of settings.
    SettingsStatus status = newSettings.getStatusOfSettings();
    if (status.getNumOfErrors() > 0) {
        throw new InvalidSettingsException(status.getAllErrorMessages(0));
    }
    if (!validateOnly) {
        // everything looks good - take over the new settings.
        m_frSettings = newSettings;
        // save the filename to our filehistory
        StringHistory h = StringHistory.getInstance(FILEREADER_HISTORY_ID);
        h.add(m_frSettings.getDataFileLocation().toString());
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringHistory(org.knime.core.node.util.StringHistory) SettingsStatus(org.knime.core.util.tokenizer.SettingsStatus)

Example 10 with SettingsStatus

use of org.knime.core.util.tokenizer.SettingsStatus in project knime-core by knime.

the class FileReaderNodeModel method createStreamableOperator.

@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            assert inputs.length == 0;
            LOGGER.info("Preparing to read from '" + m_frSettings.getDataFileLocation().toString() + "'.");
            // check again the settings - especially file existence (under Linux
            // files could be deleted/renamed since last config-call...
            SettingsStatus status = m_frSettings.getStatusOfSettings(true, null);
            if (status.getNumOfErrors() > 0) {
                throw new InvalidSettingsException(status.getAllErrorMessages(10));
            }
            DataTableSpec tSpec = m_frSettings.createDataTableSpec();
            FileTable fTable = new FileTable(tSpec, m_frSettings, m_frSettings.getSkippedColumns(), exec);
            // data output port
            RowOutput rowOutput = (RowOutput) outputs[0];
            int row = 0;
            FileRowIterator it = fTable.iterator();
            try {
                if (it.getZipEntryName() != null) {
                    // seems we are reading a ZIP archive.
                    LOGGER.info("Reading entry '" + it.getZipEntryName() + "' from the specified ZIP archive.");
                }
                while (it.hasNext()) {
                    row++;
                    DataRow next = it.next();
                    final int finalRow = row;
                    exec.setMessage(() -> "Reading row #" + finalRow + " (\"" + next.getKey() + "\")");
                    exec.checkCanceled();
                    rowOutput.push(next);
                }
                rowOutput.close();
                if (it.zippedSourceHasMoreEntries()) {
                    // after reading til the end of the file this returns a valid
                    // result
                    setWarningMessage("Source is a ZIP archive with multiple " + "entries. Only reading first entry!");
                }
            } catch (DuplicateKeyException dke) {
                String msg = dke.getMessage();
                if (msg == null) {
                    msg = "Duplicate row IDs";
                }
                msg += ". Consider making IDs unique in the advanced settings.";
                DuplicateKeyException newDKE = new DuplicateKeyException(msg);
                newDKE.initCause(dke);
                throw newDKE;
            }
            // user settings allow for truncating the table
            if (it.iteratorEndedEarly()) {
                setWarningMessage("Data was truncated due to user settings.");
            }
            // closes all sources.
            fTable.dispose();
        }
    };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput) RowOutput(org.knime.core.node.streamable.RowOutput) ExecutionContext(org.knime.core.node.ExecutionContext) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) SettingsStatus(org.knime.core.util.tokenizer.SettingsStatus) DataRow(org.knime.core.data.DataRow) DuplicateKeyException(org.knime.core.util.DuplicateKeyException)

Aggregations

SettingsStatus (org.knime.core.util.tokenizer.SettingsStatus)11 DataTableSpec (org.knime.core.data.DataTableSpec)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)5 DataRow (org.knime.core.data.DataRow)2 ExecutionContext (org.knime.core.node.ExecutionContext)2 DuplicateKeyException (org.knime.core.util.DuplicateKeyException)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Vector (java.util.Vector)1 FileReaderExecutionMonitor (org.knime.base.node.io.filereader.FileReaderExecutionMonitor)1 FileReaderNodeSettings (org.knime.base.node.io.filereader.FileReaderNodeSettings)1 FileTable (org.knime.base.node.io.filereader.FileTable)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)1 NotConfigurableException (org.knime.core.node.NotConfigurableException)1 BufferedDataTableRowOutput (org.knime.core.node.streamable.BufferedDataTableRowOutput)1