Search in sources :

Example 1 with BIND_CALL

use of org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_CALL in project drools by kiegroup.

the class AccumulateVisitor method composeTwoBindings.

private void composeTwoBindings(String binding, MethodCallExpr newBindingExpression) {
    context.findBindingExpression(binding).ifPresent(oldBind -> {
        // compose newComposedBinding using oldBind and newBindingExpression. But still keep oldBind.
        LambdaExpr oldBindLambda = oldBind.findFirst(LambdaExpr.class).orElseThrow(RuntimeException::new);
        LambdaExpr newBindLambda = newBindingExpression.findFirst(LambdaExpr.class).orElseThrow(RuntimeException::new);
        LambdaExpr tmpOldBindLambda = oldBindLambda.clone();
        Expression newComposedLambda = LambdaUtil.appendNewLambdaToOld(tmpOldBindLambda, newBindLambda);
        MethodCallExpr newComposedBinding = new MethodCallExpr(BIND_CALL, newBindingExpression.getArgument(0), newComposedLambda);
        newComposedBinding.setScope(oldBind.getScope().orElseThrow(RuntimeException::new));
        Optional<MethodCallExpr> optReactOn = oldBind.getArguments().stream().filter(MethodCallExpr.class::isInstance).map(MethodCallExpr.class::cast).filter(exp -> exp.getName().asString().equals(REACT_ON_CALL)).findFirst();
        if (optReactOn.isPresent()) {
            newComposedBinding.addArgument(optReactOn.get().clone());
        }
        // insert newComposedBinding at the first in the chain
        oldBind.setScope(newComposedBinding);
    });
}
Also used : REACT_ON_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.REACT_ON_CALL) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ACCUMULATE_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ACCUMULATE_CALL) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) PatternDescr(org.drools.drl.ast.descr.PatternDescr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) AccumulateUtil(org.drools.compiler.rule.builder.util.AccumulateUtil) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) Expression(com.github.javaparser.ast.expr.Expression) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) PatternExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.PatternExpressionBuilder) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) Node(com.github.javaparser.ast.Node) PackageModel(org.drools.modelcompiler.builder.PackageModel) NodeList(com.github.javaparser.ast.NodeList) AND_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.AND_CALL) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) LambdaUtil(org.drools.modelcompiler.util.LambdaUtil) BaseDescr(org.drools.drl.ast.descr.BaseDescr) VALUE_OF_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.VALUE_OF_CALL) Objects(java.util.Objects) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DrlxParseUtil.validateDuplicateBindings(org.drools.modelcompiler.builder.generator.DrlxParseUtil.validateDuplicateBindings) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AbstractExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.AbstractExpressionBuilder) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Parameter(com.github.javaparser.ast.body.Parameter) Pattern(org.drools.core.rule.Pattern) ArrayList(java.util.ArrayList) ACC_FUNCTION_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ACC_FUNCTION_CALL) HashSet(java.util.HashSet) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) DslMethodNames.createDslTopLevelMethod(org.drools.modelcompiler.builder.generator.DslMethodNames.createDslTopLevelMethod) AndDescr(org.drools.drl.ast.descr.AndDescr) ReplaceTypeInLambda.replaceTypeInExprLambda(org.drools.modelcompiler.util.lambdareplace.ReplaceTypeInLambda.replaceTypeInExprLambda) BIND_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_CALL) Optional.ofNullable(java.util.Optional.ofNullable) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_AS_CALL) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) Collectors.toList(java.util.stream.Collectors.toList) CollectAccumulator(org.drools.core.base.accumulators.CollectAccumulator) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) Collections(java.util.Collections) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1 Parameter (com.github.javaparser.ast.body.Parameter)1 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)1 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1