KiePMMLRegressionTableRegressionFactory { static MethodDeclaration addGroupedCategoricalPredictor(final List<CategoricalPredictor> categoricalPredictors, final ClassOrInterfaceDeclaration tableTemplate, int predictorArity) { try { templateEvaluate = getFromFileName(KIE_PMML_EVALUATE_METHOD_TEMPLATE_JAVA); cloneEvaluate = templateEvaluate.clone(); ClassOrInterfaceDeclaration evaluateTemplateClass = cloneEvaluate.getClassByName(KIE_PMML_EVALUATE_METHOD_TEMPLATE) .orElseThrow(() -> new RuntimeException(MAIN_CLASS_NOT_FOUND)); MethodDeclaration methodTemplate = evaluateTemplateClass.getMethodsByName("evaluateCategorical").get(0); final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); IfStmt ifStmt = new IfStmt(); for (int i = 0; i < categoricalPredictors.size(); i++) { CategoricalPredictor categoricalPredictor = categoricalPredictors.get(i); Expression lhe; if (categoricalPredictor.getValue() instanceof String) { lhe = new StringLiteralExpr((String) categoricalPredictor.getValue()); } else { lhe = new NameExpr(categoricalPredictor.getValue().toString()); } NodeList<Expression> expressions = NodeList.nodeList(lhe, new NameExpr("input")); MethodCallExpr conditionExpr = new MethodCallExpr(new NameExpr("Objects"), "equals", expressions); if (i == 0) { ifStmt.setCondition(conditionExpr); ifStmt.setThenStmt(new ReturnStmt(new DoubleLiteralExpr(String.valueOf(categoricalPredictor.getCoefficient())))); body.addStatement(ifStmt); } else { IfStmt elseIf = new IfStmt(); elseIf.setCondition(conditionExpr); elseIf.setThenStmt(new ReturnStmt(new DoubleLiteralExpr(String.valueOf(categoricalPredictor.getCoefficient())))); ifStmt.setElseStmt(elseIf); ifStmt = elseIf; } } ifStmt.setElseStmt(new ReturnStmt(new DoubleLiteralExpr("0.0"))); return addMethod(methodTemplate, tableTemplate, "evaluateCategoricalPredictor" + predictorArity); } catch (Exception e) { throw new KiePMMLInternalException("Failed to add CategoricalPredictors", e); } } private  KiePMMLRegressionTableRegressionFactory(); static LinkedHashMap<String, KiePMMLTableSourceCategory> getRegressionTables(final List<RegressionTable> regressionTables,
                                                                                        final RegressionModel.NormalizationMethod normalizationMethod,
                                                                                        final List<KiePMMLOutputField> outputFields,
                                                                                        final String targetField,
                                                                                        final String packageName); static Map.Entry<String, String> getRegressionTable(final RegressionTable regressionTable,
                                                               final RegressionModel.NormalizationMethod normalizationMethod,
                                                               final List<KiePMMLOutputField> outputFields,
                                                               final String targetField,
                                                               final String packageName); static final String KIE_PMML_REGRESSION_TABLE_REGRESSION_TEMPLATE_JAVA; static final String KIE_PMML_REGRESSION_TABLE_REGRESSION_TEMPLATE; }
@Test public void addGroupedCategoricalPredictor() { final List<CategoricalPredictor> categoricalPredictors = IntStream.range(0, 3).mapToObj(index -> { String predictorName = "predictorName-" + index; double coefficient = 1.23 * index; return PMMLModelTestUtils.getCategoricalPredictor(predictorName, index, coefficient); }).collect(Collectors.toList()); ClassOrInterfaceDeclaration tableTemplate = new ClassOrInterfaceDeclaration(); int arity = 3; MethodDeclaration retrieved = KiePMMLRegressionTableRegressionFactory.addGroupedCategoricalPredictor(categoricalPredictors, tableTemplate, arity); String expected = String.format("evaluateCategoricalPredictor%d", arity); assertEquals(expected, retrieved.getNameAsString()); BlockStmt body = retrieved.getBody().get(); expected = String.format("{\n" + " if (Objects.equals(%s, input))\n" + " return %s;\n" + " else if (Objects.equals(%s, input))\n" + " return %s;\n" + " else if (Objects.equals(%s, input))\n" + " return %s;\n" + " else\n" + " return 0.0;\n" + "}", categoricalPredictors.get(0).getValue(), categoricalPredictors.get(0).getCoefficient(), categoricalPredictors.get(1).getValue(), categoricalPredictors.get(1).getCoefficient(), categoricalPredictors.get(2).getValue(), categoricalPredictors.get(2).getCoefficient() ); assertEquals(expected, body.toString()); }