KiePMMLModelFactoryUtils { public static void addOutputFieldsPopulation(final BlockStmt body, final List<KiePMMLOutputField> outputFields) { for (KiePMMLOutputField outputField : outputFields) { NodeList<Expression> expressions = NodeList.nodeList(new StringLiteralExpr(outputField.getName()), new NameExpr("Collections.emptyList()")); MethodCallExpr builder = new MethodCallExpr(new NameExpr("KiePMMLOutputField"), "builder", expressions); if (outputField.getRank() != null) { expressions = NodeList.nodeList(new IntegerLiteralExpr(outputField.getRank())); builder = new MethodCallExpr(builder, "withRank", expressions); } if (outputField.getValue() != null) { expressions = NodeList.nodeList(new StringLiteralExpr(outputField.getValue().toString())); builder = new MethodCallExpr(builder, "withValue", expressions); } String targetField = outputField.getTargetField().orElse(null); if (targetField != null) { expressions = NodeList.nodeList(new StringLiteralExpr(targetField)); builder = new MethodCallExpr(builder, "withTargetField", expressions); } if (outputField.getResultFeature() != null) { expressions = NodeList.nodeList(new NameExpr(RESULT_FEATURE.class.getName() + "." + outputField.getResultFeature().toString())); builder = new MethodCallExpr(builder, "withResultFeature", expressions); } Expression newOutputField = new MethodCallExpr(builder, "build"); expressions = NodeList.nodeList(newOutputField); body.addStatement(new MethodCallExpr(new NameExpr("outputFields"), "add", expressions)); } } private  KiePMMLModelFactoryUtils(); static void setConstructorSuperNameInvocation(final String generatedClassName,
                                      final ConstructorDeclaration constructorDeclaration,
                                      final String name); static void addOutputFieldsPopulation(final BlockStmt body, final List<KiePMMLOutputField> outputFields); static void addTransformationsInClassOrInterfaceDeclaration(final ClassOrInterfaceDeclaration toPopulate, final TransformationDictionary transformationDictionary, final LocalTransformations localTransformations);  }
@Test public void addOutputFieldsPopulation() { BlockStmt blockStmt = new BlockStmt(); List<KiePMMLOutputField> outputFields = IntStream.range(0, 3) .mapToObj(index -> KiePMMLOutputField.builder("OUTPUTFIELD-" + index, Collections.emptyList()) .withRank(new Random().nextInt(3)) .withValue("VALUE-" + index) .withTargetField("TARGETFIELD-" + index) .build()) .collect(Collectors.toList()); KiePMMLModelFactoryUtils.addOutputFieldsPopulation(blockStmt, outputFields); List<MethodCallExpr> retrieved = getMethodCallExprList(blockStmt, outputFields.size(), "outputFields", "add"); for (KiePMMLOutputField outputField : outputFields) { assertTrue(retrieved.stream() .filter(methodCallExpr -> methodCallExpr.getArguments().size() == 1) .map(methodCallExpr -> methodCallExpr.getArgument(0)) .filter(Expression::isMethodCallExpr) .map(expressionArgument -> (MethodCallExpr) expressionArgument) .anyMatch(methodCallExpr -> evaluateOutputFieldPopulation(methodCallExpr, outputField))); } }