ExpressionFunctionUtils { static BlockStmt getFieldRefExpressionFromCommonDataBlockStmt(final String variableName, final FieldRef fieldRef, final ClassOrInterfaceType returnedType, final LinkedHashMap<String, ClassOrInterfaceType> parameterNameTypeMap) { final BlockStmt toReturn = new BlockStmt(); String fieldNameToRef = fieldRef.getField().getValue(); toReturn.addStatement(getFilteredKiePMMLNameValueExpression(KIEPMMLNAMEVALUE_LIST_PARAM, fieldNameToRef, true)); MethodReferenceExpr methodReferenceExpr = new MethodReferenceExpr(); methodReferenceExpr.setScope(new TypeExpr(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()))); methodReferenceExpr.setIdentifier("getValue"); MethodCallExpr expressionScope = new MethodCallExpr("map"); expressionScope.setScope(new NameExpr(OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME)); expressionScope.setArguments(NodeList.nodeList(methodReferenceExpr)); MethodCallExpr expression = new MethodCallExpr("orElse"); expression.setScope(expressionScope); com.github.javaparser.ast.expr.Expression orElseExpression = fieldRef.getMapMissingTo() != null ? new StringLiteralExpr(fieldRef.getMapMissingTo()) : new NullLiteralExpr(); expression.setArguments(NodeList.nodeList(orElseExpression)); CastExpr initializer = new CastExpr(); initializer.setType(returnedType); initializer.setExpression(expression); VariableDeclarator variableDeclarator = new VariableDeclarator(); variableDeclarator.setType(returnedType); variableDeclarator.setName(variableName); variableDeclarator.setInitializer(initializer); VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(); variableDeclarationExpr.setVariables(NodeList.nodeList(variableDeclarator)); toReturn.addStatement(variableDeclarationExpr); return toReturn; } private  ExpressionFunctionUtils();   }
@Test public void getFieldRefExpressionFromCommonDataBlockStmt() { String variableName = "VARIABLE_NAME"; FieldRef fieldRef = fieldRefSupplier.get(); BlockStmt retrieved = ExpressionFunctionUtils.getFieldRefExpressionFromCommonDataBlockStmt(variableName, fieldRef, parseClassOrInterfaceType(Object.class.getName()), DEFAULT_PARAMETERTYPE_MAP); String expected = "{\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 VARIABLE_NAME = (java.lang.Object) kiePMMLNameValue.map(org.kie.pmml.commons" + ".model.tuples.KiePMMLNameValue::getValue).orElse(null);\n" + "}"; assertEquals(expected, retrieved.toString()); String mapMissingTo = "MAP_MISSING_TO"; fieldRef.setMapMissingTo(mapMissingTo); retrieved = ExpressionFunctionUtils.getFieldRefExpressionFromCommonDataBlockStmt(variableName, fieldRef, parseClassOrInterfaceType(Object.class.getName()), DEFAULT_PARAMETERTYPE_MAP); expected = "{\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 VARIABLE_NAME = (java.lang.Object) kiePMMLNameValue.map(org.kie.pmml.commons" + ".model.tuples.KiePMMLNameValue::getValue).orElse(\"MAP_MISSING_TO\");\n" + "}"; assertEquals(expected, retrieved.toString()); }