use of org.dmg.pmml.tree.Node in project jpmml-sparkml by jpmml.
the class TreeModelCompactor method isCategoricalField.
private boolean isCategoricalField(FieldName name) {
Deque<PMMLObject> parents = getParents();
for (PMMLObject parent : parents) {
if (parent instanceof Node) {
Node node = (Node) parent;
Predicate predicate = node.getPredicate();
if (predicate instanceof SimpleSetPredicate) {
SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) predicate;
FieldName categoricalField = simpleSetPredicate.getField();
if ((name).equals(categoricalField)) {
return true;
}
} else if (predicate instanceof True) {
True truePredicate = (True) predicate;
FieldName categoricalField = this.categoricalFields.get(truePredicate);
if ((name).equals(categoricalField)) {
return true;
}
}
} else {
return false;
}
}
return false;
}
use of org.dmg.pmml.tree.Node in project drools by kiegroup.
the class KiePMMLNodeFactory method populatedNestedNodes.
/**
* Recursively populate the <code>JavaParserDTO.nodeTemplate</code> with methods generated by
* <b>nested</b> <code>Node</code>s.
*
* It also dump generated sources when <code>JavaParserDTO.limitReach() == true</code>,
* actually starting creation of a new class.
*
* @param toPopulate
* @param sourcesMap
* @param fields
* @param nodeNamesDTO
*/
static void populatedNestedNodes(final JavaParserDTO toPopulate, final Map<String, String> sourcesMap, final List<Field<?>> fields, final NodeNamesDTO nodeNamesDTO) {
final Node node = nodeNamesDTO.node;
if (node.hasNodes()) {
for (Node nestedNode : node.getNodes()) {
final NodeNamesDTO nestedNodeNamesDTO = new NodeNamesDTO(nestedNode, nodeNamesDTO.getNestedNodeClassName(nestedNode), nodeNamesDTO.nodeClassName, nodeNamesDTO.missingValuePenalty);
if (toPopulate.limitReach()) {
// start creating new node
// 1) dump generated class source
sourcesMap.put(toPopulate.fullNodeClassName, toPopulate.getSource());
// 2) start creation of new node
final JavaParserDTO javaParserDTO = new JavaParserDTO(nestedNodeNamesDTO, toPopulate.packageName);
populateJavaParserDTOAndSourcesMap(javaParserDTO, sourcesMap, nestedNodeNamesDTO, fields, true);
} else {
// Set 'evaluateNode'
populateEvaluateNode(toPopulate, nestedNodeNamesDTO, fields, false);
mergeNode(toPopulate, nestedNodeNamesDTO);
populatedNestedNodes(toPopulate, sourcesMap, fields, nestedNodeNamesDTO);
}
}
}
}
use of org.dmg.pmml.tree.Node in project drools by kiegroup.
the class KiePMMLNodeFactoryTest method mergeNodeReferences.
@Test
public void mergeNodeReferences() {
KiePMMLNodeFactory.NodeNamesDTO nodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(nodeRoot, createNodeClassName(), null, 1.0);
KiePMMLNodeFactory.JavaParserDTO toPopulate = new KiePMMLNodeFactory.JavaParserDTO(nodeNamesDTO, PACKAGE_NAME);
Node nestedNode = nodeRoot.getNodes().get(0);
// Creating evaluateNodeInitializer
String nestedNodeClassName = nodeNamesDTO.childrenNodes.get(nestedNode);
String fullNestedNodeClassName = String.format(PACKAGE_CLASS_TEMPLATE, PACKAGE_NAME, nestedNodeClassName);
final NodeList<Expression> methodReferenceExprs = NodeList.nodeList(KiePMMLNodeFactory.getEvaluateNodeMethodReference(fullNestedNodeClassName));
MethodReferenceExpr evaluateNodeReference = new MethodReferenceExpr();
evaluateNodeReference.setScope(new NameExpr(fullNestedNodeClassName));
evaluateNodeReference.setIdentifier(EVALUATE_NODE);
MethodCallExpr evaluateNodeInitializer = new MethodCallExpr();
evaluateNodeInitializer.setScope(new TypeExpr(parseClassOrInterfaceType(Arrays.class.getName())));
evaluateNodeInitializer.setName(AS_LIST);
evaluateNodeInitializer.setArguments(methodReferenceExprs);
//
KiePMMLNodeFactory.NodeNamesDTO nestedNodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(nestedNode, nodeNamesDTO.getNestedNodeClassName(nestedNode), nodeNamesDTO.nodeClassName, nodeNamesDTO.missingValuePenalty);
KiePMMLNodeFactory.mergeNodeReferences(toPopulate, nestedNodeNamesDTO, evaluateNodeInitializer);
MethodReferenceExpr retrieved = evaluateNodeInitializer.getArguments().get(0).asMethodReferenceExpr();
String expected = toPopulate.nodeClassName;
assertEquals(expected, retrieved.getScope().asNameExpr().toString());
expected = EVALUATE_NODE + nestedNodeClassName;
assertEquals(expected, retrieved.getIdentifier());
}
use of org.dmg.pmml.tree.Node in project drools by kiegroup.
the class KiePMMLTreeModelNodeASTFactoryTest method declareRulesFromRootIrisNode.
@Test
public void declareRulesFromRootIrisNode() {
Node rootNode = irisModel.getNode();
assertEquals("setosa", rootNode.getScore());
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
final List<Field<?>> fields = getFieldsFromDataDictionary(irisPmml.getDataDictionary());
DATA_TYPE targetType = getTargetFieldType(fields, irisModel);
KiePMMLDataDictionaryASTFactory.factory(fieldTypeMap).declareTypes(fields);
KiePMMLTreeModelNodeASTFactory.factory(fieldTypeMap, Collections.emptyList(), TreeModel.NoTrueChildStrategy.RETURN_NULL_PREDICTION, targetType).declareRulesFromRootNode(rootNode, "_setosa");
assertFalse(fieldTypeMap.isEmpty());
}
use of org.dmg.pmml.tree.Node in project drools by kiegroup.
the class KiePMMLTreeModelNodeASTFactoryTest method isFinalLeaf.
@Test
public void isFinalLeaf() {
Node node = new LeafNode();
DATA_TYPE targetType = DATA_TYPE.STRING;
KiePMMLTreeModelNodeASTFactory.factory(new HashMap<>(), Collections.emptyList(), TreeModel.NoTrueChildStrategy.RETURN_NULL_PREDICTION, targetType).isFinalLeaf(node);
assertTrue(KiePMMLTreeModelNodeASTFactory.factory(new HashMap<>(), Collections.emptyList(), TreeModel.NoTrueChildStrategy.RETURN_NULL_PREDICTION, targetType).isFinalLeaf(node));
node = new ClassifierNode();
assertTrue(KiePMMLTreeModelNodeASTFactory.factory(new HashMap<>(), Collections.emptyList(), TreeModel.NoTrueChildStrategy.RETURN_NULL_PREDICTION, targetType).isFinalLeaf(node));
node.addNodes(new LeafNode());
assertFalse(KiePMMLTreeModelNodeASTFactory.factory(new HashMap<>(), Collections.emptyList(), TreeModel.NoTrueChildStrategy.RETURN_NULL_PREDICTION, targetType).isFinalLeaf(node));
}
Aggregations