Search in sources :

Example 71 with MethodCallExpr

use of org.drools.javaparser.ast.expr.MethodCallExpr 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)

Example 72 with MethodCallExpr

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

the class FlowDSLPattern method createInputExpression.

private MethodCallExpr createInputExpression(PatternDescr pattern) {
    MethodCallExpr exprDSL = new MethodCallExpr(null, INPUT_CALL);
    exprDSL.addArgument(new NameExpr(toVar(pattern.getIdentifier())));
    Set<String> watchedProperties = new HashSet<>();
    watchedProperties.addAll(context.getRuleDescr().lookAheadFieldsOfIdentifier(pattern));
    watchedProperties.addAll(getPatternListenedProperties(pattern));
    if (!watchedProperties.isEmpty()) {
        exprDSL = new MethodCallExpr(exprDSL, "watch");
        watchedProperties.stream().map(StringLiteralExpr::new).forEach(exprDSL::addArgument);
    }
    return exprDSL;
}
Also used : NameExpr(org.drools.javaparser.ast.expr.NameExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) HashSet(java.util.HashSet)

Example 73 with MethodCallExpr

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

the class PatternDSLPattern method createPatternExpression.

private MethodCallExpr createPatternExpression(PatternDescr pattern) {
    MethodCallExpr dslExpr = new MethodCallExpr(null, PATTERN_CALL);
    dslExpr.addArgument(QueryGenerator.substituteBindingWithQueryParameter(context, pattern.getIdentifier()));
    return dslExpr;
}
Also used : MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 74 with MethodCallExpr

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

the class PatternDSLPattern method buildPattern.

@Override
public void buildPattern() {
    generatePatternIdentifierIfMissing();
    final Optional<Expression> declarationSource = buildFromDeclaration(pattern);
    context.addDeclaration(new DeclarationSpec(pattern.getIdentifier(), patternType, Optional.of(pattern), declarationSource));
    if (constraintDescrs.isEmpty() && !(pattern.getSource() instanceof AccumulateDescr)) {
        context.addExpression(addWatchToPattern(createPatternExpression(pattern)));
    } else {
        if (!context.hasErrors()) {
            final List<PatternConstraintParseResult> patternConstraintParseResults = findAllConstraint(pattern, constraintDescrs, patternType);
            MethodCallExpr patternExpression = createPatternExpression(pattern);
            List<Expression> exprs = new ArrayList<>();
            context.pushExprPointer(exprs::add);
            buildConstraints(pattern, patternType, patternConstraintParseResults);
            context.popExprPointer();
            List<Expression> additionalPatterns = new ArrayList<>();
            for (Expression expr : exprs) {
                Optional<Expression> rootScope = findRootNodeViaScope(expr);
                if (rootScope.isPresent() && ((MethodCallExpr) rootScope.get()).getNameAsString().equals("pattern")) {
                    additionalPatterns.add(expr);
                } else {
                    MethodCallExpr currentExpr = (MethodCallExpr) expr;
                    ((MethodCallExpr) expr).setScope(patternExpression);
                    patternExpression = currentExpr;
                }
            }
            context.addExpression(addWatchToPattern(patternExpression));
            additionalPatterns.forEach(context::addExpression);
        }
    }
}
Also used : DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(org.drools.javaparser.ast.expr.Expression) ArrayList(java.util.ArrayList) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 75 with MethodCallExpr

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

the class QueryCall method buildPattern.

@Override
public void buildPattern() {
    MethodCallExpr callMethod = new MethodCallExpr(new NameExpr(queryDef), QUERY_INVOCATION_CALL);
    callMethod.addArgument("" + !pattern.isQuery());
    List<QueryParameter> parameters = packageModel.getQueryDefWithType().get(queryDef).getContext().getQueryParameters();
    for (int i = 0; i < parameters.size(); i++) {
        String queryName = context.getQueryName().orElseThrow(RuntimeException::new);
        ExprConstraintDescr variableExpr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(i);
        String variableName = variableExpr.toString();
        int unifPos = variableName.indexOf(":=");
        if (unifPos > 0) {
            variableName = variableName.substring(0, unifPos).trim();
        }
        Optional<String> unificationId = context.getUnificationId(variableName);
        int queryIndex = i + 1;
        Expression parameterCall = unificationId.map(name -> (Expression) new NameExpr(toVar(name))).orElseGet(() -> new MethodCallExpr(new NameExpr(queryName), QueryGenerator.toQueryArg(queryIndex)));
        callMethod.addArgument(parameterCall);
    }
    context.addExpression(callMethod);
}
Also used : PackageModel(org.drools.modelcompiler.builder.PackageModel) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Expression(org.drools.javaparser.ast.expr.Expression) QUERY_INVOCATION_CALL(org.drools.modelcompiler.builder.generator.ModelGenerator.QUERY_INVOCATION_CALL) QueryGenerator(org.drools.modelcompiler.builder.generator.QueryGenerator) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) NameExpr(org.drools.javaparser.ast.expr.NameExpr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DSLNode(org.drools.modelcompiler.builder.generator.visitor.DSLNode) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) Optional(java.util.Optional) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) Expression(org.drools.javaparser.ast.expr.Expression) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) 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