Search in sources :

Example 1 with BufferedDataContainer

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

the class RuleSetToTable method execute.

/**
 * Performs the conversion.
 *
 * @param exec An {@link ExecutionContext}.
 * @param pmmlPo The input {@link PMMLPortObject}.
 * @return The created {@link BufferedDataTable}.
 * @throws CanceledExecutionException Execition was cancelled.
 * @throws InvalidSettingsException No or more than one RuleSet model is in the PMML input.
 */
public BufferedDataTable execute(final ExecutionContext exec, final PMMLPortObject pmmlPo) throws CanceledExecutionException, InvalidSettingsException {
    // TODO should the rule selection method be an output flow variable?
    if (pmmlPo.getPMMLValue().getModels(PMMLModelType.RuleSetModel).size() != 1) {
        throw new InvalidSettingsException("Only a single RuleSet model is supported.");
    }
    PMMLRuleTranslator ruleTranslator = new PMMLRuleTranslator();
    pmmlPo.initializeModelTranslator(ruleTranslator);
    List<Rule> rules = ruleTranslator.getRules();
    final DataTableSpec confSpec = configure(pmmlPo.getSpec());
    final List<String> scoreValues = new ArrayList<>();
    final DataTableSpec properSpec = confSpec != null ? confSpec : properSpec(rules, scoreValues);
    BufferedDataContainer container = exec.createDataContainer(properSpec);
    List<DataColumnSpec> targetCols = pmmlPo.getSpec().getTargetCols();
    DataType outcomeType = targetCols.get(0).getType();
    long idx = 0L;
    int rulesSize = rules.size();
    Map<String, DataType> types = new LinkedHashMap<>();
    for (DataColumnSpec col : pmmlPo.getSpec().getLearningCols()) {
        types.put(col.getName(), col.getType());
    }
    for (Rule rule : rules) {
        exec.checkCanceled();
        exec.setProgress(1.0 * idx++ / rulesSize);
        container.addRowToTable(new DefaultRow(RowKey.createRowKey(idx), createRow(rule, outcomeType, types, scoreValues)));
    }
    container.close();
    return container.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ArrayList(java.util.ArrayList) PMMLRuleTranslator(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) Rule(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator.Rule) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 2 with BufferedDataContainer

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

the class RuleEngineFilter2PortsNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable ruleTable = inData[RuleEngine2PortsNodeModel.RULE_PORT];
    BufferedDataTable dataTable = inData[RuleEngine2PortsNodeModel.DATA_PORT];
    // m_rulesList.clear();
    // m_rulesList.addAll(RuleEngineVariable2PortsNodeModel.rules(ruleTable, m_settings, RuleNodeSettings.RuleFilter));
    // final List<Rule> rules = parseRules(dataTable.getDataTableSpec(), RuleNodeSettings.RuleFilter);
    final BufferedDataContainer first = exec.createDataContainer(dataTable.getDataTableSpec(), true);
    final int nrOutPorts = getNrOutPorts();
    final RowAppender second = nrOutPorts > 1 ? exec.createDataContainer(dataTable.getDataTableSpec(), true) : new RowAppender() {

        @Override
        public void addRowToTable(final DataRow row) {
        // do nothing
        }
    };
    // final RowAppender[] containers = new RowAppender[]{first, second};
    // final int matchIndex = m_includeOnMatch ? 0 : 1;
    // final int otherIndex = 1 - matchIndex;
    // 
    final BufferedDataTable[] ret = new BufferedDataTable[nrOutPorts];
    // try {
    // final MutableLong rowIdx = new MutableLong();
    // final long rows = inData[DATA_PORT].size();
    // final VariableProvider provider = new VariableProvider() {
    // @Override
    // public Object readVariable(final String name, final Class<?> type) {
    // return RuleEngineFilter2PortsNodeModel.this.readVariable(name, type);
    // }
    // 
    // @Override
    // @Deprecated
    // public int getRowCount() {
    // throw new UnsupportedOperationException();
    // }
    // 
    // @Override
    // public long getRowCountLong() {
    // return rows;
    // }
    // 
    // @Override
    // @Deprecated
    // public int getRowIndex() {
    // throw new UnsupportedOperationException();
    // }
    // 
    // @Override
    // public long getRowIndexLong() {
    // return rowIdx.longValue();
    // }
    // };
    // for (DataRow row : inData[DATA_PORT]) {
    // rowIdx.increment();
    // exec.setProgress(rowIdx.longValue() / (double)rows, "Adding row " + rowIdx.longValue() + " of " + rows);
    // exec.checkCanceled();
    // boolean wasMatch = false;
    // for (final Rule r : rules) {
    // if (r.getCondition().matches(row, provider).getOutcome() == MatchState.matchedAndStop) {
    // //                        r.getSideEffect().perform(row, provider);
    // DataValue value = r.getOutcome().getComputedResult(row, provider);
    // if (value instanceof BooleanValue) {
    // final BooleanValue bv = (BooleanValue)value;
    // containers[bv.getBooleanValue() ? matchIndex : otherIndex].addRowToTable(row);
    // } else {
    // containers[matchIndex].addRowToTable(row);
    // }
    // wasMatch = true;
    // break;
    // }
    // }
    // if (!wasMatch) {
    // containers[otherIndex].addRowToTable(row);
    // }
    // }
    // } finally {
    // first.close();
    // ret[0] = first.getTable();
    // if (second instanceof BufferedDataContainer) {
    // BufferedDataContainer container = (BufferedDataContainer)second;
    // container.close();
    // ret[1] = container.getTable();
    // }
    // }
    final PortOutput[] outputs = new PortOutput[] { new BufferedDataTableRowOutput(first), new RowAppenderRowOutput(second) };
    final StreamableOperator streamableOperator = createStreamableOperator(new PartitionInfo(0, 1), new DataTableSpec[] { inData[0].getSpec(), inData[1].getSpec() });
    final PortInput[] inputs = new PortInput[] { new DataTableRowInput(dataTable), new DataTableRowInput(ruleTable) };
    final SimpleStreamableOperatorInternals internals = new SimpleStreamableOperatorInternals();
    internals.getConfig().addLong(CFG_ROW_COUNT, dataTable.size());
    streamableOperator.loadInternals(internals);
    streamableOperator.runFinal(inputs, outputs, exec);
    ret[0] = first.getTable();
    if (ret.length > 1) {
        ret[1] = ((BufferedDataContainer) second).getTable();
    }
    return ret;
}
Also used : SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) PortOutput(org.knime.core.node.streamable.PortOutput) DataRow(org.knime.core.data.DataRow) RowAppender(org.knime.core.data.container.RowAppender) BufferedDataTable(org.knime.core.node.BufferedDataTable) RowAppenderRowOutput(org.knime.base.node.rules.engine.RowAppenderRowOutput) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) PartitionInfo(org.knime.core.node.streamable.PartitionInfo) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput) PortInput(org.knime.core.node.streamable.PortInput)

Example 3 with BufferedDataContainer

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

the class ExtractTimeWindowNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    boolean useTime = m_fromDate.useTime() || m_toDate.useTime();
    BufferedDataTable in = inData[0];
    DataTableSpec outs = in.getDataTableSpec();
    final int colIndex = outs.findColumnIndex(m_columnName.getStringValue());
    BufferedDataContainer t = exec.createDataContainer(outs);
    final long totalRowCount = in.size();
    int currentIteration = 0;
    try {
        for (DataRow r : in) {
            // increment before printing to achieve a 1-based index
            currentIteration++;
            exec.checkCanceled();
            exec.setProgress(currentIteration / (double) totalRowCount, "Processing row " + currentIteration);
            DataCell cell = r.getCell(colIndex);
            if (cell.isMissing()) {
                // do not include missing values -> skip it
                continue;
            }
            Calendar time = ((DateAndTimeValue) cell).getUTCCalendarClone();
            // which is implemented as a real < or >
            if (!useTime) {
                DateAndTimeCell.resetTimeFields(time);
            }
            if (time.compareTo(m_fromDate.getCalendar()) >= 0 && time.compareTo(m_toDate.getCalendar()) <= 0) {
                t.addRowToTable(r);
            }
        }
    } finally {
        t.close();
    }
    return new BufferedDataTable[] { t.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 4 with BufferedDataContainer

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

the class MovingAggregationTableFactory method createTable.

/**
 * @param exec {@link ExecutionContext} to provide progress
 * @param table the {@link BufferedDataTable} to process
 * @return the result table
 * @throws CanceledExecutionException if the user has canceled the operation
 */
public BufferedDataTable createTable(final ExecutionContext exec, final BufferedDataTable table) throws CanceledExecutionException {
    final DataTableSpec resultSpec = createResultSpec();
    final BufferedDataContainer dc = exec.createDataContainer(resultSpec);
    final int rowCount = table.getRowCount();
    if (rowCount == 0) {
        dc.close();
        return dc.getTable();
    }
    if (m_cumulativeComp) {
        return getCumulativeTable(exec, table, dc);
    }
    switch(m_type) {
        case BACKWARD:
            return getBackwardTable(exec, table, dc);
        case CENTER:
            return getCenterTable(exec, table, dc);
        case FORWARD:
            return getForwardTable(exec, table, dc);
    }
    throw new RuntimeException("Unknown window type " + m_type);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer)

Example 5 with BufferedDataContainer

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

the class RuleEngineFilterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    RowInput input = new DataTableRowInput(inData[0]);
    final BufferedDataContainer first = exec.createDataContainer(inData[0].getDataTableSpec(), true);
    final int nrOutPorts = getNrOutPorts();
    final BufferedDataContainer second = exec.createDataContainer(inData[0].getDataTableSpec(), true);
    BufferedDataTableRowOutput[] outputs = new BufferedDataTableRowOutput[] { new BufferedDataTableRowOutput(first), new BufferedDataTableRowOutput(second) };
    execute(input, outputs, inData[0].size(), exec);
    return nrOutPorts == 2 ? new BufferedDataTable[] { outputs[0].getDataTable(), outputs[1].getDataTable() } : new BufferedDataTable[] { outputs[0].getDataTable() };
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput)

Aggregations

BufferedDataContainer (org.knime.core.node.BufferedDataContainer)157 BufferedDataTable (org.knime.core.node.BufferedDataTable)96 DefaultRow (org.knime.core.data.def.DefaultRow)93 DataCell (org.knime.core.data.DataCell)88 DataTableSpec (org.knime.core.data.DataTableSpec)88 DataRow (org.knime.core.data.DataRow)80 RowKey (org.knime.core.data.RowKey)38 DoubleCell (org.knime.core.data.def.DoubleCell)37 StringCell (org.knime.core.data.def.StringCell)26 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)24 ArrayList (java.util.ArrayList)23 DataColumnSpec (org.knime.core.data.DataColumnSpec)21 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)21 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)17 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)16 IOException (java.io.IOException)15 ExecutionContext (org.knime.core.node.ExecutionContext)15 LinkedHashMap (java.util.LinkedHashMap)14 HashSet (java.util.HashSet)13 IntCell (org.knime.core.data.def.IntCell)13