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);
}
}
}
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);
}
}
}
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();
}
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());
}
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;
}
}
Aggregations