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