Search in sources :

Example 1 with ConstraintParser

use of org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser 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 2 with ConstraintParser

use of org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser in project drools by kiegroup.

the class FromVisitor method createFromCall.

private Expression createFromCall(String expression, Optional<String> optContainsBinding, String bindingId) {
    MethodCallExpr fromCall = new MethodCallExpr(null, FROM_CALL);
    fromCall.addArgument(new NameExpr(toVar(bindingId)));
    if (optContainsBinding.isPresent()) {
        DeclarationSpec declarationSpec = ruleContext.getDeclarationById(bindingId).orElseThrow(RuntimeException::new);
        Class<?> clazz = declarationSpec.getDeclarationClass();
        DrlxParseResult drlxParseResult = new ConstraintParser(ruleContext, packageModel).drlxParse(clazz, bindingId, expression);
        drlxParseResult.accept(drlxParseSuccess -> {
            Expression parsedExpression = drlxParseSuccess.getExpr();
            Expression exprArg = generateLambdaWithoutParameters(drlxParseSuccess.getUsedDeclarations(), parsedExpression);
            fromCall.addArgument(exprArg);
        });
    }
    return fromCall;
}
Also used : DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Expression(org.drools.javaparser.ast.expr.Expression) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 3 with ConstraintParser

use of org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser in project drools by kiegroup.

the class FlowAccumulateConstraint method buildPattern.

@Override
public void buildPattern() {
    for (BaseDescr constraint : constraintDescrs) {
        String expression = constraint.toString();
        final DrlxParseResult drlxParseResult = new ConstraintParser(context, packageModel).drlxParse(null, null, expression, false);
        drlxParseResult.accept(success -> {
            success.setSkipThisAsParam(true);
            new FlowExpressionBuilder(context).processExpression(success);
        });
    }
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) FlowExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder)

Example 4 with ConstraintParser

use of org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser in project drools by kiegroup.

the class EvalVisitor method visit.

public void visit(EvalDescr descr) {
    String expression = descr.getContent().toString();
    DrlxParseResult drlxParseResult = new ConstraintParser(context, packageModel).drlxParse(null, null, expression);
    drlxParseResult.accept(drlxParseSuccess -> {
        Expression rewriteExprAsLambdaWithoutThisParam = DrlxParseUtil.generateLambdaWithoutParameters(drlxParseSuccess.getUsedDeclarations(), drlxParseSuccess.getExpr(), true);
        // rewrites the DrlxParserResult expr as directly the lambda to use
        drlxParseSuccess.setExpr(rewriteExprAsLambdaWithoutThisParam);
        drlxParseSuccess.setStatic(true);
        new FlowExpressionBuilder(context).processExpression(drlxParseSuccess);
    });
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) FlowExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder)

Example 5 with ConstraintParser

use of org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser in project drools by kiegroup.

the class NamedConsequenceVisitor method whenThenDSL.

private MethodCallExpr whenThenDSL(ConditionalBranchDescr desc, PatternDescr patternRelated, Class<?> patternType, String callMethod, MethodCallExpr parentExpression) {
    MethodCallExpr when = new MethodCallExpr(parentExpression, callMethod);
    final String condition = desc.getCondition().toString();
    if (!condition.equals("true")) {
        // Default case
        when.addArgument(new StringLiteralExpr(context.getConditionId(patternType, condition)));
        when.addArgument(new NameExpr(toVar(patternRelated.getIdentifier())));
        DrlxParseResult parseResult = new ConstraintParser(context, packageModel).drlxParse(patternType, patternRelated.getIdentifier(), condition);
        parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(Collections.emptySortedSet(), parseSuccess.getExpr())));
    }
    MethodCallExpr then = new MethodCallExpr(when, THEN_CALL);
    MethodCallExpr rhs = onDSL(desc.getConsequence());
    then.addArgument(rhs);
    return then;
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

ConstraintParser (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser)7 DrlxParseResult (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)7 Expression (org.drools.javaparser.ast.expr.Expression)4 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)4 NameExpr (org.drools.javaparser.ast.expr.NameExpr)4 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)2 DrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess)2 FlowExpressionBuilder (org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)1 OOPathChunk (org.drools.javaparser.ast.drlx.OOPathChunk)1 OOPathExpr (org.drools.javaparser.ast.drlx.OOPathExpr)1 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)1 BooleanLiteralExpr (org.drools.javaparser.ast.expr.BooleanLiteralExpr)1 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)1 DrlxParseFail (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail)1 ParseResultVisitor (org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor)1