use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.
the class OldToNewTimeNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
SimpleStreamableOperatorInternals m_internals = new SimpleStreamableOperatorInternals();
/**
* {@inheritDoc}
*/
@Override
public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
if (partitionInfo.getPartitionIndex() == 0) {
final RowInput rowInput = (RowInput) inputs[0];
final DataRow row = rowInput.poll();
if (row != null) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells()];
final DataTableSpec inSpec = rowInput.getDataTableSpec();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
for (int i = 0; i < inSpec.getNumColumns(); i++) {
final int searchIdx = Arrays.binarySearch(includeIndexes, i);
if (searchIdx < 0) {
colSpecs[i] = inSpec.getColumnSpec(i);
} else {
colSpecs[i] = newColumnSpecs[searchIdx];
}
}
final Config config = m_internals.getConfig();
config.addBoolean("hasIterated", false);
for (int i = 0; i < inSpec.getNumColumns(); i++) {
config.addDataType("type" + i, colSpecs[i].getType());
config.addString("colname" + i, colSpecs[i].getName());
}
config.addInt("sizeRow", colSpecs.length);
} else {
final DataTableSpec inSpec = rowInput.getDataTableSpec();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells() + includeIndexes.length];
for (int i = 0; i < inSpec.getNumColumns(); i++) {
colSpecs[i] = inSpec.getColumnSpec(i);
}
for (int i = 0; i < newColumnSpecs.length; i++) {
colSpecs[i + inSpec.getNumColumns()] = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
}
final Config config = m_internals.getConfig();
config.addBoolean("hasIterated", false);
for (int i = 0; i < colSpecs.length; i++) {
config.addDataType("type" + i, colSpecs[i].getType());
config.addString("colname" + i, colSpecs[i].getName());
}
config.addInt("sizeRow", colSpecs.length);
}
} else {
m_internals.getConfig().addInt("sizeRow", 0);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final RowInput in = (RowInput) inputs[0];
final RowOutput out = (RowOutput) outputs[0];
final DataTableSpec inSpec = in.getDataTableSpec();
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
DataRow row;
while ((row = in.poll()) != null) {
exec.checkCanceled();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
DataCell[] datacells = new DataCell[includeIndexes.length];
for (int i = 0; i < includeIndexes.length; i++) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(newColumnSpecs[i], i, includeIndexes[i]);
datacells[i] = cellFac.getCells(row)[0];
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(dataColSpec, i, includeIndexes[i]);
datacells[i] = cellFac.getCells(row)[0];
}
}
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
out.push(new ReplacedColumnsDataRow(row, datacells, includeIndexes));
} else {
out.push(new AppendedColumnRow(row, datacells));
}
}
in.close();
out.close();
}
};
}
use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.
the class RuleEngineFilterNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DataTableSpec spec = (DataTableSpec) inSpecs[0];
try {
parseRules(spec, RuleNodeSettings.RuleFilter);
} 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;
if (inputs[0] instanceof RowInput) {
final RowInput rowInput = (RowInput) inputs[0];
while (rowInput.poll() != null) {
count++;
}
} else if (inputs[0] instanceof PortObjectInput) {
final PortObjectInput portObjectInput = (PortObjectInput) inputs[0];
count += ((BufferedDataTable) portObjectInput.getPortObject()).size();
}
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);
}
RowOutput[] rowOutputs = (outputs instanceof RowOutput[]) ? (RowOutput[]) outputs : outputs.length > 1 ? new RowOutput[] { (RowOutput) outputs[0], (RowOutput) outputs[1] } : new RowOutput[] { (RowOutput) outputs[0] };
execute((RowInput) inputs[0], rowOutputs, rowCount, exec);
}
};
}
use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.
the class AbstractConditionalStreamingNodeModel 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[0];
while (rowInput.poll() != null) {
count++;
}
m_internals.getConfig().addLong(CFG_ROW_COUNT, count);
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
long rowCount = -1;
if (m_internals.getConfig().containsKey(CFG_ROW_COUNT)) {
rowCount = m_internals.getConfig().getLong(CFG_ROW_COUNT);
}
StreamableFunction func = createColumnRearranger((DataTableSpec) inSpecs[0], rowCount).createStreamableFunction();
func.runFinal(inputs, outputs, exec);
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
};
}
Aggregations