Search in sources :

Example 11 with CastExpr

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

the class KiePMMLRegressionTableFactoryTest method getNumericPredictorExpressionWithoutExponent.

@Test
public void getNumericPredictorExpressionWithoutExponent() throws IOException {
    String predictorName = "predictorName";
    int exponent = 1;
    double coefficient = 1.23;
    NumericPredictor numericPredictor = PMMLModelTestUtils.getNumericPredictor(predictorName, exponent, coefficient);
    CastExpr retrieved = KiePMMLRegressionTableFactory.getNumericPredictorExpression(numericPredictor);
    String text = getFileContent(TEST_02_SOURCE);
    Expression expected = JavaParserUtils.parseExpression(String.format(text, coefficient));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) NumericPredictor(org.dmg.pmml.regression.NumericPredictor) Test(org.junit.Test)

Example 12 with CastExpr

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

the class FunctionDefs method asMethodCall.

public static Expression asMethodCall(String className, String methodSignature, List<String> params) {
    // creating a simple algorithm to find the method in java
    // without using any external libraries in this initial implementation
    // might need to explicitly use a classloader here
    String[] mp = FunctionDefNode.parseMethod(methodSignature);
    try {
        String methodName = mp[0];
        String[] paramTypeNames = FunctionDefNode.parseParams(mp[1]);
        ArrayList<Expression> paramExprs = new ArrayList<>();
        if (paramTypeNames.length == params.size()) {
            for (int i = 0; i < params.size(); i++) {
                String paramName = params.get(i);
                String paramTypeName = paramTypeNames[i];
                Type paramTypeCanonicalName = parseType(FunctionDefNode.getType(paramTypeName, null).getCanonicalName());
                Expression param = new CastExpr(paramTypeCanonicalName, new MethodCallExpr(null, "coerceTo", new NodeList<>(new ClassExpr(paramTypeCanonicalName), new MethodCallExpr(new NameExpr("feelExprCtx"), "getValue", new NodeList<>(new StringLiteralExpr(paramName))))));
                paramExprs.add(param);
            }
            return new MethodCallExpr(new NameExpr(className), methodName, new NodeList<>(paramExprs));
        } else {
            throw new FEELCompilationError(Msg.createMessage(Msg.ERROR_RESOLVING_EXTERNAL_FUNCTION_AS_DEFINED_BY, methodSignature));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : NodeList(com.github.javaparser.ast.NodeList) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) FEELCompilationError(org.kie.dmn.feel.codegen.feel11.FEELCompilationError) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 13 with CastExpr

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

the class CompilerBytecodeLoader method getCompilationUnit.

public <T> CompilationUnit getCompilationUnit(Class<T> clazz, String templateResourcePath, String cuPackage, String cuClass, String feelExpression, Expression theExpression, Set<FieldDeclaration> fieldDeclarations) {
    CompilationUnit cu = parse(CompilerBytecodeLoader.class.getResourceAsStream(templateResourcePath));
    cu.setPackageDeclaration(cuPackage);
    final String className = templateResourcePath.substring(1, templateResourcePath.length() - 5);
    ClassOrInterfaceDeclaration classSource = cu.getClassByName(className).orElseThrow(() -> new IllegalArgumentException("Cannot find class by name " + className));
    classSource.setName(cuClass);
    MethodDeclaration lookupMethod = cu.findFirst(MethodDeclaration.class).orElseThrow(() -> new RuntimeException("Something unexpected changed in the template."));
    lookupMethod.setComment(new JavadocComment("   FEEL: " + feelExpression + "   "));
    ReturnStmt returnStmt = lookupMethod.findFirst(ReturnStmt.class).orElseThrow(() -> new RuntimeException("Something unexpected changed in the template."));
    Expression expr;
    if (clazz.equals(CompiledFEELUnaryTests.class)) {
        expr = new CastExpr(StaticJavaParser.parseType("java.util.List"), new EnclosedExpr(theExpression));
    } else {
        expr = theExpression;
    }
    returnStmt.setExpression(expr);
    List<ClassOrInterfaceDeclaration> classDecls = cu.findAll(ClassOrInterfaceDeclaration.class);
    if (classDecls.size() != 1) {
        throw new RuntimeException("Something unexpected changed in the template.");
    }
    ClassOrInterfaceDeclaration classDecl = classDecls.get(0);
    fieldDeclarations.stream().filter(fd -> !isUnaryTest(fd)).sorted(new SortFieldDeclarationStrategy()).forEach(classDecl::addMember);
    fieldDeclarations.stream().filter(fd -> fd.getVariable(0).getName().asString().startsWith("UT")).sorted(new SortFieldDeclarationStrategy()).forEach(classDecl::addMember);
    if (generateClassListener != null) {
        generateClassListener.generatedClass(cu);
    }
    LOG.debug("{}", cu);
    return cu;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 14 with CastExpr

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

the class DoubleJavaParserVisitor method visit.

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

Example 15 with CastExpr

use of com.github.javaparser.ast.expr.CastExpr in project javaparser by javaparser.

the class JavaParserTest method rangeOfLambda.

@Test
public void rangeOfLambda() {
    String code = "class A {" + EOL + "  Object f() {" + EOL + "    return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
    CompilationUnit cu = JavaParser.parse(code);
    MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
    CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
    LambdaExpr lambdaExpr = castExpr.getExpression().asLambdaExpr();
    assertEquals(range(3, 56, 3, 101), lambdaExpr.getRange().get());
    assertEquals(GeneratedJavaParserConstants.LPAREN, lambdaExpr.getTokenRange().get().getBegin().getKind());
    assertEquals(GeneratedJavaParserConstants.RPAREN, lambdaExpr.getTokenRange().get().getEnd().getKind());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Aggregations

CastExpr (com.github.javaparser.ast.expr.CastExpr)32 Expression (com.github.javaparser.ast.expr.Expression)21 NameExpr (com.github.javaparser.ast.expr.NameExpr)13 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)11 Test (org.junit.Test)9 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)7 CompilationUnit (com.github.javaparser.ast.CompilationUnit)6 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)6 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)6 Type (com.github.javaparser.ast.type.Type)6 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)6 NodeList (com.github.javaparser.ast.NodeList)5 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)5 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)5 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)4 UnificationTypedExpression (org.drools.modelcompiler.builder.generator.UnificationTypedExpression)4 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)4 SerializableFunction (org.kie.pmml.api.iinterfaces.SerializableFunction)4 Node (com.github.javaparser.ast.Node)3 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)3