Search in sources :

Example 11 with MethodCallExpr

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

the class OOPathExprGenerator method toPatternExpr.

private void toPatternExpr(String bindingId, List<DrlxParseResult> list) {
    MethodCallExpr patternExpr = new MethodCallExpr(null, "pattern");
    patternExpr.addArgument(toVar(bindingId));
    for (DrlxParseResult drlx : list) {
        if (drlx instanceof DrlxParseSuccess) {
            MethodCallExpr expr = (MethodCallExpr) expressionBuilder.buildExpressionWithIndexing(((DrlxParseSuccess) drlx));
            expr.setScope(patternExpr);
            patternExpr = expr;
        }
    }
    context.addExpression(patternExpr);
}
Also used : DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 12 with MethodCallExpr

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

the class OOPathExprGenerator method toFlowExpr.

private void toFlowExpr(String bindingId, List<DrlxParseResult> list) {
    final List<DrlxParseSuccess> value = list.stream().filter(r -> r instanceof DrlxParseSuccess).map(DrlxParseSuccess.class::cast).collect(Collectors.toList());
    if (value.size() == 1) {
        context.addExpression(expressionBuilder.buildExpressionWithIndexing(value.get(0)));
    } else {
        final MethodCallExpr andDSL = new MethodCallExpr(null, "and");
        value.forEach(e -> {
            final Expression expression = expressionBuilder.buildExpressionWithIndexing(e);
            andDSL.addArgument(expression);
        });
        context.addExpression(andDSL);
    }
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 13 with MethodCallExpr

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

the class OOPathExprGenerator method visit.

public void visit(Class<?> originalClass, String originalBind, DrlxParseSuccess patternParseResult) {
    final OOPathExpr ooPathExpr = (OOPathExpr) patternParseResult.getExpr();
    Class<?> previousClass = originalClass;
    String previousBind = originalBind;
    Map<String, List<DrlxParseResult>> ooPathConditionExpressions = new LinkedHashMap<>();
    for (Iterator<OOPathChunk> iterator = ooPathExpr.getChunks().iterator(); iterator.hasNext(); ) {
        OOPathChunk chunk = iterator.next();
        final String fieldName = chunk.getField().toString();
        final TypedExpression callExpr = DrlxParseUtil.nameExprToMethodCallExpr(fieldName, previousClass, null);
        Class<?> fieldType = (chunk.getInlineCast() != null) ? DrlxParseUtil.getClassFromContext(context.getTypeResolver(), chunk.getInlineCast().toString()) : callExpr.getType();
        if (Iterable.class.isAssignableFrom(fieldType)) {
            fieldType = extractGenericType(previousClass, ((MethodCallExpr) callExpr.getExpression()).getName().toString());
        }
        final String chunkKey = originalBind + fieldName;
        final String bindingId;
        if (!iterator.hasNext() && patternParseResult.getExprBinding() != null) {
            bindingId = patternParseResult.getExprBinding();
            context.removeDeclarationById(bindingId);
        } else {
            bindingId = context.getOOPathId(fieldType, chunkKey);
        }
        final Expression accessorLambda = generateLambdaWithoutParameters(Collections.emptySortedSet(), prepend(new NameExpr("_this"), callExpr.getExpression()));
        final MethodCallExpr reactiveFrom = new MethodCallExpr(null, "reactiveFrom");
        reactiveFrom.addArgument(new NameExpr(toVar(previousBind)));
        reactiveFrom.addArgument(accessorLambda);
        DeclarationSpec newDeclaration = new DeclarationSpec(bindingId, fieldType, reactiveFrom);
        context.addDeclaration(newDeclaration);
        context.addOOPathDeclaration(newDeclaration);
        final List<Expression> conditions = chunk.getConditions();
        if (!conditions.isEmpty()) {
            Class<?> finalFieldType = fieldType;
            final List<DrlxParseResult> conditionParseResult = conditions.stream().map((Expression c) -> new ConstraintParser(context, packageModel).drlxParse(finalFieldType, bindingId, c.toString())).collect(Collectors.toList());
            ooPathConditionExpressions.put(bindingId, conditionParseResult);
        } else {
            if (context.isPatternDSL()) {
                ooPathConditionExpressions.put(bindingId, Collections.emptyList());
            } else {
                final DrlxParseSuccess drlxParseResult = new DrlxParseSuccess(fieldType, "", bindingId, new BooleanLiteralExpr(true), fieldType);
                ooPathConditionExpressions.put(bindingId, Collections.singletonList(drlxParseResult));
            }
        }
        previousBind = bindingId;
        previousClass = fieldType;
    }
    ooPathConditionExpressions.forEach(context.isPatternDSL() ? this::toPatternExpr : this::toFlowExpr);
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) NameExpr(org.drools.javaparser.ast.expr.NameExpr) LinkedHashMap(java.util.LinkedHashMap) OOPathExpr(org.drools.javaparser.ast.drlx.OOPathExpr) OOPathChunk(org.drools.javaparser.ast.drlx.OOPathChunk) Expression(org.drools.javaparser.ast.expr.Expression) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) List(java.util.List) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 14 with MethodCallExpr

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

the class QueryGenerator method processQuery.

public static void processQuery(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, QueryDescr queryDescr) {
    String queryDefVariableName = toQueryDef(queryDescr.getName());
    RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
    new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
    final Type queryType = JavaParser.parseType(Query.class.getCanonicalName());
    MethodDeclaration queryMethod = new MethodDeclaration(EnumSet.of(Modifier.PRIVATE), queryType, "query_" + toId(queryDescr.getName()));
    BlockStmt queryBody = new BlockStmt();
    ModelGenerator.createVariables(kbuilder, queryBody, packageModel, context);
    queryMethod.setBody(queryBody);
    String queryBuildVarName = toId(queryDescr.getName()) + "_build";
    VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
    MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
    context.getExpressions().forEach(buildCall::addArgument);
    AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
    queryBody.addStatement(queryBuildAssign);
    queryBody.addStatement(new ReturnStmt(queryBuildVarName));
    packageModel.putQueryMethod(queryMethod);
}
Also used : VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) Query(org.drools.model.Query) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) NameExpr(org.drools.javaparser.ast.expr.NameExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 15 with MethodCallExpr

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

the class WindowReferenceGenerator method addField.

private void addField(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, WindowDeclarationDescr descr) {
    final String windowName = toVar(descr.getName());
    final MethodCallExpr initializer = new MethodCallExpr(null, "window");
    final PatternDescr pattern = descr.getPattern();
    ParsedBehavior behavior = pattern.getBehaviors().stream().map(this::parseTypeFromBehavior).findFirst().orElseThrow(RuntimeException::new);
    final WindowDefinition.Type windowType = behavior.windowType;
    initializer.addArgument(new NameExpr(windowType.getDeclaringClass().getCanonicalName() + "." + windowType.toString()));
    initializer.addArgument(new IntegerLiteralExpr(behavior.duration.getValue()));
    final TimeUnit timeUnit = behavior.duration.getTimeUnit();
    initializer.addArgument(new NameExpr(timeUnit.getDeclaringClass().getCanonicalName() + "." + timeUnit.name()));
    final Class<?> initClass = DrlxParseUtil.getClassFromContext(typeResolver, pattern.getObjectType());
    final Type initType = JavaParser.parseType(initClass.getCanonicalName());
    initializer.addArgument(new ClassExpr(initType));
    parseConditions(kbuilder, packageModel, pattern, initClass).forEach(initializer::addArgument);
    packageModel.addAllWindowReferences(windowName, initializer);
}
Also used : IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) Type(org.drools.javaparser.ast.type.Type) TimeUnit(java.util.concurrent.TimeUnit) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) WindowDefinition(org.drools.model.WindowDefinition) 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