Search in sources :

Example 11 with ClassExpr

use of org.drools.javaparser.ast.expr.ClassExpr 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 12 with ClassExpr

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

the class LegacyAccumulate method build.

public void build() {
    new PatternBuilder().build(ruleBuildContext, basePattern);
    final Set<String> imports = ruleBuildContext.getPkg().getImports().keySet();
    final String packageName = ruleBuildContext.getPkg().getName();
    GeneratedClassWithPackage generatedClassWithPackage = createAllAccumulateClass(imports, packageName);
    packageModel.addGeneratedAccumulateClasses(generatedClassWithPackage);
    GeneratedClassWithPackage invokerGenerated = createInvokerClass(imports, packageName);
    packageModel.addGeneratedAccumulateClasses(invokerGenerated);
    final String typeWithPackage = String.format("%s.%s", packageName, invokerGenerated.getGeneratedClass().getName().asString());
    final ClassExpr accFunctionName = new ClassExpr(JavaParser.parseType(typeWithPackage));
    final MethodCallExpr accFunctionCall = new MethodCallExpr(null, "accFunction", nodeList(accFunctionName));
    final NameExpr bindingVariable = new NameExpr(toVar(basePattern.getIdentifier()));
    final MethodCallExpr asDSL = new MethodCallExpr(accFunctionCall, "as", nodeList(bindingVariable));
    context.addExpression(asDSL);
}
Also used : GeneratedClassWithPackage(org.drools.modelcompiler.builder.GeneratedClassWithPackage) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) PatternBuilder(org.drools.compiler.rule.builder.PatternBuilder) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)12 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)12 NameExpr (org.drools.javaparser.ast.expr.NameExpr)7 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)6 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)5 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)5 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)5 Type (org.drools.javaparser.ast.type.Type)5 Parameter (org.drools.javaparser.ast.body.Parameter)4 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)4 Objects (java.util.Objects)3 Expression (org.drools.javaparser.ast.expr.Expression)3 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)3 BlockStmt (org.drools.javaparser.ast.stmt.BlockStmt)3 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)3 UnknownType (org.drools.javaparser.ast.type.UnknownType)3 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2