ExpressionFunctionUtils { static BlockStmt getApplyExpressionBlockStmt(final String variableName, final Apply apply, final ClassOrInterfaceType returnedType, final LinkedHashMap<String, ClassOrInterfaceType> parameterNameTypeMap) { final BlockStmt toReturn = new BlockStmt(); List<String> innerVariables = new ArrayList<>(); innerVariables.add(KIEPMMLNAMEVALUE_LIST_PARAM); final ClassOrInterfaceType objectReturnedType = parseClassOrInterfaceType(Object.class.getName()); if (apply.getExpressions() != null) { int counter = 1; for (Expression expression : apply.getExpressions()) { String innerVariable = String.format(INNER_VARIABLE_NAME, variableName, expression.getClass().getSimpleName(), counter); BlockStmt innerBlockStmt = getExpressionBlockStmt(innerVariable, expression, objectReturnedType, parameterNameTypeMap); toReturn.getStatements().addAll(innerBlockStmt.getStatements()); innerVariables.add(innerVariable); counter++; } } MethodCallExpr functionMethodCall = new MethodCallExpr(); functionMethodCall.setScope(new ThisExpr()); functionMethodCall.setName(apply.getFunction()); NodeList<com.github.javaparser.ast.expr.Expression> functionCallArguments = NodeList.nodeList(innerVariables.stream().map(NameExpr::new).collect(Collectors.toList())); functionMethodCall.setArguments(functionCallArguments); VariableDeclarator variableDeclarator = new VariableDeclarator(); variableDeclarator.setType(returnedType); variableDeclarator.setName(variableName); variableDeclarator.setInitializer(functionMethodCall); VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(); variableDeclarationExpr.setVariables(NodeList.nodeList(variableDeclarator)); toReturn.addStatement(variableDeclarationExpr); return toReturn; } private  ExpressionFunctionUtils();   }
@Test public void getApplyExpressionBlockStmt() { Apply apply = applySupplier.get(); String variableName = "VARIABLE_NAME"; BlockStmt retrieved = ExpressionFunctionUtils.getApplyExpressionBlockStmt(variableName, apply, parseClassOrInterfaceType(Object.class.getName()), DEFAULT_PARAMETERTYPE_MAP); String expected = "{\n" + " java.lang.Object variableVARIABLE_NAMEConstant1 = 34.6;\n" + " java.util.Optional<org.kie.pmml.commons.model.tuples.KiePMMLNameValue> kiePMMLNameValue = param1" + ".stream().filter((org.kie.pmml.commons.model.tuples.KiePMMLNameValue lmbdParam) -> java.util.Objects" + ".equals(\"FIELD_REF\", lmbdParam.getName())).findFirst();\n" + " java.lang.Object variableVARIABLE_NAMEFieldRef2 = (java.lang.Object) kiePMMLNameValue.map(org" + ".kie.pmml.commons.model.tuples.KiePMMLNameValue::getValue).orElse(null);\n" + " java.lang.Object VARIABLE_NAME = this.FUNCTION_NAME(param1, variableVARIABLE_NAMEConstant1, " + "variableVARIABLE_NAMEFieldRef2);\n" + "}"; assertEquals(expected, retrieved.toString()); }
@Test public void getApplyExpressionNestedBlockStmt() { Apply nestedApply = applySupplier.get(); Constant constant = new Constant(); constant.setDataType(DataType.STRING); constant.setValue("STRING_VALUE"); String functionName = "EXTERNAL_FUNCTION_NAME"; Apply apply = new Apply(); apply.addExpressions(constant, nestedApply); apply.setFunction(functionName); String variableName = "VARIABLE_NAME"; BlockStmt retrieved = ExpressionFunctionUtils.getApplyExpressionBlockStmt(variableName, apply, parseClassOrInterfaceType(Object.class.getName()), DEFAULT_PARAMETERTYPE_MAP); String expected = "{\n" + " java.lang.Object variableVARIABLE_NAMEConstant1 = \"STRING_VALUE\";\n" + " java.lang.Object variablevariableVARIABLE_NAMEApply2Constant1 = 34.6;\n" + " java.util.Optional<org.kie.pmml.commons.model.tuples.KiePMMLNameValue> kiePMMLNameValue = param1" + ".stream().filter((org.kie.pmml.commons.model.tuples.KiePMMLNameValue lmbdParam) -> java.util.Objects" + ".equals(\"FIELD_REF\", lmbdParam.getName())).findFirst();\n" + " java.lang.Object variablevariableVARIABLE_NAMEApply2FieldRef2 = (java.lang.Object) " + "kiePMMLNameValue.map(org.kie.pmml.commons.model.tuples.KiePMMLNameValue::getValue).orElse(null);\n" + " java.lang.Object variableVARIABLE_NAMEApply2 = this.FUNCTION_NAME(param1, " + "variablevariableVARIABLE_NAMEApply2Constant1, variablevariableVARIABLE_NAMEApply2FieldRef2);\n" + " java.lang.Object VARIABLE_NAME = this.EXTERNAL_FUNCTION_NAME(param1, " + "variableVARIABLE_NAMEConstant1, variableVARIABLE_NAMEApply2);\n" + "}"; assertEquals(expected, retrieved.toString()); }