use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate in project knime-core by knime.
the class FromDecisionTreeNodeModel method addRules.
/**
* Adds the rules to {@code rs} (recursively on each leaf).
*
* @param rs The output {@link RuleSet}.
* @param parents The parent stack.
* @param node The actual node.
*/
private void addRules(final RuleSet rs, final List<DecisionTreeNode> parents, final DecisionTreeNode node) {
if (node.isLeaf()) {
SimpleRule rule = rs.addNewSimpleRule();
if (m_rulesToTable.getScorePmmlRecordCount().getBooleanValue()) {
// This increases the PMML quite significantly
BigDecimal sum = BigDecimal.ZERO;
final MathContext mc = new MathContext(7, RoundingMode.HALF_EVEN);
final boolean computeProbability = m_rulesToTable.getScorePmmlProbability().getBooleanValue();
if (computeProbability) {
sum = new BigDecimal(node.getClassCounts().entrySet().stream().mapToDouble(e -> e.getValue().doubleValue()).sum(), mc);
}
for (final Entry<DataCell, Double> entry : node.getClassCounts().entrySet()) {
final ScoreDistribution scoreDistrib = rule.addNewScoreDistribution();
scoreDistrib.setValue(entry.getKey().toString());
scoreDistrib.setRecordCount(entry.getValue());
if (computeProbability) {
if (Double.compare(entry.getValue().doubleValue(), 0.0) == 0) {
scoreDistrib.setProbability(new BigDecimal(0.0));
} else {
scoreDistrib.setProbability(new BigDecimal(entry.getValue().doubleValue(), mc).divide(sum, mc));
}
}
}
}
CompoundPredicate and = rule.addNewCompoundPredicate();
and.setBooleanOperator(BooleanOperator.AND);
DecisionTreeNode n = node;
do {
PMMLPredicate pmmlPredicate = ((DecisionTreeNodeSplitPMML) n.getParent()).getSplitPred()[n.getParent().getIndex(n)];
if (pmmlPredicate instanceof PMMLSimplePredicate) {
PMMLSimplePredicate simple = (PMMLSimplePredicate) pmmlPredicate;
SimplePredicate predicate = and.addNewSimplePredicate();
copy(predicate, simple);
} else if (pmmlPredicate instanceof PMMLCompoundPredicate) {
PMMLCompoundPredicate compound = (PMMLCompoundPredicate) pmmlPredicate;
CompoundPredicate predicate = and.addNewCompoundPredicate();
copy(predicate, compound);
} else if (pmmlPredicate instanceof PMMLSimpleSetPredicate) {
PMMLSimpleSetPredicate simpleSet = (PMMLSimpleSetPredicate) pmmlPredicate;
copy(and.addNewSimpleSetPredicate(), simpleSet);
} else if (pmmlPredicate instanceof PMMLTruePredicate) {
and.addNewTrue();
} else if (pmmlPredicate instanceof PMMLFalsePredicate) {
and.addNewFalse();
}
n = n.getParent();
} while (n.getParent() != null);
// Simple fix for the case when a single condition was used.
while (and.getFalseList().size() + and.getCompoundPredicateList().size() + and.getSimplePredicateList().size() + and.getSimpleSetPredicateList().size() + and.getTrueList().size() < 2) {
and.addNewTrue();
}
if (m_rulesToTable.getProvideStatistics().getBooleanValue()) {
rule.setNbCorrect(node.getOwnClassCount());
rule.setRecordCount(node.getEntireClassCount());
}
rule.setScore(node.getMajorityClass().toString());
} else {
parents.add(node);
for (int i = 0; i < node.getChildCount(); ++i) {
addRules(rs, parents, node.getChildAt(i));
}
parents.remove(node);
}
}
use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate in project knime-core by knime.
the class PMMLRuleTranslator method createRule.
/**
* Converts an xml {@link SimpleRule} to {@link Rule}.
*
* @param r An xml {@link SimpleRule}.
* @return The corresponding {@link Rule} object.
*/
private Rule createRule(final SimpleRule r) {
PMMLPredicate pred;
if (r.getTrue() != null) {
pred = new PMMLTruePredicate();
} else if (r.getFalse() != null) {
pred = new PMMLFalsePredicate();
} else if (r.getCompoundPredicate() != null) {
CompoundPredicate c = r.getCompoundPredicate();
pred = parseCompoundPredicate(c);
} else if (r.getSimplePredicate() != null) {
pred = parseSimplePredicate(r.getSimplePredicate());
} else if (r.getSimpleSetPredicate() != null) {
pred = parseSimpleSetPredicate(r.getSimpleSetPredicate());
} else {
throw new UnsupportedOperationException(r.toString());
}
final Map<String, ScoreProbabilityAndRecordCount> scores = r.getScoreDistributionList().stream().map(sd -> Pair.create(sd.getValue(), new ScoreProbabilityAndRecordCount(sd.isSetProbability() ? sd.getProbability() : null, sd.getRecordCount()))).collect(Collectors.toMap(Pair::getFirst, Pair::getSecond));
final Rule ret = new Rule(pred, r.getScore(), r.isSetWeight() ? r.getWeight() : null, r.isSetConfidence() ? r.getConfidence() : null, scores);
if (r.isSetNbCorrect()) {
ret.setNbCorrect(r.getNbCorrect());
}
if (r.isSetRecordCount()) {
ret.setRecordCount(r.getRecordCount());
}
return ret;
}
use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate in project knime-core by knime.
the class LiteralConditionParser method parseCondition.
/**
* {@inheritDoc}
*/
@Override
public TreeNodeCondition parseCondition(final Node node) {
CompoundPredicate compound = node.getCompoundPredicate();
if (compound != null) {
return handleCompoundPredicate(compound);
}
SimplePredicate simplePred = node.getSimplePredicate();
if (simplePred != null) {
return handleSimplePredicate(simplePred, false);
}
SimpleSetPredicate simpleSetPred = node.getSimpleSetPredicate();
if (simpleSetPred != null) {
return handleSimpleSetPredicate(simpleSetPred, false);
}
True truePred = node.getTrue();
if (truePred != null) {
return TreeNodeTrueCondition.INSTANCE;
}
False falsePred = node.getFalse();
if (falsePred != null) {
throw new IllegalArgumentException("There is no False condition in KNIME.");
}
throw new IllegalStateException("The pmmlNode contains no valid Predicate.");
}
use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate 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 + "\".");
}
}
}
use of org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate 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 + "\".");
}
}
}
Aggregations