use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class CommonCodegenUtilsTest method addMapPopulation.
@Test
public void addMapPopulation() {
final Map<String, MethodDeclaration> toAdd = IntStream.range(0, 5).boxed().collect(Collectors.toMap(index -> "KEY_" + index, index -> getMethodDeclaration("METHOD_" + index)));
BlockStmt body = new BlockStmt();
String mapName = "MAP_NAME";
CommonCodegenUtils.addMapPopulation(toAdd, body, mapName);
NodeList<Statement> statements = body.getStatements();
assertEquals(toAdd.size(), statements.size());
for (Statement statement : statements) {
assertTrue(statement instanceof ExpressionStmt);
ExpressionStmt expressionStmt = (ExpressionStmt) statement;
com.github.javaparser.ast.expr.Expression expression = expressionStmt.getExpression();
assertTrue(expression instanceof MethodCallExpr);
MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
final NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments();
assertEquals(2, arguments.size());
assertTrue(arguments.get(0) instanceof StringLiteralExpr);
assertTrue(arguments.get(1) instanceof MethodReferenceExpr);
MethodReferenceExpr methodReferenceExpr = (MethodReferenceExpr) arguments.get(1);
assertTrue(methodReferenceExpr.getScope() instanceof ThisExpr);
final com.github.javaparser.ast.expr.Expression scope = methodCallExpr.getScope().orElse(null);
assertNotNull(scope);
assertTrue(scope instanceof NameExpr);
assertEquals(mapName, ((NameExpr) scope).getNameAsString());
}
for (Map.Entry<String, MethodDeclaration> entry : toAdd.entrySet()) {
int matchingDeclarations = (int) statements.stream().filter(statement -> {
ExpressionStmt expressionStmt = (ExpressionStmt) statement;
com.github.javaparser.ast.expr.Expression expression = expressionStmt.getExpression();
MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
final NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments();
if (!entry.getKey().equals(((StringLiteralExpr) arguments.get(0)).getValue())) {
return false;
}
MethodReferenceExpr methodReferenceExpr = (MethodReferenceExpr) arguments.get(1);
return entry.getValue().getName().asString().equals(methodReferenceExpr.getIdentifier());
}).count();
assertEquals(1, matchingDeclarations);
}
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class CommonCodegenUtilsTest method addListPopulation.
@Test
public void addListPopulation() {
final List<ObjectCreationExpr> toAdd = IntStream.range(0, 5).mapToObj(i -> {
ObjectCreationExpr toReturn = new ObjectCreationExpr();
toReturn.setType(String.class);
Expression value = new StringLiteralExpr("String" + i);
toReturn.setArguments(NodeList.nodeList(value));
return toReturn;
}).collect(Collectors.toList());
BlockStmt body = new BlockStmt();
String listName = "LIST_NAME";
CommonCodegenUtils.addListPopulationByObjectCreationExpr(toAdd, body, listName);
NodeList<Statement> statements = body.getStatements();
assertEquals(toAdd.size(), statements.size());
for (Statement statement : statements) {
assertTrue(statement instanceof ExpressionStmt);
ExpressionStmt expressionStmt = (ExpressionStmt) statement;
assertTrue(expressionStmt.getExpression() instanceof MethodCallExpr);
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
assertEquals(listName, methodCallExpr.getScope().get().asNameExpr().getNameAsString());
NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments();
assertEquals(1, arguments.size());
assertTrue(arguments.get(0) instanceof ObjectCreationExpr);
ObjectCreationExpr objectCreationExpr = (ObjectCreationExpr) arguments.get(0);
assertEquals(objectCreationExpr.getType().asString(), String.class.getSimpleName());
arguments = objectCreationExpr.getArguments();
assertEquals(1, arguments.size());
assertTrue(arguments.get(0) instanceof StringLiteralExpr);
}
for (ObjectCreationExpr entry : toAdd) {
int matchingDeclarations = (int) statements.stream().filter(statement -> {
ExpressionStmt expressionStmt = (ExpressionStmt) statement;
com.github.javaparser.ast.expr.Expression expression = expressionStmt.getExpression();
MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
final NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments();
return entry.equals(arguments.get(0).asObjectCreationExpr());
}).count();
assertEquals(1, matchingDeclarations);
}
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method comparisonMeasureCreationExprFrom.
private static ObjectCreationExpr comparisonMeasureCreationExprFrom(ComparisonMeasure comparisonMeasure) {
NodeList<Expression> arguments = new NodeList<>();
arguments.add(literalExprFrom(comparisonMeasureKindFrom(comparisonMeasure.getKind())));
arguments.add(literalExprFrom(aggregateFunctionFrom(comparisonMeasure.getMeasure())));
arguments.add(literalExprFrom(compareFunctionFrom(comparisonMeasure.getCompareFunction())));
return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLComparisonMeasure.class.getCanonicalName()), arguments);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLRegressionTableFactory method getCategoricalPredictorExpression.
/**
* Create <b>CategoricalPredictor</b> <code>CastExpr</code> to the class
* @param categoricalPredictorMapName
* @return
*/
static CastExpr getCategoricalPredictorExpression(final String categoricalPredictorMapName) {
final String lambdaExpressionMethodName = "evaluateCategoricalPredictor";
final String parameterName = "input";
final MethodCallExpr lambdaMethodCallExpr = new MethodCallExpr();
lambdaMethodCallExpr.setName(lambdaExpressionMethodName);
final NodeList<Expression> arguments = new NodeList<>();
arguments.add(0, new NameExpr(parameterName));
arguments.add(1, new NameExpr(categoricalPredictorMapName));
lambdaMethodCallExpr.setArguments(arguments);
final ExpressionStmt lambdaExpressionStmt = new ExpressionStmt(lambdaMethodCallExpr);
final LambdaExpr lambdaExpr = new LambdaExpr();
final Parameter lambdaParameter = new Parameter(new UnknownType(), parameterName);
lambdaExpr.setParameters(NodeList.nodeList(lambdaParameter));
lambdaExpr.setBody(lambdaExpressionStmt);
lambdaMethodCallExpr.setScope(new NameExpr(KiePMMLRegressionTable.class.getSimpleName()));
final ClassOrInterfaceType serializableFunctionType = getTypedClassOrInterfaceTypeByTypeNames(SerializableFunction.class.getCanonicalName(), Arrays.asList(String.class.getSimpleName(), Double.class.getSimpleName()));
final CastExpr toReturn = new CastExpr();
toReturn.setType(serializableFunctionType);
toReturn.setExpression(lambdaExpr);
return toReturn;
}
use of com.github.javaparser.ast.NodeList 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);
}
}
Aggregations