Search in sources :

Example 31 with BinaryExpr

use of com.github.javaparser.ast.expr.BinaryExpr 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 32 with BinaryExpr

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

the class PropagatorCompilerHandler method startHashedAlphaNodes.

@Override
public void startHashedAlphaNodes(IndexableConstraint indexableConstraint) {
    final InternalReadAccessor fieldExtractor = indexableConstraint.getFieldExtractor();
    fieldType = fieldExtractor.getExtractToClass();
    final SwitchStmt switchStmt;
    final Statement nullCheck;
    if (canInlineValue(fieldType)) {
        String switchVariableName = "switchVar";
        ExpressionStmt switchVariable = localVariableWithCastInitializer(toJPType(fieldType), switchVariableName, parseExpression("readAccessor.getValue(fact)"));
        this.allStatements.addStatement(switchVariable);
        switchStmt = new SwitchStmt().setSelector(new NameExpr(switchVariableName));
        if (fieldType.isPrimitive()) {
            nullCheck = new BlockStmt().addStatement(switchStmt);
        } else {
            nullCheck = new IfStmt().setCondition(new BinaryExpr(new NameExpr(switchVariableName), new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS)).setThenStmt(switchStmt);
        }
    } else {
        // Hashable but not inlinable
        String localVariableName = "NodeId";
        ExpressionStmt expressionStmt = localVariableWithCastInitializer(parseType("java.lang.Integer"), localVariableName, parseExpression("ToNodeId.get(readAccessor.getValue(fact))"));
        this.allStatements.addStatement(expressionStmt);
        switchStmt = new SwitchStmt().setSelector(new MethodCallExpr(new NameExpr(localVariableName), "intValue", nodeList()));
        // ensure that the value is present in the node map
        nullCheck = new IfStmt().setCondition(new BinaryExpr(new NameExpr(localVariableName), new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS)).setThenStmt(switchStmt);
    }
    this.allStatements.addStatement(nullCheck);
    this.currentStatement.push(switchStmt);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) IfStmt(com.github.javaparser.ast.stmt.IfStmt) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 33 with BinaryExpr

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

the class DroolsMvelParserTest method testAndWithImplicitParameterAndParenthesisComplexOnField.

@Test
public void testAndWithImplicitParameterAndParenthesisComplexOnField() {
    String expr = "value.length ((> 1 && < 2) || (> 3 && < 4))";
    Expression expression = parseExpression(parser, expr).getExpr();
    BinaryExpr comboExpr = ((BinaryExpr) expression);
    assertEquals(Operator.OR, comboExpr.getOperator());
    BinaryExpr comboExprLeft = ((BinaryExpr) comboExpr.getLeft());
    assertEquals(Operator.AND, comboExprLeft.getOperator());
    BinaryExpr first = (BinaryExpr) comboExprLeft.getLeft();
    assertTrue(first.getLeft() instanceof FieldAccessExpr);
    assertEquals("value.length", toString(first.getLeft()));
    assertEquals("1", toString(first.getRight()));
    assertEquals(Operator.GREATER, first.getOperator());
    HalfBinaryExpr second = (HalfBinaryExpr) comboExprLeft.getRight();
    assertEquals("2", toString(second.getRight()));
    assertEquals(HalfBinaryExpr.Operator.LESS, second.getOperator());
    BinaryExpr comboExprRight = ((BinaryExpr) comboExpr.getRight());
    assertEquals(Operator.AND, comboExprRight.getOperator());
    BinaryExpr third = (BinaryExpr) comboExprRight.getLeft();
    assertEquals("value.length", toString(third.getLeft()));
    assertEquals("3", toString(third.getRight()));
    assertEquals(Operator.GREATER, third.getOperator());
    HalfBinaryExpr forth = (HalfBinaryExpr) comboExprRight.getRight();
    assertEquals("4", toString(forth.getRight()));
    assertEquals(HalfBinaryExpr.Operator.LESS, forth.getOperator());
}
Also used : HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) Test(org.junit.Test)

Example 34 with BinaryExpr

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

the class DroolsMvelParserTest method testAndWithImplicitNegativeParameter.

@Test
public void testAndWithImplicitNegativeParameter() {
    String expr = "value > -2 && < -1";
    Expression expression = parseExpression(parser, expr).getExpr();
    BinaryExpr comboExpr = ((BinaryExpr) expression);
    assertEquals(Operator.AND, comboExpr.getOperator());
    BinaryExpr first = (BinaryExpr) comboExpr.getLeft();
    assertEquals("value", toString(first.getLeft()));
    assertEquals("-2", toString(first.getRight()));
    assertEquals(Operator.GREATER, first.getOperator());
    HalfBinaryExpr second = (HalfBinaryExpr) comboExpr.getRight();
    assertEquals("-1", toString(second.getRight()));
    assertEquals(HalfBinaryExpr.Operator.LESS, second.getOperator());
}
Also used : HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Test(org.junit.Test)

Example 35 with BinaryExpr

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

the class DroolsMvelParserTest method testParseSimpleExpr.

@Test
public void testParseSimpleExpr() {
    String expr = "name == \"Mark\"";
    Expression expression = parseExpression(parser, expr).getExpr();
    BinaryExpr binaryExpr = ((BinaryExpr) expression);
    assertEquals("name", toString(binaryExpr.getLeft()));
    assertEquals("\"Mark\"", toString(binaryExpr.getRight()));
    assertEquals(Operator.EQUALS, binaryExpr.getOperator());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Test(org.junit.Test)

Aggregations

BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)43 Expression (com.github.javaparser.ast.expr.Expression)37 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)33 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)30 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)20 Test (org.junit.Test)20 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)12 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)10 NameExpr (com.github.javaparser.ast.expr.NameExpr)9 SimpleName (com.github.javaparser.ast.expr.SimpleName)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)8 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)7 ArrayList (java.util.ArrayList)7 Node (com.github.javaparser.ast.Node)6 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)6 NodeList (com.github.javaparser.ast.NodeList)5 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)5 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)5 ListCreationLiteralExpression (org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression)5