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);
}
}
}
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));
}
}
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).");
}
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;
}
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);
}
}
Aggregations