Search in sources :

Example 6 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate in project knime-core by knime.

the class PMMLConditionTranslator method parseCompoundPredicate.

/**
 * Create a KNIME compound predicate from a PMML compound predicate. Note that the "order" of the sub-predicates is
 * important (because of surrogate predicate). Therefore, we need to use xmlCursor to retrieve the order of the
 * predicates
 *
 * @param xmlCompoundPredicate the PMML Compound Predicate element
 * @return the KNIME Compound Predicate
 */
protected PMMLCompoundPredicate parseCompoundPredicate(final CompoundPredicate xmlCompoundPredicate) {
    List<PMMLPredicate> tempPredicateList = new ArrayList<PMMLPredicate>();
    if (xmlCompoundPredicate.sizeOfSimplePredicateArray() != 0) {
        for (SimplePredicate xmlSubSimplePredicate : xmlCompoundPredicate.getSimplePredicateList()) {
            tempPredicateList.add(parseSimplePredicate(xmlSubSimplePredicate));
        }
    }
    if (xmlCompoundPredicate.sizeOfCompoundPredicateArray() != 0) {
        for (CompoundPredicate xmlSubCompoundPredicate : xmlCompoundPredicate.getCompoundPredicateList()) {
            tempPredicateList.add(parseCompoundPredicate(xmlSubCompoundPredicate));
        }
    }
    if (xmlCompoundPredicate.sizeOfSimpleSetPredicateArray() != 0) {
        for (SimpleSetPredicate xmlSubSimpleSetPredicate : xmlCompoundPredicate.getSimpleSetPredicateList()) {
            tempPredicateList.add(parseSimpleSetPredicate(xmlSubSimpleSetPredicate));
        }
    }
    if (xmlCompoundPredicate.sizeOfTrueArray() != 0) {
        for (int i = 0; i < xmlCompoundPredicate.sizeOfTrueArray(); i++) {
            tempPredicateList.add(new PMMLTruePredicate());
        }
    }
    if (xmlCompoundPredicate.sizeOfFalseArray() != 0) {
        for (int i = 0; i < xmlCompoundPredicate.sizeOfFalseArray(); i++) {
            tempPredicateList.add(new PMMLFalsePredicate());
        }
    }
    List<String> predicateNames = new ArrayList<String>();
    XmlCursor xmlCursor = xmlCompoundPredicate.newCursor();
    if (xmlCursor.toFirstChild()) {
        do {
            XmlObject xmlElement = xmlCursor.getObject();
            XmlCursor elementCursor = xmlElement.newCursor();
            if (xmlElement instanceof CompoundPredicateDocument.CompoundPredicate) {
                predicateNames.add(COMPOUND);
            } else if (xmlElement instanceof TrueDocument.True) {
                predicateNames.add(TRUE);
            } else if (xmlElement instanceof FalseDocument.False) {
                predicateNames.add(FALSE);
            } else {
                elementCursor.toFirstAttribute();
                do {
                    if ("field".equals(elementCursor.getName().getLocalPart())) {
                        predicateNames.add(m_nameMapper.getColumnName(elementCursor.getTextValue()));
                        break;
                    }
                } while (elementCursor.toNextAttribute());
            }
        } while (xmlCursor.toNextSibling());
    }
    // ------------------------------------------------------
    // sort the predicate list
    List<PMMLPredicate> predicateList = new ArrayList<PMMLPredicate>();
    List<PMMLPredicate> compoundList = new ArrayList<PMMLPredicate>();
    for (PMMLPredicate tempPredicate : tempPredicateList) {
        if (tempPredicate instanceof PMMLCompoundPredicate) {
            compoundList.add(tempPredicate);
        }
    }
    for (String name : predicateNames) {
        if (name.equals(COMPOUND)) {
            predicateList.add(compoundList.get(0));
            compoundList.remove(0);
        } else if (name.equals(TRUE)) {
            predicateList.add(new PMMLTruePredicate());
        } else if (name.equals(FALSE)) {
            predicateList.add(new PMMLFalsePredicate());
        } else {
            int foundIndex = -1, i = 0;
            for (PMMLPredicate tempPredicate : tempPredicateList) {
                if (tempPredicate instanceof PMMLSimplePredicate) {
                    if (name.equals(((PMMLSimplePredicate) tempPredicate).getSplitAttribute())) {
                        predicateList.add(tempPredicate);
                        foundIndex = i;
                        break;
                    }
                } else if (tempPredicate instanceof PMMLSimpleSetPredicate) {
                    if (name.equals(((PMMLSimpleSetPredicate) tempPredicate).getSplitAttribute())) {
                        predicateList.add(tempPredicate);
                        foundIndex = i;
                        break;
                    }
                }
                ++i;
            }
            assert foundIndex >= 0 : tempPredicateList + "\n" + name;
            tempPredicateList.remove(foundIndex);
        }
    }
    LinkedList<PMMLPredicate> subPredicates = new LinkedList<PMMLPredicate>(predicateList);
    String operator = xmlCompoundPredicate.getBooleanOperator().toString();
    PMMLCompoundPredicate compoundPredicate = newCompoundPredicate(operator);
    compoundPredicate.setPredicates(subPredicates);
    return compoundPredicate;
}
Also used : TrueDocument(org.dmg.pmml.TrueDocument) ArrayList(java.util.ArrayList) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) LinkedList(java.util.LinkedList) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate) XmlCursor(org.apache.xmlbeans.XmlCursor) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) XmlObject(org.apache.xmlbeans.XmlObject)

Example 7 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate in project knime-core by knime.

the class PMMLDecisionTreeTranslator method exportCompoundPredicate.

/**
 * We need to handle compound predicates separately because the {@link PMMLPredicateTranslator} can currently
 * not deal with derived fields.
 *
 * @param outer the outer compound predicate to which we need to export <b>inner</b>
 * @param inner the inner compound predicate to export
 * @param mapper the derived field mapper
 */
private static void exportCompoundPredicate(final CompoundPredicate outer, final PMMLCompoundPredicate inner, final DerivedFieldMapper mapper) {
    CompoundPredicate pmmlInner = outer.addNewCompoundPredicate();
    pmmlInner.setBooleanOperator(PMMLPredicateTranslator.getOperator(inner.getBooleanOperator()));
    for (PMMLPredicate pred : inner.getPredicates()) {
        if (pred instanceof PMMLCompoundPredicate) {
            exportCompoundPredicate(pmmlInner, (PMMLCompoundPredicate) pred, mapper);
        } else {
            pred.setSplitAttribute(mapper.getDerivedFieldName(pred.getSplitAttribute()));
            PMMLPredicateTranslator.exportTo(pred, pmmlInner);
        }
    }
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)

Example 8 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate in project knime-core by knime.

the class PMMLDecisionTreeTranslator method exportCompoundPredicate.

/**
 * Exports {@link PMMLCompoundPredicate compound} to {@link Node pmmlNode} using {@link DerivedFieldMapper mapper}
 * to get names of derived fields. It is recommended to use this method in favor of
 * {@link PMMLPredicateTranslator#exportTo(PMMLPredicate, Node)}.
 *
 * @param pmmlNode the node to export the compound predicate to
 * @param compound the compound predicate to export
 * @param mapper the derived field mapper
 */
private static void exportCompoundPredicate(final Node pmmlNode, final PMMLCompoundPredicate compound, final DerivedFieldMapper mapper) {
    CompoundPredicate pmmlCp = pmmlNode.addNewCompoundPredicate();
    pmmlCp.setBooleanOperator(PMMLPredicateTranslator.getOperator(compound.getBooleanOperator()));
    for (PMMLPredicate pred : compound.getPredicates()) {
        if (pred instanceof PMMLCompoundPredicate) {
            exportCompoundPredicate(pmmlCp, (PMMLCompoundPredicate) pred, mapper);
        } else {
            pred.setSplitAttribute(mapper.getDerivedFieldName(pred.getSplitAttribute()));
            PMMLPredicateTranslator.exportTo(pred, pmmlCp);
        }
    }
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)

Example 9 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate in project knime-core by knime.

the class PMMLPredicateTranslator method exportTo.

/**
 * @param predicate the predicate to export
 * @param compound the CompundPredicate element to add the predicate to
 */
public static void exportTo(final PMMLPredicate predicate, final CompoundPredicate compound) {
    /**
     * 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) {
        compound.addNewFalse();
    } else if (predicate instanceof PMMLTruePredicate) {
        compound.addNewTrue();
    } else if (predicate instanceof PMMLFalsePredicate) {
        compound.addNewFalse();
    } else if (predicate instanceof PMMLSimplePredicate) {
        PMMLSimplePredicate sp = (PMMLSimplePredicate) predicate;
        SimplePredicate simplePred = compound.addNewSimplePredicate();
        initSimplePredicate(sp, simplePred);
    } else if (predicate instanceof PMMLSimpleSetPredicate) {
        PMMLSimpleSetPredicate sp = (PMMLSimpleSetPredicate) predicate;
        SimpleSetPredicate setPred = compound.addNewSimpleSetPredicate();
        initSimpleSetPred(sp, setPred);
    } else if (predicate instanceof PMMLCompoundPredicate) {
        PMMLCompoundPredicate compPred = (PMMLCompoundPredicate) predicate;
        CompoundPredicate cp = CompoundPredicate.Factory.newInstance();
        cp.setBooleanOperator(getOperator(compPred.getBooleanOperator()));
        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 10 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate 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)

Aggregations

CompoundPredicate (org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)13 SimplePredicate (org.dmg.pmml.SimplePredicateDocument.SimplePredicate)9 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)8 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)7 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)7 PMMLFalsePredicate (org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)6 PMMLSimpleSetPredicate (org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate)6 PMMLTruePredicate (org.knime.base.node.mine.decisiontree2.PMMLTruePredicate)6 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)5 ArrayList (java.util.ArrayList)4 XmlCursor (org.apache.xmlbeans.XmlCursor)3 XmlObject (org.apache.xmlbeans.XmlObject)3 SimpleRule (org.dmg.pmml.SimpleRuleDocument.SimpleRule)3 PMMLBooleanOperator (org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator)3 BigDecimal (java.math.BigDecimal)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 MININGFUNCTION (org.dmg.pmml.MININGFUNCTION)2 PMMLDocument (org.dmg.pmml.PMMLDocument)2