use of org.kie.pmml.commons.Constants.MISSING_BODY_TEMPLATE in project drools by kiegroup.
the class KiePMMLRegressionTableFactory method getPredictorTermBody.
/**
* Add a <b>PredictorTerm</b> <code>MethodDeclaration</code> to the class
* @param predictorTerm
* @return
*/
static BlockStmt getPredictorTermBody(final PredictorTerm predictorTerm) {
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 methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName())));
} catch (Exception e) {
throw new KiePMMLInternalException(String.format("Failed to add PredictorTerm %s", predictorTerm), e);
}
}
Aggregations