Search in sources :

Example 1 with SimpleStreamableOperatorInternals

use of org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals 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 2 with SimpleStreamableOperatorInternals

use of org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals 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();
                }
            }
        }
    };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataValue(org.knime.core.data.DataValue) 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) DataRow(org.knime.core.data.DataRow) PortObjectInput(org.knime.core.node.streamable.PortObjectInput) RowAppenderRowOutput(org.knime.base.node.rules.engine.RowAppenderRowOutput) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput) RowOutput(org.knime.core.node.streamable.RowOutput) VariableProvider(org.knime.base.node.rules.engine.VariableProvider) BooleanValue(org.knime.core.data.BooleanValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortInput(org.knime.core.node.streamable.PortInput) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) MutableLong(org.apache.commons.lang3.mutable.MutableLong) ExecutionContext(org.knime.core.node.ExecutionContext) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ParseException(java.text.ParseException) Rule(org.knime.base.node.rules.engine.Rule)

Example 3 with SimpleStreamableOperatorInternals

use of org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals 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);
        }
    };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) ExecutionContext(org.knime.core.node.ExecutionContext) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) ParseException(java.text.ParseException) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) RowInput(org.knime.core.node.streamable.RowInput)

Example 4 with SimpleStreamableOperatorInternals

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

the class StringToDurationPeriodNodeModel method createInitialStreamableOperatorInternals.

@Override
public StreamableOperatorInternals createInitialStreamableOperatorInternals() {
    final SimpleStreamableOperatorInternals simpleStreamableOperatorInternals = new SimpleStreamableOperatorInternals();
    simpleStreamableOperatorInternals.getConfig().addBoolean("needsIteration", true);
    simpleStreamableOperatorInternals.getConfig().addInt("sizeRow", 0);
    return simpleStreamableOperatorInternals;
}
Also used : SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals)

Example 5 with SimpleStreamableOperatorInternals

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

the class StringToDurationPeriodNodeModel method computeFinalOutputSpecs.

@Override
public PortObjectSpec[] computeFinalOutputSpecs(final StreamableOperatorInternals internals, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    if (m_type.getStringValue().equals(OutputType.Automatic.name())) {
        final SimpleStreamableOperatorInternals simpleInternals = (SimpleStreamableOperatorInternals) internals;
        final Config config = simpleInternals.getConfig();
        final DataColumnSpec[] colSpecs = new DataColumnSpec[config.getInt("sizeRow")];
        for (int i = 0; i < colSpecs.length; i++) {
            final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(config.getString("colname" + i), config.getDataType("type" + i));
            colSpecs[i] = dataColumnSpecCreator.createSpec();
        }
        return new DataTableSpec[] { new DataTableSpec(colSpecs) };
    } else {
        return configure(new DataTableSpec[] { (DataTableSpec) inSpecs[0] });
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Config(org.knime.core.node.config.Config)

Aggregations

SimpleStreamableOperatorInternals (org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals)24 DataTableSpec (org.knime.core.data.DataTableSpec)9 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)9 BufferedDataTable (org.knime.core.node.BufferedDataTable)7 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)7 StreamableOperatorInternals (org.knime.core.node.streamable.StreamableOperatorInternals)7 ExecutionContext (org.knime.core.node.ExecutionContext)6 RowInput (org.knime.core.node.streamable.RowInput)6 Config (org.knime.core.node.config.Config)5 MergeOperator (org.knime.core.node.streamable.MergeOperator)5 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)4 DataRow (org.knime.core.data.DataRow)4 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)4 DataTableRowInput (org.knime.core.node.streamable.DataTableRowInput)4 PortInput (org.knime.core.node.streamable.PortInput)4 RowOutput (org.knime.core.node.streamable.RowOutput)4 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 PartitionInfo (org.knime.core.node.streamable.PartitionInfo)3 PortOutput (org.knime.core.node.streamable.PortOutput)3