Search in sources :

Example 46 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class KiePMMLRegressionTableFactory method getNumericPredictorExpression.

/**
 * Create a <b>NumericPredictor</b> <code>CastExpr</code>
 * @param numericPredictor
 * @return
 */
static CastExpr getNumericPredictorExpression(final NumericPredictor numericPredictor) {
    boolean withExponent = !Objects.equals(1, numericPredictor.getExponent());
    final String lambdaExpressionMethodName = withExponent ? "evaluateNumericWithExponent" : "evaluateNumericWithoutExponent";
    final String parameterName = "input";
    final MethodCallExpr lambdaMethodCallExpr = new MethodCallExpr();
    lambdaMethodCallExpr.setName(lambdaExpressionMethodName);
    lambdaMethodCallExpr.setScope(new NameExpr(KiePMMLRegressionTable.class.getSimpleName()));
    final NodeList<Expression> arguments = new NodeList<>();
    arguments.add(0, new NameExpr(parameterName));
    arguments.add(1, getExpressionForObject(numericPredictor.getCoefficient().doubleValue()));
    if (withExponent) {
        arguments.add(2, getExpressionForObject(numericPredictor.getExponent().doubleValue()));
    }
    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);
    final String doubleClassName = Double.class.getSimpleName();
    final ClassOrInterfaceType serializableFunctionType = getTypedClassOrInterfaceTypeByTypeNames(SerializableFunction.class.getCanonicalName(), Arrays.asList(doubleClassName, doubleClassName));
    final CastExpr toReturn = new CastExpr();
    toReturn.setType(serializableFunctionType);
    toReturn.setExpression(lambdaExpr);
    return toReturn;
}
Also used : SerializableFunction(org.kie.pmml.api.iinterfaces.SerializableFunction) NodeList(com.github.javaparser.ast.NodeList) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) UnknownType(com.github.javaparser.ast.type.UnknownType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) Parameter(com.github.javaparser.ast.body.Parameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 47 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class KiePMMLRegressionTableFactory method getPredictorTermFunction.

/**
 * Get the <b>PredictorTerm</b> <code>VariableDeclarationExpr</code>
 * @param predictorTerm
 * @return
 */
static LambdaExpr getPredictorTermFunction(final PredictorTerm predictorTerm) {
    try {
        LambdaExpr toReturn = new LambdaExpr();
        toReturn.setParameters(NodeList.nodeList(new Parameter(new UnknownType(), "resultMap")));
        final BlockStmt body = getPredictorTermBody(predictorTerm);
        toReturn.setBody(body);
        return toReturn;
    } catch (Exception e) {
        throw new KiePMMLInternalException(String.format("Failed to get PredictorTermFunction for %s", predictorTerm), e);
    }
}
Also used : UnknownType(com.github.javaparser.ast.type.UnknownType) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Parameter(com.github.javaparser.ast.body.Parameter) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 48 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class KiePMMLRegressionTableFactoryTest method getPredictorTermFunction.

@Test
public void getPredictorTermFunction() throws IOException {
    String predictorName = "predictorName";
    double coefficient = 23.12;
    String fieldRef = "fieldRef";
    PredictorTerm predictorTerm = PMMLModelTestUtils.getPredictorTerm(predictorName, coefficient, Collections.singletonList(fieldRef));
    LambdaExpr retrieved = KiePMMLRegressionTableFactory.getPredictorTermFunction(predictorTerm);
    String text = getFileContent(TEST_07_SOURCE);
    Expression expected = JavaParserUtils.parseExpression(String.format(text, fieldRef, coefficient));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
Also used : PredictorTerm(org.dmg.pmml.regression.PredictorTerm) Expression(com.github.javaparser.ast.expr.Expression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) Test(org.junit.Test)

Example 49 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitLambdaExpression.

@Override
public Void visitLambdaExpression(LambdaExpressionTree javacTree, Node javaParserNode) {
    LambdaExpr node = castNode(LambdaExpr.class, javaParserNode, javacTree);
    processLambdaExpression(javacTree, node);
    visitLists(javacTree.getParameters(), node.getParameters());
    switch(javacTree.getBodyKind()) {
        case EXPRESSION:
            assert node.getBody() instanceof ExpressionStmt;
            ExpressionStmt body = (ExpressionStmt) node.getBody();
            javacTree.getBody().accept(this, body.getExpression());
            break;
        case STATEMENT:
            javacTree.getBody().accept(this, node.getBody());
            break;
    }
    return null;
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 50 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final LambdaExpr node1, final Node other) {
    LambdaExpr node2 = (LambdaExpr) other;
    defaultAction(node1, node2);
    node1.getBody().accept(this, node2.getBody());
    visitLists(node1.getParameters(), node2.getParameters());
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr)

Aggregations

LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)70 Parameter (com.github.javaparser.ast.body.Parameter)37 NameExpr (com.github.javaparser.ast.expr.NameExpr)30 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)29 Expression (com.github.javaparser.ast.expr.Expression)27 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)25 UnknownType (com.github.javaparser.ast.type.UnknownType)24 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)19 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)15 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)12 CompilationUnit (com.github.javaparser.ast.CompilationUnit)10 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)10 Test (org.junit.jupiter.api.Test)10 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)9 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)9 CastExpr (com.github.javaparser.ast.expr.CastExpr)8 ArrayList (java.util.ArrayList)8 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8