Search in sources :

Example 66 with MethodCallExpr

use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class OrVisitor method visit.

public void visit(ConditionalElementDescr descr, String methodName) {
    final MethodCallExpr ceDSL = new MethodCallExpr(null, methodName);
    context.addExpression(ceDSL);
    for (BaseDescr subDescr : descr.getDescrs()) {
        final MethodCallExpr andDSL = new MethodCallExpr(null, "and");
        context.pushExprPointer(andDSL::addArgument);
        subDescr.accept(modelGeneratorVisitor);
        context.popExprPointer();
        ceDSL.addArgument(andDSL);
    }
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 67 with MethodCallExpr

use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class AccumulateVisitor method visit.

protected Optional<AccumulateVisitorPatternDSL.NewBinding> visit(RuleContext context, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr accumulateDSL, PatternDescr basePattern, boolean inputPatternHasConstraints) {
    context.pushExprPointer(accumulateDSL::addArgument);
    final MethodCallExpr functionDSL = new MethodCallExpr(null, "accFunction");
    final String expression = function.getParams()[0];
    final Expression expr = DrlxParseUtil.parseExpression(expression).getExpr();
    final String bindingId = Optional.ofNullable(function.getBind()).orElse(basePattern.getIdentifier());
    Optional<AccumulateVisitorPatternDSL.NewBinding> newBinding = Optional.empty();
    if (expr instanceof BinaryExpr) {
        final DrlxParseResult parseResult = new ConstraintParser(context, packageModel).drlxParse(Object.class, bindingId, expression);
        newBinding = parseResult.acceptWithReturnValue(new ParseResultVisitor<Optional<AccumulateVisitorPatternDSL.NewBinding>>() {

            @Override
            public Optional<AccumulateVisitorPatternDSL.NewBinding> onSuccess(DrlxParseSuccess drlxParseResult) {
                final AccumulateFunction accumulateFunction = AccumulateVisitor.this.getAccumulateFunction(function, drlxParseResult.getExprType());
                final String bindExpressionVariable = context.getExprId(accumulateFunction.getResultType(), drlxParseResult.getLeft().toString());
                drlxParseResult.setExprBinding(bindExpressionVariable);
                context.addDeclarationReplacing(new DeclarationSpec(drlxParseResult.getPatternBinding(), drlxParseResult.getExprType()));
                functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
                final MethodCallExpr newBindingFromBinary = AccumulateVisitor.this.buildBinding(bindExpressionVariable, drlxParseResult.getUsedDeclarations(), drlxParseResult.getExpr());
                context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, drlxParseResult.getExprType()));
                functionDSL.addArgument(new NameExpr(toVar(bindExpressionVariable)));
                return Optional.of(new AccumulateVisitorPatternDSL.NewBinding(Optional.empty(), newBindingFromBinary));
            }

            @Override
            public Optional<AccumulateVisitorPatternDSL.NewBinding> onFail(DrlxParseFail failure) {
                return Optional.empty();
            }
        });
    } else if (expr instanceof MethodCallExpr) {
        final DrlxParseUtil.RemoveRootNodeResult methodCallWithoutRootNode = DrlxParseUtil.removeRootNode(expr);
        final String rootNodeName = getRootNodeName(methodCallWithoutRootNode);
        final TypedExpression typedExpression = parseMethodCallType(context, rootNodeName, methodCallWithoutRootNode.getWithoutRootNode());
        final Class<?> methodCallExprType = typedExpression.getType();
        final AccumulateFunction accumulateFunction = getAccumulateFunction(function, methodCallExprType);
        final Class accumulateFunctionResultType = accumulateFunction.getResultType();
        functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
        // Every expression in an accumulate function gets transformed in a bind expression with a generated id
        // Then the accumulate function will have that binding expression as a source
        final String bindExpressionVariable = context.getExprId(accumulateFunctionResultType, typedExpression.toString());
        Expression withThis = DrlxParseUtil.prepend(DrlxParseUtil._THIS_EXPR, typedExpression.getExpression());
        DrlxParseSuccess result = new DrlxParseSuccess(accumulateFunctionResultType, "", rootNodeName, withThis, accumulateFunctionResultType).setLeft(typedExpression).setExprBinding(bindExpressionVariable);
        final MethodCallExpr binding = expressionBuilder.buildBinding(result);
        newBinding = Optional.of(new AccumulateVisitorPatternDSL.NewBinding(Optional.of(result.getPatternBinding()), binding));
        context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, methodCallExprType));
        functionDSL.addArgument(new NameExpr(toVar(bindExpressionVariable)));
        context.addDeclarationReplacing(new DeclarationSpec(bindingId, accumulateFunctionResultType));
    } else if (expr instanceof NameExpr) {
        final Class<?> declarationClass = context.getDeclarationById(expr.toString()).orElseThrow(RuntimeException::new).getDeclarationClass();
        final String nameExpr = ((NameExpr) expr).getName().asString();
        final AccumulateFunction accumulateFunction = getAccumulateFunction(function, declarationClass);
        functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
        functionDSL.addArgument(new NameExpr(toVar(nameExpr)));
        Class accumulateFunctionResultType = accumulateFunction.getResultType();
        if (accumulateFunctionResultType == Comparable.class && (Comparable.class.isAssignableFrom(declarationClass) || declarationClass.isPrimitive())) {
            accumulateFunctionResultType = declarationClass;
        }
        context.addDeclarationReplacing(new DeclarationSpec(bindingId, accumulateFunctionResultType));
    } else {
        throw new UnsupportedOperationException("Unsupported expression " + expr);
    }
    final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, "as");
    asDSL.addArgument(new NameExpr(toVar(bindingId)));
    accumulateDSL.addArgument(asDSL);
    context.popExprPointer();
    return newBinding;
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 68 with MethodCallExpr

use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class AccumulateVisitorFlowDSL method buildBinding.

@Override
protected MethodCallExpr buildBinding(String bindingName, Collection<String> usedDeclaration, Expression expression) {
    MethodCallExpr bindDSL = new MethodCallExpr(null, BIND_CALL);
    bindDSL.addArgument(toVar(bindingName));
    MethodCallExpr bindAsDSL = new MethodCallExpr(bindDSL, BIND_AS_CALL);
    usedDeclaration.stream().map(d -> new NameExpr(toVar(d))).forEach(bindAsDSL::addArgument);
    bindAsDSL.addArgument(buildConstraintExpression(expression, usedDeclaration));
    return bindAsDSL;
}
Also used : PackageModel(org.drools.modelcompiler.builder.PackageModel) Parameter(org.drools.javaparser.ast.body.Parameter) Expression(org.drools.javaparser.ast.expr.Expression) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.ModelGenerator.BIND_AS_CALL) FlowExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder) Collection(java.util.Collection) BIND_CALL(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder.BIND_CALL) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseUtil.fromVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.fromVar) ArrayList(java.util.ArrayList) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) Optional(java.util.Optional) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 69 with MethodCallExpr

use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class AccumulateVisitorFlowDSL method processNewBinding.

@Override
protected void processNewBinding(Optional<NewBinding> optNewBinding) {
    optNewBinding.ifPresent(newBinding -> {
        final Optional<String> patterBinding = newBinding.patternBinding;
        final List<Expression> allExpressions = context.getExpressions();
        final MethodCallExpr newBindingExpression = newBinding.bindExpression;
        replaceBindingWithPatternBinding(newBindingExpression, findLastPattern(allExpressions));
        newBindingResults.add(newBinding);
    });
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 70 with MethodCallExpr

use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class AccumulateVisitorPatternDSL method processNewBinding.

@Override
protected void processNewBinding(Optional<NewBinding> optNewBinding) {
    optNewBinding.ifPresent(newBinding -> {
        final Optional<String> patterBinding = newBinding.patternBinding;
        final List<Expression> allExpressions = context.getExpressions();
        final MethodCallExpr newBindingExpression = newBinding.bindExpression;
        if (patterBinding.isPresent()) {
            final Optional<MethodCallExpr> optPattern = DrlxParseUtil.findPatternWithBinding(patterBinding.get(), allExpressions);
            optPattern.ifPresent(pattern -> addBindAsLastChainCall(newBindingExpression, pattern));
        } else {
            final MethodCallExpr lastPattern = DrlxParseUtil.findLastPattern(allExpressions).orElseThrow(() -> new RuntimeException("Need the last pattern to add the binding"));
            final MethodCallExpr replacedBinding = replaceBindingWithPatternBinding(newBindingExpression, lastPattern);
            addBindAsLastChainCall(replacedBinding, lastPattern);
        }
    });
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)75 NameExpr (org.drools.javaparser.ast.expr.NameExpr)36 Expression (org.drools.javaparser.ast.expr.Expression)32 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)26 ArrayList (java.util.ArrayList)16 List (java.util.List)15 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)15 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)14 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)13 PackageModel (org.drools.modelcompiler.builder.PackageModel)13 Parameter (org.drools.javaparser.ast.body.Parameter)11 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)11 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)11 Collection (java.util.Collection)10 Optional (java.util.Optional)10 Type (org.drools.javaparser.ast.type.Type)10 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)10 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)9 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)9 JavaParser (org.drools.javaparser.JavaParser)8