@Test public void addPredictorTerm() { String predictorName = "predictorName"; double coefficient = 23.12; String fieldRef = "fieldRef"; int arity = 3; PredictorTerm predictorTerm = PMMLModelTestUtils.getPredictorTerm(predictorName, coefficient, Collections.singletonList(fieldRef)); ClassOrInterfaceDeclaration tableTemplate = new ClassOrInterfaceDeclaration(); MethodDeclaration retrieved = KiePMMLRegressionTableRegressionFactory.addPredictorTerm(predictorTerm, tableTemplate, arity); String expected = String.format("evaluatePredictorTerm%d", arity); assertEquals(expected, retrieved.getNameAsString()); BlockStmt body = retrieved.getBody().get(); expected = String.format("{\n" + " final AtomicReference<Double> result = new AtomicReference<>(1.0);\n" + " List<String> fieldRefs = Arrays.asList(\"%s\");\n" + " for (String key : resultMap.keySet()) {\n" + " if (fieldRefs.contains(key)) {\n" + " result.set(result.get() * (Double) resultMap.get(key));\n" + " }\n" + " }\n" + " double coefficient = %s;\n" + " return result.get() * coefficient;\n" + "}", fieldRef, coefficient); assertEquals(expected, body.toString()); }
static MethodDeclaration addPredictorTerm(final PredictorTerm predictorTerm, 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("evaluatePredictor").get(0); final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); VariableDeclarator variableDeclarator = getVariableDeclarator(body, "fieldRefs") .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, "fieldRefs", body))); final List<Expression> nodeList = predictorTerm.getFieldRefs().stream() .map(fieldRef -> new StringLiteralExpr(fieldRef.getField().getValue())) .collect(Collectors.toList()); NodeList<Expression> expressions = NodeList.nodeList(nodeList); MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions); variableDeclarator.setInitializer(methodCallExpr); variableDeclarator = getVariableDeclarator(body, COEFFICIENT) .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, COEFFICIENT, body))); variableDeclarator.setInitializer(String.valueOf(predictorTerm.getCoefficient().doubleValue())); return addMethod(methodTemplate, tableTemplate, "evaluatePredictorTerm" + predictorArity); } catch (Exception e) { throw new KiePMMLInternalException(String.format("Failed to add PredictorTerm %s", predictorTerm), e); } }
KiePMMLRegressionTableRegressionFactory { static MethodDeclaration addPredictorTerm(final PredictorTerm predictorTerm, 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("evaluatePredictor").get(0); final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); VariableDeclarator variableDeclarator = getVariableDeclarator(body, "fieldRefs") .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, "fieldRefs", body))); final List<Expression> nodeList = predictorTerm.getFieldRefs().stream() .map(fieldRef -> new StringLiteralExpr(fieldRef.getField().getValue())) .collect(Collectors.toList()); NodeList<Expression> expressions = NodeList.nodeList(nodeList); MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions); variableDeclarator.setInitializer(methodCallExpr); variableDeclarator = getVariableDeclarator(body, COEFFICIENT) .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, COEFFICIENT, body))); variableDeclarator.setInitializer(String.valueOf(predictorTerm.getCoefficient().doubleValue())); return addMethod(methodTemplate, tableTemplate, "evaluatePredictorTerm" + predictorArity); } catch (Exception e) { throw new KiePMMLInternalException(String.format("Failed to add PredictorTerm %s", predictorTerm), e); } } }
KiePMMLRegressionTableRegressionFactory { static MethodDeclaration addPredictorTerm(final PredictorTerm predictorTerm, 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("evaluatePredictor").get(0); final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); VariableDeclarator variableDeclarator = getVariableDeclarator(body, "fieldRefs") .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, "fieldRefs", body))); final List<Expression> nodeList = predictorTerm.getFieldRefs().stream() .map(fieldRef -> new StringLiteralExpr(fieldRef.getField().getValue())) .collect(Collectors.toList()); NodeList<Expression> expressions = NodeList.nodeList(nodeList); MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions); variableDeclarator.setInitializer(methodCallExpr); variableDeclarator = getVariableDeclarator(body, COEFFICIENT) .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, COEFFICIENT, body))); variableDeclarator.setInitializer(String.valueOf(predictorTerm.getCoefficient().doubleValue())); return addMethod(methodTemplate, tableTemplate, "evaluatePredictorTerm" + predictorArity); } catch (Exception e) { throw new KiePMMLInternalException(String.format("Failed to add PredictorTerm %s", predictorTerm), e); } } private  KiePMMLRegressionTableRegressionFactory(); }
KiePMMLRegressionTableRegressionFactory { static MethodDeclaration addPredictorTerm(final PredictorTerm predictorTerm, 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("evaluatePredictor").get(0); final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); VariableDeclarator variableDeclarator = getVariableDeclarator(body, "fieldRefs") .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, "fieldRefs", body))); final List<Expression> nodeList = predictorTerm.getFieldRefs().stream() .map(fieldRef -> new StringLiteralExpr(fieldRef.getField().getValue())) .collect(Collectors.toList()); NodeList<Expression> expressions = NodeList.nodeList(nodeList); MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions); variableDeclarator.setInitializer(methodCallExpr); variableDeclarator = getVariableDeclarator(body, COEFFICIENT) .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, COEFFICIENT, body))); variableDeclarator.setInitializer(String.valueOf(predictorTerm.getCoefficient().doubleValue())); return addMethod(methodTemplate, tableTemplate, "evaluatePredictorTerm" + predictorArity); } catch (Exception e) { throw new KiePMMLInternalException(String.format("Failed to add PredictorTerm %s", predictorTerm), 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); }
KiePMMLRegressionTableRegressionFactory { static MethodDeclaration addPredictorTerm(final PredictorTerm predictorTerm, 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("evaluatePredictor").get(0); final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); VariableDeclarator variableDeclarator = getVariableDeclarator(body, "fieldRefs") .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, "fieldRefs", body))); final List<Expression> nodeList = predictorTerm.getFieldRefs().stream() .map(fieldRef -> new StringLiteralExpr(fieldRef.getField().getValue())) .collect(Collectors.toList()); NodeList<Expression> expressions = NodeList.nodeList(nodeList); MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions); variableDeclarator.setInitializer(methodCallExpr); variableDeclarator = getVariableDeclarator(body, COEFFICIENT) .orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, COEFFICIENT, body))); variableDeclarator.setInitializer(String.valueOf(predictorTerm.getCoefficient().doubleValue())); return addMethod(methodTemplate, tableTemplate, "evaluatePredictorTerm" + predictorArity); } catch (Exception e) { throw new KiePMMLInternalException(String.format("Failed to add PredictorTerm %s", predictorTerm), 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; }