Search in sources :

Example 1 with CompoundRule

use of org.dmg.pmml.CompoundRuleDocument.CompoundRule in project knime-core by knime.

the class PMMLRuleTranslator method findFirst.

/**
 * Finds the first xml {@link SimpleRule} within the {@code rule} {@link CompoundRule}.
 *
 * @param rule A {@link CompoundRule}.
 * @return The first {@link SimpleRule} the should provide the outcome.
 */
private SimpleRule findFirst(final CompoundRule rule) {
    XmlCursor newCursor = rule.newCursor();
    if (newCursor.toFirstChild()) {
        do {
            XmlObject object = newCursor.getObject();
            if (object instanceof SimpleRuleDocument.SimpleRule) {
                SimpleRuleDocument.SimpleRule sr = (SimpleRuleDocument.SimpleRule) object;
                return sr;
            }
            if (object instanceof CompoundRule) {
                CompoundRule cp = (CompoundRule) object;
                SimpleRule first = findFirst(cp);
                if (first != null) {
                    return first;
                }
            }
        } while (newCursor.toNextSibling());
    }
    assert false : rule;
    return null;
}
Also used : CompoundRule(org.dmg.pmml.CompoundRuleDocument.CompoundRule) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) XmlObject(org.apache.xmlbeans.XmlObject) SimpleRuleDocument(org.dmg.pmml.SimpleRuleDocument) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 2 with CompoundRule

use of org.dmg.pmml.CompoundRuleDocument.CompoundRule 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 3 with CompoundRule

use of org.dmg.pmml.CompoundRuleDocument.CompoundRule in project knime-core by knime.

the class PMMLRuleTranslator method createRule.

/**
 * The compound rules are tricky... We have to pull each simple rule out of them in order and find the first simple
 * rule to get the outcome. The result is a simple {@link Rule}.
 *
 * @param compoundRule An xml {@link CompoundRule}.
 * @return The corresponding {@link Rule}.
 */
private Rule createRule(final CompoundRule compoundRule) {
    final LinkedList<PMMLPredicate> predicates = new LinkedList<PMMLPredicate>();
    predicates.addAll(collectPredicates(compoundRule));
    final PMMLCompoundPredicate condition = newCompoundPredicate(PMMLBooleanOperator.AND.toString());
    condition.setPredicates(predicates);
    // This is suspicious, as the later outcomes are discarded, but this is the right thing
    // according to the spec 4.1 (http://www.dmg.org/v4-1/RuleSet.html)
    final SimpleRule firstRule = findFirst(compoundRule);
    if (firstRule == null) {
        throw new IllegalStateException("No SimpleRule was found in " + compoundRule);
    }
    return new Rule(condition, firstRule.getScore(), firstRule.isSetWeight() ? firstRule.getWeight() : null, firstRule.isSetConfidence() ? firstRule.getConfidence() : null);
}
Also used : SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) CompoundRule(org.dmg.pmml.CompoundRuleDocument.CompoundRule) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) LinkedList(java.util.LinkedList) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)

Aggregations

CompoundRule (org.dmg.pmml.CompoundRuleDocument.CompoundRule)3 SimpleRule (org.dmg.pmml.SimpleRuleDocument.SimpleRule)3 XmlCursor (org.apache.xmlbeans.XmlCursor)2 XmlObject (org.apache.xmlbeans.XmlObject)2 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)2 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CompoundPredicate (org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)1 CompoundRuleDocument (org.dmg.pmml.CompoundRuleDocument)1 SimplePredicate (org.dmg.pmml.SimplePredicateDocument.SimplePredicate)1 SimpleRuleDocument (org.dmg.pmml.SimpleRuleDocument)1 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)1