Search in sources :

Example 1 with InsertAction

use of org.drools.impact.analysis.model.right.InsertAction in project drools by kiegroup.

the class RhsParser method processInsert.

private InsertAction processInsert(RuleContext context, MethodCallExpr consequenceExpr, MethodCallExpr statement, BlockStmt ruleVariablesBlock) {
    Class<?> actionClass = getActionClass(context, consequenceExpr, statement);
    InsertAction action = new InsertAction(actionClass);
    Expression insertedArgument = statement.getArgument(0);
    String insertedId = insertedArgument.toString();
    // Process setters
    List<MethodCallExpr> insertedExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals(insertedId) || s.toString().equals("(" + insertedId + ")")).orElse(false)).collect(Collectors.toList());
    for (MethodCallExpr expr : insertedExprs) {
        String methodName = expr.getNameAsString();
        String property = ClassUtils.setter2property(methodName);
        if (property != null) {
            Object value = getLiteralValue(context, expr.getArgument(0));
            action.addInsertedProperty(new InsertedProperty(property, value));
        }
    }
    // Process literal insert
    if (isLiteral(actionClass) && insertedArgument.isLiteralExpr()) {
        action.addInsertedProperty(new InsertedProperty("this", literalToValue(insertedArgument.asLiteralExpr())));
    }
    return action;
}
Also used : ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ParserUtil.literalToValue(org.drools.impact.analysis.parser.impl.ParserUtil.literalToValue) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ParserUtil.isLiteral(org.drools.impact.analysis.parser.impl.ParserUtil.isLiteral) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) Rule(org.drools.impact.analysis.model.Rule) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ParserUtil.getLiteralValue(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralValue) ParserUtil.literalType(org.drools.impact.analysis.parser.impl.ParserUtil.literalType) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) ParserUtil.objectCreationExprToValue(org.drools.impact.analysis.parser.impl.ParserUtil.objectCreationExprToValue) Method(java.lang.reflect.Method) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ParserUtil.stripEnclosedAndCast(org.drools.impact.analysis.parser.impl.ParserUtil.stripEnclosedAndCast) ClassUtils(org.drools.core.util.ClassUtils) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Collectors(java.util.stream.Collectors) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Objects(java.util.Objects) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) InsertAction(org.drools.impact.analysis.model.right.InsertAction) Consequence(org.drools.modelcompiler.builder.generator.Consequence) Optional(java.util.Optional) ConsequenceAction(org.drools.impact.analysis.model.right.ConsequenceAction) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) Expression(com.github.javaparser.ast.expr.Expression) InsertAction(org.drools.impact.analysis.model.right.InsertAction) ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with InsertAction

use of org.drools.impact.analysis.model.right.InsertAction 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

List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Rule (org.drools.impact.analysis.model.Rule)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 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Objects (java.util.Objects)1