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