-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DROOLS-7578] Evaluate Impact analysis for ansible integration rules #94
Merged
mariofusco
merged 2 commits into
kiegroup:main
from
tkobayas:DROOLS-7578-ansible-visualization
Dec 13, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>drools-ansible-rulebook-integration</artifactId> | ||
<groupId>org.drools</groupId> | ||
<version>1.0.5-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>drools-ansible-rulebook-integration-visualization</artifactId> | ||
|
||
<name>Drools :: Ansible Rulebook Integration :: Visualization</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.drools</groupId> | ||
<artifactId>drools-ansible-rulebook-integration-api</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.drools</groupId> | ||
<artifactId>drools-impact-analysis-parser</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.drools</groupId> | ||
<artifactId>drools-impact-analysis-graph-common</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.drools</groupId> | ||
<artifactId>drools-impact-analysis-graph-graphviz</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
80 changes: 80 additions & 0 deletions
80
...src/main/java/org/drools/ansible/rulebook/integration/visualization/parser/LhsParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.drools.ansible.rulebook.integration.visualization.parser; | ||
|
||
import java.util.List; | ||
|
||
import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext; | ||
import org.drools.ansible.rulebook.integration.api.domain.conditions.AstCondition; | ||
import org.drools.ansible.rulebook.integration.api.domain.conditions.Condition; | ||
import org.drools.ansible.rulebook.integration.api.domain.conditions.MapCondition; | ||
import org.drools.ansible.rulebook.integration.api.rulesmodel.ParsedCondition; | ||
import org.drools.base.facttemplates.FactTemplateObjectType; | ||
import org.drools.impact.analysis.model.left.Constraint; | ||
import org.drools.impact.analysis.model.left.LeftHandSide; | ||
import org.drools.impact.analysis.model.left.Pattern; | ||
import org.drools.model.ConstraintOperator; | ||
import org.drools.model.Index; | ||
import org.drools.model.PrototypeExpression; | ||
|
||
public class LhsParser { | ||
|
||
private LhsParser() { | ||
// intentionally left blank | ||
} | ||
|
||
public static void parse(Condition condition, LeftHandSide lhs) { | ||
Pattern pattern = new Pattern(FactTemplateObjectType.class, true); | ||
parseConditions(condition, pattern); | ||
lhs.addPattern(pattern); | ||
} | ||
|
||
private static void parseConditions(Condition condition, Pattern pattern) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, |
||
if (condition instanceof MapCondition mapCondition) { | ||
// Firstly, process the raw condition | ||
RuleGenerationContext ruleContext = new RuleGenerationContext(); | ||
Condition astCondition = MapCondition.map2Ast(ruleContext, mapCondition, null); | ||
parseConditions(astCondition, pattern); | ||
} else if (condition instanceof AstCondition.MultipleConditions multipleConditions) { | ||
// All and Any conditions | ||
List<Condition> conditions = multipleConditions.getConditions(); | ||
conditions.forEach(c -> parseConditions(c, pattern)); | ||
} else if (condition instanceof AstCondition.SingleCondition singleCondition) { | ||
// Single condition turns into a constraint | ||
ParsedCondition parsedCondition = singleCondition.getParsedCondition(); | ||
PrototypeExpression left = parsedCondition.getLeft(); | ||
ConstraintOperator operator = parsedCondition.getOperator(); | ||
PrototypeExpression right = parsedCondition.getRight(); | ||
Constraint constraint = createConstraint(operator, left, right); | ||
pattern.addConstraint(constraint); | ||
pattern.addReactOn(constraint.getProperty()); | ||
} else { | ||
throw new UnsupportedOperationException("Unsupported condition type: " + condition.getClass().getName()); | ||
} | ||
} | ||
|
||
private static Constraint createConstraint(ConstraintOperator operator, PrototypeExpression left, PrototypeExpression right) { | ||
// quick implementation assuming that the right is always a fixed value | ||
return new Constraint(convertConstraintOperator(operator), left.getIndexingKey().get(), ((PrototypeExpression.FixedValue) right).getValue()); | ||
} | ||
|
||
private static Constraint.Type convertConstraintOperator(ConstraintOperator operator) { | ||
if (operator instanceof Index.ConstraintType constraintType) { | ||
switch (constraintType) { | ||
case EQUAL: | ||
return Constraint.Type.EQUAL; | ||
case NOT_EQUAL: | ||
return Constraint.Type.NOT_EQUAL; | ||
case GREATER_THAN: | ||
return Constraint.Type.GREATER_THAN; | ||
case GREATER_OR_EQUAL: | ||
return Constraint.Type.GREATER_OR_EQUAL; | ||
case LESS_OR_EQUAL: | ||
return Constraint.Type.LESS_OR_EQUAL; | ||
case LESS_THAN: | ||
return Constraint.Type.LESS_THAN; | ||
default: | ||
return Constraint.Type.UNKNOWN; | ||
} | ||
} | ||
return Constraint.Type.UNKNOWN; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...src/main/java/org/drools/ansible/rulebook/integration/visualization/parser/RhsParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.drools.ansible.rulebook.integration.visualization.parser; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.drools.ansible.rulebook.integration.api.domain.actions.MapAction; | ||
import org.drools.base.facttemplates.FactTemplateObjectType; | ||
import org.drools.impact.analysis.model.right.DeleteSpecificFactAction; | ||
import org.drools.impact.analysis.model.right.InsertAction; | ||
import org.drools.impact.analysis.model.right.InsertedProperty; | ||
import org.drools.impact.analysis.model.right.RightHandSide; | ||
import org.drools.impact.analysis.model.right.SpecificProperty; | ||
|
||
public class RhsParser { | ||
|
||
private RhsParser() { | ||
// intentionally private | ||
} | ||
|
||
public static void parse(List<MapAction> mapActions, RightHandSide rhs) { | ||
for (MapAction mapAction : mapActions) { | ||
Map map = (Map) mapAction.get("Action"); | ||
String action = (String) map.get("action"); | ||
Map actionArgs = (Map) map.get("action_args"); | ||
switch (action) { | ||
case "post_event": | ||
addInsertAction((Map<String, String>) actionArgs.get("event"), rhs); | ||
break; | ||
case "set_fact": | ||
addInsertAction((Map<String, String>) actionArgs.get("fact"), rhs); | ||
break; | ||
case "retract_fact": | ||
addDeleteAction((Map<String, String>) actionArgs.get("fact"), rhs); | ||
break; | ||
default: | ||
// ignore any other actions | ||
} | ||
} | ||
} | ||
|
||
private static void addInsertAction(Map<String, String> propertyMap, RightHandSide rhs) { | ||
InsertAction action = new InsertAction(FactTemplateObjectType.class); | ||
propertyMap.entrySet().forEach(entry -> { | ||
InsertedProperty insertedProperty = new InsertedProperty(entry.getKey(), entry.getValue()); | ||
action.addInsertedProperty(insertedProperty); | ||
}); | ||
rhs.addAction(action); | ||
} | ||
|
||
private static void addDeleteAction(Map<String, String> propertyMap, RightHandSide rhs) { | ||
DeleteSpecificFactAction action = new DeleteSpecificFactAction(FactTemplateObjectType.class); | ||
propertyMap.entrySet().forEach(entry -> { | ||
SpecificProperty specificProperty = new SpecificProperty(entry.getKey(), entry.getValue()); | ||
action.addSpecificProperty(specificProperty); | ||
}); | ||
rhs.addAction(action); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ain/java/org/drools/ansible/rulebook/integration/visualization/parser/RulesSetParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.drools.ansible.rulebook.integration.visualization.parser; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.drools.ansible.rulebook.integration.api.domain.RulesSet; | ||
import org.drools.ansible.rulebook.integration.api.domain.conditions.ConditionParseUtil; | ||
import org.drools.impact.analysis.model.AnalysisModel; | ||
import org.drools.impact.analysis.model.Package; | ||
import org.drools.impact.analysis.model.Rule; | ||
import org.drools.impact.analysis.model.left.LeftHandSide; | ||
import org.drools.impact.analysis.model.right.RightHandSide; | ||
import org.drools.model.impl.RuleBuilder; | ||
|
||
/** | ||
* Parse drools-ansible RulesSet to drools-impact-analysis AnalysisModel | ||
*/ | ||
public class RulesSetParser { | ||
|
||
public static AnalysisModel parse(RulesSet rulesSet) { | ||
AnalysisModel analysisModel = new AnalysisModel(); | ||
List<org.drools.impact.analysis.model.Rule> rules = new ArrayList<>(); | ||
rulesSet.getRules().forEach(ruleContainer -> { | ||
rules.add(parseRule(ruleContainer.getRule())); | ||
}); | ||
Package pkg = new Package(RuleBuilder.DEFAULT_PACKAGE, rules); | ||
analysisModel.addPackage(pkg); | ||
return analysisModel; | ||
} | ||
|
||
private static Rule parseRule(org.drools.ansible.rulebook.integration.api.domain.Rule ansibleRule) { | ||
Rule analysisRule = new Rule(RuleBuilder.DEFAULT_PACKAGE, ansibleRule.getName(), null); | ||
LhsParser.parse(ansibleRule.getCondition(), analysisRule.getLhs()); | ||
RhsParser.parse(ansibleRule.getActions(), analysisRule.getRhs()); | ||
return analysisRule; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
drools-ansible-rulebook-integration-visualization/src/main/resources/simplelogger.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Default logging detail level for all instances of SimpleLogger. | ||
org.slf4j.simpleLogger.defaultLogLevel=info | ||
|
||
# Logging detail level | ||
org.slf4j.simpleLogger.log.org.drools.ansible.rulebook.integration=info | ||
org.slf4j.simpleLogger.log.guru.nidi.graphviz=warn | ||
|
||
# Set to true if you want the current date and time to be included in output messages. | ||
# Default is false, and will output the number of milliseconds elapsed since startup. | ||
org.slf4j.simpleLogger.showDateTime=true | ||
|
||
# The date and time format to be used in the output messages. | ||
# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat. | ||
# If the format is not specified or is invalid, the default format is used. | ||
# The default format is yyyy-MM-dd HH:mm:ss:SSS Z. | ||
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z | ||
|
||
# Set to true if you want to output the current thread name. | ||
# Defaults to true. | ||
org.slf4j.simpleLogger.showThreadName=true | ||
|
||
# Log output | ||
# Defaults to System.err | ||
org.slf4j.simpleLogger.logFile=System.out |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now changed from
kiegroup/drools
toapache/incubator-kie-drools
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @lampajr , this is a little irregular case.
drools-ansible-rulebook-integration
is developed inkiegroup
, but it depends on the latest SNAPSHOT ofapache/incubator-kie-drools
. Do you think these yaml changes are valid?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @tkobayas, If I am not missing anything the change is good to adapt the PR checks as the only change is the
drools
org here (so I am not expecting anything else).Let me tag @rgdoliveira such that he can also review it as he is much more involved.
PS: I think the same change should be backported to
1.0.x
branch if still in active development.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reviewing, @lampajr !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tkobayas I think this change is ok. But keep in mind that the idea is that we will have a kiegroup/drools main branch in sync with apache/incubator-kie-drools at some point (that will be our midstream). So maybe later we can move back to kiegroup/drools when that branch is in place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rgdoliveira I wasn't aware that we wanted to restore kiegroup/drools and keep it in sync with the apache project (and tbh I don't see why we should), but anyway are you ok with this change for now? If so can you please approve it so I could merge? Thank you.