use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class CommonCodegenUtils method setConstructorDeclarationReferenceArgument.
/**
* Set the <b>value</b> of the given <b>parameterName</b> in the given <code>ConstructorDeclaration</code>
* @param constructorDeclaration
* @param referenceName
* @param value
* @throws KiePMMLException if the given parameter is not found
*/
public static void setConstructorDeclarationReferenceArgument(final ConstructorDeclaration constructorDeclaration, final String referenceName, final String value) {
final BlockStmt body = constructorDeclaration.getBody();
final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
final MethodReferenceExpr methodReferenceExpr = getExplicitConstructorInvocationMethodReference(superStatement, referenceName).orElseThrow(() -> new KiePMMLException(String.format(MISSING_PARAMETER_IN_CONSTRUCTOR_INVOCATION, referenceName, constructorDeclaration)));
if (value != null) {
methodReferenceExpr.setScope(new TypeExpr(parseClassOrInterfaceType(value)));
} else {
superStatement.getArguments().replace(methodReferenceExpr, new NullLiteralExpr());
}
}
use of com.github.javaparser.ast.expr.MethodReferenceExpr 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.expr.MethodReferenceExpr in project drools by kiegroup.
the class KiePMMLClassificationTableFactory method getProbabilityMapFunctionSupportedExpression.
/**
* Create <b>probabilityMapFunction</b> <code>MethodReferenceExpr</code>
* @param normalizationMethod
* @param isBinary
* @return
*/
static MethodReferenceExpr getProbabilityMapFunctionSupportedExpression(final RegressionModel.NormalizationMethod normalizationMethod, final boolean isBinary) {
String normalizationName = normalizationMethod.name();
if (RegressionModel.NormalizationMethod.NONE.equals(normalizationMethod) && isBinary) {
normalizationName += "Binary";
}
final String thisExpressionMethodName = String.format("get%sProbabilityMap", normalizationName);
final CastExpr castExpr = new CastExpr();
final String stringClassName = String.class.getSimpleName();
final String doubleClassName = Double.class.getSimpleName();
final ClassOrInterfaceType linkedHashMapReferenceType = getTypedClassOrInterfaceTypeByTypeNames(LinkedHashMap.class.getCanonicalName(), Arrays.asList(stringClassName, doubleClassName));
final ClassOrInterfaceType consumerType = getTypedClassOrInterfaceTypeByTypes(SerializableFunction.class.getCanonicalName(), Arrays.asList(linkedHashMapReferenceType, linkedHashMapReferenceType));
castExpr.setType(consumerType);
castExpr.setExpression("KiePMMLClassificationTable");
final MethodReferenceExpr toReturn = new MethodReferenceExpr();
toReturn.setScope(castExpr);
toReturn.setIdentifier(thisExpressionMethodName);
return toReturn;
}
use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class KiePMMLClassificationTableFactoryTest method getProbabilityMapFunctionSupportedExpression.
@Test
public void getProbabilityMapFunctionSupportedExpression() throws IOException {
MethodReferenceExpr retrieved = KiePMMLClassificationTableFactory.getProbabilityMapFunctionSupportedExpression(RegressionModel.NormalizationMethod.CAUCHIT, true);
String text = getFileContent(TEST_01_SOURCE);
Expression expected = JavaParserUtils.parseExpression(String.format(text, RegressionModel.NormalizationMethod.CAUCHIT.name()));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
use of com.github.javaparser.ast.expr.MethodReferenceExpr in project drools by kiegroup.
the class KiePMMLRegressionTableFactoryTest method getResultUpdaterSupportedExpression.
@Test
public void getResultUpdaterSupportedExpression() throws IOException {
MethodReferenceExpr retrieved = KiePMMLRegressionTableFactory.getResultUpdaterSupportedExpression(RegressionModel.NormalizationMethod.CAUCHIT);
String text = getFileContent(TEST_03_SOURCE);
Expression expected = JavaParserUtils.parseExpression(String.format(text, RegressionModel.NormalizationMethod.CAUCHIT.name()));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
Aggregations