Search in sources :

Example 6 with SingleDrlxParseSuccess

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

the class OOPathExprGenerator method toPatternExpr.

private void toPatternExpr(String bindingId, List<DrlxParseResult> list, DrlxParseSuccess patternParseResult, Class<?> fieldType) {
    MethodCallExpr patternExpr = createDslTopLevelMethod(PATTERN_CALL);
    patternExpr.addArgument(context.getVar(bindingId));
    SingleDrlxParseSuccess oopathConstraint = null;
    for (DrlxParseResult drlx : list) {
        if (drlx.isSuccess()) {
            SingleDrlxParseSuccess singleDrlx = (SingleDrlxParseSuccess) drlx;
            if (singleDrlx.isOOPath()) {
                if (oopathConstraint != null) {
                    throw new UnsupportedOperationException("An oopath chunk can only have a single oopath constraint");
                }
                oopathConstraint = singleDrlx;
                continue;
            }
            if (singleDrlx.getExprBinding() != null) {
                MethodCallExpr expr = expressionBuilder.buildBinding(singleDrlx);
                expr.setScope(patternExpr);
                patternExpr = expr;
            }
            if (singleDrlx.getExpr() != null && !(singleDrlx.getExpr() instanceof NameExpr)) {
                MethodCallExpr expr = expressionBuilder.buildExpressionWithIndexing(singleDrlx);
                expr.setScope(patternExpr);
                patternExpr = expr;
            }
        }
    }
    context.addExpression(patternExpr);
    if (bindingId.equals(patternParseResult.getExprBinding())) {
        context.registerOOPathPatternExpr(bindingId, patternExpr);
    }
    if (oopathConstraint != null) {
        new OOPathExprGenerator(context, packageModel).visit(fieldType, bindingId, oopathConstraint);
    }
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) NameExpr(com.github.javaparser.ast.expr.NameExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 7 with SingleDrlxParseSuccess

use of org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess 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 = parentExpression == null ? createDslTopLevelMethod(callMethod) : new MethodCallExpr(parentExpression, callMethod);
    final String condition = desc.getCondition().toString();
    if (!condition.equals("true")) {
        // Default case
        when.addArgument(new StringLiteralExpr(context.getConditionId(patternType, condition)));
        String identifier = patternRelated.getIdentifier();
        DrlxParseResult parseResult;
        if (identifier == null) {
            // The accumulate pattern doesn't have an identifier. Let's parse the consequence and use the acc functions
            parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(Object.class, "", condition);
            parseResult.accept((DrlxParseSuccess parseSuccess) -> {
                SingleDrlxParseSuccess parseSuccess1 = (SingleDrlxParseSuccess) parseSuccess;
                AccumulateDescr source = (AccumulateDescr) patternRelated.getSource();
                for (String usedDeclaration : parseSuccess1.getUsedDeclarations()) {
                    for (AccumulateDescr.AccumulateFunctionCallDescr functionCallDescr : source.getFunctions()) {
                        if (functionCallDescr.getBind().equals(usedDeclaration)) {
                            addVariable(patternRelated, when, functionCallDescr);
                        }
                    }
                }
                when.addArgument(generateLambdaWithoutParameters(parseSuccess1.getUsedDeclarations(), parseSuccess.getExpr(), true, Optional.empty()));
            });
        } else {
            when.addArgument(context.getVarExpr(identifier));
            parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(patternType, identifier, condition);
            Collection<String> usedDeclarations = ((SingleDrlxParseSuccess) parseResult).getUsedDeclarations();
            if (usedDeclarations.isEmpty()) {
                // _this
                parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(Collections.emptySortedSet(), parseSuccess.getExpr())));
            } else {
                parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(usedDeclarations, parseSuccess.getExpr(), true, Optional.empty())));
            }
        }
    }
    MethodCallExpr then = new MethodCallExpr(when, THEN_CALL);
    MethodCallExpr rhs = onDSL(desc.getConsequence());
    then.addArgument(rhs);
    return then;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 8 with SingleDrlxParseSuccess

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

the class EvalVisitor method visit.

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

Example 9 with SingleDrlxParseSuccess

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

the class AccumulateVisitor method methodCallExprParameter.

private Optional<NewBinding> methodCallExprParameter(PatternDescr basePattern, BaseDescr input, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr functionDSL, String bindingId, Expression accumulateFunctionParameter) {
    final Expression parameterConverted = convertParameter(accumulateFunctionParameter);
    final DrlxParseUtil.RemoveRootNodeResult methodCallWithoutRootNode = DrlxParseUtil.removeRootNode(parameterConverted);
    String rootNodeName = getRootNodeName(methodCallWithoutRootNode);
    Optional<DeclarationSpec> decl = context.getDeclarationById(rootNodeName);
    Class<?> clazz = decl.map(DeclarationSpec::getDeclarationClass).orElseGet(() -> {
        try {
            return context.getTypeResolver().resolveType(rootNodeName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    });
    final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
    final ExpressionTyper expressionTyper = new ExpressionTyper(context, clazz, bindingId, false, expressionTyperContext);
    TypedExpression typedExpression = expressionTyper.toTypedExpression(methodCallWithoutRootNode.getWithoutRootNode()).typedExpressionOrException();
    final Class<?> methodCallExprType = typedExpression.getRawClass();
    final AccumulateFunction accumulateFunction = getAccumulateFunction(function, methodCallExprType);
    validateAccFunctionTypeAgainstPatternType(context, basePattern, accumulateFunction);
    functionDSL.addArgument(createAccSupplierExpr(accumulateFunction));
    // Every expression in an accumulate function gets transformed in a bind expression with a generated id
    // Then the accumulate function will have that binding expression as a source
    final Class accumulateFunctionResultType = accumulateFunction.getResultType();
    final String bindExpressionVariable = context.getExprId(accumulateFunctionResultType, typedExpression.toString());
    String paramExprBindingId = rootNodeName;
    Class<?> patternType = clazz;
    PatternDescr inputPattern = decl.isPresent() ? null : findInputPattern(input);
    if (inputPattern != null) {
        String inputId = inputPattern.getIdentifier();
        Optional<DeclarationSpec> accumulateClassDeclOpt = context.getDeclarationById(inputId);
        if (accumulateClassDeclOpt.isPresent()) {
            // when static method is used in accumulate function, "_this" is a pattern input
            // Note that DrlxParseUtil.generateLambdaWithoutParameters() takes the patternType as a class of "_this"
            paramExprBindingId = inputId;
            patternType = accumulateClassDeclOpt.get().getDeclarationClass();
        }
    }
    SingleDrlxParseSuccess drlxParseResult = (SingleDrlxParseSuccess) ConstraintParser.defaultConstraintParser(context, context.getPackageModel()).drlxParse(patternType, paramExprBindingId, printNode(parameterConverted));
    if (inputPattern != null) {
        drlxParseResult.setAccumulateBinding(inputPattern.getIdentifier());
    }
    return drlxParseResult.acceptWithReturnValue(new ReplaceBindingVisitor(functionDSL, bindingId, methodCallExprType, accumulateFunctionResultType, bindExpressionVariable, drlxParseResult));
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) PatternDescr(org.drools.drl.ast.descr.PatternDescr) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 10 with SingleDrlxParseSuccess

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

the class ConstraintUtil method normalizeConstraint.

/**
 * Swap left and right operands in a constraint when a fact property is located on the right side.
 *
 * e.g. Person(20 < age) should be normalized to Person(age > 20)
 *
 * @param drlxParseResult
 * @return Normalized <code>DrlxParseResult</code>
 */
public static DrlxParseResult normalizeConstraint(DrlxParseResult drlxParseResult) {
    if (!ENABLE_NORMALIZE) {
        return drlxParseResult;
    }
    if (drlxParseResult instanceof SingleDrlxParseSuccess) {
        // Create a copy
        SingleDrlxParseSuccess drlx = new SingleDrlxParseSuccess((SingleDrlxParseSuccess) drlxParseResult);
        Expression expr = drlx.getExpr();
        if (expr == null) {
            return drlxParseResult;
        }
        if (expr instanceof MethodCallExpr) {
            processTopLevelExpression(drlx, (MethodCallExpr) expr);
        } else if (expr instanceof EnclosedExpr) {
            Expression inner = stripEnclosedExpr((EnclosedExpr) expr);
            if (inner instanceof MethodCallExpr) {
                processTopLevelExpression(drlx, (MethodCallExpr) inner);
            } else {
                processExpression(expr);
            }
        } else {
            processExpression(expr);
        }
        return drlx;
    }
    return drlxParseResult;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) Expression(com.github.javaparser.ast.expr.Expression) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

SingleDrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess)10 DrlxParseResult (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)7 Expression (com.github.javaparser.ast.expr.Expression)6 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)6 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)4 DrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess)4 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)3 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 ArrayList (java.util.ArrayList)2 CollectListAccumulateFunction (org.drools.core.base.accumulators.CollectListAccumulateFunction)2 CollectSetAccumulateFunction (org.drools.core.base.accumulators.CollectSetAccumulateFunction)2 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)2 BaseDescr (org.drools.drl.ast.descr.BaseDescr)2 PatternDescr (org.drools.drl.ast.descr.PatternDescr)2 InvalidExpressionErrorResult (org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult)2 ConstraintExpression (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression)2 ConstraintParser (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser)2 Node (com.github.javaparser.ast.Node)1