Search in sources :

Example 1 with NodeProgressListener

use of org.knime.core.node.workflow.NodeProgressListener in project knime-core by knime.

the class VariableFileReaderNodeDialog method analyzeAction.

/**
 * triggers analysis.
 */
protected void analyzeAction() {
    synchronized (m_analysisRunning) {
        // wait until we have a chance to run the analysis
        while (m_analysisRunning.booleanValue()) {
            getLogger().error("Internal error: Re-entering analysis thread - " + "canceling it - waiting for it to finish...");
            m_analysisExecMonitor.setExecuteInterrupted();
            // wait until it finishes
            try {
                m_analysisRunning.wait();
                getLogger().error("Alright - continuing with new analysis...");
            } catch (InterruptedException ie) {
            // huh?!?
            }
        }
        // Create execution context for progress and cancellations
        // We use our own progress monitor, we need to distinguish
        // between user cancel and code interrupts.
        m_analysisExecMonitor = new FileReaderExecutionMonitor();
        m_analysisExecMonitor.getProgressMonitor().addProgressListener(new NodeProgressListener() {

            @Override
            public void progressChanged(final NodeProgressEvent pEvent) {
                if (pEvent.getNodeProgress().getMessage() != null) {
                    ViewUtils.runOrInvokeLaterInEDT(new Runnable() {

                        @Override
                        public void run() {
                            Double p = pEvent.getNodeProgress().getProgress();
                            if (p == null) {
                                p = new Double(0.0);
                            }
                            m_analyzeProgressMsg.setText(pEvent.getNodeProgress().getMessage());
                            m_analyzeProgressBar.setValue((int) Math.round(100 * p.doubleValue()));
                            getPanel().revalidate();
                            getPanel().repaint();
                        }
                    });
                }
            }
        });
        // the analysis thread, when finished, clears this flag.
        m_analysisRunning.setValue(true);
        // allow for quickies from now on
        m_analyzeCancel.setEnabled(true);
        setPreviewTable(null);
        setErrorLabelText("");
    }
    // clone current settings
    VariableFileReaderNodeSettings newFRSettings = new VariableFileReaderNodeSettings(m_frSettings);
    Vector<ColProperty> oldColProps = m_frSettings.getColumnProperties();
    // prepare the settings object for re-analysis
    newFRSettings.setNumberOfColumns(-1);
    Vector<ColProperty> newProps = new Vector<ColProperty>();
    if (oldColProps != null) {
        for (ColProperty cProp : oldColProps) {
            // take over only the ones modified by the user
            if ((cProp != null) && (cProp.getUserSettings())) {
                newProps.add(cProp);
            } else {
                newProps.add(null);
            }
        }
    }
    newFRSettings.setColumnProperties(newProps);
    analyzeInThread(newFRSettings);
}
Also used : NodeProgressEvent(org.knime.core.node.workflow.NodeProgressEvent) NodeProgressListener(org.knime.core.node.workflow.NodeProgressListener) Vector(java.util.Vector)

Example 2 with NodeProgressListener

use of org.knime.core.node.workflow.NodeProgressListener in project knime-core by knime.

the class DefaultNodeProgressMonitorTest method internalTestManyMessageEvents.

/**
 * A lot of incremental numeric progress updates + many message events
 * Previously, this took significantly longer due to expensive string construction.
 */
private void internalTestManyMessageEvents(final NodeProgressMonitor toMonitor, final NodeProgressMonitor toControl) throws Exception {
    final int parts = 1000000;
    final MutableLong stringComposeCounter = new MutableLong();
    Function<Integer, String> msgFct = (index) -> {
        stringComposeCounter.increment();
        return "Row " + index + " (Row \"" + RowKey.createRowKey((long) index) + "\")";
    };
    final Pointer<NodeProgress> progressPointer = new Pointer<>();
    String lastExpectedMsg = msgFct.apply(parts);
    final Function<NodeProgress, Boolean> isLastEventFunction = p -> p.getMessage().equals(lastExpectedMsg);
    NodeProgressListener l = createListener(progressPointer, isLastEventFunction);
    toMonitor.addProgressListener(l);
    try {
        for (int i = 1; i < parts + 1; i++) {
            final int index = i;
            // if this line is replaced by a direct string composition this takes an order of magnitude longer
            toControl.setProgress(i / (double) parts, () -> msgFct.apply(index));
        }
        synchronized (isLastEventFunction) {
            isLastEventFunction.wait(500);
        }
        assertThat(progressPointer.get().getProgress(), is(closeTo(1.0, PROG_EPSILON)));
        assertThat(progressPointer.get().getMessage(), is(equalTo(lastExpectedMsg)));
        // the lazy string creation should only be called 4 times a second at most,
        // it must be at least two - one for the reference string creation and one during an event
        Assert.assertThat(stringComposeCounter.getValue(), is(allOf(greaterThanOrEqualTo(2L), lessThan(5L))));
    } finally {
        toMonitor.removeProgressListener(l);
    }
}
Also used : Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) RowKey(org.knime.core.data.RowKey) Matchers.allOf(org.hamcrest.Matchers.allOf) Test(org.junit.Test) Function(java.util.function.Function) NodeProgressEvent(org.knime.core.node.workflow.NodeProgressEvent) Assert.assertThat(org.junit.Assert.assertThat) Matchers.closeTo(org.hamcrest.Matchers.closeTo) NodeProgressListener(org.knime.core.node.workflow.NodeProgressListener) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Matchers.is(org.hamcrest.Matchers.is) Assert(org.junit.Assert) SubNodeProgressMonitor(org.knime.core.node.DefaultNodeProgressMonitor.SubNodeProgressMonitor) Pointer(org.knime.core.util.Pointer) NodeProgress(org.knime.core.node.workflow.NodeProgress) MutableLong(org.apache.commons.lang3.mutable.MutableLong) NodeProgressListener(org.knime.core.node.workflow.NodeProgressListener) NodeProgress(org.knime.core.node.workflow.NodeProgress) Pointer(org.knime.core.util.Pointer)

Example 3 with NodeProgressListener

use of org.knime.core.node.workflow.NodeProgressListener in project knime-core by knime.

the class DefaultNodeProgressMonitorTest method createListener.

private static NodeProgressListener createListener(final Pointer<NodeProgress> progressPointer, final Function<NodeProgress, Boolean> notificationFunction) {
    return new NodeProgressListener() {

        @Override
        public void progressChanged(final NodeProgressEvent pe) {
            NodeProgress prog = pe.getNodeProgress();
            progressPointer.set(prog);
            if (notificationFunction.apply(prog)) {
                synchronized (notificationFunction) {
                    notificationFunction.notifyAll();
                }
            }
        }
    };
}
Also used : NodeProgressEvent(org.knime.core.node.workflow.NodeProgressEvent) NodeProgressListener(org.knime.core.node.workflow.NodeProgressListener) NodeProgress(org.knime.core.node.workflow.NodeProgress)

Example 4 with NodeProgressListener

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

use of org.knime.core.node.workflow.NodeProgressListener in project knime-core by knime.

the class FileReaderNodeDialog method analyzeAction.

/**
 * triggers analysis.
 */
protected void analyzeAction() {
    synchronized (m_analysisRunning) {
        // wait until we have a chance to run the analysis
        while (m_analysisRunning.booleanValue()) {
            LOGGER.error("Internal error: Re-entering analysis thread - " + "canceling it - waiting for it to finish...");
            m_analysisExecMonitor.setExecuteInterrupted();
            // wait until it finishes
            try {
                m_analysisRunning.wait();
                LOGGER.error("Alright - continuing with new analysis...");
            } catch (InterruptedException ie) {
            // huh?!?
            }
        }
        // Create execution context for progress and cancellations
        // We use our own progress monitor, we need to distinguish
        // between user cancel and code interrupts.
        m_analysisExecMonitor = new FileReaderExecutionMonitor();
        m_analysisExecMonitor.getProgressMonitor().addProgressListener(new NodeProgressListener() {

            @Override
            public void progressChanged(final NodeProgressEvent pEvent) {
                if (pEvent.getNodeProgress().getMessage() != null) {
                    ViewUtils.runOrInvokeLaterInEDT(new Runnable() {

                        @Override
                        public void run() {
                            Double p = pEvent.getNodeProgress().getProgress();
                            if (p == null) {
                                p = new Double(0.0);
                            }
                            m_analyzeProgressMsg.setText(pEvent.getNodeProgress().getMessage());
                            m_analyzeProgressBar.setValue((int) Math.round(100 * p.doubleValue()));
                            getPanel().revalidate();
                            getPanel().repaint();
                        }
                    });
                }
            }
        });
        // the analysis thread, when finished, clears this flag.
        m_analysisRunning.setValue(true);
        // allow for quickies from now on
        m_analyzeCancel.setEnabled(true);
        m_scanLimitSpinner.setEnabled(true);
        setPreviewTable(null);
        setErrorLabelText("");
    }
    // clone current settings
    FileReaderNodeSettings newFRSettings = new FileReaderNodeSettings(m_frSettings);
    Vector<ColProperty> oldColProps = m_frSettings.getColumnProperties();
    // prepare the settings object for re-analysis
    newFRSettings.setNumberOfColumns(-1);
    Vector<ColProperty> newProps = new Vector<ColProperty>();
    if (oldColProps != null) {
        for (ColProperty cProp : oldColProps) {
            // take over only the ones modified by the user
            if ((cProp != null) && (cProp.getUserSettings())) {
                newProps.add(cProp);
            } else {
                newProps.add(null);
            }
        }
    }
    newFRSettings.setColumnProperties(newProps);
    analyzeInThread(newFRSettings);
}
Also used : NodeProgressEvent(org.knime.core.node.workflow.NodeProgressEvent) NodeProgressListener(org.knime.core.node.workflow.NodeProgressListener) Vector(java.util.Vector)

Aggregations

NodeProgressEvent (org.knime.core.node.workflow.NodeProgressEvent)8 NodeProgressListener (org.knime.core.node.workflow.NodeProgressListener)8 NodeProgress (org.knime.core.node.workflow.NodeProgress)6 Vector (java.util.Vector)2 Function (java.util.function.Function)2 MutableLong (org.apache.commons.lang3.mutable.MutableLong)2 Matchers.allOf (org.hamcrest.Matchers.allOf)2 Matchers.closeTo (org.hamcrest.Matchers.closeTo)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Matchers.greaterThanOrEqualTo (org.hamcrest.Matchers.greaterThanOrEqualTo)2 Matchers.is (org.hamcrest.Matchers.is)2 Matchers.lessThan (org.hamcrest.Matchers.lessThan)2 Assert (org.junit.Assert)2 Assert.assertThat (org.junit.Assert.assertThat)2 Test (org.junit.Test)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 RowKey (org.knime.core.data.RowKey)2 SubNodeProgressMonitor (org.knime.core.node.DefaultNodeProgressMonitor.SubNodeProgressMonitor)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 Pointer (org.knime.core.util.Pointer)2