Search in sources :

Example 56 with Expression

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

the class AccumulateVisitor method getRootNodeName.

private String getRootNodeName(DrlxParseUtil.RemoveRootNodeResult methodCallWithoutRootNode) {
    final Expression rootNode = methodCallWithoutRootNode.getRootNode().orElseThrow(UnsupportedOperationException::new);
    final String rootNodeName;
    if (rootNode instanceof NameExpr) {
        rootNodeName = ((NameExpr) rootNode).getName().asString();
    } else {
        throw new AccumulateParsingFailedException("Root node of expression should be a declaration");
    }
    return rootNodeName;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr)

Example 57 with Expression

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

the class FromVisitor method fromFieldOrName.

private Optional<Expression> fromFieldOrName(String expression) {
    Optional<String> optContainsBinding = DrlxParseUtil.findBindingIdFromDotExpression(expression);
    final String bindingId = optContainsBinding.orElse(expression);
    final DrlxExpression drlxExpression = DrlxParseUtil.parseExpression(expression);
    final Expression parsedExpression = drlxExpression.getExpr();
    Optional<TypedExpression> staticField = parsedExpression instanceof FieldAccessExpr ? ExpressionTyper.tryParseAsConstantField(context.getTypeResolver(), ((FieldAccessExpr) parsedExpression).getScope(), ((FieldAccessExpr) parsedExpression).getNameAsString()) : Optional.empty();
    if (staticField.isPresent()) {
        return of(createSupplier(parsedExpression));
    }
    if (packageModel.hasEntryPoint(bindingId)) {
        return of(createEntryPointCall(bindingId));
    }
    if (contextHasDeclaration(bindingId)) {
        return of(createFromCall(expression, bindingId, optContainsBinding.isPresent(), null));
    }
    return of(createUnitDataCall(bindingId));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 58 with Expression

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

the class FromVisitor method createArg.

private Expression createArg(String expression, String bindingId, MethodCallExpr fromCall) {
    if (bindingId != null) {
        DeclarationSpec declarationSpec = context.getDeclarationById(bindingId).orElseThrow(RuntimeException::new);
        Class<?> clazz = declarationSpec.getDeclarationClass();
        DrlxParseResult drlxParseResult = ConstraintParser.withoutVariableValidationConstraintParser(context, packageModel).drlxParse(clazz, bindingId, expression);
        return drlxParseResult.acceptWithReturnValue(drlxParseSuccess -> {
            SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) drlxParseResult;
            if (!isCompatibleWithFromReturnType(patternType, singleResult.getExprRawClass())) {
                context.addCompilationError(new InvalidExpressionErrorResult("Pattern of type: '" + patternType.getCanonicalName() + "' on rule '" + context.getRuleName() + "' is not compatible with type " + singleResult.getExprRawClass().getCanonicalName() + " returned by source"));
            }
            Expression parsedExpression = drlxParseSuccess.getExpr();
            Expression newExpr = generateLambdaWithoutParameters(singleResult.getUsedDeclarations(), parsedExpression, singleResult.isSkipThisAsParam(), ofNullable(singleResult.getPatternType()), context);
            if (newExpr instanceof LambdaExpr) {
                context.getPackageModel().registerLambdaReturnType((LambdaExpr) newExpr, singleResult.getExprType());
            }
            addArgumentWithPreexistingCheck(fromCall, singleResult.getUsedDeclarations());
            return newExpr;
        });
    }
    return null;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)

Example 59 with Expression

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

the class SingleDrlxParseSuccess method combineWith.

@Override
public DrlxParseResult combineWith(DrlxParseResult other, BinaryExpr.Operator operator) {
    if (!other.isSuccess()) {
        return other;
    }
    SingleDrlxParseSuccess otherDrlx = (SingleDrlxParseSuccess) other;
    Collection<String> newUsedDeclarations = new LinkedHashSet<>();
    newUsedDeclarations.addAll(this.usedDeclarations);
    newUsedDeclarations.addAll(otherDrlx.usedDeclarations);
    Collection<String> newUsedDeclarationsOnLeft = null;
    if (this.usedDeclarationsOnLeft != null && otherDrlx.usedDeclarationsOnLeft != null) {
        newUsedDeclarationsOnLeft = new LinkedHashSet<>();
        newUsedDeclarationsOnLeft.addAll(this.usedDeclarationsOnLeft);
        newUsedDeclarationsOnLeft.addAll(otherDrlx.usedDeclarationsOnLeft);
    }
    Set<String> newReactOnProperties = new HashSet<>();
    newReactOnProperties.addAll(this.reactOnProperties);
    newReactOnProperties.addAll(otherDrlx.reactOnProperties);
    List<Expression> newNullSafeExpressions = new ArrayList<>();
    if (operator == BinaryExpr.Operator.OR) {
        // NullSafeExpressions are combined here because the order is complex
        this.expr = combinePredicatesWithAnd(this.expr, this.nullSafeExpressions);
        otherDrlx.expr = combinePredicatesWithAnd(otherDrlx.expr, otherDrlx.nullSafeExpressions);
        // Also combine implicitCast earlier than null-check
        this.expr = combinePredicatesWithAnd(this.expr, StreamUtils.optionalToList(this.implicitCastExpression));
        otherDrlx.expr = combinePredicatesWithAnd(otherDrlx.expr, StreamUtils.optionalToList(otherDrlx.implicitCastExpression));
    } else {
        // NullSafeExpressions will be added by PatternDSL.addNullSafeExpr
        newNullSafeExpressions.addAll(this.nullSafeExpressions);
        newNullSafeExpressions.addAll(otherDrlx.nullSafeExpressions);
    }
    return new SingleDrlxParseSuccess(patternType, patternBinding, new EnclosedExpr(new BinaryExpr(expr, otherDrlx.expr, operator)), exprType).setDecodeConstraintType(Index.ConstraintType.UNKNOWN).setUsedDeclarations(newUsedDeclarations).setUsedDeclarationsOnLeft(newUsedDeclarationsOnLeft).setUnification(this.isUnification() || otherDrlx.isUnification()).setCombined(true).setReactOnProperties(newReactOnProperties).setBetaConstraint(betaConstraint).setLeft(new TypedExpression(this.expr, left != null ? left.getType() : boolean.class)).setRight(new TypedExpression(otherDrlx.expr, right != null ? right.getType() : boolean.class)).setBoundExpr(left).setIsPredicate(this.isPredicate && otherDrlx.isPredicate).setNullSafeExpressions(newNullSafeExpressions).setExprBinding(// only left exprBinding
    this.exprBinding);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ArrayList(java.util.ArrayList) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Expression(com.github.javaparser.ast.expr.Expression) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 60 with Expression

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

the class AbstractExpressionBuilder method processExpression.

public void processExpression(MultipleDrlxParseSuccess drlxParseResult) {
    if (drlxParseResult.isPredicate()) {
        Expression dslExpr = buildExpressionWithIndexing(drlxParseResult);
        context.addExpression(dslExpr);
    }
}
Also used : DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) Expression(com.github.javaparser.ast.expr.Expression) CoercedExpression(org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

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