use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class RuleEngine2PortsNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
private StreamInternalWithPortObject m_internals;
/**
* {@inheritDoc}
*/
@Override
public void loadInternals(final StreamableOperatorInternals internals) {
m_internals = (StreamInternalWithPortObject) internals;
}
/**
* {@inheritDoc}
*/
@Override
public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
// count number of rows
long count = 0;
final RowInput rowInput = (RowInput) inputs[DATA_PORT];
while (rowInput.poll() != null) {
count++;
}
if (inputs[RULE_PORT] instanceof RowInput) {
final RowInput ruleInput = (RowInput) inputs[RULE_PORT];
final Pair<ColumnRearranger, PortObject> pair = createColumnRearranger(rowInput.getDataTableSpec(), ruleInput);
final ColumnRearranger rearranger = pair.getFirst();
final DataTableSpec spec = rearranger.createSpec();
m_internals.setTableSpec(spec);
if (pair.getSecond() instanceof PMMLPortObject) {
PMMLPortObject po = (PMMLPortObject) pair.getSecond();
m_internals.setObject(po);
} else {
m_internals.setObject(null);
}
}
m_internals.setRowCount(count);
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
if (m_internals.getTableSpec() != null) {
m_rowCount = m_internals.getRowCount();
}
final Pair<ColumnRearranger, PortObject> pair = createColumnRearranger((DataTableSpec) inSpecs[DATA_PORT], (RowInput) inputs[RULE_PORT]);
pair.getFirst().createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
if (pair.getSecond() != null) {
((PortObjectOutput) outputs[1]).setPortObject(pair.getSecond());
}
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class RuleEngineFilter2PortsNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
private SimpleStreamableOperatorInternals m_internals;
/**
* {@inheritDoc}
*/
@Override
public void loadInternals(final StreamableOperatorInternals internals) {
m_internals = (SimpleStreamableOperatorInternals) internals;
}
/**
* {@inheritDoc}
*/
@Override
public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
// count number of rows
long count = 0;
RowInput rowInput = (RowInput) inputs[DATA_PORT];
while (rowInput.poll() != null) {
count++;
}
m_internals.getConfig().addLong(CFG_ROW_COUNT, count);
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
long rowCount = -1L;
if (m_internals.getConfig().containsKey(CFG_ROW_COUNT)) {
rowCount = m_internals.getConfig().getLong(CFG_ROW_COUNT);
}
m_rulesList.clear();
final PortInput rulePort = inputs[RULE_PORT];
if (rulePort instanceof PortObjectInput) {
PortObjectInput poRule = (PortObjectInput) rulePort;
m_rulesList.addAll(RuleEngineVariable2PortsNodeModel.rules((BufferedDataTable) poRule.getPortObject(), m_settings, RuleNodeSettings.RuleFilter));
} else if (rulePort instanceof RowInput) {
RowInput riRule = (RowInput) rulePort;
m_rulesList.addAll(RuleEngineVariable2PortsNodeModel.rules(riRule, m_settings, RuleNodeSettings.RuleFilter));
}
final DataTableSpec spec = (DataTableSpec) inSpecs[DATA_PORT];
try {
parseRules(spec, RuleNodeSettings.RuleSplitter);
} catch (final ParseException e) {
throw new InvalidSettingsException(e);
}
final RowInput inputPartitions = (RowInput) inputs[DATA_PORT];
final List<Rule> rules = parseRules(inputPartitions.getDataTableSpec(), RuleNodeSettings.RuleFilter);
final RowOutput first = (RowOutput) outputs[0];
final int nrOutPorts = getNrOutPorts();
final RowOutput second = nrOutPorts > 1 ? (RowOutput) outputs[1] : new RowOutput() {
@Override
public void push(final DataRow row) throws InterruptedException {
// do nothing
}
@Override
public void close() throws InterruptedException {
// do nothing
}
};
final RowOutput[] containers = new RowOutput[] { first, second };
final int matchIndex = m_includeOnMatch ? 0 : 1;
final int otherIndex = 1 - matchIndex;
try {
final MutableLong rowIdx = new MutableLong(0L);
final long rows = rowCount;
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();
}
};
DataRow row;
while ((row = inputPartitions.poll()) != null) {
rowIdx.increment();
if (rows > 0) {
exec.setProgress(rowIdx.longValue() / (double) rows, () -> "Adding row " + rowIdx.longValue() + " of " + rows);
} else {
exec.setMessage(() -> "Adding row " + rowIdx.longValue() + " of " + rows);
}
exec.checkCanceled();
boolean wasMatch = false;
for (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].push(row);
} else {
containers[matchIndex].push(row);
}
wasMatch = true;
break;
}
}
if (!wasMatch) {
containers[otherIndex].push(row);
}
}
} finally {
try {
second.close();
} finally {
first.close();
}
}
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class TimeMissValueNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
ExecutionContext createExec = exec.createSubExecutionContext(0.4);
DataTableSpec spec = inData[0].getDataTableSpec();
double maxRow = inData[0].getRowCount();
double currRow = 0;
Map<String, TSMissVHandler> nameToMVHandler = checkInputAndCreateHandlerMap(spec);
Map<String, Integer> nameToInt = findColumns(spec, nameToMVHandler);
if (nameToMVHandler.isEmpty()) {
return inData;
}
for (DataRow row : inData[0]) {
RowKey key = row.getKey();
for (String s : nameToInt.keySet()) {
nameToMVHandler.get(s).incomingValue(key, row.getCell(nameToInt.get(s)));
}
createExec.checkCanceled();
createExec.setProgress(++currRow / maxRow, "Preprocessing... Row " + row.getKey().getString());
}
for (String s : nameToMVHandler.keySet()) {
nameToMVHandler.get(s).close();
}
ExecutionContext builtExec = exec.createSubExecutionContext(0.6);
ColumnRearranger colR = createColumnRearranger(nameToMVHandler, nameToInt, inData[0].getDataTableSpec());
BufferedDataTable outTable = exec.createColumnRearrangeTable(inData[0], colR, builtExec);
return new BufferedDataTable[] { outTable };
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class RuleEngineNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DataTableSpec spec = (DataTableSpec) inSpecs[0];
final List<Rule> parsedRules;
try {
parsedRules = parseRules(spec, RuleNodeSettings.RuleEngine);
} catch (final ParseException e) {
throw new InvalidSettingsException(e);
}
return new StreamableOperator() {
private SimpleStreamableOperatorInternals m_internals;
/**
* {@inheritDoc}
*/
@Override
public void loadInternals(final StreamableOperatorInternals internals) {
m_internals = (SimpleStreamableOperatorInternals) internals;
}
/**
* {@inheritDoc}
*/
@Override
public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
// count number of rows
long count = 0;
RowInput rowInput = (RowInput) inputs[0];
while (rowInput.poll() != null) {
count++;
}
m_internals.getConfig().addLong(CFG_ROW_COUNT, count);
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
long rowCount = -1L;
if (m_internals.getConfig().containsKey(CFG_ROW_COUNT)) {
rowCount = m_internals.getConfig().getLong(CFG_ROW_COUNT);
}
createRearranger(((RowInput) inputs[0]).getDataTableSpec(), parsedRules, rowCount, false).createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class PMMLRuleEditorNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
return new StreamableOperator() {
private ColumnRearranger m_rearrangerx;
private PMMLPortObject m_portObject;
{
try {
final PMMLDocument doc = PMMLDocument.Factory.newInstance();
final PMML pmml = doc.addNewPMML();
RuleSetModel ruleSetModel = pmml.addNewRuleSetModel();
RuleSet ruleSet = ruleSetModel.addNewRuleSet();
PMMLRuleParser parser = new PMMLRuleParser(tableSpec, getAvailableInputFlowVariables());
m_rearrangerx = createRearranger(tableSpec, ruleSet, parser);
} catch (ParseException e) {
throw new InvalidSettingsException(e);
}
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
m_rearrangerx.createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
}
/**
* {@inheritDoc}
*/
@Override
public void loadInternals(final StreamableOperatorInternals internals) {
super.loadInternals(internals);
m_portObject = ((StreamInternalForPMMLPortObject) internals).getObject();
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return createInitialStreamableOperatorInternals().setObject(m_portObject);
}
};
}
Aggregations