Search in sources :

Example 1 with Outcome

use of org.knime.base.node.rules.engine.Rule.Outcome 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)

Example 2 with Outcome

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

the class RuleEngineVariableNodeModel 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) throws InvalidSettingsException {
    String newFlowVar = m_settings.getNewColName();
    final DataType outType = computeOutputType(rules);
    final VariableProvider provider = new VariableProvider() {

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

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

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

        @Override
        public long getRowCountLong() {
            throw new IllegalStateException("Row count 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) 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)

Example 3 with Outcome

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

the class RuleEngineNodeModel method getRulesOutcome.

/**
 * @param outType
 * @param row
 * @param r
 * @param isDisallowlongOutputForCompatibility TODO
 * @param variableProvider TODO
 * @return
 * @noreference This method is not intended to be referenced by clients.
 */
public static final DataCell getRulesOutcome(final DataType outType, final DataRow row, final List<Rule> rules, final boolean isDisallowLongOutputForCompatibility, final VariableProvider variableProvider) {
    for (Rule r : rules) {
        if (r.getCondition().matches(row, variableProvider).getOutcome() == MatchState.matchedAndStop) {
            Outcome outcome2 = r.getOutcome();
            // r.getSideEffect().perform(row, this);
            DataCell cell = (DataCell) outcome2.getComputedResult(row, variableProvider);
            // ... don't want Booleans (also implementing Long), for instance)
            if (cell instanceof LongCell && isDisallowLongOutputForCompatibility) {
                long l = ((LongValue) cell).getLongValue();
                if (l > Integer.MAX_VALUE) {
                    throw new RuntimeException("Values larger than " + Integer.MAX_VALUE + " not supported in old instances of the node -- recreate the node " + "(node was created using an KNIME version < 3.2");
                }
                cell = new IntCell((int) l);
            }
            if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
                return new StringCell(cell.toString());
            } else {
                return cell;
            }
        }
    }
    return DataType.getMissingCell();
}
Also used : LongCell(org.knime.core.data.def.LongCell) StringCell(org.knime.core.data.def.StringCell) Outcome(org.knime.base.node.rules.engine.Rule.Outcome) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell) IntCell(org.knime.core.data.def.IntCell)

Example 4 with Outcome

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

the class SimpleRuleParser method parse.

/**
 * Parses a whole {@link Rule}.
 *
 * @param rule A line representing a rule (possibly comment).
 * @param booleanOutcome Should the rule include only a boolean outcome ({@code true}), or it cannot ({@code false}
 *            ), or it can be either boolean or non-boolean ({@code null})?
 * @return The parsed {@link Rule}.
 * @throws ParseException Problem during parsing.
 */
public Rule parse(final String rule, final Boolean booleanOutcome) throws ParseException {
    if (RuleSupport.isComment(rule)) {
        return new GenericRule(rule, new Condition.Comment(rule), NoOutcome.getInstance());
    }
    final ParseState state = new ParseState(rule);
    final Condition condition = parseCondition(state);
    if (!state.isEnd()) {
        final Outcome outcome = parseOutcome(state, booleanOutcome);
        return new GenericRule(rule, condition, outcome);
    }
    throw new ParseException("No outcome specified.", state.getPosition());
}
Also used : GenericRule(org.knime.base.node.rules.engine.Rule.GenericRule) Outcome(org.knime.base.node.rules.engine.Rule.Outcome) NoOutcome(org.knime.base.node.rules.engine.Rule.Outcome.NoOutcome) ParseException(java.text.ParseException)

Example 5 with Outcome

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

the class StreamingUtil method checkExpressions.

private static boolean checkExpressions(final Rule rule, final Predicate<Expression> check) {
    if (!rule.getCondition().isEnabled()) {
        // comment
        return true;
    }
    if (rule instanceof GenericRule) {
        final GenericRule gr = (GenericRule) rule;
        final Condition condition = gr.getCondition();
        if (condition instanceof GenericCondition) {
            final GenericCondition gc = (GenericCondition) condition;
            final Expression ce = gc.getExpression();
            final Outcome outcome = gr.getOutcome();
            if (outcome instanceof GenericOutcome) {
                final GenericOutcome go = (GenericOutcome) outcome;
                final Expression oe = go.getExpression();
                return check.test(oe) && check.test(ce);
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Also used : GenericCondition(org.knime.base.node.rules.engine.Condition.GenericCondition) GenericRule(org.knime.base.node.rules.engine.Rule.GenericRule) Outcome(org.knime.base.node.rules.engine.Rule.Outcome) GenericOutcome(org.knime.base.node.rules.engine.Rule.Outcome.GenericOutcome) GenericOutcome(org.knime.base.node.rules.engine.Rule.Outcome.GenericOutcome) GenericCondition(org.knime.base.node.rules.engine.Condition.GenericCondition)

Aggregations

Outcome (org.knime.base.node.rules.engine.Rule.Outcome)5 DataCell (org.knime.core.data.DataCell)3 GenericRule (org.knime.base.node.rules.engine.Rule.GenericRule)2 DataType (org.knime.core.data.DataType)2 DoubleValue (org.knime.core.data.DoubleValue)2 FlowVariableProvider (org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider)2 ParseException (java.text.ParseException)1 GenericCondition (org.knime.base.node.rules.engine.Condition.GenericCondition)1 Rule (org.knime.base.node.rules.engine.Rule)1 GenericOutcome (org.knime.base.node.rules.engine.Rule.Outcome.GenericOutcome)1 NoOutcome (org.knime.base.node.rules.engine.Rule.Outcome.NoOutcome)1 VariableProvider (org.knime.base.node.rules.engine.VariableProvider)1 LongValue (org.knime.core.data.LongValue)1 IntCell (org.knime.core.data.def.IntCell)1 LongCell (org.knime.core.data.def.LongCell)1 StringCell (org.knime.core.data.def.StringCell)1