Java Code Examples for org.dmg.pmml.tree.Node#getNodes()

The following examples show how to use org.dmg.pmml.tree.Node#getNodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeResolver.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(Node node){
	Object defaultChild = node.getDefaultChild();

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		for(int i = 0, max = children.size(); i < max; i++){
			Node child = children.get(i);

			Object id = child.getId();
			if(id != null && (id).equals(defaultChild)){
				node.setDefaultChild(child);

				break;
			}
		}
	}

	return super.visit(node);
}
 
Example 2
Source File: TreeModelFlattener.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@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;
		}
	}
}
 
Example 3
Source File: TreeModelFlattener.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@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)){
			parentNode.setScore(null);

			initScore(parentNode, node);
		} else

		if((MiningFunction.CLASSIFICATION).equals(this.miningFunction)){
			initScoreDistribution(parentNode, node);
		} else

		{
			throw new IllegalArgumentException();
		}
	}
}
 
Example 4
Source File: TreeModelFlattener.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Iterator<Node> getChildren(Node node){
	Predicate predicate = node.getPredicate();

	if(!(predicate instanceof SimplePredicate)){
		return null;
	}

	SimplePredicate simplePredicate = (SimplePredicate)predicate;

	if(!hasOperator(simplePredicate, SimplePredicate.Operator.LESS_OR_EQUAL)){
		return null;
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		int endPos = 0;

		for(Node child : children){
			Predicate childPredicate = child.getPredicate();

			if(!hasFieldReference(childPredicate, simplePredicate.getField()) || !hasOperator(childPredicate, simplePredicate.getOperator())){
				break;
			}

			endPos++;
		}

		if(endPos > 0){
			return (children.subList(0, endPos)).iterator();
		}

		return null;
	}

	return null;
}
 
Example 5
Source File: RDFUpdateIT.java    From oryx with Apache License 2.0 5 votes vote down vote up
private static void checkNode(Node node) {
  assertNotNull(node.getId());
  if (!node.hasScoreDistributions()) {
    // Non-leaf
    List<Node> children = node.getNodes();
    assertEquals(2, children.size());
    Node rightChild = children.get(0);
    Node leftChild = children.get(1);
    assertInstanceOf(leftChild.getPredicate(), True.class);
    assertEquals(node.getRecordCount().intValue(),
                 leftChild.getRecordCount().intValue() + rightChild.getRecordCount().intValue());
    assertEquals(node.getId() + "+", rightChild.getId());
    assertEquals(node.getId() + "-", leftChild.getId());
    checkNode(rightChild);
    checkNode(leftChild);
  } else {
    // Leaf
    List<ScoreDistribution> scoreDists = node.getScoreDistributions();
    int numDists = scoreDists.size();
    assertRange(numDists, 1, 2);
    ScoreDistribution first = scoreDists.get(0);
    if (numDists == 1) {
      assertEquals(1.0, first.getConfidence().doubleValue());
    } else {
      assertGreater(first.getConfidence().doubleValue(), 0.0);
      assertLess(first.getConfidence().doubleValue(), 1.0);
      ScoreDistribution second = scoreDists.get(1);
      assertGreater(second.getConfidence().doubleValue(), 0.0);
      assertLess(second.getConfidence().doubleValue(), 1.0);
    }
  }
}
 
Example 6
Source File: TreeModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
	}
 
Example 7
Source File: TreeModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: TreeModelCompactor.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void enterNode(Node node){
	Object id = node.getId();
	Object score = node.getScore();
	Object defaultChild = node.getDefaultChild();

	if(id == null){
		throw new IllegalArgumentException();
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		if(children.size() != 2 || score != null || defaultChild == null){
			throw new IllegalArgumentException();
		}

		Node firstChild = children.get(0);
		Node secondChild = children.get(1);

		if(equalsNode(defaultChild, firstChild)){
			children = swapChildren(node);

			firstChild = children.get(0);
			secondChild = children.get(1);
		} else

		if(equalsNode(defaultChild, secondChild)){
			// Ignored
		} else

		{
			throw new IllegalArgumentException();
		}

		node.setDefaultChild(null);

		secondChild.setPredicate(True.INSTANCE);
	} else

	{
		if(score == null || defaultChild != null){
			throw new IllegalArgumentException();
		}
	}

	node.setId(null);
}
 
Example 9
Source File: TreeModelCompactor.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void enterNode(Node node){
	Object id = node.getId();
	Object score = node.getScore();

	if(id != null){
		throw new IllegalArgumentException();
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		if(children.size() != 2 || score != null){
			throw new IllegalArgumentException();
		}

		Node firstChild = children.get(0);
		Node secondChild = children.get(1);

		Predicate firstPredicate = firstChild.getPredicate();
		Predicate secondPredicate = secondChild.getPredicate();

		checkFieldReference(firstPredicate, secondPredicate);

		boolean update = true;

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			update = isCategoricalField((SimplePredicate)firstPredicate);
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.NOT_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			children = swapChildren(node);

			firstChild = children.get(0);
			secondChild = children.get(1);
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.NOT_EQUAL)){
			// Ignored
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){
			// Ignored
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			addCategoricalField(secondChild);
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			children = swapChildren(node);

			firstChild = children.get(0);
			secondChild = children.get(1);

			addCategoricalField(secondChild);
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			addCategoricalField(secondChild);
		} else

		{
			throw new IllegalArgumentException();
		} // End if

		if(update){
			secondChild.setPredicate(True.INSTANCE);
		}
	} else

	{
		if(score == null){
			throw new IllegalArgumentException();
		}
	}
}
 
Example 10
Source File: TreeModelCompactor.java    From jpmml-xgboost with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void enterNode(Node node){
	Object id = node.getId();
	Object score = node.getScore();
	Object defaultChild = node.getDefaultChild();

	if(id == null){
		throw new IllegalArgumentException();
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		if(children.size() != 2 || score != null || defaultChild == null){
			throw new IllegalArgumentException();
		}

		Node firstChild = children.get(0);
		Node secondChild = children.get(1);

		if(equalsNode(defaultChild, firstChild)){
			children = swapChildren(node);

			firstChild = children.get(0);
			secondChild = children.get(1);
		} else

		if(equalsNode(defaultChild, secondChild)){
			// Ignored
		} else

		{
			throw new IllegalArgumentException();
		}

		node.setDefaultChild(null);

		secondChild.setPredicate(True.INSTANCE);
	} else

	{
		if(score == null || defaultChild != null){
			throw new IllegalArgumentException();
		}
	}

	node.setId(null);
}
 
Example 11
Source File: TreeModelCompactor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void enterNode(Node node){
	Object id = node.getId();
	Object score = node.getScore();

	if(id == null){
		throw new IllegalArgumentException();
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		if(children.size() != 2){
			throw new IllegalArgumentException();
		}

		Node firstChild = children.get(0);
		Node secondChild = children.get(1);

		Predicate firstPredicate = firstChild.getPredicate();
		Predicate secondPredicate = secondChild.getPredicate();

		checkFieldReference(firstPredicate, secondPredicate);
		checkValue(firstPredicate, secondPredicate);

		if(hasOperator(firstPredicate, SimplePredicate.Operator.NOT_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			children = swapChildren(node);

			firstChild = children.get(0);
			secondChild = children.get(1);
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){
			// Ignored
		} else

		{
			throw new IllegalArgumentException();
		}

		secondChild.setPredicate(True.INSTANCE);
	} else

	{
		if(score == null){
			throw new IllegalArgumentException();
		}
	}

	node.setId(null);
}
 
Example 12
Source File: RandomForestCompactor.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void enterNode(Node node){
	Object id = node.getId();
	Object score = node.getScore();

	if(id == null){
		throw new IllegalArgumentException();
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		if(children.size() != 2 || score != null){
			throw new IllegalArgumentException();
		}

		Node firstChild = children.get(0);
		Node secondChild = children.get(1);

		Predicate firstPredicate = firstChild.getPredicate();
		Predicate secondPredicate = secondChild.getPredicate();

		checkFieldReference(firstPredicate, secondPredicate);

		boolean update = isDefinedField((HasFieldReference<?>)firstPredicate);

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			// Ignored
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){
			update = true;
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			// Ignored
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){

			if(update){
				children = swapChildren(node);

				firstChild = children.get(0);
				secondChild = children.get(1);
			}
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			// Ignored
		} else

		{
			throw new IllegalArgumentException();
		} // End if

		if(update){
			secondChild.setPredicate(True.INSTANCE);
		}
	} else

	{
		if(score == null){
			throw new IllegalArgumentException();
		}
	}

	node.setId(null);
}