Search in sources :

Example 11 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class MovingAggregationTableFactory method getBackwardTable.

private BufferedDataTable getBackwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
    final int rowCount = table.getRowCount();
    final LinkedList<DataRow> window = new LinkedList<>();
    int rowIdx = 0;
    for (final DataRow row : table) {
        exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
        exec.checkCanceled();
        window.add(row);
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        int idx = 0;
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = row.getCell(colIdx);
        }
        final boolean windowFull = window.size() >= m_windowLength;
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (windowFull || m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        if (windowFull) {
            // remove the first row only when the window is full
            // not during the missing value handling phase!
            window.removeFirst();
        }
        dc.addRowToTable(new DefaultRow(row.getKey(), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : AggregationOperator(org.knime.base.data.aggregation.AggregationOperator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList)

Example 12 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class MovingAggregationTableFactory method getCenterTable.

private BufferedDataTable getCenterTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
    final int rowCount = table.getRowCount();
    final LinkedList<DataRow> window = new LinkedList<>();
    int rowIdx = 0;
    int centerIdx = -1;
    for (final DataRow row : table) {
        exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
        exec.checkCanceled();
        window.add(row);
        // we have to subtract 1 since the indexing of the array starts with 0
        centerIdx = window.size() - m_windowLength / 2 - 1;
        if (centerIdx < 0) {
            // we have to fill the window first
            continue;
        }
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        final DataRow centerRow = window.get(centerIdx);
        int idx = 0;
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = centerRow.getCell(colIdx);
        }
        final boolean windowFull = window.size() >= m_windowLength;
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (windowFull || m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
        // not during the missing value handling phase!
        if (windowFull) {
            window.removeFirst();
        }
    }
    // we have to handle the remaining rows in the window
    while (window.size() > centerIdx) {
        exec.checkCanceled();
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        int idx = 0;
        final DataRow centerRow = window.get(centerIdx);
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = centerRow.getCell(colIdx);
        }
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        window.removeFirst();
        dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : AggregationOperator(org.knime.base.data.aggregation.AggregationOperator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList)

Example 13 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class MovingAggregationTableFactory method getForwardTable.

private BufferedDataTable getForwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
    final int rowCount = table.getRowCount();
    final LinkedList<DataRow> window = new LinkedList<>();
    int rowIdx = 0;
    for (final DataRow row : table) {
        exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
        exec.checkCanceled();
        window.add(row);
        final boolean windowFull = window.size() >= m_windowLength;
        if (!windowFull) {
            // we have to fill the window first
            continue;
        }
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        final DataRow firstRow = window.getFirst();
        int idx = 0;
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = firstRow.getCell(colIdx);
        }
        for (int i = 0, length = m_ops.length; i < length; i++) {
            final int colIdx = m_aggrColIdxs[i];
            final AggregationOperator op = m_ops[i];
            for (final DataRow windowRow : window) {
                op.compute(windowRow, colIdx);
            }
            cells[idx++] = op.getResult();
            op.reset();
        }
        dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
        // remove the first row only when the window is full
        // not during the missing value handling phase!
        window.removeFirst();
    }
    // we have to handle the remaining rows in the window
    while (!window.isEmpty()) {
        exec.checkCanceled();
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        int idx = 0;
        final DataRow firstRow = window.getFirst();
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = firstRow.getCell(colIdx);
        }
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        window.removeFirst();
        dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : AggregationOperator(org.knime.base.data.aggregation.AggregationOperator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList)

Example 14 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class BaseRuleParser method parseColumnExpression.

/**
 * Creates an {@link Expression} to compute a column.
 *
 * @param state The {@link ParseState}.
 * @param fromMissing This {@link Expression} is created within a {@link Operators#MISSING}.
 * @return The {@link Expression} parsed representing a column.
 * @throws ParseException Problem during parsing.
 */
private Expression parseColumnExpression(final ParseState state, final boolean fromMissing) throws ParseException {
    state.skipWS();
    int startPos = state.getPosition();
    if (state.isColumnRef()) {
        String columnRef = state.readColumnRef();
        if (!m_checkColumns) {
            // Create a dummy expression
            return new Expression() {

                @Override
                public boolean isConstant() {
                    return false;
                }

                @Override
                public ASTType getTreeType() {
                    return ASTType.ColRef;
                }

                @Override
                public DataType getOutputType() {
                    return DataType.getMissingCell().getType();
                }

                @Override
                public List<DataType> getInputArgs() {
                    return Collections.emptyList();
                }

                @Override
                public List<Expression> getChildren() {
                    return Collections.emptyList();
                }

                @Override
                public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
                    throw new IllegalStateException("This expression is only for configuration.");
                }
            };
        }
        if (m_spec == null) {
            throw new ParseException("No columns present.", startPos);
        }
        try {
            Expression expr = m_factoryRef.columnRef(m_spec, columnRef);
            if (BooleanCell.TYPE.isASuperTypeOf(expr.getOutputType()) && fromMissing) {
                expr = m_factoryRef.columnRefForMissing(m_spec, columnRef);
            }
            return expr;
        } catch (IllegalStateException e) {
            throw new ParseException(e.getMessage(), startPos);
        }
    }
    throw new ParseException("Expected a column reference", state.getPosition());
}
Also used : DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) DataRow(org.knime.core.data.DataRow)

Example 15 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class BaseRuleParser method parseFlowVariableExpression.

/**
 * Creates an {@link Expression} to compute a {@link FlowVariable}.
 *
 * @param state The {@link ParseState}.
 * @return The {@link Expression} parsed representing a {@link FlowVariable}.
 * @throws ParseException Problem during parsing.
 */
private Expression parseFlowVariableExpression(final ParseState state) throws ParseException {
    state.skipWS();
    int startPos = state.getPosition();
    if (state.isFlowVariableRef()) {
        String flowVarRef = state.readFlowVariable();
        if (!m_checkFlowVars) {
            // Create a dummy expression
            return new Expression() {

                @Override
                public boolean isConstant() {
                    return false;
                }

                @Override
                public ASTType getTreeType() {
                    return ASTType.FlowVarRef;
                }

                @Override
                public DataType getOutputType() {
                    return DataType.getMissingCell().getType();
                }

                @Override
                public List<DataType> getInputArgs() {
                    return Collections.emptyList();
                }

                @Override
                public List<Expression> getChildren() {
                    return Collections.emptyList();
                }

                @Override
                public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
                    throw new IllegalStateException("This expression is only for configuration.");
                }
            };
        }
        try {
            return m_factoryRef.flowVarRef(m_flowVariables, flowVarRef);
        } catch (IllegalStateException e) {
            throw new ParseException(e.getMessage(), startPos);
        }
    }
    throw new ParseException("Expected a flow variable reference", state.getPosition());
}
Also used : DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) DataRow(org.knime.core.data.DataRow)

Aggregations

DataRow (org.knime.core.data.DataRow)482 DataCell (org.knime.core.data.DataCell)268 DataTableSpec (org.knime.core.data.DataTableSpec)159 BufferedDataTable (org.knime.core.node.BufferedDataTable)125 DataColumnSpec (org.knime.core.data.DataColumnSpec)109 RowKey (org.knime.core.data.RowKey)88 DefaultRow (org.knime.core.data.def.DefaultRow)88 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)80 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)76 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)73 DoubleValue (org.knime.core.data.DoubleValue)72 ArrayList (java.util.ArrayList)65 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)65 RowIterator (org.knime.core.data.RowIterator)62 DataType (org.knime.core.data.DataType)61 DoubleCell (org.knime.core.data.def.DoubleCell)57 StringCell (org.knime.core.data.def.StringCell)53 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)48 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)44 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)43