Search in sources :

Example 1 with MethodReferenceExpr

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());
    }
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) TypeExpr(com.github.javaparser.ast.expr.TypeExpr)

Example 2 with MethodReferenceExpr

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);
    }
}
Also used : StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) Arrays(java.util.Arrays) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) CommonCodegenUtils.literalExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.literalExprFrom) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Objects(java.util.Objects) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IntStream(java.util.stream.IntStream) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) Assert.assertThrows(org.junit.Assert.assertThrows) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Assert.assertSame(org.junit.Assert.assertSame) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME) StaticJavaParser.parseBlock(com.github.javaparser.StaticJavaParser.parseBlock) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) Assert.assertNotNull(org.junit.Assert.assertNotNull) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CodegenTestUtils.commonValidateCompilation(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilation) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) LAMBDA_PARAMETER_NAME(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.LAMBDA_PARAMETER_NAME) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Assert.assertEquals(org.junit.Assert.assertEquals) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) Expression(com.github.javaparser.ast.expr.Expression) Map(java.util.Map) HashMap(java.util.HashMap) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 3 with MethodReferenceExpr

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;
}
Also used : SerializableFunction(org.kie.pmml.api.iinterfaces.SerializableFunction) CastExpr(com.github.javaparser.ast.expr.CastExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) LinkedHashMap(java.util.LinkedHashMap) CommonCodegenUtils.createPopulatedLinkedHashMap(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.createPopulatedLinkedHashMap)

Example 4 with MethodReferenceExpr

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));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) Test(org.junit.Test)

Example 5 with MethodReferenceExpr

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));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) Test(org.junit.Test)

Aggregations

MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)18 NameExpr (com.github.javaparser.ast.expr.NameExpr)10 Expression (com.github.javaparser.ast.expr.Expression)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)6 Test (org.junit.Test)6 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)5 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)5 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)5 NodeList (com.github.javaparser.ast.NodeList)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)3 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)3 Parameter (com.github.javaparser.ast.body.Parameter)3 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)3 CastExpr (com.github.javaparser.ast.expr.CastExpr)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)3 TypeExpr (com.github.javaparser.ast.expr.TypeExpr)3 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)3