Search in sources :

Example 1 with EndOfTableException

use of org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException in project knime-core by knime.

the class RowFilterIterator method getNextMatch.

/*
     * returns the next row that is supposed to be returned or null if it met
     * the end of it before.
     */
private DataRow getNextMatch() {
    while (true) {
        try {
            m_exec.checkCanceled();
        } catch (CanceledExecutionException cee) {
            throw new RuntimeCanceledExecutionException(cee);
        }
        // we must not cause any trouble.
        if (!m_orig.hasNext()) {
            return null;
        }
        m_exec.setProgress(m_rowNumber / (double) m_totalCountInOrig);
        DataRow next = m_orig.next();
        if (m_includeRest) {
            m_rowNumber++;
            return next;
        } else {
            // consult the filter whether to include this row
            try {
                if (m_filter.matches(next, m_rowNumber)) {
                    return next;
                }
            // else fall through and get the next row from the orig
            // table.
            } catch (EndOfTableException eote) {
                // filter: there are now more matching rows. Reached our
                // EOT.
                m_nextRow = null;
                return null;
            } catch (IncludeFromNowOn ifno) {
                // filter: include all rows from now on
                m_includeRest = true;
                return next;
            } finally {
                m_rowNumber++;
            }
        }
    }
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IncludeFromNowOn(org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn) EndOfTableException(org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException) DataRow(org.knime.core.data.DataRow)

Example 2 with EndOfTableException

use of org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException in project knime-core by knime.

the class PartitionNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    BufferedDataTable in = inData[0];
    BufferedDataTable[] outs = new BufferedDataTable[2];
    IRowFilter filter = getSamplingRowFilter(in, exec);
    BufferedDataContainer firstOutCont = exec.createDataContainer(in.getDataTableSpec());
    BufferedDataContainer secondOutCont = exec.createDataContainer(in.getDataTableSpec());
    // floating point op. below
    final double rowCount = in.size();
    // one of the flags will be set if one of the exceptions below
    // is thrown.
    boolean putRestInOut1 = false;
    boolean putRestInOut2 = false;
    try {
        int count = 0;
        for (DataRow row : in) {
            boolean matches = putRestInOut1;
            try {
                // conditional check, will call "matches" only if necessary
                matches |= (!putRestInOut2 && filter.matches(row, count));
            } catch (IncludeFromNowOn icf) {
                assert !putRestInOut2;
                putRestInOut1 = true;
                matches = true;
            } catch (EndOfTableException ete) {
                assert !putRestInOut1;
                putRestInOut2 = true;
                matches = false;
            }
            if (matches) {
                firstOutCont.addRowToTable(row);
            } else {
                secondOutCont.addRowToTable(row);
            }
            exec.setProgress(count / rowCount, "Processed row " + count + " (\"" + row.getKey() + "\")");
            exec.checkCanceled();
            count++;
        }
    } finally {
        firstOutCont.close();
        secondOutCont.close();
    }
    outs[0] = firstOutCont.getTable();
    outs[1] = secondOutCont.getTable();
    if (filter instanceof StratifiedSamplingRowFilter) {
        int classCount = ((StratifiedSamplingRowFilter) filter).getClassCount();
        if (classCount > outs[0].size()) {
            setWarningMessage("Class column contains more classes (" + classCount + ") than sampled rows (" + outs[0].size() + ")");
        }
    }
    return outs;
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) BufferedDataTable(org.knime.core.node.BufferedDataTable) IncludeFromNowOn(org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn) EndOfTableException(org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException) IRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter) StratifiedSamplingRowFilter(org.knime.base.node.preproc.sample.StratifiedSamplingRowFilter) DataRow(org.knime.core.data.DataRow)

Example 3 with EndOfTableException

use of org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException in project knime-core by knime.

the class RowFilter2PortNodeModel method execute.

/*
     * The main work is done here
     *
     * @param rows total number of rows. Can be -1 if not available.
     */
private void execute(final RowInput in, final RowOutput match, final RowOutput miss, final long rows, final ExecutionContext exec) throws InterruptedException, CanceledExecutionException {
    try {
        long rowIdx = -1;
        boolean allMatch = false;
        boolean allMiss = false;
        DataRow row;
        while ((row = in.poll()) != null) {
            rowIdx++;
            if (rows > 0) {
                exec.setProgress(rowIdx / (double) rows, "Adding row " + rowIdx + " of " + rows);
            } else {
                exec.setProgress("Adding row " + rowIdx + ".");
            }
            exec.checkCanceled();
            if (allMatch) {
                match.push(row);
                continue;
            }
            if (allMiss) {
                miss.push(row);
                continue;
            }
            try {
                if (m_rowFilter.matches(row, rowIdx)) {
                    match.push(row);
                } else {
                    miss.push(row);
                }
            } catch (EndOfTableException eote) {
                miss.push(row);
                allMiss = true;
            } catch (IncludeFromNowOn ifnoe) {
                match.push(row);
                allMatch = true;
            }
        }
    } finally {
        match.close();
        miss.close();
    }
}
Also used : IncludeFromNowOn(org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn) EndOfTableException(org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException) DataRow(org.knime.core.data.DataRow)

Example 4 with EndOfTableException

use of org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException in project knime-core by knime.

the class RowFilterNodeModel method execute.

private void execute(final RowInput inData, final RowOutput output, final ExecutionContext exec) throws Exception {
    // in case the node was configured and the workflow is closed
    // (and saved), the row filter isn't configured upon reloading.
    // here, we give it a chance to configure itself (e.g. find the column
    // index)
    m_rowFilter.configure(inData.getDataTableSpec());
    exec.setMessage("Searching first matching row...");
    DataRow row;
    int index = 0;
    boolean isAlwaysExcluded = false;
    boolean isAlwaysIncluded = false;
    while ((row = inData.poll()) != null) {
        boolean matches;
        if (isAlwaysExcluded) {
            matches = false;
        } else if (isAlwaysIncluded) {
            matches = true;
        } else {
            try {
                matches = m_rowFilter.matches(row, index++);
            } catch (EndOfTableException eot) {
                break;
            } catch (IncludeFromNowOn ifn) {
                isAlwaysIncluded = true;
                matches = true;
            }
        }
        exec.checkCanceled();
        if (matches) {
            final int indexFinal = index;
            final DataRow rowFinal = row;
            exec.setMessage(() -> "Added row " + indexFinal + " (\"" + rowFinal.getKey() + "\")");
            output.push(row);
        }
    }
    inData.close();
    output.close();
}
Also used : IncludeFromNowOn(org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn) EndOfTableException(org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException) DataRow(org.knime.core.data.DataRow)

Aggregations

EndOfTableException (org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException)4 IncludeFromNowOn (org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn)4 DataRow (org.knime.core.data.DataRow)4 IRowFilter (org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter)1 StratifiedSamplingRowFilter (org.knime.base.node.preproc.sample.StratifiedSamplingRowFilter)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1