use of org.knime.base.node.rules.engine.Condition in project knime-core by knime.
the class RuleEngine2PortsSimpleNodeDialog method updateErrorsAndWarnings.
/**
* Updates the errors table, the warning text area and the computed outcome type.
*/
protected void updateErrorsAndWarnings() {
m_errorsModel.setRowCount(0);
hideErrors();
m_warnings.setText("");
m_outcomeType.setIcon(DataType.getMissingCell().getType().getIcon());
// Checking data from second input port
final int ruleIdx = getRules() == null ? -1 : getRules().getSpec().findColumnIndex(m_ruleColumn.getSelectedColumn());
final int outcomeIdx = getRules() == null ? -1 : getRules().getSpec().findColumnIndex(m_outcomeColumn.getSelectedColumn());
if (getRules() != null && isSpecAvailable() && ruleIdx >= 0) {
RuleFactory factory = ruleFactory();
long lineNo = 0;
boolean wasCatchAll = false;
final boolean firstHit = isFirstHit();
List<Rule> rules = new ArrayList<>();
for (DataRow dataRow : getRules()) {
++lineNo;
DataCell ruleCell = dataRow.getCell(ruleIdx);
if (ruleCell.isMissing()) {
// String cellValue = "?";
// if (ruleCell instanceof MissingValue) {
// cellValue += " (" + ((MissingValue)ruleCell).getError() + ")";
// }
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleCell, "Missing cell" });
showErrors();
}
if (ruleCell instanceof StringValue) {
StringValue ruleSV = (StringValue) ruleCell;
String ruleText = ruleSV.getStringValue().replaceAll("[\r\n]+", " ");
if (outcomeIdx >= 0) {
DataCell outcome = dataRow.getCell(outcomeIdx);
String outcomeString;
try {
outcomeString = m_settings.asStringFailForMissing(outcome);
} catch (InvalidSettingsException e) {
outcomeString = "?";
}
if (m_ruleType.onlyBooleanOutcome()) {
if ("\"TRUE\"".equalsIgnoreCase(outcomeString)) {
outcomeString = "TRUE";
} else if ("\"FALSE\"".equalsIgnoreCase(outcomeString)) {
outcomeString = "FALSE";
}
}
ruleText += " => " + outcomeString;
}
try {
Rule rule = factory.parse(ruleText, getDataSpec(), getAvailableFlowVariables());
rules.add(rule);
String origWarning = !m_warnings.getText().isEmpty() ? m_warnings.getText() + "\n" : "";
Condition cond = rule.getCondition();
if (cond.isEnabled()) {
// not comment
if (cond.isCatchAll() && !wasCatchAll && firstHit && lineNo < getRules().size()) {
m_warnings.setText(origWarning + "No rules will match after line " + lineNo + " (" + dataRow.getKey() + "). Because of rule: " + ruleText);
}
wasCatchAll |= cond.isCatchAll() && firstHit;
if (!wasCatchAll && cond.isConstantFalse()) {
m_warnings.setText(origWarning + "The rule in line " + lineNo + " (" + dataRow.getKey() + ") will never match: " + ruleText);
}
}
} catch (ParseException e) {
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleText, e.getMessage() });
showErrors();
}
} else {
// Missings were handled previously
if (!ruleCell.isMissing()) {
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleCell.toString(), "Wrong type: " + ruleCell.getType() });
}
}
}
final DataColumnSpec outcomeSpec = m_outcomeColumn.getSelectedColumnAsSpec();
DataType dataType = RuleEngineNodeModel.computeOutputType(rules, outcomeSpec == null ? StringCell.TYPE : outcomeSpec.getType(), m_ruleType, getSettings().isDisallowLongOutputForCompatibility());
if (dataType != null) {
m_outcomeType.setIcon(dataType.getIcon());
}
}
}
Aggregations