Search in sources :

Example 1 with Condition

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());
        }
    }
}
Also used : Condition(org.knime.base.node.rules.engine.Condition) RuleFactory(org.knime.base.node.rules.engine.RuleFactory) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) PortObject(org.knime.core.node.port.PortObject) Rule(org.knime.base.node.rules.engine.Rule) ParseException(java.text.ParseException) StringValue(org.knime.core.data.StringValue)

Aggregations

ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Condition (org.knime.base.node.rules.engine.Condition)1 Rule (org.knime.base.node.rules.engine.Rule)1 RuleFactory (org.knime.base.node.rules.engine.RuleFactory)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataRow (org.knime.core.data.DataRow)1 DataType (org.knime.core.data.DataType)1 StringValue (org.knime.core.data.StringValue)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 PortObject (org.knime.core.node.port.PortObject)1