use of org.drools.impact.analysis.model.Rule in project drools by kiegroup.
the class ModelToGraphConverter method generateGraphAnalysis.
private GraphAnalysis generateGraphAnalysis(AnalysisModel model) {
GraphAnalysis graphAnalysis = new GraphAnalysis();
for (Package pkg : model.getPackages()) {
List<Rule> rules = pkg.getRules();
for (Rule rule : rules) {
graphAnalysis.addNode(new Node(rule));
LeftHandSide lhs = rule.getLhs();
List<Pattern> patterns = lhs.getPatterns();
for (Pattern pattern : patterns) {
Class<?> patternClass = pattern.getPatternClass();
Collection<String> reactOnFields = pattern.getReactOnFields();
if (pattern.isClassReactive()) {
// Pattern which cannot analyze reactivity (e.g. Person(blackBoxMethod())) so reacts to all properties
graphAnalysis.addClassReactiveRule(patternClass, rule, pattern.isPositive());
} else if (reactOnFields.isEmpty()) {
// Pattern without constraint (e.g. Person()) so doesn't react to properties (only react to insert/delete)
graphAnalysis.addInsertReactiveRule(patternClass, rule, pattern.isPositive());
} else {
for (String field : reactOnFields) {
graphAnalysis.addPropertyReactiveRule(patternClass, field, rule, pattern.isPositive());
}
}
}
}
}
return graphAnalysis;
}
use of org.drools.impact.analysis.model.Rule in project drools by kiegroup.
the class ModelToGraphConverter method parseGraphAnalysis.
private void parseGraphAnalysis(AnalysisModel model, GraphAnalysis graphAnalysis) {
for (Package pkg : model.getPackages()) {
String pkgName = pkg.getName();
List<Rule> rules = pkg.getRules();
for (Rule rule : rules) {
String ruleName = rule.getName();
RightHandSide rhs = rule.getRhs();
List<ConsequenceAction> actions = rhs.getActions();
for (ConsequenceAction action : actions) {
switch(action.getType()) {
case INSERT:
processInsert(graphAnalysis, pkgName, ruleName, (InsertAction) action);
break;
case DELETE:
processDelete(graphAnalysis, pkgName, ruleName, action);
break;
case MODIFY:
processModify(graphAnalysis, pkgName, ruleName, (ModifyAction) action);
break;
}
}
}
}
}
use of org.drools.impact.analysis.model.Rule in project drools by kiegroup.
the class RhsParser method parse.
public void parse(RuleDescr ruleDescr, RuleContext context, Rule rule) {
BlockStmt ruleVariablesBlock = new BlockStmt();
MethodCallExpr consequenceExpr = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals("drools")).orElse(false)).map(m -> processStatement(context, consequenceExpr, m, ruleVariablesBlock)).filter(Objects::nonNull).forEach(a -> rule.getRhs().addAction(a));
}
use of org.drools.impact.analysis.model.Rule in project drools by kiegroup.
the class PackageParser method parseRule.
private Rule parseRule(RuleDescr ruleDescr) {
RuleContext context = new ImpactAnalysisRuleContext(kbuilder, packageModel, pkgRegistry.getTypeResolver(), ruleDescr);
context.addGlobalDeclarations();
context.setDialectFromAttributes(packageDescr.getAttributes());
Rule rule = new Rule(packageDescr.getName(), ruleDescr.getName(), ruleDescr.getResource().getSourcePath());
logger.debug("Parsing : " + rule.getName());
new LhsParser(packageModel, pkgRegistry).parse(ruleDescr, context, rule);
new RhsParser(pkgRegistry).parse(ruleDescr, context, rule);
return rule;
}
use of org.drools.impact.analysis.model.Rule 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);
}
}
}
Aggregations