Search in sources :

Example 1 with VariableProvider

use of org.knime.base.node.rules.engine.VariableProvider 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 2 with VariableProvider

use of org.knime.base.node.rules.engine.VariableProvider in project knime-core by knime.

the class RuleEngineVariable2PortsNodeModel method performExecute.

/**
 * Creates the flow variable according to the computed value.
 *
 * @param rules The rules to check for match.
 * @throws InvalidSettingsException When there is an error in the settings.
 */
private void performExecute(final List<Rule> rules, final DataType outcomeColumnType) throws InvalidSettingsException {
    String newFlowVar = m_newVariableName;
    if (newFlowVar == null || newFlowVar.isEmpty()) {
        newFlowVar = DEFAULT_VARIABLE_NAME;
    }
    final DataType outType = computeOutputType(rules, outcomeColumnType);
    final VariableProvider provider = new VariableProvider() {

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return RuleEngineVariable2PortsNodeModel.this.readVariable(name, type);
        }

        @Override
        @Deprecated
        public int getRowCount() {
            throw new IllegalStateException("Row count is not available.");
        }

        @Override
        public long getRowCountLong() {
            throw new IllegalStateException("Row count is not available.");
        }

        @Override
        @Deprecated
        public int getRowIndex() {
            throw new IllegalStateException("Row index is not available.");
        }

        @Override
        public long getRowIndexLong() {
            throw new IllegalStateException("Row index is not available.");
        }
    };
    boolean wasMatch = false;
    for (Rule r : rules) {
        if (r.getCondition().matches(null, provider).getOutcome() == MatchState.matchedAndStop) {
            Outcome outcome2 = r.getOutcome();
            // r.getSideEffect().perform(row, this);
            final DataCell cell = (DataCell) outcome2.getComputedResult(null, provider);
            wasMatch = true;
            if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
                pushFlowVariableString(newFlowVar, cell.toString());
                break;
            } else {
                if (cell.isMissing()) {
                    throw new UnsupportedOperationException("Missing result, TODO");
                }
                if (outType.equals(IntCell.TYPE)) {
                    pushFlowVariableInt(newFlowVar, ((IntValue) cell).getIntValue());
                    break;
                } else if (outType.equals(DoubleCell.TYPE)) {
                    pushFlowVariableDouble(newFlowVar, ((DoubleValue) cell).getDoubleValue());
                    break;
                } else if (outType.equals(StringCell.TYPE)) {
                    pushFlowVariableString(newFlowVar, ((StringValue) cell).getStringValue());
                    break;
                } else {
                    // TODO
                    throw new UnsupportedOperationException("Wrong type: " + cell.getClass());
                }
            }
        }
    }
    if (!wasMatch) {
        if (outType.equals(StringCell.TYPE)) {
            pushFlowVariableString(newFlowVar, "");
        } else if (outType.equals(IntCell.TYPE)) {
            pushFlowVariableInt(newFlowVar, 0);
        } else {
            pushFlowVariableDouble(newFlowVar, 0.0);
        }
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) VariableProvider(org.knime.base.node.rules.engine.VariableProvider) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) Outcome(org.knime.base.node.rules.engine.Rule.Outcome) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) Rule(org.knime.base.node.rules.engine.Rule)

Aggregations

Rule (org.knime.base.node.rules.engine.Rule)2 VariableProvider (org.knime.base.node.rules.engine.VariableProvider)2 ParseException (java.text.ParseException)1 MutableLong (org.apache.commons.lang3.mutable.MutableLong)1 RowAppenderRowOutput (org.knime.base.node.rules.engine.RowAppenderRowOutput)1 Outcome (org.knime.base.node.rules.engine.Rule.Outcome)1 BooleanValue (org.knime.core.data.BooleanValue)1 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 DataType (org.knime.core.data.DataType)1 DataValue (org.knime.core.data.DataValue)1 DoubleValue (org.knime.core.data.DoubleValue)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 ExecutionContext (org.knime.core.node.ExecutionContext)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 BufferedDataTableRowOutput (org.knime.core.node.streamable.BufferedDataTableRowOutput)1 DataTableRowInput (org.knime.core.node.streamable.DataTableRowInput)1 PortInput (org.knime.core.node.streamable.PortInput)1 PortObjectInput (org.knime.core.node.streamable.PortObjectInput)1