Search in sources :

Example 1 with PMMLBooleanOperator

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

the class RuleSetToTable method handleSurrogate.

/**
 * (This is a recursive method.)
 *
 * @param cp A SURROGATE {@link PMMLCompoundPredicate}.
 * @param predicates The predicates to be converted.
 * @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 cp}.
 * @throws IllegalStateException If cannot be transformed.
 */
private static String handleSurrogate(final PMMLCompoundPredicate cp, final List<PMMLPredicate> predicates, final boolean usePrecedence, final PMMLBooleanOperator parentOperator, final Map<String, DataType> types) {
    // surrogate(a, b) = if not missing(a) then rel(a) else b = ((NOT MISSING a) AND rel(a)) OR ((MISSING a) AND b)
    // surrogate(a, surrogate(b, c)) = if not missing(a) then rel(a) else if not missing(b) then rel(b) else c =
    // ((NOT MISSING a) AND rel(a)) OR ((MISSING a) AND (((NOT MISSING b) AND rel(b)) OR ((MISSING b) AND rel(c))))
    PMMLPredicate first = predicates.get(0);
    List<PMMLPredicate> rest = predicates.subList(1, predicates.size());
    if (predicates.size() == 1) {
        return convertToStringPrecedence(first, usePrecedence, PMMLBooleanOperator.AND, types);
    }
    CheckUtils.checkState(first instanceof PMMLTruePredicate || first instanceof PMMLFalsePredicate || first instanceof PMMLSimplePredicate || first instanceof PMMLSimpleSetPredicate, "Compound predicates are not supported by the SURROGATE transformation: " + first + " in\n" + cp);
    if (first instanceof PMMLFalsePredicate || first instanceof PMMLTruePredicate) {
        return convertToString(first, usePrecedence, types);
    }
    if (first instanceof PMMLSimplePredicate || first instanceof PMMLSimpleSetPredicate) {
        return parentheses(!usePrecedence || (parentOperator != null && parentOperator != PMMLBooleanOperator.OR), parentheses(!usePrecedence, /*OR is outside of this AND*/
        "NOT MISSING " + dollars(first.getSplitAttribute()) + " AND " + convertToStringPrecedence(first, usePrecedence, PMMLBooleanOperator.AND, types)) + " OR " + parentheses(!usePrecedence, /*OR is outside of this AND*/
        "MISSING " + dollars(first.getSplitAttribute()) + " AND " + handleSurrogate(cp, rest, usePrecedence, PMMLBooleanOperator.AND, types)));
    }
    throw new IllegalStateException("Compound predicates are not supported at this position: " + first + " in\n" + cp);
}
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) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)

Example 2 with PMMLBooleanOperator

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

the class PMMLRuleTranslator method newCompoundPredicate.

/**
 * {@inheritDoc}
 */
@Override
protected PMMLCompoundPredicate newCompoundPredicate(final String operator) {
    try {
        PMMLBooleanOperator op;
        op = PMMLBooleanOperator.get(operator);
        return new PMMLCompoundPredicate(op);
    } catch (InstantiationException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}
Also used : PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)

Example 3 with PMMLBooleanOperator

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

the class PMMLRuleTranslator method setCompound.

/**
 * Sets {@code cp}s xml content based on {@code compound}'s properties.
 *
 * @param cp An xml {@link CompoundPredicate}.
 * @param compound A {@link PMMLCompoundPredicate}.
 */
private void setCompound(final CompoundPredicate cp, final PMMLCompoundPredicate compound) {
    PMMLBooleanOperator op = compound.getBooleanOperator();
    org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate.BooleanOperator.Enum boolOp = PMMLPredicateTranslator.getOperator(op);
    if (boolOp == null) {
        throw new UnsupportedOperationException("Not supported: " + op);
    }
    cp.setBooleanOperator(boolOp);
    for (PMMLPredicate pp : compound.getPredicates()) {
        setPredicate(cp, pp);
    }
}
Also used : PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator)

Example 4 with PMMLBooleanOperator

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

the class ConditionExporter method setValuesFromPMMLCompoundPredicate.

private void setValuesFromPMMLCompoundPredicate(final CompoundPredicate to, final PMMLCompoundPredicate from) {
    final PMMLBooleanOperator boolOp = from.getBooleanOperator();
    switch(boolOp) {
        case AND:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
            break;
        case OR:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.OR);
            break;
        case SURROGATE:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.SURROGATE);
            break;
        case XOR:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
            break;
        default:
            throw new IllegalStateException("Unknown boolean predicate \"" + boolOp + "\".");
    }
    final List<PMMLPredicate> predicates = from.getPredicates();
    for (final PMMLPredicate predicate : predicates) {
        if (predicate instanceof PMMLSimplePredicate) {
            setValuesFromPMMLSimplePredicate(to.addNewSimplePredicate(), (PMMLSimplePredicate) predicate);
        } else if (predicate instanceof PMMLSimpleSetPredicate) {
            setValuesFromPMMLSimpleSetPredicate(to.addNewSimpleSetPredicate(), (PMMLSimpleSetPredicate) predicate);
        } else if (predicate instanceof PMMLTruePredicate) {
            to.addNewTrue();
        } else if (predicate instanceof PMMLFalsePredicate) {
            to.addNewFalse();
        } else if (predicate instanceof PMMLCompoundPredicate) {
            final CompoundPredicate compound = to.addNewCompoundPredicate();
            final PMMLCompoundPredicate knimeCompound = (PMMLCompoundPredicate) predicate;
            setValuesFromPMMLCompoundPredicate(compound, knimeCompound);
        } else {
            throw new IllegalStateException("Unknown predicate type \"" + predicate + "\".");
        }
    }
}
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) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)

Example 5 with PMMLBooleanOperator

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

the class TreeModelPMMLTranslator method setValuesFromPMMLCompoundPredicate.

private static void setValuesFromPMMLCompoundPredicate(final CompoundPredicate to, final PMMLCompoundPredicate from) {
    final PMMLBooleanOperator boolOp = from.getBooleanOperator();
    switch(boolOp) {
        case AND:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
            break;
        case OR:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.OR);
            break;
        case SURROGATE:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.SURROGATE);
            break;
        case XOR:
            to.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
            break;
        default:
            throw new IllegalStateException("Unknown boolean predicate \"" + boolOp + "\".");
    }
    final List<PMMLPredicate> predicates = from.getPredicates();
    for (final PMMLPredicate predicate : predicates) {
        if (predicate instanceof PMMLSimplePredicate) {
            setValuesFromPMMLSimplePredicate(to.addNewSimplePredicate(), (PMMLSimplePredicate) predicate);
        } else if (predicate instanceof PMMLSimpleSetPredicate) {
            setValuesFromPMMLSimpleSetPredicate(to.addNewSimpleSetPredicate(), (PMMLSimpleSetPredicate) predicate);
        } else if (predicate instanceof PMMLTruePredicate) {
            to.addNewTrue();
        } else if (predicate instanceof PMMLFalsePredicate) {
            to.addNewFalse();
        } else if (predicate instanceof PMMLCompoundPredicate) {
            final CompoundPredicate compound = to.addNewCompoundPredicate();
            final PMMLCompoundPredicate knimeCompound = (PMMLCompoundPredicate) predicate;
            setValuesFromPMMLCompoundPredicate(compound, knimeCompound);
        } else {
            throw new IllegalStateException("Unknown predicate type \"" + predicate + "\".");
        }
    }
}
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) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)

Aggregations

PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)6 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)5 PMMLBooleanOperator (org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator)4 PMMLFalsePredicate (org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)4 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)4 PMMLSimpleSetPredicate (org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate)4 PMMLTruePredicate (org.knime.base.node.mine.decisiontree2.PMMLTruePredicate)4 CompoundPredicate (org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)2 DataType (org.knime.core.data.DataType)1