Search in sources :

Example 36 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class KiePMMLNodeFactoryTest method populateEvaluateNodeWithMissingValuePenalty.

@Test
public void populateEvaluateNodeWithMissingValuePenalty() {
    final BlockStmt toPopulate = new BlockStmt();
    final VariableDeclarator variableDeclarator = new VariableDeclarator();
    variableDeclarator.setType("double");
    variableDeclarator.setName(MISSING_VALUE_PENALTY);
    toPopulate.addStatement(new VariableDeclarationExpr(variableDeclarator));
    assertFalse(variableDeclarator.getInitializer().isPresent());
    final double missingValuePenalty = new Random().nextDouble();
    KiePMMLNodeFactory.populateEvaluateNodeWithMissingValuePenalty(toPopulate, missingValuePenalty);
    assertTrue(variableDeclarator.getInitializer().isPresent());
    Expression expression = variableDeclarator.getInitializer().get();
    assertTrue(expression instanceof DoubleLiteralExpr);
    DoubleLiteralExpr doubleLiteralExpr = (DoubleLiteralExpr) expression;
    assertEquals(missingValuePenalty, doubleLiteralExpr.asDouble(), 0.0);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Random(java.util.Random) Expression(com.github.javaparser.ast.expr.Expression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 37 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class KiePMMLNodeFactoryTest method populateEvaluateNodeWithScore.

@Test
public void populateEvaluateNodeWithScore() {
    final BlockStmt toPopulate = new BlockStmt();
    final VariableDeclarator variableDeclarator = new VariableDeclarator();
    variableDeclarator.setType("Object");
    variableDeclarator.setName(SCORE);
    toPopulate.addStatement(new VariableDeclarationExpr(variableDeclarator));
    assertFalse(variableDeclarator.getInitializer().isPresent());
    // null score
    Object score = null;
    KiePMMLNodeFactory.populateEvaluateNodeWithScore(toPopulate, score);
    commonVerifyEvaluateNodeWithScore(variableDeclarator, score);
    // string score
    score = "scoreValue";
    KiePMMLNodeFactory.populateEvaluateNodeWithScore(toPopulate, score);
    commonVerifyEvaluateNodeWithScore(variableDeclarator, score);
    // not-string score
    score = 54345.34;
    KiePMMLNodeFactory.populateEvaluateNodeWithScore(toPopulate, score);
    commonVerifyEvaluateNodeWithScore(variableDeclarator, score);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 38 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class KiePMMLRegressionTableFactory method populateWithGroupedCategoricalPredictorMap.

/**
 * Populate the given <b>body</b> with the creation of a <code>Map</code> for the given <b>categoricalPredictors</b>
 * @param categoricalPredictors
 * @param toPopulate
 * @param categoricalPredictorMapName
 * @return
 */
static void populateWithGroupedCategoricalPredictorMap(final List<CategoricalPredictor> categoricalPredictors, final BlockStmt toPopulate, final String categoricalPredictorMapName) {
    final VariableDeclarator categoricalMapDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Map.class.getName(), Arrays.asList(String.class.getSimpleName(), Double.class.getSimpleName())), categoricalPredictorMapName);
    final ObjectCreationExpr categoricalMapInitializer = new ObjectCreationExpr();
    categoricalMapInitializer.setType(getTypedClassOrInterfaceTypeByTypeNames(HashMap.class.getName(), Arrays.asList(String.class.getSimpleName(), Double.class.getSimpleName())));
    categoricalMapDeclarator.setInitializer(categoricalMapInitializer);
    final VariableDeclarationExpr categoricalMapDeclarationExpr = new VariableDeclarationExpr(categoricalMapDeclarator);
    toPopulate.addStatement(categoricalMapDeclarationExpr);
    final Map<String, Expression> mapExpressions = new LinkedHashMap<>();
    categoricalPredictors.forEach(categoricalPredictor -> mapExpressions.put(categoricalPredictor.getValue().toString(), getExpressionForObject(categoricalPredictor.getCoefficient().doubleValue())));
    addMapPopulationExpressions(mapExpressions, toPopulate, categoricalPredictorMapName);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)38 Test (org.junit.Test)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)16 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 Expression (com.github.javaparser.ast.expr.Expression)11 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 Parameter (com.github.javaparser.ast.body.Parameter)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 Type (com.github.javaparser.ast.type.Type)4 Node (com.github.javaparser.ast.Node)3 CastExpr (com.github.javaparser.ast.expr.CastExpr)3 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)3 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)2