Search in sources :

Example 16 with PMMLSimpleSetPredicate

use of org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate in project knime-core by knime.

the class RuleSetToTable method convertToStringPrecedence.

/**
 * @param condition A {@link PMMLPredicate}.
 * @param usePrecedence Should we simplify the condition?
 * @param parentOperator The parent operator's (logical connective) type, used for precedence, can be {@code null}.
 * @param types The type of input columns.
 * @return Converted {@code condition} with precedence.
 */
private static String convertToStringPrecedence(final PMMLPredicate condition, final boolean usePrecedence, final PMMLBooleanOperator parentOperator, final Map<String, DataType> types) {
    if (condition instanceof PMMLTruePredicate) {
        return "TRUE";
    }
    if (condition instanceof PMMLFalsePredicate) {
        return "FALSE";
    }
    if (condition instanceof PMMLSimplePredicate) {
        PMMLSimplePredicate sp = (PMMLSimplePredicate) condition;
        DataType dataType = types.get(sp.getSplitAttribute());
        switch(sp.getOperator()) {
            // intentional fall-through
            case EQUAL:
            // intentional fall-through
            case GREATER_OR_EQUAL:
            // intentional fall-through
            case GREATER_THAN:
            // intentional fall-through
            case LESS_OR_EQUAL:
            case LESS_THAN:
                return dollars(sp.getSplitAttribute()) + " " + sp.getOperator().getSymbol() + " " + asComparisonValue(sp.getThreshold(), dataType);
            case NOT_EQUAL:
                return "NOT " + dollars(sp.getSplitAttribute()) + " = " + asComparisonValue(sp.getThreshold(), dataType);
            case IS_MISSING:
                return "MISSING " + dollars(sp.getSplitAttribute());
            case IS_NOT_MISSING:
                return "NOT MISSING " + dollars(sp.getSplitAttribute());
            default:
                throw new UnsupportedOperationException("Unknown operator: " + sp.getOperator());
        }
    }
    if (condition instanceof PMMLSimpleSetPredicate) {
        PMMLSimpleSetPredicate ssp = (PMMLSimpleSetPredicate) condition;
        DataType dataType = types.get(ssp.getSplitAttribute());
        switch(ssp.getSetOperator()) {
            case IS_IN:
                return dollars(ssp.getSplitAttribute()) + " IN (" + asComparisonValues(ssp.getValues(), dataType) + ")";
            case IS_NOT_IN:
                return "NOT " + dollars(ssp.getSplitAttribute()) + " IN (" + asComparisonValues(ssp.getValues(), dataType) + ")";
            default:
                throw new UnsupportedOperationException("Unknown operator: " + ssp.getOperator());
        }
    }
    if (condition instanceof PMMLCompoundPredicate) {
        PMMLCompoundPredicate cp = (PMMLCompoundPredicate) condition;
        List<PMMLPredicate> predicates = cp.getPredicates();
        switch(cp.getBooleanOperator()) {
            case AND:
                // never parentheses, parent XOR, OR, AND, nothing: all fine, SURROGATE is not supported
                return parentheses(!usePrecedence, join(PMMLBooleanOperator.AND, cp, types, usePrecedence));
            case OR:
                // if not nothing or OR, we have to use parentheses
                return parentheses(!usePrecedence || (predicates.size() > 1 && parentOperator != null && parentOperator != PMMLBooleanOperator.OR), join(PMMLBooleanOperator.OR, cp, types, usePrecedence));
            case XOR:
                // if not nothing or XOR or OR, we have to use parentheses, so when it is an AND
                return parentheses(!usePrecedence || (predicates.size() > 1 && parentOperator == PMMLBooleanOperator.AND), join(PMMLBooleanOperator.XOR, cp, types, usePrecedence));
            case SURROGATE:
                {
                    CheckUtils.checkState(predicates.size() > 1, "At least two arguments are required for SURROGATE, but got only: " + predicates.size() + "\nValues: " + predicates);
                    return handleSurrogate(cp, predicates, usePrecedence, parentOperator, types);
                }
            default:
                throw new UnsupportedOperationException("Unknown operator: " + cp.getOperator());
        }
    }
    throw new IllegalArgumentException("Unknown predicate type: " + condition + " (" + condition.getClass());
}
Also used : PMMLTruePredicate(org.knime.base.node.mine.decisiontree2.PMMLTruePredicate) PMMLSimpleSetPredicate(org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) DataType(org.knime.core.data.DataType) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)

Aggregations

PMMLSimpleSetPredicate (org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate)13 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)12 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)9 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)9 PMMLFalsePredicate (org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)8 PMMLTruePredicate (org.knime.base.node.mine.decisiontree2.PMMLTruePredicate)8 CompoundPredicate (org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)5 ArrayList (java.util.ArrayList)4 SimplePredicate (org.dmg.pmml.SimplePredicateDocument.SimplePredicate)4 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)4 DecisionTreeNode (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)4 DecisionTreeNodeSplitPMML (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML)4 DataCell (org.knime.core.data.DataCell)4 ArrayType (org.dmg.pmml.ArrayType)3 DecisionTreeNodeLeaf (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeLeaf)3 LinkedHashSet (java.util.LinkedHashSet)2 Entry (java.util.Map.Entry)2 ScoreDistribution (org.dmg.pmml.ScoreDistributionDocument.ScoreDistribution)2 Enum (org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate.BooleanOperator.Enum)2 PMMLArrayType (org.knime.base.node.mine.decisiontree2.PMMLArrayType)2