Search in sources :

Example 1 with Parameter

use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.

the class Consequence method executeCall.

private MethodCallExpr executeCall(BlockStmt ruleVariablesBlock, BlockStmt ruleConsequence, Collection<String> verifiedDeclUsedInRHS, MethodCallExpr onCall) {
    boolean requireDrools = rewriteRHS(ruleVariablesBlock, ruleConsequence);
    MethodCallExpr executeCall = new MethodCallExpr(onCall, EXECUTE_CALL);
    LambdaExpr executeLambda = new LambdaExpr();
    executeCall.addArgument(executeLambda);
    executeLambda.setEnclosingParameters(true);
    if (requireDrools) {
        executeLambda.addParameter(new Parameter(new UnknownType(), "drools"));
    }
    verifiedDeclUsedInRHS.stream().map(x -> new Parameter(new UnknownType(), x)).forEach(executeLambda::addParameter);
    executeLambda.setBody(ruleConsequence);
    return executeCall;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) BitMask(org.drools.model.BitMask) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseUtil.isNameExprWithName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isNameExprWithName) DroolsImpl(org.drools.modelcompiler.consequence.DroolsImpl) JavaParser(org.drools.javaparser.JavaParser) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) BREAKING_CALL(org.drools.modelcompiler.builder.generator.visitor.NamedConsequenceVisitor.BREAKING_CALL) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) Collectors.toSet(java.util.stream.Collectors.toSet) PackageModel(org.drools.modelcompiler.builder.PackageModel) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) DrlxParseUtil.hasScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.hasScope) Parameter(org.drools.javaparser.ast.body.Parameter) ClassUtils(org.drools.core.util.ClassUtils) Collection(java.util.Collection) DrlxParseUtil.parseBlock(org.drools.modelcompiler.builder.generator.DrlxParseUtil.parseBlock) Set(java.util.Set) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Objects(java.util.Objects) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) DrlxParseUtil.findAllChildrenRecursive(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findAllChildrenRecursive) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Modifier(org.drools.javaparser.ast.Modifier) SimpleName(org.drools.javaparser.ast.expr.SimpleName) StringUtils(org.drools.core.util.StringUtils) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with Parameter

use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.

the class AccumulateVisitorFlowDSL method replaceBindingWithPatternBinding.

private void replaceBindingWithPatternBinding(MethodCallExpr bindExpression, MethodCallExpr lastPattern) {
    if (lastPattern == null) {
        return;
    }
    final Expression bindingId = lastPattern.getArgument(1);
    bindExpression.findFirst(NameExpr.class, e -> e.equals(bindingId)).ifPresent(name -> {
        LambdaExpr lambda = (LambdaExpr) bindExpression.getArgument(bindExpression.getArguments().size() - 1);
        if (lambda.getParameters().size() > 1) {
            String formalArg = fromVar(name.getNameAsString());
            for (Parameter param : lambda.getParameters()) {
                if (param.getNameAsString().equals(formalArg)) {
                    lambda.getParameters().remove(param);
                    lambda.getParameters().add(0, param);
                    break;
                }
            }
        }
        bindExpression.getArguments().remove(name);
        bindExpression.getArguments().add(0, name);
    });
}
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) Expression(org.drools.javaparser.ast.expr.Expression) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) Parameter(org.drools.javaparser.ast.body.Parameter)

Example 3 with Parameter

use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.

the class AccumulateVisitorPatternDSL method replaceBindingWithPatternBinding.

private MethodCallExpr replaceBindingWithPatternBinding(MethodCallExpr bindExpression, MethodCallExpr lastPattern) {
    final Expression bindingId = lastPattern.getArgument(0);
    bindExpression.findFirst(NameExpr.class, e -> e.equals(bindingId)).ifPresent(name -> {
        bindExpression.remove(name);
        LambdaExpr lambda = (LambdaExpr) bindExpression.getArgument(bindExpression.getArguments().size() - 1);
        if (lambda.getParameters().size() > 1) {
            String formalArg = fromVar(name.getNameAsString());
            for (Parameter param : lambda.getParameters()) {
                if (param.getNameAsString().equals(formalArg)) {
                    lambda.getParameters().remove(param);
                    lambda.getParameters().add(0, param);
                    break;
                }
            }
        }
    });
    return (MethodCallExpr) bindExpression;
}
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) Collection(java.util.Collection) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) BIND_CALL(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder.BIND_CALL) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseUtil.fromVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.fromVar) 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) PatternExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.PatternExpressionBuilder) Node(org.drools.javaparser.ast.Node) Expression(org.drools.javaparser.ast.expr.Expression) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) Parameter(org.drools.javaparser.ast.body.Parameter) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 4 with Parameter

use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.

the class DrlxParseUtil method generateLambdaWithoutParameters.

public static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr, boolean skipFirstParamAsThis) {
    if (skipFirstParamAsThis && usedDeclarations.isEmpty()) {
        return expr;
    }
    LambdaExpr lambdaExpr = new LambdaExpr();
    lambdaExpr.setEnclosingParameters(true);
    if (!skipFirstParamAsThis) {
        lambdaExpr.addParameter(new Parameter(new UnknownType(), "_this"));
    }
    usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
    lambdaExpr.setBody(new ExpressionStmt(expr));
    return lambdaExpr;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) Expression(org.drools.javaparser.ast.expr.Expression) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) Optional.of(java.util.Optional.of) JavaParser(org.drools.javaparser.JavaParser) Operator(org.drools.javaparser.ast.expr.BinaryExpr.Operator) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ConstraintType(org.drools.core.util.index.IndexUtil.ConstraintType) ClassUtil(org.drools.modelcompiler.util.ClassUtil) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) PrimitiveType(org.drools.javaparser.ast.type.PrimitiveType) Method(java.lang.reflect.Method) LongLiteralExpr(org.drools.javaparser.ast.expr.LongLiteralExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) Parameter(org.drools.javaparser.ast.body.Parameter) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) IndexUtil(org.drools.core.util.index.IndexUtil) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) NodeWithSimpleName(org.drools.javaparser.ast.nodeTypes.NodeWithSimpleName) ClassUtil.findMethod(org.drools.modelcompiler.util.ClassUtil.findMethod) StringUtils(org.drools.core.util.StringUtils) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) ArrayAccessExpr(org.drools.javaparser.ast.expr.ArrayAccessExpr) NodeWithTraversableScope(org.drools.javaparser.ast.nodeTypes.NodeWithTraversableScope) CastExpr(org.drools.javaparser.ast.expr.CastExpr) CharLiteralExpr(org.drools.javaparser.ast.expr.CharLiteralExpr) Deque(java.util.Deque) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) DrlxParser(org.drools.drlx.DrlxParser) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) LinkedList(java.util.LinkedList) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) ClassUtils(org.drools.core.util.ClassUtils) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Field(java.lang.reflect.Field) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Type(org.drools.javaparser.ast.type.Type) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) DoubleLiteralExpr(org.drools.javaparser.ast.expr.DoubleLiteralExpr) NodeWithOptionalScope(org.drools.javaparser.ast.nodeTypes.NodeWithOptionalScope) Collections(java.util.Collections) Node(org.drools.javaparser.ast.Node) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt)

Example 5 with Parameter

use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.

the class FlowExpressionBuilder method buildIndexedBy.

private MethodCallExpr buildIndexedBy(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
    if (!hasIndex(drlxParseResult)) {
        return exprDSL;
    }
    TypedExpression left = drlxParseResult.getLeft();
    TypedExpression right = drlxParseResult.getRight();
    Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
    ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
    // not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
    FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), drlxParseResult.getDecodeConstraintType().toString());
    LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
    indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
    boolean leftContainsThis = left.getExpression().toString().contains("_this");
    indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
    MethodCallExpr indexedByDSL = new MethodCallExpr(exprDSL, INDEXED_BY_CALL);
    indexedByDSL.addArgument(indexedBy_indexedClass);
    indexedByDSL.addArgument(indexedBy_constraintType);
    indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
    indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
    Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
    if (isAlphaIndex(usedDeclarations)) {
        indexedByDSL.addArgument(right.getExpression());
    } else {
        LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
        indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
        indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
        indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
    }
    return indexedByDSL;
}
Also used : LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) UnknownType(org.drools.javaparser.ast.type.UnknownType) Objects(java.util.Objects) Parameter(org.drools.javaparser.ast.body.Parameter) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

Parameter (org.drools.javaparser.ast.body.Parameter)7 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)7 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)7 NameExpr (org.drools.javaparser.ast.expr.NameExpr)7 Collection (java.util.Collection)5 List (java.util.List)5 Expression (org.drools.javaparser.ast.expr.Expression)5 UnknownType (org.drools.javaparser.ast.type.UnknownType)5 ArrayList (java.util.ArrayList)4 Objects (java.util.Objects)4 Optional (java.util.Optional)4 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)4 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)4 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)4 IndexUtil (org.drools.core.util.index.IndexUtil)3 JavaParser (org.drools.javaparser.JavaParser)3 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)3 PackageModel (org.drools.modelcompiler.builder.PackageModel)3 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)3 Optional.of (java.util.Optional.of)2