List<Node> children = root.getNodes(); if (children.isEmpty()) {
if (numDists == 0) { List<Node> children = node.getNodes(); assertEquals(2, children.size()); Node rightChild = children.get(0);
public Node addNodes(Node... nodes){ getNodes().addAll(Arrays.asList(nodes)); return this; }
public Node addNodes(Node... nodes){ getNodes().addAll(Arrays.asList(nodes)); return this; }
@Override public VisitorAction visit(Node node){ if(node.hasNodes()){ List<Node> nodes = node.getNodes(); Collections.sort(nodes, NodeSorter.COMPARATOR); } return super.visit(node); }
@Override public VisitorAction visit(Node node){ if(node.hasNodes()){ List<Node> nodes = node.getNodes(); Collections.sort(nodes, NodeSorter.COMPARATOR); } return super.visit(node); }
@Override public void enterNode(Node node){ if(node.hasNodes()){ List<Node> children = node.getNodes(); children: while(true){ ListIterator<Node> childIt = children.listIterator(); grandChildren: while(childIt.hasNext()){ Node child = childIt.next(); Iterator<Node> grandChildIt = getChildren(child); if(grandChildIt == null){ continue grandChildren; } childIt.remove(); while(grandChildIt.hasNext()){ Node grandChild = grandChildIt.next(); grandChildIt.remove(); childIt.add(grandChild); } childIt.add(child); continue children; } break; } } }
private ImmutableBiMap.Builder<String, Node> collectNodes(Node node, AtomicInteger index, ImmutableBiMap.Builder<String, Node> builder){ builder = EntityUtil.put(node, index, builder); if(!node.hasNodes()){ return builder; } List<Node> children = node.getNodes(); for(Node child : children){ builder = collectNodes(child, index, builder); } return builder; } });
private Trail handleDefaultChild(Trail trail, Node node, EvaluationContext context){ // "The defaultChild missing value strategy requires the presence of the defaultChild attribute in every non-leaf Node" String defaultChild = node.getDefaultChild(); if(defaultChild == null){ throw new MissingAttributeException(node, PMMLAttributes.NODE_DEFAULTCHILD); } trail.addMissingLevel(); List<Node> children = node.getNodes(); for(int i = 0, max = children.size(); i < max; i++){ Node child = children.get(i); String id = child.getId(); if(id != null && (id).equals(defaultChild)){ // The predicate of the referenced Node is not evaluated return handleTrue(trail, child, context); } } // "Only Nodes which are immediate children of the respective Node can be referenced" throw new InvalidAttributeException(node, PMMLAttributes.NODE_DEFAULTCHILD, defaultChild); }
@Override public void exitNode(Node node){ Predicate predicate = node.getPredicate(); if(predicate instanceof True){ Node parentNode = getParentNode(); if(parentNode == null){ return; } List<Node> parentChildren = parentNode.getNodes(); if(parentChildren.size() != 1){ return; } boolean success = parentChildren.remove(node); if(!success){ throw new IllegalArgumentException(); } // End if if((MiningFunction.REGRESSION).equals(this.miningFunction)){ initScore(parentNode, node); } else if((MiningFunction.CLASSIFICATION).equals(this.miningFunction)){ initScoreDistribution(parentNode, node); } else { throw new IllegalArgumentException(); } } }
private Trail handleTrue(Trail trail, Node node, EvaluationContext context){ // A "true" leaf node if(!node.hasNodes()){ return trail.selectNode(node); } trail.push(node); List<Node> children = node.getNodes(); for(int i = 0, max = children.size(); i < max; i++){ Node child = children.get(i); Boolean status = evaluateNode(trail, child, context); if(status == null){ Trail destination = handleMissingValue(trail, node, child, context); if(destination != null){ return destination; } } else if(status.booleanValue()){ return handleTrue(trail, child, context); } } // A "true" non-leaf node return handleNoTrueChild(trail); }
List<Node> children = node.getNodes();
List<Node> children = node.getNodes();
static public ComplexNode toComplexNode(Node node){ ComplexNode result = new ComplexNode() .setId(node.getId()) .setScore(node.getScore()) .setRecordCount(node.getRecordCount()) .setDefaultChild(node.getDefaultChild()) .setPredicate(node.getPredicate()); if(node.hasNodes()){ (result.getNodes()).addAll(node.getNodes()); } // End if if(node.hasScoreDistributions()){ (result.getScoreDistributions()).addAll(node.getScoreDistributions()); } return result; } }
@Test public void transform(){ Node node1a = new BranchNode(); Node node2a = new BranchNode(); Node node2b = new LeafNode(); node1a.addNodes(node2a, node2b); Node node3a = new LeafNode(); node2a.addNodes(node3a); assertTrue(node1a.getNodes() instanceof ArrayList); assertTrue(node2a.getNodes() instanceof ArrayList); TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a); ArrayListTransformer transformer = new ArrayListTransformer(); transformer.applyTo(treeModel); assertTrue(node1a.getNodes() instanceof DoubletonList); assertTrue(node2a.getNodes() instanceof SingletonList); } }
(value.getNodes()).addAll(node.getNodes());
(value.getNodes()).addAll(node.getNodes());
@Override public Node unmarshal(ComplexNode value){ if(value.getRecordCount() != null){ return value; } // End if if(value.hasExtensions() || (value.getPartition() != null) || value.hasScoreDistributions() || (value.getEmbeddedModel() != null)){ return value; } Node node; if(value.hasNodes()){ node = new BranchNode() .setId(value.getId()) .setDefaultChild(value.getDefaultChild()); (node.getNodes()).addAll(value.getNodes()); } else { node = new LeafNode() .setId(value.getId()); } node .setScore(value.getScore()) .setPredicate(value.getPredicate()); return node; }
@Override public Node unmarshal(ComplexNode value){ if(value.getRecordCount() != null){ return value; } // End if if(value.hasExtensions() || (value.getPartition() != null) || value.hasScoreDistributions() || (value.getEmbeddedModel() != null)){ return value; } Node node; if(value.hasNodes()){ node = new BranchNode() .setId(value.getId()) .setDefaultChild(value.getDefaultChild()); (node.getNodes()).addAll(value.getNodes()); } else { node = new LeafNode() .setId(value.getId()); } node .setScore(value.getScore()) .setPredicate(value.getPredicate()); return node; }