Search in sources :

Example 66 with Expression

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

the class PatternDSLPattern method buildPattern.

@Override
protected void buildPattern(DeclarationSpec declarationSpec, List<PatternConstraintParseResult> patternConstraintParseResults) {
    MethodCallExpr patternExpression = createPatternExpression(pattern, declarationSpec);
    List<Expression> exprs = new ArrayList<>();
    context.pushExprPointer(exprs::add);
    buildConstraints(pattern, patternType, patternConstraintParseResults);
    context.popExprPointer();
    List<Expression> additionalPatterns = new ArrayList<>();
    for (Expression expr : exprs) {
        Optional<Expression> rootScope = findRootNodeViaScope(expr);
        if (rootScope.isPresent() && ((MethodCallExpr) rootScope.get()).getNameAsString().equals(PATTERN_CALL)) {
            additionalPatterns.add(expr);
        } else {
            MethodCallExpr currentExpr = (MethodCallExpr) expr;
            MethodCallExpr lastMethodInChain = findLastMethodInChain(currentExpr);
            if (!NO_OP_EXPR.equals(lastMethodInChain.getNameAsString())) {
                lastMethodInChain.setScope(patternExpression);
                patternExpression = currentExpr;
            }
        }
    }
    if (pattern.isQuery()) {
        patternExpression = new MethodCallExpr(patternExpression, PASSIVE_CALL);
    }
    context.addExpression(addWatchToPattern(patternExpression));
    additionalPatterns.forEach(context::addExpression);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) ArrayList(java.util.ArrayList) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 67 with Expression

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

the class PatternDSLPattern method createPatternExpression.

private MethodCallExpr createPatternExpression(PatternDescr pattern, DeclarationSpec declarationSpec) {
    MethodCallExpr dslExpr = createDslTopLevelMethod(PATTERN_CALL);
    dslExpr.addArgument(context.getVarExpr(pattern.getIdentifier()));
    if (context.isQuery()) {
        Optional<Expression> declarationSource = declarationSpec.getDeclarationSource();
        declarationSource.ifPresent(dslExpr::addArgument);
    }
    return dslExpr;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 68 with Expression

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

the class FlowDSLQueryGenerator method queryNameClassArg.

private void queryNameClassArg(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
    query1.addParameter(STRINGLITERAL, "name");
    BlockStmt stmts = new BlockStmt();
    NodeList<Type> typeArguments = nodeList();
    NodeList<TypeParameter> typeParameters = nodeList();
    NodeList<Expression> arguments = nodeList();
    ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
    ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
    objCreationExpr.setDiamondOperator();
    stmts.addStatement(new ReturnStmt(objCreationExpr));
    objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
    objCreationExpr.addArgument("name");
    rangeArity().forEach(i -> {
        String genericTypeName = stringWithIndex("T", i);
        String type = stringWithIndex("type", i);
        String name = stringWithIndexInside(i);
        typeArguments.add(parseType(genericTypeName));
        arguments.add(new NameExpr(type));
        arguments.add(new NameExpr(name));
        String classGenericType = genericType(genericTypeName);
        Type genericType = parseType(classGenericType);
        query1.addParameter(genericType, type);
        query1.addParameter(stringType(), name);
        typeParameters.add(new TypeParameter(genericTypeName));
    });
    query1.setTypeParameters(typeParameters);
    query1.setBody(stmts);
    ClassOrInterfaceType type = queryDef();
    type.setTypeArguments(typeArguments);
    query1.setType(type);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 69 with Expression

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

the class FlowDSLQueryGenerator method queryPkgNameClassArg.

private void queryPkgNameClassArg(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
    query1.addParameter(STRINGLITERAL, "pkg");
    query1.addParameter(STRINGLITERAL, "name");
    BlockStmt stmts = new BlockStmt();
    NodeList<Type> typeArguments = nodeList();
    NodeList<TypeParameter> typeParameters = nodeList();
    NodeList<Expression> arguments = nodeList();
    ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
    ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
    objCreationExpr.setDiamondOperator();
    stmts.addStatement(new ReturnStmt(objCreationExpr));
    objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
    objCreationExpr.addArgument("pkg");
    objCreationExpr.addArgument("name");
    rangeArity().forEach(i -> {
        String genericTypeName = stringWithIndex("T", i);
        String type = stringWithIndex("type", i);
        String name = stringWithIndexInside(i);
        typeArguments.add(parseType(genericTypeName));
        arguments.add(new NameExpr(type));
        arguments.add(new NameExpr(name));
        String classGenericType = genericType(genericTypeName);
        Type genericType = parseType(classGenericType);
        query1.addParameter(genericType, type);
        query1.addParameter(stringType(), name);
        typeParameters.add(new TypeParameter(genericTypeName));
    });
    query1.setTypeParameters(typeParameters);
    query1.setBody(stmts);
    ClassOrInterfaceType type = queryDef();
    type.setTypeArguments(typeArguments);
    query1.setType(type);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 70 with Expression

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

the class QueryDefImplGenerator method generateEquals.

private void generateEquals(ClassOrInterfaceDeclaration clazz) {
    String template = "   @Override\n" + "    public boolean isEqualTo( ModelComponent other ) {\n" + "        if ( this == other ) return true;\n" + "        if ( !(other instanceof DEF_IMPL_TYPE) ) return false;\n" + "\n" + "        DEF_IMPL_TYPE that = (DEF_IMPL_TYPE) other;\n" + "\n" + "        return EQUALS_CALL;\n" + "    }";
    BodyDeclaration<?> parse = parseBodyDeclaration(template);
    Expression andExpr = new BooleanLiteralExpr(true);
    for (int i : rangeArity().toArray()) {
        String argWithIndex = argIndex(i);
        Expression e = parseExpression(String.format("ModelComponent.areEqualInModel( %s, that.%s )", argWithIndex, argWithIndex));
        andExpr = new BinaryExpr(andExpr, e, BinaryExpr.Operator.AND);
    }
    final Expression finalAndExpr = andExpr;
    parse.findAll(ClassOrInterfaceType.class, n -> n.toString().equals("DEF_IMPL_TYPE")).forEach(s -> s.replace(parseType("Query" + arity + "DefImpl")));
    parse.findAll(NameExpr.class, n -> n.toString().equals("EQUALS_CALL")).forEach(s -> s.replace(finalAndExpr));
    clazz.addMember(parse);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) StaticJavaParser.parseBodyDeclaration(com.github.javaparser.StaticJavaParser.parseBodyDeclaration) Type(com.github.javaparser.ast.type.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) NodeList(com.github.javaparser.ast.NodeList) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Modifier(com.github.javaparser.ast.Modifier) QueryCallViewItemImpl(org.drools.model.view.QueryCallViewItemImpl) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) StaticJavaParser.parseImport(com.github.javaparser.StaticJavaParser.parseImport) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43