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();
}
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;
}
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() };
}
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);
}
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() };
}
Aggregations