Search in sources :

Example 1 with OOPathChunk

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

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 OOPathChunk (org.drools.javaparser.ast.drlx.OOPathChunk)1 OOPathExpr (org.drools.javaparser.ast.drlx.OOPathExpr)1 BooleanLiteralExpr (org.drools.javaparser.ast.expr.BooleanLiteralExpr)1 Expression (org.drools.javaparser.ast.expr.Expression)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 NameExpr (org.drools.javaparser.ast.expr.NameExpr)1 ConstraintParser (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser)1 DrlxParseResult (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)1 DrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess)1