Search in sources :

Example 1 with Constraint

use of org.drools.impact.analysis.model.left.Constraint 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)

Example 2 with Constraint

use of org.drools.impact.analysis.model.left.Constraint in project drools by kiegroup.

the class LhsParser method addConstraintIfBooleanProperty.

private void addConstraintIfBooleanProperty(Pattern pattern, MethodCallExpr mce, boolean value) {
    if (isThisExpression(mce.getScope().orElse(null))) {
        String methodName = mce.getName().asString();
        String prop = ClassUtils.getter2property(methodName);
        if (prop != null) {
            Method method = MethodUtils.findMethod(pattern.getPatternClass(), methodName, new Class[0]);
            if (method != null && isBooleanBoxedUnboxed(method.getReturnType())) {
                Constraint constraint = new Constraint(Constraint.Type.EQUAL, prop, value);
                pattern.addConstraint(constraint);
            }
        }
    }
}
Also used : MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) Constraint(org.drools.impact.analysis.model.left.Constraint) ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) Method(java.lang.reflect.Method)

Example 3 with Constraint

use of org.drools.impact.analysis.model.left.Constraint in project drools by kiegroup.

the class ModelToGraphConverter method processModify.

private void processModify(GraphAnalysis graphAnalysis, String pkgName, String ruleName, ModifyAction action) {
    Node source = graphAnalysis.getNode(fqdn(pkgName, ruleName));
    Class<?> modifiedClass = action.getActionClass();
    if (!graphAnalysis.isRegisteredClass(modifiedClass)) {
        // Not likely happen but not invalid
        logger.warn("Not found {} in reactiveMap", modifiedClass);
        return;
    }
    List<ModifiedProperty> modifiedProperties = action.getModifiedProperties();
    for (ModifiedProperty modifiedProperty : modifiedProperties) {
        String property = modifiedProperty.getProperty();
        for (AnalyzedRule reactedRule : graphAnalysis.getRulesReactiveTo(modifiedClass, property)) {
            List<Constraint> constraints = reactedRule.getRule().getLhs().getPatterns().stream().filter(pattern -> pattern.getPatternClass() == modifiedClass).flatMap(pattern -> pattern.getConstraints().stream()).filter(constraint -> constraint.getProperty() != null && constraint.getProperty().equals(property)).filter(constraint -> {
                if (constraint instanceof MapConstraint) {
                    return doesAssertSameKey((MapConstraint) constraint, modifiedProperty);
                } else {
                    return true;
                }
            }).collect(Collectors.toList());
            ReactivityType combinedLinkType = ReactivityType.UNKNOWN;
            if (constraints.isEmpty()) {
                // This rule is reactive to the property but cannot find its constraint (e.g. [age > $a] non-literal constraint). It means UNKNOWN impact
                combinedLinkType = ReactivityType.UNKNOWN;
            } else {
                // If constraints contain at least one POSITIVE, we consider it's POSITIVE.
                for (Constraint constraint : constraints) {
                    ReactivityType linkType = linkType(constraint, modifiedProperty);
                    if (linkType == ReactivityType.POSITIVE) {
                        combinedLinkType = ReactivityType.POSITIVE;
                        break;
                    } else if (linkType == ReactivityType.NEGATIVE) {
                        // NEGATIVE is stronger than UNKNOWN (but may be configurable)
                        combinedLinkType = ReactivityType.NEGATIVE;
                    } else if (combinedLinkType == ReactivityType.NEGATIVE && linkType == ReactivityType.UNKNOWN) {
                    // Don't overwrite with UNKNOWN
                    } else {
                        // UNKNOWN
                        combinedLinkType = linkType;
                    }
                }
            }
            if (reactedRule.getReactivityType() == ReactivityType.NEGATIVE) {
                combinedLinkType = combinedLinkType.negate();
            }
            Node target = graphAnalysis.getNode(fqdn(pkgName, reactedRule.getRule().getName()));
            linkNodesIfExpected(source, target, combinedLinkType);
        }
    }
}
Also used : LeftHandSide(org.drools.impact.analysis.model.left.LeftHandSide) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) AnalysisModel(org.drools.impact.analysis.model.AnalysisModel) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Collectors(java.util.stream.Collectors) Pattern(org.drools.impact.analysis.model.left.Pattern) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) List(java.util.List) InsertAction(org.drools.impact.analysis.model.right.InsertAction) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) Rule(org.drools.impact.analysis.model.Rule) RightHandSide(org.drools.impact.analysis.model.right.RightHandSide) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) ConsequenceAction(org.drools.impact.analysis.model.right.ConsequenceAction) Package(org.drools.impact.analysis.model.Package) Constraint(org.drools.impact.analysis.model.left.Constraint) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) Constraint(org.drools.impact.analysis.model.left.Constraint) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty)

Example 4 with Constraint

use of org.drools.impact.analysis.model.left.Constraint in project drools by kiegroup.

the class ModelToGraphConverter method processInsert.

private void processInsert(GraphAnalysis graphAnalysis, String pkgName, String ruleName, InsertAction action) {
    Node source = graphAnalysis.getNode(fqdn(pkgName, ruleName));
    Class<?> insertedClass = action.getActionClass();
    if (!graphAnalysis.isRegisteredClass(insertedClass)) {
        // Not likely happen but not invalid
        logger.warn("Not found {} in reactiveMap", insertedClass);
        return;
    }
    // property based link
    List<InsertedProperty> insertedProperties = action.getInsertedProperties();
    for (InsertedProperty insertedProperty : insertedProperties) {
        String property = insertedProperty.getProperty();
        for (AnalyzedRule reactedRule : graphAnalysis.getRulesReactiveTo(insertedClass, property)) {
            List<Pattern> patterns = reactedRule.getRule().getLhs().getPatterns().stream().filter(pattern -> pattern.getPatternClass() == insertedClass).collect(Collectors.toList());
            for (Pattern pattern : patterns) {
                List<Constraint> constraints = pattern.getConstraints().stream().filter(constraint -> constraint.getProperty() != null && constraint.getProperty().equals(property)).collect(Collectors.toList());
                ReactivityType combinedLinkType = ReactivityType.UNKNOWN;
                if (constraints.isEmpty()) {
                    // This rule is reactive to the property but cannot find its constraint (e.g. [age > $a] non-literal constraint). It means UNKNOWN impact
                    combinedLinkType = ReactivityType.UNKNOWN;
                } else {
                    // If constraints contain at least one POSITIVE, we consider it's POSITIVE.
                    for (Constraint constraint : constraints) {
                        ReactivityType linkType = linkType(constraint, insertedProperty);
                        if (linkType == ReactivityType.POSITIVE) {
                            combinedLinkType = ReactivityType.POSITIVE;
                            break;
                        } else if (linkType == ReactivityType.NEGATIVE) {
                            // NEGATIVE is stronger than UNKNOWN (but may be configurable)
                            combinedLinkType = ReactivityType.NEGATIVE;
                        } else if (combinedLinkType == ReactivityType.NEGATIVE && linkType == ReactivityType.UNKNOWN) {
                        // Don't overwrite with UNKNOWN
                        } else {
                            // UNKNOWN
                            combinedLinkType = linkType;
                        }
                    }
                }
                if (combinedLinkType == ReactivityType.NEGATIVE) {
                    // TODO: handle "exists" case
                    continue;
                }
                if (combinedLinkType == ReactivityType.POSITIVE && !pattern.isPositive()) {
                    // POSITIVE insert in not() means NEGATIVE
                    combinedLinkType = combinedLinkType.negate();
                }
                Node target = graphAnalysis.getNode(fqdn(pkgName, reactedRule.getRule().getName()));
                linkNodesIfExpected(source, target, combinedLinkType);
            }
        }
    }
    // class based link
    for (AnalyzedRule reactedRule : graphAnalysis.getRulesReactiveToWithoutProperty(insertedClass)) {
        Node target = graphAnalysis.getNode(fqdn(pkgName, reactedRule.getRule().getName()));
        linkNodesIfExpected(source, target, reactedRule.getReactivityType());
    }
}
Also used : LeftHandSide(org.drools.impact.analysis.model.left.LeftHandSide) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) AnalysisModel(org.drools.impact.analysis.model.AnalysisModel) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Collectors(java.util.stream.Collectors) Pattern(org.drools.impact.analysis.model.left.Pattern) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) List(java.util.List) InsertAction(org.drools.impact.analysis.model.right.InsertAction) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) Rule(org.drools.impact.analysis.model.Rule) RightHandSide(org.drools.impact.analysis.model.right.RightHandSide) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) ConsequenceAction(org.drools.impact.analysis.model.right.ConsequenceAction) Package(org.drools.impact.analysis.model.Package) Constraint(org.drools.impact.analysis.model.left.Constraint) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) Pattern(org.drools.impact.analysis.model.left.Pattern) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) Constraint(org.drools.impact.analysis.model.left.Constraint)

Aggregations

Constraint (org.drools.impact.analysis.model.left.Constraint)4 MapConstraint (org.drools.impact.analysis.model.left.MapConstraint)4 Collection (java.util.Collection)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 AnalysisModel (org.drools.impact.analysis.model.AnalysisModel)2 Package (org.drools.impact.analysis.model.Package)2 Rule (org.drools.impact.analysis.model.Rule)2 LeftHandSide (org.drools.impact.analysis.model.left.LeftHandSide)2 Pattern (org.drools.impact.analysis.model.left.Pattern)2 ConsequenceAction (org.drools.impact.analysis.model.right.ConsequenceAction)2 InsertAction (org.drools.impact.analysis.model.right.InsertAction)2 InsertedProperty (org.drools.impact.analysis.model.right.InsertedProperty)2 ModifiedMapProperty (org.drools.impact.analysis.model.right.ModifiedMapProperty)2 ModifiedProperty (org.drools.impact.analysis.model.right.ModifiedProperty)2 ModifyAction (org.drools.impact.analysis.model.right.ModifyAction)2 RightHandSide (org.drools.impact.analysis.model.right.RightHandSide)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1