Search in sources :

Example 36 with ObjectCreationExpr

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

the class QueryDefImplGenerator method callMethod.

private void callMethod(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration method = clazz.addMethod("call", Modifier.Keyword.PUBLIC);
    addOverride(method);
    method.addParameter("boolean", "open");
    method.setType(toClassOrInterfaceType(QueryCallViewItem.class));
    BlockStmt statements = new BlockStmt();
    NodeList<Expression> arguments = nodeList();
    ClassOrInterfaceType queryCallViewItemImpl = toClassOrInterfaceType(QueryCallViewItemImpl.class);
    ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
    statements.addStatement(new ReturnStmt(objCreationExpr));
    objCreationExpr.addArgument("this");
    objCreationExpr.addArgument("open");
    rangeArity().forEach(i -> {
        String varWithIndex = stringWithIndex("var", i);
        String genericTypeName = genericTypeName(i);
        method.addParameter(genericType("Argument", genericTypeName), varWithIndex);
        objCreationExpr.addArgument(varWithIndex);
    });
    method.setBody(statements);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 37 with ObjectCreationExpr

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

the class PreprocessPhase method addTypeToInitialization.

private Optional<Expression> addTypeToInitialization(WithStatement withStatement, Deque<Statement> allNewStatements) {
    if (withStatement.getWithObject().isAssignExpr()) {
        AssignExpr assignExpr = withStatement.getWithObject().asAssignExpr();
        Expression assignExprValue = assignExpr.getValue();
        Expression assignExprTarget = assignExpr.getTarget();
        if (assignExprValue.isObjectCreationExpr() && assignExprTarget instanceof DrlNameExpr) {
            ObjectCreationExpr constructor = assignExprValue.asObjectCreationExpr();
            ClassOrInterfaceType ctorType = constructor.getType();
            String targetVariableName = ((DrlNameExpr) assignExprTarget).getNameAsString();
            VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(ctorType, targetVariableName);
            AssignExpr withTypeAssignmentExpr = new AssignExpr(variableDeclarationExpr, assignExprValue, assignExpr.getOperator());
            allNewStatements.add(new ExpressionStmt(withTypeAssignmentExpr));
            return of(new DrlNameExpr(targetVariableName));
        }
    }
    return empty();
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Example 38 with ObjectCreationExpr

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

the class RhsParser method processModify.

private ModifyAction processModify(RuleContext context, MethodCallExpr consequenceExpr, MethodCallExpr statement, BlockStmt ruleVariablesBlock) {
    String modifiedId = statement.getArgument(0).toString();
    Class<?> modifiedClass = context.getDeclarationById(modifiedId).orElseThrow(() -> new RuntimeException("Unknown declaration: " + modifiedId)).getDeclarationClass();
    ModifyAction action = new ModifyAction(modifiedClass);
    if (statement.getArguments().size() > 1) {
        String maskId = statement.getArgument(1).toString();
        AssignExpr maskAssignExpr = ruleVariablesBlock.findAll(AssignExpr.class).stream().filter(assign -> ((VariableDeclarationExpr) assign.getTarget()).getVariable(0).toString().equals(maskId)).findFirst().orElseThrow(() -> new RuntimeException("Unknown mask: " + maskId));
        MethodCallExpr maskMethod = ((MethodCallExpr) maskAssignExpr.getValue());
        List<MethodCallExpr> modifyingExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals(modifiedId) || s.toString().equals("(" + modifiedId + ")")).orElse(false)).collect(Collectors.toList());
        for (int i = 1; i < maskMethod.getArguments().size(); i++) {
            String property = maskMethod.getArgument(i).asStringLiteralExpr().asString();
            String setter = "set" + ucFirst(property);
            MethodCallExpr setterExpr = modifyingExprs.stream().filter(m -> m.getNameAsString().equals(setter)).filter(m -> m.getArguments().size() == 1).findFirst().orElse(null);
            Object value = null;
            if (setterExpr != null) {
                Expression arg = setterExpr.getArgument(0);
                arg = stripEnclosedAndCast(arg);
                if (arg.isLiteralExpr()) {
                    value = literalToValue(arg.asLiteralExpr());
                } else if (arg.isNameExpr()) {
                    value = ((ImpactAnalysisRuleContext) context).getBindVariableLiteralMap().get(arg.asNameExpr().getName().asString());
                } else if (arg.isObjectCreationExpr()) {
                    value = objectCreationExprToValue((ObjectCreationExpr) arg, context);
                }
            }
            Method accessor = ClassUtils.getAccessor(modifiedClass, property);
            if (accessor != null && Map.class.isAssignableFrom(accessor.getReturnType())) {
                String mapName = property;
                List<MethodCallExpr> mapPutExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> isMapPutExpr(m, modifiedId, accessor.getName())).collect(Collectors.toList());
                mapPutExprs.stream().forEach(expr -> {
                    String mapKey = getLiteralString(context, expr.getArgument(0));
                    Object mapValue = getLiteralValue(context, expr.getArgument(1));
                    action.addModifiedProperty(new ModifiedMapProperty(mapName, mapKey, mapValue));
                });
            } else {
                action.addModifiedProperty(new ModifiedProperty(property, value));
            }
        }
    }
    return action;
}
Also used : ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ParserUtil.literalToValue(org.drools.impact.analysis.parser.impl.ParserUtil.literalToValue) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ParserUtil.isLiteral(org.drools.impact.analysis.parser.impl.ParserUtil.isLiteral) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) Rule(org.drools.impact.analysis.model.Rule) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ParserUtil.getLiteralValue(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralValue) ParserUtil.literalType(org.drools.impact.analysis.parser.impl.ParserUtil.literalType) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) ParserUtil.objectCreationExprToValue(org.drools.impact.analysis.parser.impl.ParserUtil.objectCreationExprToValue) Method(java.lang.reflect.Method) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ParserUtil.stripEnclosedAndCast(org.drools.impact.analysis.parser.impl.ParserUtil.stripEnclosedAndCast) ClassUtils(org.drools.core.util.ClassUtils) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Collectors(java.util.stream.Collectors) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Objects(java.util.Objects) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) InsertAction(org.drools.impact.analysis.model.right.InsertAction) Consequence(org.drools.modelcompiler.builder.generator.Consequence) Optional(java.util.Optional) ConsequenceAction(org.drools.impact.analysis.model.right.ConsequenceAction) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) Method(java.lang.reflect.Method) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) Expression(com.github.javaparser.ast.expr.Expression) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Map(java.util.Map) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 39 with ObjectCreationExpr

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

the class DrlxParseUtil method getExpressionType.

public static java.lang.reflect.Type getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
    if (expr instanceof LiteralExpr) {
        return getLiteralExpressionType((LiteralExpr) expr);
    }
    if (expr instanceof UnaryExpr) {
        return getExpressionType(context, typeResolver, expr.asUnaryExpr().getExpression(), usedDeclarations);
    }
    if (expr instanceof ArrayAccessExpr) {
        return getClassFromContext(typeResolver, ((ArrayCreationExpr) ((ArrayAccessExpr) expr).getName()).getElementType().asString());
    }
    if (expr instanceof ArrayCreationExpr) {
        return getClassFromContext(typeResolver, ((ArrayCreationExpr) expr).getElementType().asString());
    }
    if (expr instanceof MapCreationLiteralExpression) {
        return Map.class;
    }
    if (expr instanceof ListCreationLiteralExpression) {
        return List.class;
    }
    if (expr instanceof NameExpr) {
        return expressionTypeNameExpr(context, usedDeclarations, ((NameExpr) expr).getNameAsString());
    }
    if (expr instanceof DrlNameExpr) {
        return expressionTypeNameExpr(context, usedDeclarations, ((DrlNameExpr) expr).getNameAsString());
    }
    if (expr instanceof BinaryExpr) {
        return boolean.class;
    }
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
        Optional<Expression> scopeExpression = methodCallExpr.getScope();
        if (scopeExpression.isPresent()) {
            java.lang.reflect.Type scopeType = getExpressionType(context, typeResolver, scopeExpression.get(), usedDeclarations);
            return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
        } else {
            throw new IllegalStateException("Scope expression is not present for " + ((MethodCallExpr) expr).getNameAsString() + "!");
        }
    }
    if (expr instanceof ObjectCreationExpr) {
        final ClassOrInterfaceType type = ((ObjectCreationExpr) expr).getType();
        return getClassFromContext(typeResolver, type.asString());
    }
    if (expr.isCastExpr()) {
        String typeName = expr.asCastExpr().getType().toString();
        try {
            return typeResolver.resolveType(expr.asCastExpr().getType().toString());
        } catch (ClassNotFoundException e) {
            context.addCompilationError(new InvalidExpressionErrorResult("Unknown type in cast expression: " + typeName));
            throw new RuntimeException("Unknown type in cast expression: " + typeName);
        }
    }
    if (expr instanceof ConditionalExpr) {
        ConditionalExpr ternaryExpr = ((ConditionalExpr) expr);
        java.lang.reflect.Type conditionType = getExpressionType(context, typeResolver, ternaryExpr.getCondition(), usedDeclarations);
        if (conditionType != Boolean.class && conditionType != boolean.class) {
            context.addCompilationError(new InvalidExpressionErrorResult("Condtion used in ternary expression '" + expr + "' isn't boolean"));
            return Object.class;
        }
        java.lang.reflect.Type leftType = getExpressionType(context, typeResolver, ternaryExpr.getThenExpr(), usedDeclarations);
        java.lang.reflect.Type rightType = getExpressionType(context, typeResolver, ternaryExpr.getElseExpr(), usedDeclarations);
        Class<?> leftClass = toRawClass(leftType);
        Class<?> rightClass = toRawClass(rightType);
        if (leftClass.isAssignableFrom(rightClass)) {
            return leftType;
        }
        if (rightClass.isAssignableFrom(leftClass)) {
            return rightType;
        }
        return Object.class;
    }
    if (expr.isClassExpr()) {
        return Class.class;
    }
    throw new RuntimeException("Unknown expression type: " + PrintUtil.printNode(expr));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Collectors.toList(java.util.stream.Collectors.toList) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) Expression(com.github.javaparser.ast.expr.Expression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) ClassUtil.toRawClass(org.drools.modelcompiler.util.ClassUtil.toRawClass) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 40 with ObjectCreationExpr

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

the class FromVisitor method createSingleFrom.

private Optional<Expression> createSingleFrom(String expression) {
    final Expression parsedExpression = DrlxParseUtil.parseExpression(expression).getExpr();
    if (parsedExpression instanceof FieldAccessExpr || parsedExpression instanceof NameExpr || parsedExpression instanceof DrlNameExpr) {
        return fromFieldOrName(expression);
    }
    if (parsedExpression instanceof ObjectCreationExpr) {
        return fromConstructorExpr(expression, (ObjectCreationExpr) parsedExpression);
    }
    if (parsedExpression instanceof LiteralExpr) {
        MethodCallExpr fromCall = createDslTopLevelMethod(FROM_CALL);
        fromCall.addArgument(parsedExpression);
        return of(fromCall);
    }
    return fromExpression(expression, parsedExpression);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)83 Expression (com.github.javaparser.ast.expr.Expression)59 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)44 NameExpr (com.github.javaparser.ast.expr.NameExpr)41 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)38 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)31 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)30 NodeList (com.github.javaparser.ast.NodeList)24 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)24 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)21 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)19 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14 List (java.util.List)13 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)12 Type (com.github.javaparser.ast.type.Type)12 Test (org.junit.Test)12 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)11 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10