Search in sources :

Example 6 with SimplePredicate

use of org.dmg.pmml.SimplePredicateDocument.SimplePredicate in project knime-core by knime.

the class PMMLPredicateTranslator method exportTo.

/**
 * Note that the export of compound predicates does not support derived fields.
 *
 * @param predicate the predicate to export
 * @param node the Node element to add the predicate to
 */
public static void exportTo(final PMMLPredicate predicate, final Node node) {
    /**
     * Is basically a duplicate of the other export methods but there is no common parent class and therefore the
     * code is not really reusable.
     */
    if (predicate instanceof PMMLFalsePredicate) {
        node.addNewFalse();
    } else if (predicate instanceof PMMLTruePredicate) {
        node.addNewTrue();
    } else if (predicate instanceof PMMLFalsePredicate) {
        node.addNewFalse();
    } else if (predicate instanceof PMMLSimplePredicate) {
        PMMLSimplePredicate sp = (PMMLSimplePredicate) predicate;
        SimplePredicate simplePred = node.addNewSimplePredicate();
        initSimplePredicate(sp, simplePred);
    } else if (predicate instanceof PMMLSimpleSetPredicate) {
        PMMLSimpleSetPredicate sp = (PMMLSimpleSetPredicate) predicate;
        SimpleSetPredicate setPred = node.addNewSimpleSetPredicate();
        initSimpleSetPred(sp, setPred);
    } else if (predicate instanceof PMMLCompoundPredicate) {
        PMMLCompoundPredicate compPred = (PMMLCompoundPredicate) predicate;
        // compound predicates were only instantiated but not added to the node
        // this never caused any problems up until v3.3 because the KNIME decision tree
        // for classification does not use compound predicates
        CompoundPredicate cp = node.addNewCompoundPredicate();
        cp.setBooleanOperator(getOperator(compPred.getBooleanOperator()));
        // the derived field names of the contained predicates
        for (PMMLPredicate pred : compPred.getPredicates()) {
            exportTo(pred, cp);
        }
    }
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)

Example 7 with SimplePredicate

use of org.dmg.pmml.SimplePredicateDocument.SimplePredicate in project knime-core by knime.

the class TreeModelPMMLTranslator method addTreeNode.

/**
 * @param pmmlNode
 * @param node
 */
private void addTreeNode(final Node pmmlNode, final TreeNodeClassification node) {
    int index = m_nodeIndex++;
    pmmlNode.setId(Integer.toString(index));
    pmmlNode.setScore(node.getMajorityClassName());
    double[] targetDistribution = node.getTargetDistribution();
    NominalValueRepresentation[] targetVals = node.getTargetMetaData().getValues();
    double sum = 0.0;
    for (Double v : targetDistribution) {
        sum += v;
    }
    pmmlNode.setRecordCount(sum);
    TreeNodeCondition condition = node.getCondition();
    if (condition instanceof TreeNodeTrueCondition) {
        pmmlNode.addNewTrue();
    } else if (condition instanceof TreeNodeColumnCondition) {
        final TreeNodeColumnCondition colCondition = (TreeNodeColumnCondition) condition;
        final String colName = colCondition.getColumnMetaData().getAttributeName();
        final Operator.Enum operator;
        final String value;
        if (condition instanceof TreeNodeNominalCondition) {
            final TreeNodeNominalCondition nominalCondition = (TreeNodeNominalCondition) condition;
            operator = Operator.EQUAL;
            value = nominalCondition.getValue();
        } else if (condition instanceof TreeNodeBitCondition) {
            final TreeNodeBitCondition bitCondition = (TreeNodeBitCondition) condition;
            operator = Operator.EQUAL;
            value = bitCondition.getValue() ? "1" : "0";
        } else if (condition instanceof TreeNodeNumericCondition) {
            final TreeNodeNumericCondition numCondition = (TreeNodeNumericCondition) condition;
            NumericOperator numOperator = numCondition.getNumericOperator();
            switch(numOperator) {
                case LargerThan:
                    operator = Operator.GREATER_THAN;
                    break;
                case LessThanOrEqual:
                    operator = Operator.LESS_OR_EQUAL;
                    break;
                default:
                    throw new IllegalStateException("Unsupported operator (not " + "implemented): " + numOperator);
            }
            value = Double.toString(numCondition.getSplitValue());
        } else {
            throw new IllegalStateException("Unsupported condition (not " + "implemented): " + condition.getClass().getSimpleName());
        }
        SimplePredicate pmmlSimplePredicate = pmmlNode.addNewSimplePredicate();
        pmmlSimplePredicate.setField(colName);
        pmmlSimplePredicate.setOperator(operator);
        pmmlSimplePredicate.setValue(value);
    } else {
        throw new IllegalStateException("Unsupported condition (not " + "implemented): " + condition.getClass().getSimpleName());
    }
    // adding score distribution (class counts)
    for (int i = 0; i < targetDistribution.length; i++) {
        String className = targetVals[i].getNominalValue();
        double freq = targetDistribution[i];
        ScoreDistribution pmmlScoreDist = pmmlNode.addNewScoreDistribution();
        pmmlScoreDist.setValue(className);
        pmmlScoreDist.setRecordCount(freq);
    }
    for (int i = 0; i < node.getNrChildren(); i++) {
        addTreeNode(pmmlNode.addNewNode(), node.getChild(i));
    }
}
Also used : NominalValueRepresentation(org.knime.base.node.mine.treeensemble.data.NominalValueRepresentation) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) ScoreDistribution(org.dmg.pmml.ScoreDistributionDocument.ScoreDistribution) NumericOperator(org.knime.base.node.mine.treeensemble.model.TreeNodeNumericCondition.NumericOperator)

Example 8 with SimplePredicate

use of org.dmg.pmml.SimplePredicateDocument.SimplePredicate in project knime-core by knime.

the class LiteralConditionParser method parseOrCompound.

private TreeNodeColumnCondition parseOrCompound(final CompoundPredicate compound) {
    CheckUtils.checkArgument(compound.getCompoundPredicateList().isEmpty(), "Currently only simple or-compounds are supported that consist of exactly one" + " SimpleSetPredicate or SimplePredicate followed by a SimplePredicate with operator isMissing.");
    List<SimplePredicate> simplePredicates = compound.getSimplePredicateList();
    CheckUtils.checkArgument(simplePredicates.size() <= OR_COMPOUND_SIMPLEPREDICATE_LIMIT, "Currently at most two SimplePredicates are supported in an or-compound.");
    List<SimpleSetPredicate> simpleSetPredicates = compound.getSimpleSetPredicateList();
    CheckUtils.checkArgument(simpleSetPredicates.size() <= OR_COMPOUND_SIMPLESETPREDICATE_LIMIT, "Currently at most one SimpleSetPredicate is supported in an or-compound.");
    boolean acceptsMissings = false;
    for (SimplePredicate simplePred : simplePredicates) {
        if (simplePred.getOperator() == SimplePredicate.Operator.IS_MISSING) {
            acceptsMissings = true;
            // we may not try to parse a simple predicate with operator is_missing
            simplePredicates.remove(simplePred);
            break;
        }
    }
    if (!simplePredicates.isEmpty()) {
        return handleSimplePredicate(simplePredicates.get(0), acceptsMissings);
    } else if (!simpleSetPredicates.isEmpty()) {
        return handleSimpleSetPredicate(simpleSetPredicates.get(0), acceptsMissings);
    }
    throw new IllegalArgumentException("There was only a is_missing predicate contained in the or-compound \"" + compound + "\" (or at least no more SimplePredicates or SimpleSetPredicates).");
}
Also used : SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)

Example 9 with SimplePredicate

use of org.dmg.pmml.SimplePredicateDocument.SimplePredicate in project knime-core by knime.

the class PMMLRuleTranslator method collectPredicates.

/**
 * The predicates of a {@link CompoundRule} in the order they appear.
 *
 * @param compoundRule An xml {@link CompoundRule}.
 * @return The flat list of {@link PMMLPredicate}s.
 */
private List<PMMLPredicate> collectPredicates(final CompoundRule compoundRule) {
    List<PMMLPredicate> ret = new ArrayList<PMMLPredicate>();
    XmlCursor cursor = compoundRule.newCursor();
    if (cursor.toFirstChild()) {
        do {
            XmlObject object = cursor.getObject();
            if (object instanceof CompoundRuleDocument.CompoundRule) {
                CompoundRuleDocument.CompoundRule cr = (CompoundRuleDocument.CompoundRule) object;
                ret.addAll(collectPredicates(cr));
            } else if (object instanceof SimpleRule) {
                SimpleRule sr = (SimpleRule) object;
                ret.add(createRule(sr).getCondition());
            } else if (object instanceof SimplePredicate) {
                SimplePredicate sp = (SimplePredicate) object;
                ret.add(parseSimplePredicate(sp));
            } else if (object instanceof CompoundPredicate) {
                CompoundPredicate cp = (CompoundPredicate) object;
                ret.add(parseCompoundPredicate(cp));
            }
        } while (cursor.toNextSibling());
    }
    return ret;
}
Also used : CompoundRule(org.dmg.pmml.CompoundRuleDocument.CompoundRule) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) CompoundRule(org.dmg.pmml.CompoundRuleDocument.CompoundRule) ArrayList(java.util.ArrayList) CompoundRuleDocument(org.dmg.pmml.CompoundRuleDocument) XmlObject(org.apache.xmlbeans.XmlObject) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 10 with SimplePredicate

use of org.dmg.pmml.SimplePredicateDocument.SimplePredicate in project knime-core by knime.

the class PMMLRuleTranslator method setPredicate.

/**
 * As the predicates can be of different subclasses of {@link PMMLPredicate}, creating them adding their properties
 * to the {@code simpleRule} is done with this method.
 *
 * @param simpleRule An xml {@link SimpleRule} (recently created).
 * @param predicate A {@link PMMLPredicate} with preferably from the Rule versions of
 *            {@link PMMLRuleSimplePredicate} and {@link PMMLRuleCompoundPredicate}.
 * @since 2.12
 */
public void setPredicate(final SimpleRule simpleRule, final PMMLPredicate predicate) {
    if (predicate instanceof PMMLFalsePredicate) {
        simpleRule.addNewFalse();
    } else if (predicate instanceof PMMLTruePredicate) {
        simpleRule.addNewTrue();
    } else if (predicate instanceof PMMLSimplePredicate) {
        PMMLSimplePredicate simple = (PMMLSimplePredicate) predicate;
        SimplePredicate pred = simpleRule.addNewSimplePredicate();
        pred.setField(simple.getSplitAttribute());
        setOperator(pred, simple);
        if (simple.getThreshold() != null) {
            pred.setValue(simple.getThreshold());
        }
    } else if (predicate instanceof PMMLCompoundPredicate) {
        PMMLCompoundPredicate comp = (PMMLCompoundPredicate) predicate;
        CompoundPredicate p = simpleRule.addNewCompoundPredicate();
        setCompound(p, comp);
    } else if (predicate instanceof PMMLSimpleSetPredicate) {
        PMMLSimpleSetPredicate set = (PMMLSimpleSetPredicate) predicate;
        SimpleSetPredicate s = simpleRule.addNewSimpleSetPredicate();
        setSetPredicate(s, set);
    }
}
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) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) PMMLSimpleSetPredicate(org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)

Aggregations

SimplePredicate (org.dmg.pmml.SimplePredicateDocument.SimplePredicate)11 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)9 CompoundPredicate (org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)8 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)4 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)4 ArrayList (java.util.ArrayList)3 XmlCursor (org.apache.xmlbeans.XmlCursor)3 ScoreDistribution (org.dmg.pmml.ScoreDistributionDocument.ScoreDistribution)3 PMMLFalsePredicate (org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)3 PMMLSimpleSetPredicate (org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate)3 Entry (java.util.Map.Entry)2 XmlObject (org.apache.xmlbeans.XmlObject)2 SimpleRule (org.dmg.pmml.SimpleRuleDocument.SimpleRule)2 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)2 PMMLTruePredicate (org.knime.base.node.mine.decisiontree2.PMMLTruePredicate)2 DecisionTreeNode (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)2 DecisionTreeNodeSplitPMML (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML)2 DataCell (org.knime.core.data.DataCell)2 File (java.io.File)1 IOException (java.io.IOException)1