Search in sources :

Example 1 with SingleDrlxParseSuccess

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

the class PatternDSL method findAllConstraint.

private List<PatternConstraintParseResult> findAllConstraint(PatternDescr pattern, List<? extends BaseDescr> constraintDescrs, Class<?> patternType) {
    ConstraintParser constraintParser = ConstraintParser.defaultConstraintParser(context, packageModel);
    List<PatternConstraintParseResult> patternConstraintParseResults = new ArrayList<>();
    for (BaseDescr constraint : constraintDescrs) {
        List<PatternConstraintParseResult> patternConstraintParseResultsPerConstraintDescr = new ArrayList<>();
        String patternIdentifier = pattern.getIdentifier();
        boolean isPositional = isPositional(constraint);
        ConstraintExpression constraintExpression = ConstraintExpression.createConstraintExpression(context, patternType, constraint, isPositional);
        if (constraintExpression == null) {
            continue;
        }
        DrlxParseResult drlxParseResult;
        try {
            context.setCurrentConstraintDescr(Optional.of(constraint));
            drlxParseResult = constraintParser.drlxParse(patternType, patternIdentifier, constraintExpression, isPositional);
        } finally {
            context.resetCurrentConstraintDescr();
        }
        String expression = constraintExpression.getExpression();
        if (drlxParseResult.isSuccess() && ((DrlxParseSuccess) drlxParseResult).isRequiresSplit() && ((DrlxParseSuccess) drlxParseResult).getExpr().isBinaryExpr()) {
            BinaryExpr expr = ((DrlxParseSuccess) drlxParseResult).getExpr().asBinaryExpr();
            String leftExpression = printNode(((SingleDrlxParseSuccess) drlxParseResult).getLeft().getExpression());
            DrlxParseResult leftExpressionReparsed = constraintParser.drlxParse(patternType, patternIdentifier, leftExpression, isPositional);
            patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(leftExpression, patternIdentifier, leftExpressionReparsed));
            String rightExpression = printNode(((SingleDrlxParseSuccess) drlxParseResult).getRight().getExpression());
            DrlxParseResult rightExpressionReparsed = constraintParser.drlxParse(patternType, patternIdentifier, rightExpression, isPositional);
            DrlxParseResult normalizedParseResult = ConstraintUtil.normalizeConstraint(rightExpressionReparsed);
            patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(rightExpression, patternIdentifier, normalizedParseResult));
        } else {
            DrlxParseResult normalizedParseResult = ConstraintUtil.normalizeConstraint(drlxParseResult);
            patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(expression, patternIdentifier, normalizedParseResult));
        }
        // Cast-check should be placed earlier than Null-check (calling the add method later means pushing the constraint earlier)
        addNullSafeExpr(constraintParser, pattern.getIdentifier(), patternConstraintParseResultsPerConstraintDescr);
        addImplicitCastExpr(constraintParser, pattern.getIdentifier(), patternConstraintParseResultsPerConstraintDescr);
        patternConstraintParseResults.addAll(patternConstraintParseResultsPerConstraintDescr);
    }
    return patternConstraintParseResults;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ArrayList(java.util.ArrayList) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) BaseDescr(org.drools.drl.ast.descr.BaseDescr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)

Example 2 with SingleDrlxParseSuccess

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

the class AccumulateVisitor method binaryExprParameter.

private Optional<NewBinding> binaryExprParameter(PatternDescr basePattern, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr functionDSL, String bindingId, String accumulateFunctionParameterStr) {
    final DrlxParseResult parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(Object.class, bindingId, accumulateFunctionParameterStr);
    Optional<NewBinding> optNewBinding = parseResult.acceptWithReturnValue(new ParseResultVisitor<Optional<NewBinding>>() {

        @Override
        public Optional<NewBinding> onSuccess(DrlxParseSuccess drlxParseResult) {
            SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) drlxParseResult;
            Class<?> exprRawClass = singleResult.getExprRawClass();
            AccumulateFunction accumulateFunction = getAccumulateFunction(function, exprRawClass);
            validateAccFunctionTypeAgainstPatternType(context, basePattern, accumulateFunction);
            final String bindExpressionVariable = context.getExprId(accumulateFunction.getResultType(), singleResult.getLeft().toString());
            singleResult.setExprBinding(bindExpressionVariable);
            context.addDeclarationReplacing(new DeclarationSpec(singleResult.getPatternBinding(), exprRawClass));
            context.getExpressions().forEach(expression -> replaceTypeInExprLambda(bindingId, exprRawClass, expression));
            functionDSL.addArgument(createAccSupplierExpr(accumulateFunction));
            final MethodCallExpr newBindingFromBinary = AccumulateVisitor.this.buildBinding(bindExpressionVariable, singleResult.getUsedDeclarations(), singleResult.getExpr());
            context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, exprRawClass));
            functionDSL.addArgument(context.getVarExpr(bindExpressionVariable));
            return Optional.of(new NewBinding(Collections.emptyList(), newBindingFromBinary));
        }

        @Override
        public Optional<NewBinding> onFail(DrlxParseFail failure) {
            return Optional.empty();
        }
    });
    return optNewBinding;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) REACT_ON_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.REACT_ON_CALL) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ACCUMULATE_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ACCUMULATE_CALL) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) PatternDescr(org.drools.drl.ast.descr.PatternDescr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) AccumulateUtil(org.drools.compiler.rule.builder.util.AccumulateUtil) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) Expression(com.github.javaparser.ast.expr.Expression) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) PatternExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.PatternExpressionBuilder) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) Node(com.github.javaparser.ast.Node) PackageModel(org.drools.modelcompiler.builder.PackageModel) NodeList(com.github.javaparser.ast.NodeList) AND_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.AND_CALL) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) LambdaUtil(org.drools.modelcompiler.util.LambdaUtil) BaseDescr(org.drools.drl.ast.descr.BaseDescr) VALUE_OF_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.VALUE_OF_CALL) Objects(java.util.Objects) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DrlxParseUtil.validateDuplicateBindings(org.drools.modelcompiler.builder.generator.DrlxParseUtil.validateDuplicateBindings) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AbstractExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.AbstractExpressionBuilder) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Parameter(com.github.javaparser.ast.body.Parameter) Pattern(org.drools.core.rule.Pattern) ArrayList(java.util.ArrayList) ACC_FUNCTION_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ACC_FUNCTION_CALL) HashSet(java.util.HashSet) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) DslMethodNames.createDslTopLevelMethod(org.drools.modelcompiler.builder.generator.DslMethodNames.createDslTopLevelMethod) AndDescr(org.drools.drl.ast.descr.AndDescr) ReplaceTypeInLambda.replaceTypeInExprLambda(org.drools.modelcompiler.util.lambdareplace.ReplaceTypeInLambda.replaceTypeInExprLambda) BIND_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_CALL) Optional.ofNullable(java.util.Optional.ofNullable) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_AS_CALL) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) Collectors.toList(java.util.stream.Collectors.toList) CollectAccumulator(org.drools.core.base.accumulators.CollectAccumulator) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) Collections(java.util.Collections) Optional(java.util.Optional) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 3 with SingleDrlxParseSuccess

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

the class FromVisitor method createArg.

private Expression createArg(String expression, String bindingId, MethodCallExpr fromCall) {
    if (bindingId != null) {
        DeclarationSpec declarationSpec = context.getDeclarationById(bindingId).orElseThrow(RuntimeException::new);
        Class<?> clazz = declarationSpec.getDeclarationClass();
        DrlxParseResult drlxParseResult = ConstraintParser.withoutVariableValidationConstraintParser(context, packageModel).drlxParse(clazz, bindingId, expression);
        return drlxParseResult.acceptWithReturnValue(drlxParseSuccess -> {
            SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) drlxParseResult;
            if (!isCompatibleWithFromReturnType(patternType, singleResult.getExprRawClass())) {
                context.addCompilationError(new InvalidExpressionErrorResult("Pattern of type: '" + patternType.getCanonicalName() + "' on rule '" + context.getRuleName() + "' is not compatible with type " + singleResult.getExprRawClass().getCanonicalName() + " returned by source"));
            }
            Expression parsedExpression = drlxParseSuccess.getExpr();
            Expression newExpr = generateLambdaWithoutParameters(singleResult.getUsedDeclarations(), parsedExpression, singleResult.isSkipThisAsParam(), ofNullable(singleResult.getPatternType()), context);
            if (newExpr instanceof LambdaExpr) {
                context.getPackageModel().registerLambdaReturnType((LambdaExpr) newExpr, singleResult.getExprType());
            }
            addArgumentWithPreexistingCheck(fromCall, singleResult.getUsedDeclarations());
            return newExpr;
        });
    }
    return null;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)

Example 4 with SingleDrlxParseSuccess

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

the class PatternExpressionBuilder method buildExpressionWithIndexing.

@Override
public MethodCallExpr buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
    if (drlxParseResult instanceof MultipleDrlxParseSuccess) {
        MultipleDrlxParseSuccess multi = (MultipleDrlxParseSuccess) drlxParseResult;
        MethodCallExpr exprDSL = new MethodCallExpr(null, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_OR_CALL : EXPR_AND_CALL);
        for (DrlxParseSuccess child : multi.getResults()) {
            MethodCallExpr childExpr = buildExpressionWithIndexing(child);
            childExpr.setScope(exprDSL);
            exprDSL = childExpr;
            if (child instanceof SingleDrlxParseSuccess && child.getExprBinding() != null) {
                SingleDrlxParseSuccess singleDrlxChild = (SingleDrlxParseSuccess) child;
                context.addDeclaration(child.getExprBinding(), singleDrlxChild.getLeftExprRawClass());
                Expression dslExpr = buildBinding(singleDrlxChild);
                context.addExpression(dslExpr);
            }
        }
        return new MethodCallExpr(exprDSL, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_END_OR_CALL : EXPR_END_AND_CALL);
    }
    return buildSingleExpressionWithIndexing((SingleDrlxParseSuccess) drlxParseResult);
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) MultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) MultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 5 with SingleDrlxParseSuccess

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

the class LhsParser method parseConstraint.

private void parseConstraint(RuleContext context, PatternDescr patternDescr, Pattern pattern, ConstraintParser constraintParser, BaseDescr constraintDescr) {
    ConstraintExpression constraintExpression = ConstraintExpression.createConstraintExpression(context, pattern.getPatternClass(), constraintDescr, false);
    DrlxParseResult drlxParseResult = constraintParser.drlxParse(pattern.getPatternClass(), patternDescr.getIdentifier(), constraintExpression, false);
    if (drlxParseResult.isSuccess()) {
        SingleDrlxParseSuccess result = (SingleDrlxParseSuccess) drlxParseResult;
        if (!result.getReactOnProperties().isEmpty()) {
            result.getReactOnProperties().forEach(pattern::addReactOn);
        } else {
            pattern.setClassReactive(true);
        }
        if (result.getRight() != null) {
            Constraint constraint = new Constraint();
            constraint = parseExpressionInConstraint(context, constraint, result.getLeft());
            boolean valueOnLeft = constraint.getValue() != null;
            constraint = parseExpressionInConstraint(context, constraint, result.getRight());
            if (constraint.getValue() != null) {
                // the constraint is relevant for impact analysis only if it checks a fixed value
                constraint.setType(decode(result.getDecodeConstraintType(), valueOnLeft));
                pattern.addConstraint(constraint);
            }
        } else if (result.getExprBinding() != null && result.getExpr() != null && result.getExpr().isLiteralExpr()) {
            ((ImpactAnalysisRuleContext) context).getBindVariableLiteralMap().put(result.getExprBinding(), ParserUtil.literalToValue(result.getExpr().asLiteralExpr()));
        } else if (isPredicateMethodCall(result)) {
            MethodCallExpr mce = result.getExpr().asMethodCallExpr();
            addConstraintIfBooleanProperty(pattern, mce, true);
        } else if (isNegatedPredicateMethodCall(result)) {
            MethodCallExpr mce = result.getExpr().asUnaryExpr().getExpression().asMethodCallExpr();
            addConstraintIfBooleanProperty(pattern, mce, false);
        }
    }
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) Constraint(org.drools.impact.analysis.model.left.Constraint) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) 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