Search in sources :

Example 1 with ConstraintExpression

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

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

ConstraintExpression (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression)2 DrlxParseResult (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)2 SingleDrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess)2 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 ArrayList (java.util.ArrayList)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 Constraint (org.drools.impact.analysis.model.left.Constraint)1 MapConstraint (org.drools.impact.analysis.model.left.MapConstraint)1 ConstraintParser (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser)1 DrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess)1