Search in sources :

Example 96 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class KiePMMLNormDiscreteFactory method getNormDiscreteVariableDeclaration.

static BlockStmt getNormDiscreteVariableDeclaration(final String variableName, final NormDiscrete normDiscrete) {
    final MethodDeclaration methodDeclaration = NORMDISCRETE_TEMPLATE.getMethodsByName(GETKIEPMMLNORMDISCRETE).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, NORM_DISCRETE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, NORM_DISCRETE, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NORM_DISCRETE, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(normDiscrete.getField().getValue());
    final Expression mapMissingToExpr = getExpressionForObject(normDiscrete.getMapMissingTo());
    objectCreationExpr.getArguments().set(0, nameExpr);
    objectCreationExpr.getArguments().set(2, new StringLiteralExpr((String) normDiscrete.getValue()));
    objectCreationExpr.getArguments().set(3, mapMissingToExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 97 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class KiePMMLNodeFactory method mergeNode.

/**
 * Adjust the <b>evaluateNode(?)</b> references to the ones declared in the given
 * <code>JavaParserDTO.nodeTemplate</code>
 *
 * @param toPopulate
 * @param nestedNodeNamesDTO
 */
static void mergeNode(final JavaParserDTO toPopulate, final NodeNamesDTO nestedNodeNamesDTO) {
    final MethodCallExpr evaluateNodeInitializer;
    // node
    if (Objects.equals(toPopulate.nodeClassName, nestedNodeNamesDTO.parentNodeClassName)) {
        evaluateNodeInitializer = toPopulate.evaluateRootNodeReferencesDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NODE_FUNCTIONS, toPopulate.evaluateRootNodeReferencesDeclarator))).asMethodCallExpr();
    } else {
        String expected = EVALUATE_NODE + nestedNodeNamesDTO.parentNodeClassName;
        final MethodDeclaration evaluateNestedNodeMethod = toPopulate.nodeTemplate.getMethodsByName(expected).get(0);
        final BlockStmt evaluateNestedNodeBody = evaluateNestedNodeMethod.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_IN_METHOD, expected)));
        final VariableDeclarator nestedNodeVariableDeclarator = evaluateNestedNodeBody.findAll(VariableDeclarator.class).stream().filter(variableDeclarator -> variableDeclarator.getName().asString().equals(NODE_FUNCTIONS)).findFirst().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, expected, evaluateNestedNodeBody)));
        evaluateNodeInitializer = nestedNodeVariableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NODE_FUNCTIONS, nestedNodeVariableDeclarator))).asMethodCallExpr();
    }
    mergeNodeReferences(toPopulate, nestedNodeNamesDTO, evaluateNodeInitializer);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 98 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class KiePMMLNodeFactoryTest method populateEvaluateNodeWithScoreDistributions.

@Test
public void populateEvaluateNodeWithScoreDistributions() {
    final BlockStmt toPopulate = new BlockStmt();
    final VariableDeclarator variableDeclarator = new VariableDeclarator();
    variableDeclarator.setType("List");
    variableDeclarator.setName(SCORE_DISTRIBUTIONS);
    toPopulate.addStatement(new VariableDeclarationExpr(variableDeclarator));
    assertFalse(variableDeclarator.getInitializer().isPresent());
    // Without probability
    List<ScoreDistribution> scoreDistributions = getRandomPMMLScoreDistributions(false);
    KiePMMLNodeFactory.populateEvaluateNodeWithScoreDistributions(toPopulate, scoreDistributions);
    commonVerifyEvaluateNodeWithScoreDistributions(variableDeclarator, scoreDistributions);
    // With probability
    scoreDistributions = getRandomPMMLScoreDistributions(true);
    KiePMMLNodeFactory.populateEvaluateNodeWithScoreDistributions(toPopulate, scoreDistributions);
    commonVerifyEvaluateNodeWithScoreDistributions(variableDeclarator, scoreDistributions);
}
Also used : ScoreDistribution(org.dmg.pmml.ScoreDistribution) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 99 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator 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 100 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator 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)

Aggregations

VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)110 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)50 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)44 Expression (com.github.javaparser.ast.expr.Expression)43 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)39 NameExpr (com.github.javaparser.ast.expr.NameExpr)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 Test (org.junit.Test)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)18 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)17 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 NodeList (com.github.javaparser.ast.NodeList)16 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)14 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)13 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)13 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)11 Parameter (com.github.javaparser.ast.body.Parameter)9