Search in sources :

Example 16 with EnclosedExpr

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

the class ConstraintParser method parseBinaryExpr.

private DrlxParseResult parseBinaryExpr(BinaryExpr binaryExpr, Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional, boolean isEnclosed) {
    BinaryExpr.Operator operator = binaryExpr.getOperator();
    boolean isOrBinary = operator == BinaryExpr.Operator.OR;
    if (isLogicalOperator(operator) && isCombinable(binaryExpr)) {
        DrlxParseResult leftResult = compileToJavaRecursive(patternType, bindingId, constraint, binaryExpr.getLeft(), hasBind, isPositional);
        Expression rightExpr = binaryExpr.getRight() instanceof HalfPointFreeExpr ? completeHalfExpr(((PointFreeExpr) binaryExpr.getLeft()).getLeft(), (HalfPointFreeExpr) binaryExpr.getRight()) : binaryExpr.getRight();
        DrlxParseResult rightResult = compileToJavaRecursive(patternType, bindingId, constraint, rightExpr, hasBind, isPositional);
        return isMultipleResult(leftResult, operator, rightResult) ? createMultipleDrlxParseSuccess(operator, (DrlxParseSuccess) leftResult, (DrlxParseSuccess) rightResult) : leftResult.combineWith(rightResult, operator);
    }
    final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
    final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
    TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getLeft());
    Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
    if (!optLeft.isPresent()) {
        return new DrlxParseFail();
    }
    TypedExpression left = optLeft.get();
    List<String> usedDeclarationsOnLeft = hasBind ? new ArrayList<>(expressionTyperContext.getUsedDeclarations()) : null;
    List<Expression> leftPrefixExpressions = new ArrayList<>();
    if (isOrBinary) {
        leftPrefixExpressions.addAll(expressionTyperContext.getNullSafeExpressions());
        expressionTyperContext.getNullSafeExpressions().clear();
        leftPrefixExpressions.addAll(expressionTyperContext.getPrefixExpresssions());
        expressionTyperContext.getPrefixExpresssions().clear();
    }
    List<Expression> rightPrefixExpresssions = new ArrayList<>();
    TypedExpression right;
    if (constraint.isNameClashingUnification()) {
        String name = constraint.getUnificationField();
        right = new TypedExpression(new NameExpr(name), left.getType());
        expressionTyperContext.addUsedDeclarations(name);
    } else {
        TypedExpressionResult rightExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getRight());
        Optional<TypedExpression> optRight = rightExpressionResult.getTypedExpression();
        if (!optRight.isPresent()) {
            return new DrlxParseFail(new ParseExpressionErrorResult(drlxExpr));
        }
        right = optRight.get();
        if (isOrBinary) {
            rightPrefixExpresssions.addAll(expressionTyperContext.getNullSafeExpressions());
            expressionTyperContext.getNullSafeExpressions().clear();
            rightPrefixExpresssions.addAll(expressionTyperContext.getPrefixExpresssions());
            expressionTyperContext.getPrefixExpresssions().clear();
        }
    }
    boolean equalityExpr = operator == EQUALS || operator == NOT_EQUALS;
    CoercedExpression.CoercedExpressionResult coerced;
    try {
        coerced = new CoercedExpression(left, right, equalityExpr).coerce();
    } catch (CoercedExpression.CoercedExpressionException e) {
        return new DrlxParseFail(e.getInvalidExpressionErrorResult());
    }
    left = coerced.getCoercedLeft();
    right = getCoercedRightExpression(packageModel, coerced);
    Expression combo;
    boolean arithmeticExpr = asList(PLUS, MINUS, MULTIPLY, DIVIDE, REMAINDER).contains(operator);
    boolean isBetaConstraint = right.getExpression() != null && hasNonGlobalDeclaration(expressionTyperContext);
    boolean requiresSplit = operator == BinaryExpr.Operator.AND && binaryExpr.getRight() instanceof HalfBinaryExpr && !isBetaConstraint;
    if (equalityExpr) {
        combo = getEqualityExpression(left, right, operator).expression;
    } else if (arithmeticExpr && (left.isBigDecimal())) {
        ConstraintCompiler constraintCompiler = createConstraintCompiler(this.context, of(patternType));
        CompiledExpressionResult compiledExpressionResult = constraintCompiler.compileExpression(binaryExpr);
        combo = compiledExpressionResult.getExpression();
    } else {
        if (left.getExpression() == null || right.getExpression() == null) {
            return new DrlxParseFail(new ParseExpressionErrorResult(drlxExpr));
        }
        // This special comparisons
        SpecialComparisonResult specialComparisonResult = handleSpecialComparisonCases(expressionTyper, operator, left, right);
        combo = specialComparisonResult.expression;
        left = requiresSplit ? left : specialComparisonResult.coercedLeft;
        right = requiresSplit ? right : specialComparisonResult.coercedRight;
    }
    if (isOrBinary) {
        // NullSafeExpressions are combined here because the order is complex
        combo = combineExpressions(leftPrefixExpressions, rightPrefixExpresssions, combo);
    } else {
        // NullSafeExpressions will be added later by PatternDSL.addNullSafeExpr() which will be separated AlphaNodes
        combo = combineExpressions(leftTypedExpressionResult, combo);
    }
    boolean isPredicate = isPredicateBooleanExpression(binaryExpr);
    if (isEnclosed && !isPredicate) {
        combo = new EnclosedExpr(combo);
    }
    Index.ConstraintType constraintType = DrlxParseUtil.toConstraintType(operator);
    if (isForallSelfJoinConstraint(left, right, constraintType)) {
        constraintType = Index.ConstraintType.FORALL_SELF_JOIN;
    }
    return new SingleDrlxParseSuccess(patternType, bindingId, combo, isBooleanOperator(operator) ? boolean.class : left.getType()).setDecodeConstraintType(constraintType).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setUsedDeclarationsOnLeft(usedDeclarationsOnLeft).setUnification(constraint.isUnification()).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setLeft(left).setRight(right).setBetaConstraint(isBetaConstraint).setRequiresSplit(requiresSplit).setIsPredicate(isPredicate).setImplicitCastExpression(leftTypedExpressionResult.getInlineCastExpression()).setNullSafeExpressions(// This would be empty if NullSafeExpressions were combined earlier
    leftTypedExpressionResult.getNullSafeExpressions());
}
Also used : CompiledExpressionResult(org.drools.mvelcompiler.CompiledExpressionResult) ArrayList(java.util.ArrayList) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Index(org.drools.model.Index) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) DrlxParseUtil.createConstraintCompiler(org.drools.modelcompiler.builder.generator.DrlxParseUtil.createConstraintCompiler) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) PointFreeExpr(org.drools.mvel.parser.ast.expr.PointFreeExpr) HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 17 with EnclosedExpr

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

the class PreprocessPhase method addScopeToMethodCallExpr.

private Statement addScopeToMethodCallExpr(PreprocessPhaseResult result, Expression scope, Statement e) {
    if (e != null && e.isExpressionStmt()) {
        Expression expression = e.asExpressionStmt().getExpression();
        if (expression.isMethodCallExpr()) {
            MethodCallExpr mcExpr = expression.asMethodCallExpr();
            MethodCallExpr rootMcExpr = findRootScope(mcExpr);
            if (rootMcExpr == null) {
                return e;
            }
            Expression enclosed = new EnclosedExpr(scope);
            rootMcExpr.setScope(enclosed);
            if (scope.isNameExpr() || scope instanceof DrlNameExpr) {
                // some classes such "AtomicInteger" have a setter called "set"
                result.addUsedBinding(printNode(scope));
            }
            return new ExpressionStmt(mcExpr);
        }
    }
    return e;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 18 with EnclosedExpr

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

the class VariableDeclaratorTExpr method toJavaExpression.

@Override
public Node toJavaExpression() {
    Optional<Type> optInitType = initExpression.flatMap(TypedExpression::getType);
    com.github.javaparser.ast.type.Type jpType = toJPType(this.type);
    return initExpression.map(ie -> {
        Expression initializer = (Expression) ie.toJavaExpression();
        // Used to downcast map.get see testAddCastToMapGetOfDeclaration
        if (!optInitType.isPresent() || optInitType.get().equals(Object.class)) {
            initializer = new CastExpr(jpType, new EnclosedExpr(initializer));
        }
        return (Node) new VariableDeclarationExpr(new VariableDeclarator(jpType, name, initializer));
    }).orElse(new VariableDeclarationExpr(jpType, name));
}
Also used : VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Type(java.lang.reflect.Type) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) TypeUtils.toJPType(org.drools.mvelcompiler.util.TypeUtils.toJPType) CastExpr(com.github.javaparser.ast.expr.CastExpr) Node(com.github.javaparser.ast.Node) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Type(java.lang.reflect.Type) TypeUtils.toJPType(org.drools.mvelcompiler.util.TypeUtils.toJPType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 19 with EnclosedExpr

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

the class CastExprT method toJavaExpression.

@Override
public Node toJavaExpression() {
    com.github.javaparser.ast.type.Type jpType = StaticJavaParser.parseType(this.type.getCanonicalName());
    Expression expression = (Expression) innerExpr.toJavaExpression();
    return new EnclosedExpr(new CastExpr(jpType, expression));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 20 with EnclosedExpr

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

the class FieldToAccessorTExpr method convertToStringIfNeeded.

private Expression convertToStringIfNeeded(TypedExpression argumentExpression) {
    boolean argumentCanBeToStringed = argumentExpression.getType().filter(this::typeCanBeToStringed).isPresent();
    boolean fieldIsString = String.class.equals(this.type);
    if (fieldIsString && argumentCanBeToStringed) {
        return new MethodCallExpr(new EnclosedExpr((Expression) argumentExpression.toJavaExpression()), "toString");
    } else {
        return (Expression) argumentExpression.toJavaExpression();
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)25 Expression (com.github.javaparser.ast.expr.Expression)22 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)12 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)9 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8 CastExpr (com.github.javaparser.ast.expr.CastExpr)7 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)7 Node (com.github.javaparser.ast.Node)6 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)6 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)6 ArrayList (java.util.ArrayList)6 DrlxParseUtil.stripEnclosedExpr (org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr)6 ParseExpressionErrorResult (org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult)5 SimpleName (com.github.javaparser.ast.expr.SimpleName)4 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)4 Optional (java.util.Optional)4 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)4