Search in sources :

Example 1 with MapConstraint

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

the class LhsParser method processMapProperty.

private Constraint processMapProperty(RuleContext context, Constraint constraint, TypedExpression expr) {
    MethodCallExpr mce = expr.getExpression().asMethodCallExpr();
    Optional<Expression> scope = mce.getScope();
    if (scope.isPresent() && scope.get().isMethodCallExpr()) {
        MethodCallExpr scopeMce = scope.get().asMethodCallExpr();
        String prop = ClassUtils.getter2property(scopeMce.getName().asString());
        Optional<Class<?>> origType = expr.getOriginalPatternType();
        if (prop != null && origType.isPresent()) {
            Method accessor = ClassUtils.getAccessor(origType.get(), prop);
            if (accessor != null && Map.class.isAssignableFrom(accessor.getReturnType()) && mce.getName().asString().equals("get")) {
                String key = getLiteralString(context, mce.getArgument(0));
                if (key != null) {
                    MapConstraint mapConstraint = new MapConstraint(constraint);
                    // map name
                    mapConstraint.setProperty(prop);
                    mapConstraint.setKey(key);
                    constraint = mapConstraint;
                }
            }
        }
    }
    return constraint;
}
Also used : ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) Method(java.lang.reflect.Method) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with MapConstraint

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

Aggregations

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