use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLNodeFactory method getKiePMMLScoreDistribution.
static ObjectCreationExpr getKiePMMLScoreDistribution(final String variableName, final ScoreDistribution scoreDistribution) {
final NodeList<Expression> scoreDistributionsArguments = new NodeList<>();
scoreDistributionsArguments.add(getExpressionForObject(variableName));
scoreDistributionsArguments.add(new NullLiteralExpr());
scoreDistributionsArguments.add(getExpressionForObject(scoreDistribution.getValue().toString()));
scoreDistributionsArguments.add(getExpressionForObject(scoreDistribution.getRecordCount().intValue()));
Expression confidenceExpression = scoreDistribution.getConfidence() != null ? getExpressionForObject(scoreDistribution.getConfidence().doubleValue()) : new NullLiteralExpr();
scoreDistributionsArguments.add(confidenceExpression);
Expression probabilityExpression = scoreDistribution.getProbability() != null ? getExpressionForObject(scoreDistribution.getProbability().doubleValue()) : new NullLiteralExpr();
scoreDistributionsArguments.add(probabilityExpression);
return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLScoreDistribution.class.getCanonicalName()), scoreDistributionsArguments);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLNodeFactory method populateEvaluateNodeWithScoreDistributions.
/**
* Set the <b>scoreDistribution</b> <code>VariableDeclarator</code> initializer of the given <code>BlockStmt</code>.
* If <b>scoreDistributionsParam</b> is <code>null</code>, a <code>NullLiteralExpr</code> is set.
* <p>
* <code>List<KiePMMLScoreDistribution> scoreDistributions = null;</code>
* </p>
* Otherwise
* <p>
* <code>List<KiePMMLScoreDistribution> scoreDistributions = arrays.asList(new KiePMMLScoreDistribution(....));</code>
* </p>
*
* @param toPopulate
* @param scoreDistributionsParam
*/
static void populateEvaluateNodeWithScoreDistributions(final BlockStmt toPopulate, final List<ScoreDistribution> scoreDistributionsParam) {
final Expression scoreDistributionsExpression;
if (scoreDistributionsParam == null) {
scoreDistributionsExpression = new NullLiteralExpr();
} else {
int counter = 0;
final NodeList<Expression> scoreDistributionsArguments = new NodeList<>();
for (ScoreDistribution scoreDistribution : scoreDistributionsParam) {
String nestedVariableName = String.format("scoreDistribution_%s", counter);
scoreDistributionsArguments.add(getKiePMMLScoreDistribution(nestedVariableName, scoreDistribution));
counter++;
}
scoreDistributionsExpression = new MethodCallExpr();
((MethodCallExpr) scoreDistributionsExpression).setScope(new NameExpr(Arrays.class.getSimpleName()));
((MethodCallExpr) scoreDistributionsExpression).setName("asList");
((MethodCallExpr) scoreDistributionsExpression).setArguments(scoreDistributionsArguments);
}
CommonCodegenUtils.setVariableDeclaratorValue(toPopulate, SCORE_DISTRIBUTIONS, scoreDistributionsExpression);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLNodeFactoryTest method commonVerifyEvaluateNodeWithScoreDistributions.
private void commonVerifyEvaluateNodeWithScoreDistributions(final VariableDeclarator variableDeclarator, final List<ScoreDistribution> scoreDistributions) {
assertTrue(variableDeclarator.getInitializer().isPresent());
Expression expression = variableDeclarator.getInitializer().get();
assertTrue(expression instanceof MethodCallExpr);
MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
assertEquals("Arrays", methodCallExpr.getScope().get().toString());
assertEquals("asList", methodCallExpr.getName().toString());
NodeList<Expression> arguments = methodCallExpr.getArguments();
assertEquals(scoreDistributions.size(), arguments.size());
arguments.forEach(argument -> assertTrue(argument instanceof ObjectCreationExpr));
List<ObjectCreationExpr> objectCreationExprs = arguments.stream().map(ObjectCreationExpr.class::cast).collect(Collectors.toList());
scoreDistributions.forEach(scoreDistribution -> {
Optional<ObjectCreationExpr> retrieved = objectCreationExprs.stream().filter(objectCreationExpr -> scoreDistribution.getValue().equals(objectCreationExpr.getArgument(2).asStringLiteralExpr().asString())).findFirst();
assertTrue(retrieved.isPresent());
Expression recordCountExpected = getExpressionForObject(scoreDistribution.getRecordCount().intValue());
Expression confidenceExpected = getExpressionForObject(scoreDistribution.getConfidence().doubleValue());
Expression probabilityExpected = scoreDistribution.getProbability() != null ? getExpressionForObject(scoreDistribution.getProbability().doubleValue()) : new NullLiteralExpr();
retrieved.ifPresent(objectCreationExpr -> {
assertEquals(recordCountExpected, objectCreationExpr.getArgument(3));
assertEquals(confidenceExpected, objectCreationExpr.getArgument(4));
assertEquals(probabilityExpected, objectCreationExpr.getArgument(5));
});
});
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class FEELPropertyAccessibleImplementation method getFeelPropertyDefinition.
private MethodDefinition getFeelPropertyDefinition() {
MethodDeclaration getFEELProperty = cloneMethodTemplate("getFEELProperty");
SwitchStmt firstSwitch = getFEELProperty.findFirst(SwitchStmt.class).orElseThrow(() -> new InvalidTemplateException("Missing Switch Statement in getFEELProperty template"));
firstSwitch.setComment(null);
List<SwitchEntry> collect = fields.stream().map(this::toGetPropertySwitchEntry).collect(Collectors.toList());
SwitchEntry defaultSwitchStmt = firstSwitch.findFirst(SwitchEntry.class, sw -> sw.getLabels().isEmpty()).orElseThrow(() -> new InvalidTemplateException("Missing Default Switch Statement in getFEELProperty template"));
collect.add(defaultSwitchStmt);
firstSwitch.setEntries(nodeList(collect));
String body = getFEELProperty.getBody().orElseThrow(() -> new InvalidTemplateException("Empty body in getFeelProperty clone")).toString();
MethodWithStringBody getFeelPropertyDefinition = new MethodWithStringBody("getFEELProperty", EvalHelper.PropertyValueResult.class.getCanonicalName(), body).addParameter(String.class.getCanonicalName(), "property");
addOverrideAnnotation(getFeelPropertyDefinition);
return getFeelPropertyDefinition;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class FEELPropertyAccessibleImplementation method allFeelProperties.
private MethodWithStringBody allFeelProperties() {
MethodDeclaration allFeelProperties = cloneMethodTemplate("allFEELProperties");
MethodCallExpr putExpression = allFeelProperties.findFirst(MethodCallExpr.class, mc -> mc.getNameAsString().equals("put")).orElseThrow(() -> new InvalidTemplateException("Missing put method in allFEELProperties"));
List<Statement> collect = fields.stream().map(fieldDefinition -> toResultPut(putExpression, fieldDefinition)).collect(Collectors.toList());
if (typeDefinition instanceof AbstractDMNSetType) {
collect.add(new ExpressionStmt(StaticJavaParser.parseExpression("result.entrySet().removeIf(entry -> java.util.Objects.isNull(entry.getValue()) && !definedKeySet.contains(entry.getKey()))")));
}
BlockStmt newBlockStatement = new BlockStmt(nodeList(collect));
putExpression.getParentNode().ifPresent(p -> p.replace(newBlockStatement));
BlockStmt body = allFeelProperties.getBody().orElseThrow(() -> new InvalidTemplateException("Missing body in generated method"));
MethodWithStringBody allFEELProperties = new MethodWithStringBody("allFEELProperties", "java.util.Map<String, Object>", body.toString());
addOverrideAnnotation(allFEELProperties);
return allFEELProperties;
}
Aggregations