Search in sources :

Example 6 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class GroupByVisitor method processAccumulateFunctions.

protected void processAccumulateFunctions(AccumulateDescr descr, PatternDescr basePattern, BaseDescr input, MethodCallExpr accumulateDSL) {
    accumulateDSL.setName(GROUP_BY_CALL);
    GroupByDescr groupByDescr = (GroupByDescr) descr;
    Expression expr = parseExpression(groupByDescr.getGroupingFunction());
    TypedExpressionResult result = new ExpressionTyper(context).toTypedExpression(expr);
    Optional<TypedExpression> optResult = result.getTypedExpression();
    if (!optResult.isPresent()) {
        context.addCompilationError(new InvalidExpressionErrorResult("Unable to parse grouping expression: " + groupByDescr.getGroupingFunction()));
        return;
    }
    for (String used : result.getUsedDeclarations()) {
        accumulateDSL.addArgument(context.getVarExpr(used));
    }
    TypedExpression typedExpression = optResult.get();
    String groupingKey = groupByDescr.getGroupingKey() != null ? groupByDescr.getGroupingKey() : generateUUID();
    context.addDeclaration(new DeclarationSpec(groupingKey, typedExpression.getRawClass()));
    accumulateDSL.addArgument(toVar(groupingKey));
    accumulateDSL.addArgument(buildConstraintExpression(typedExpression.getExpression(), result.getUsedDeclarations()));
    super.processAccumulateFunctions(descr, basePattern, input, accumulateDSL);
}
Also used : InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Expression(com.github.javaparser.ast.expr.Expression) GroupByDescr(org.drools.drl.ast.descr.GroupByDescr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 7 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ConstraintParser method parseFieldAccessExpr.

private DrlxParseResult parseFieldAccessExpr(FieldAccessExpr fieldCallExpr, Class<?> patternType, String bindingId) {
    final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
    final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, false, expressionTyperContext);
    TypedExpressionResult typedExpressionResult = expressionTyper.toTypedExpression(fieldCallExpr);
    Optional<TypedExpression> typedExpression = typedExpressionResult.getTypedExpression();
    if (!typedExpression.isPresent()) {
        return new DrlxParseFail();
    }
    TypedExpression converted = typedExpression.get();
    Type type = converted.getType();
    boolean isPredicate = isBooleanBoxedUnboxed(type);
    Expression combo = converted.getExpression();
    if (isPredicate) {
        combo = combineExpressions(typedExpressionResult, combo);
    }
    Expression withThis = DrlxParseUtil.prepend(new NameExpr(THIS_PLACEHOLDER), combo);
    return new SingleDrlxParseSuccess(patternType, bindingId, withThis, type).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setLeft(converted).setImplicitCastExpression(expressionTyperContext.getInlineCastExpression()).setIsPredicate(isPredicate);
}
Also used : ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) Type(java.lang.reflect.Type) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) 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) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 8 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ConstraintParser method parseUnaryExpr.

private DrlxParseResult parseUnaryExpr(UnaryExpr unaryExpr, Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional) {
    TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(unaryExpr);
    Optional<TypedExpression> opt = typedExpressionResult.getTypedExpression();
    if (!opt.isPresent()) {
        return new DrlxParseFail(new ParseExpressionErrorResult(drlxExpr));
    }
    TypedExpression typedExpression = opt.get();
    SingleDrlxParseSuccess innerResult = (SingleDrlxParseSuccess) compileToJavaRecursive(patternType, bindingId, constraint, unaryExpr.getExpression(), hasBind, isPositional);
    Expression innerExpression;
    if (unaryExpr.getExpression() instanceof EnclosedExpr && !(innerResult.getExpr() instanceof EnclosedExpr)) {
        // inner EnclosedExpr could be stripped
        innerExpression = new EnclosedExpr(innerResult.getExpr());
    } else {
        innerExpression = innerResult.getExpr();
    }
    return new SingleDrlxParseSuccess(patternType, bindingId, new UnaryExpr(innerExpression, unaryExpr.getOperator()), typedExpression.getType()).setDecodeConstraintType(Index.ConstraintType.UNKNOWN).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(new TypedExpression(innerResult.getExpr(), innerResult.getExprType())).setIsPredicate(innerResult.isPredicate());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr)

Example 9 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult 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 10 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ConstraintParser method parseOOPathExpr.

private DrlxParseResult parseOOPathExpr(OOPathExpr ooPathExpr, Class<?> patternType, String bindingId, Expression drlxExpr, boolean hasBind, String expression) {
    Type exprType = null;
    if (hasBind) {
        // if oopath expression isn't bound it is useless to discover its type
        final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, false, new ExpressionTyperContext());
        TypedExpressionResult typedExpressionResult = expressionTyper.toTypedExpression(ooPathExpr);
        Optional<TypedExpression> typedExpression = typedExpressionResult.getTypedExpression();
        if (!typedExpression.isPresent()) {
            return new DrlxParseFail();
        }
        exprType = typedExpression.get().getType();
    }
    return new SingleDrlxParseSuccess(patternType, bindingId, drlxExpr, exprType).setIsPredicate(true);
}
Also used : ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) Type(java.lang.reflect.Type) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Aggregations

ExpressionTyper (org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper)11 TypedExpressionResult (org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult)11 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8 Expression (com.github.javaparser.ast.expr.Expression)6 NameExpr (com.github.javaparser.ast.expr.NameExpr)5 ExpressionTyperContext (org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext)5 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)5 ArrayList (java.util.ArrayList)4 ParseExpressionErrorResult (org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult)4 DrlxParseUtil.getLiteralExpressionType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType)4 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)3 PointFreeExpr (org.drools.mvel.parser.ast.expr.PointFreeExpr)3 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)2 SimpleName (com.github.javaparser.ast.expr.SimpleName)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)2