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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations