Search in sources :

Example 6 with StreamableOperatorInternals

use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.

the class SimpleStreamableFunctionWithInternalsNodeModel method createMergeOperator.

/**
 * {@inheritDoc}
 */
@Override
public MergeOperator createMergeOperator() {
    return new MergeOperator() {

        @Override
        public StreamableOperatorInternals mergeFinal(final StreamableOperatorInternals[] internals) {
            @SuppressWarnings("unchecked") T[] castedInternals = (T[]) Array.newInstance(m_class, internals.length);
            for (int i = 0; i < internals.length; i++) {
                StreamableOperatorInternals o = internals[i];
                if (o == null) {
                    throw new NullPointerException("internals at position " + i + " is null");
                } else if (!m_class.isInstance(o)) {
                    throw new IllegalStateException(String.format("Internals at position %d is not of expected " + "class \"%s\", it's a \"%s\"", i, m_class.getSimpleName(), o.getClass().getSimpleName()));
                }
                castedInternals[i] = m_class.cast(o);
            }
            return mergeStreamingOperatorInternals(castedInternals);
        }
    };
}
Also used : MergeOperator(org.knime.core.node.streamable.MergeOperator) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals)

Example 7 with StreamableOperatorInternals

use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.

the class ReferenceColumnResorterNodeModel method createMergeOperator.

/**
 * {@inheritDoc}
 */
@Override
public MergeOperator createMergeOperator() {
    return new MergeOperator() {

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals mergeIntermediate(final StreamableOperatorInternals[] operators) {
            return mergeFinal(operators);
        }

        @Override
        public StreamableOperatorInternals mergeFinal(final StreamableOperatorInternals[] operators) {
            StreamableOperatorInternals result = operators[0];
            String[] refOrder = readOrderFromStreamableOperatorInternals(result);
            for (int i = 1; i < operators.length; i++) {
                final String[] curOrder = readOrderFromStreamableOperatorInternals(operators[i]);
                if (!Arrays.equals(refOrder, curOrder)) {
                    StringBuilder b = new StringBuilder();
                    b.append("Inconsistent internals -- all workers should see same dictionary table; ");
                    b.append("index ").append(i).append("is different to position 0: ");
                    b.append(refOrder).append(" vs. ").append(curOrder);
                }
            }
            return result;
        }
    };
}
Also used : MergeOperator(org.knime.core.node.streamable.MergeOperator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals)

Example 8 with StreamableOperatorInternals

use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.

the class ReferenceColumnResorterNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        private String[] m_streamableOperatorOrder;

        /**
         * {@inheritDoc}
         */
        @Override
        public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
            BufferedDataTable orderTable = (BufferedDataTable) ((PortObjectInput) inputs[1]).getPortObject();
            m_streamableOperatorOrder = readOrderFromTable(orderTable);
        }

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            RowInput dataInput = (RowInput) inputs[0];
            DataTableSpec dataInputSpec = dataInput.getDataTableSpec();
            ColumnRearranger rearranger = createColumnRearranger(dataInputSpec, m_streamableOperatorOrder);
            StreamableFunction streamableFunction = rearranger.createStreamableFunction();
            streamableFunction.runFinal(new PortInput[] { dataInput }, outputs, exec);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return createStreamableOperatorInternalsFromOrder(m_streamableOperatorOrder);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void loadInternals(final StreamableOperatorInternals internals) {
            m_streamableOperatorOrder = readOrderFromStreamableOperatorInternals(internals);
        }
    };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ExecutionContext(org.knime.core.node.ExecutionContext) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) BufferedDataTable(org.knime.core.node.BufferedDataTable) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) RowInput(org.knime.core.node.streamable.RowInput) StreamableFunction(org.knime.core.node.streamable.StreamableFunction)

Example 9 with StreamableOperatorInternals

use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.

the class StringToDurationPeriodNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        private SimpleStreamableOperatorInternals m_internals = new SimpleStreamableOperatorInternals();

        /**
         * {@inheritDoc}
         */
        @Override
        public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
            final RowInput rowInput = (RowInput) inputs[0];
            final DataRow row = rowInput.poll();
            if (row != null) {
                final DataTableSpec inSpec = rowInput.getDataTableSpec();
                final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
                final Config config = m_internals.getConfig();
                // detect types
                detectTypes(new OneRowAdditionalRowInput(rowInput, row));
                for (int i = 0; i < m_detectedTypes.length; i++) {
                    config.addDataType("detected_type" + i, m_detectedTypes[i]);
                }
                // write detected types and column names into config
                if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                    for (int i = 0; i < rowInput.getDataTableSpec().getNumColumns(); i++) {
                        final int searchIdx = Arrays.binarySearch(includeIndexes, i);
                        config.addString("colname" + i, inSpec.getColumnNames()[i]);
                        if (searchIdx < 0) {
                            config.addDataType("type" + i, inSpec.getColumnSpec(i).getType());
                        } else {
                            config.addDataType("type" + i, m_detectedTypes[searchIdx] != null ? m_detectedTypes[searchIdx] : null);
                        }
                    }
                    config.addInt("sizeRow", rowInput.getDataTableSpec().getNumColumns());
                } else {
                    for (int i = 0; i < inSpec.getNumColumns(); i++) {
                        config.addString("colname" + i, inSpec.getColumnNames()[i]);
                        config.addDataType("type" + i, inSpec.getColumnSpec(i).getType());
                    }
                    for (int i = 0; i < m_detectedTypes.length; i++) {
                        config.addString("colname" + (i + inSpec.getNumColumns()), new UniqueNameGenerator(inSpec).newName(inSpec.getColumnSpec(includeIndexes[i]).getName() + m_suffix.getStringValue()));
                        config.addDataType("type" + (i + inSpec.getNumColumns()), m_detectedTypes[i]);
                    }
                    config.addInt("sizeRow", inSpec.getNumColumns() + m_detectedTypes.length);
                }
                config.addBoolean("needsIteration", false);
            } else {
                m_internals.getConfig().addInt("sizeRow", 0);
            }
            rowInput.close();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return m_internals;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void loadInternals(final StreamableOperatorInternals internals) {
            m_internals = (SimpleStreamableOperatorInternals) 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();
            // read detected types from config
            final DataType[] detectedTypes = new DataType[includeIndexes.length];
            final Config config = m_internals.getConfig();
            for (int i = 0; i < includeIndexes.length; i++) {
                detectedTypes[i] = config.getDataType("detected_type" + i, null);
            }
            // compute every row
            DataRow row;
            while ((row = in.poll()) != null) {
                exec.checkCanceled();
                DataCell[] datacells = new DataCell[includeIndexes.length];
                for (int i = 0; i < includeIndexes.length; i++) {
                    if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                        final StringToDurationPeriodCellFactory cellFac = new StringToDurationPeriodCellFactory(new DataColumnSpecCreator(inSpec.getColumnNames()[includeIndexes[i]], detectedTypes[i]).createSpec(), includeIndexes[i]);
                        datacells[i] = cellFac.getCells(row)[0];
                    } else {
                        final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(inSpec.getColumnNames()[includeIndexes[i]] + m_suffix.getStringValue(), detectedTypes[i]);
                        final StringToDurationPeriodCellFactory cellFac = new StringToDurationPeriodCellFactory(dataColSpec, 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();
        }
    };
}
Also used : Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) AppendedColumnRow(org.knime.core.data.append.AppendedColumnRow) ExecutionContext(org.knime.core.node.ExecutionContext) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) LinkedList(java.util.LinkedList) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) InputFilter(org.knime.core.node.util.filter.InputFilter) PortInput(org.knime.core.node.streamable.PortInput) DurationPeriodFormatUtils(org.knime.time.util.DurationPeriodFormatUtils) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DurationCellFactory(org.knime.core.data.time.duration.DurationCellFactory) PartitionInfo(org.knime.core.node.streamable.PartitionInfo) RowInput(org.knime.core.node.streamable.RowInput) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) SettingsModelColumnFilter2(org.knime.core.node.defaultnodesettings.SettingsModelColumnFilter2) OutputPortRole(org.knime.core.node.streamable.OutputPortRole) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) NodeModel(org.knime.core.node.NodeModel) File(java.io.File) DataRow(org.knime.core.data.DataRow) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PortOutput(org.knime.core.node.streamable.PortOutput) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) InputPortRole(org.knime.core.node.streamable.InputPortRole) MissingCell(org.knime.core.data.MissingCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) Config(org.knime.core.node.config.Config) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) DataType(org.knime.core.data.DataType) PeriodCellFactory(org.knime.core.data.time.period.PeriodCellFactory) MergeOperator(org.knime.core.node.streamable.MergeOperator) RowOutput(org.knime.core.node.streamable.RowOutput) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) DataTableSpec(org.knime.core.data.DataTableSpec) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Config(org.knime.core.node.config.Config) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) DataRow(org.knime.core.data.DataRow) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) RowOutput(org.knime.core.node.streamable.RowOutput) ExecutionContext(org.knime.core.node.ExecutionContext) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) AppendedColumnRow(org.knime.core.data.append.AppendedColumnRow)

Example 10 with StreamableOperatorInternals

use of org.knime.core.node.streamable.StreamableOperatorInternals in project knime-core by knime.

the class StringToDurationPeriodNodeModel method createMergeOperator.

@Override
public MergeOperator createMergeOperator() {
    return new MergeOperator() {

        @Override
        public StreamableOperatorInternals mergeIntermediate(final StreamableOperatorInternals[] operators) {
            final SimpleStreamableOperatorInternals internals = new SimpleStreamableOperatorInternals();
            final Config config = internals.getConfig();
            for (StreamableOperatorInternals operator : operators) {
                final Config configToMerge = ((SimpleStreamableOperatorInternals) operator).getConfig();
                final int sizeRow = configToMerge.getInt("sizeRow", -1);
                config.addInt("sizeRow", sizeRow);
                for (int i = 0; i < sizeRow; i++) {
                    if (!config.containsKey("type" + i) && configToMerge.getDataType("type" + i, null) != null) {
                        config.addDataType("type" + i, configToMerge.getDataType("type" + i, null));
                        config.addString("colname" + i, configToMerge.getString("colname" + i, null));
                    }
                    if (!config.containsKey("detected_type" + i) && configToMerge.getDataType("detected_type" + i, null) != null) {
                        config.addDataType("detected_type" + i, configToMerge.getDataType("detected_type" + i, null));
                    }
                }
            }
            // if a column's type could not be detected, guess it to be a PeriodCell
            final Config configToMerge = ((SimpleStreamableOperatorInternals) operators[0]).getConfig();
            for (int i = 0; i < configToMerge.getInt("sizeRow", -1); i++) {
                if (!config.containsKey("type" + i)) {
                    config.addDataType("type" + i, PeriodCellFactory.TYPE);
                    config.addString("colname" + i, configToMerge.getString("colname" + i, null));
                }
                if (!config.containsKey("detected_type" + i)) {
                    config.addDataType("detected_type" + i, PeriodCellFactory.TYPE);
                }
            }
            return internals;
        }

        @Override
        public StreamableOperatorInternals mergeFinal(final StreamableOperatorInternals[] operators) {
            return null;
        }
    };
}
Also used : SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) Config(org.knime.core.node.config.Config) MergeOperator(org.knime.core.node.streamable.MergeOperator) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals)

Aggregations

StreamableOperatorInternals (org.knime.core.node.streamable.StreamableOperatorInternals)13 DataTableSpec (org.knime.core.data.DataTableSpec)10 ExecutionContext (org.knime.core.node.ExecutionContext)10 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)10 RowInput (org.knime.core.node.streamable.RowInput)9 SimpleStreamableOperatorInternals (org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals)9 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)7 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)6 BufferedDataTable (org.knime.core.node.BufferedDataTable)6 MergeOperator (org.knime.core.node.streamable.MergeOperator)5 RowOutput (org.knime.core.node.streamable.RowOutput)5 DataRow (org.knime.core.data.DataRow)4 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)4 DataTableRowInput (org.knime.core.node.streamable.DataTableRowInput)4 PortInput (org.knime.core.node.streamable.PortInput)4 File (java.io.File)3 IOException (java.io.IOException)3 ParseException (java.text.ParseException)3 Arrays (java.util.Arrays)3 ReplacedColumnsDataRow (org.knime.base.data.replace.ReplacedColumnsDataRow)3