use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class CommonCodegenUtilsTest method getVariableDeclarator.
@Test
public void getVariableDeclarator() {
final String variableName = "variableName";
final BlockStmt body = new BlockStmt();
assertFalse(CommonCodegenUtils.getVariableDeclarator(body, variableName).isPresent());
final VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(parseClassOrInterfaceType("String"), variableName);
body.addStatement(variableDeclarationExpr);
Optional<VariableDeclarator> retrieved = CommonCodegenUtils.getVariableDeclarator(body, variableName);
assertTrue(retrieved.isPresent());
VariableDeclarator variableDeclarator = retrieved.get();
assertEquals(variableName, variableDeclarator.getName().asString());
}
use of com.github.javaparser.ast.body.VariableDeclarator 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);
}
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLRegressionTableFactory method setStaticGetter.
// not-public code-generation
static void setStaticGetter(final RegressionTable regressionTable, final RegressionCompilationDTO compilationDTO, final MethodDeclaration staticGetterMethod, final String variableName) {
final BlockStmt regressionTableBody = staticGetterMethod.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, staticGetterMethod)));
final BlockStmt newBody = new BlockStmt();
// populate maps
String numericFunctionMapName = String.format(VARIABLE_NAME_TEMPLATE, NUMERIC_FUNCTION_MAP, variableName);
final Map<String, Expression> numericPredictorsMap = getNumericPredictorsExpressions(regressionTable.getNumericPredictors());
createPopulatedHashMap(newBody, numericFunctionMapName, Arrays.asList(String.class.getSimpleName(), "SerializableFunction<Double, Double>"), numericPredictorsMap);
final Map<String, Expression> categoricalPredictorFunctionsMap = getCategoricalPredictorsExpressions(regressionTable.getCategoricalPredictors(), newBody, variableName);
String categoricalFunctionMapName = String.format(VARIABLE_NAME_TEMPLATE, CATEGORICAL_FUNCTION_MAP, variableName);
createPopulatedHashMap(newBody, categoricalFunctionMapName, Arrays.asList(String.class.getSimpleName(), "SerializableFunction<String, " + "Double>"), categoricalPredictorFunctionsMap);
String predictorTermsFunctionMapName = String.format(VARIABLE_NAME_TEMPLATE, PREDICTOR_TERM_FUNCTION_MAP, variableName);
final Map<String, Expression> predictorTermsMap = getPredictorTermFunctions(regressionTable.getPredictorTerms());
createPopulatedHashMap(newBody, predictorTermsFunctionMapName, Arrays.asList(String.class.getSimpleName(), "SerializableFunction<Map" + "<String, " + "Object>, Double>"), predictorTermsMap);
final VariableDeclarator variableDeclarator = getVariableDeclarator(regressionTableBody, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, regressionTableBody)));
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, regressionTableBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(0, new StringLiteralExpr(variableName));
getChainedMethodCallExprFrom("withNumericFunctionMap", initializer).setArgument(0, new NameExpr(numericFunctionMapName) {
});
getChainedMethodCallExprFrom("withCategoricalFunctionMap", initializer).setArgument(0, new NameExpr(categoricalFunctionMapName));
getChainedMethodCallExprFrom("withPredictorTermsFunctionMap", initializer).setArgument(0, new NameExpr(predictorTermsFunctionMapName));
getChainedMethodCallExprFrom("withIntercept", initializer).setArgument(0, getExpressionForObject(regressionTable.getIntercept().doubleValue()));
getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, getExpressionForObject(compilationDTO.getTargetFieldName()));
getChainedMethodCallExprFrom("withTargetCategory", initializer).setArgument(0, getExpressionForObject(regressionTable.getTargetCategory()));
final Expression resultUpdaterExpression = getResultUpdaterExpression(compilationDTO.getDefaultNormalizationMethod());
getChainedMethodCallExprFrom("withResultUpdater", initializer).setArgument(0, resultUpdaterExpression);
regressionTableBody.getStatements().forEach(newBody::addStatement);
staticGetterMethod.setBody(newBody);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLRegressionModelFactory method setStaticGetter.
static void setStaticGetter(final CompilationDTO<RegressionModel> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String nestedTable) {
KiePMMLModelFactoryUtils.initStaticGetter(compilationDTO, modelTemplate);
final BlockStmt body = getMethodDeclarationBlockStmt(modelTemplate, GET_MODEL);
final VariableDeclarator variableDeclarator = getVariableDeclarator(body, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, body)));
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, body))).asMethodCallExpr();
MethodCallExpr methodCallExpr = new MethodCallExpr();
methodCallExpr.setScope(new NameExpr(nestedTable));
methodCallExpr.setName(GETKIEPMML_TABLE);
getChainedMethodCallExprFrom("withAbstractKiePMMLTable", initializer).setArgument(0, methodCallExpr);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLComplexPartialScoreFactory method getComplexPartialScoreVariableDeclaration.
static BlockStmt getComplexPartialScoreVariableDeclaration(final String variableName, final ComplexPartialScore complexPartialScore) {
final MethodDeclaration methodDeclaration = COMPLEX_PARTIAL_SCORE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPLEXPARTIALSCORE).get(0).clone();
final BlockStmt complexPartialScoreBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(complexPartialScoreBody, COMPLEX_PARTIAL_SCORE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPLEX_PARTIAL_SCORE, complexPartialScoreBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, 0);
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, complexPartialScore.getExpression());
toAdd.getStatements().forEach(toReturn::addStatement);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPLEX_PARTIAL_SCORE, toReturn))).asObjectCreationExpr();
objectCreationExpr.getArguments().set(0, new StringLiteralExpr(variableName));
objectCreationExpr.getArguments().set(2, new NameExpr(nestedVariableName));
complexPartialScoreBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
Aggregations