use of org.drools.impact.analysis.graph.GraphCollapsionHelper in project drools by kiegroup.
the class GraphCollapsionTest method testDrlRuleNamePrefix.
@Test
public void testDrlRuleNamePrefix() {
String str = "package mypkg;\n" + "import " + Order.class.getCanonicalName() + ";" + "\n" + "rule CustomerCheck_1\n" + " when\n" + " $o : Order(customerMembershipRank > 5)\n" + " then\n" + " modify($o) {\n" + " setDiscount(1000);\n" + " }\n" + "end\n" + "\n" + "rule CustomerCheck_2\n" + " when\n" + " $o : Order(customerAge > 60)\n" + " then\n" + " modify($o) {\n" + " setDiscount(2000);\n" + " }\n" + "end\n" + "\n" + "rule PriceCheck_1\n" + " when\n" + " $o : Order(itemPrice < 2000, discount >= 2000)\n" + " then\n" + " modify($o) {\n" + " setStatus(\"Too much discount\");\n" + " }\n" + "end\n" + "\n" + "rule PriceCheck_2\n" + " when\n" + " $o : Order(itemPrice > 5000)\n" + " then\n" + " modify($o) {\n" + " setStatus(\"Exclusive order\");\n" + " }\n" + "end\n" + "\n" + "rule StatusCheck_1\n" + " when\n" + " $o : Order(status == \"Too much discount\")\n" + " then\n" + " modify($o) {\n" + " setDiscount(500);\n" + " }\n" + "end\n" + "\n" + "rule StatusCheck_2\n" + " when\n" + " Order(status == \"Exclusive order\")\n" + " then\n" + " // Do some work...\n" + "end";
AnalysisModel analysisModel = new ModelBuilder().build(str);
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
Graph collapsedGraph = new GraphCollapsionHelper().collapseWithRuleNamePrefix(graph);
assertEquals(3, collapsedGraph.getNodeMap().size());
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck", ReactivityType.NEGATIVE);
// --- impact analysis
// Assuming that "modify" action in PriceCheck_X is changed
// modify action in PriceCheck_X
Node changedNode = collapsedGraph.getNodeMap().get("mypkg.PriceCheck");
ImpactAnalysisHelper impactFilter = new ImpactAnalysisHelper();
Graph impactedSubGraph = impactFilter.filterImpactedNodes(collapsedGraph, changedNode);
assertNull(impactedSubGraph.getNodeMap().get("mypkg.CustomerCheck"));
assertEquals(Status.CHANGED, impactedSubGraph.getNodeMap().get("mypkg.PriceCheck").getStatus());
assertEquals(Status.IMPACTED, impactedSubGraph.getNodeMap().get("mypkg.StatusCheck").getStatus());
}
use of org.drools.impact.analysis.graph.GraphCollapsionHelper in project drools by kiegroup.
the class LinkFilterTest method testGraphCollapsionHelper.
@Test
public void testGraphCollapsionHelper() {
// GraphCollapsionHelper creates a map with new nodes and links
// default ALL
AnalysisModel analysisModel = new ModelBuilder().build(RULE_WITH_PREFIX);
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
GraphCollapsionHelper collapsionHelper = new GraphCollapsionHelper(LinkFilter.POSITIVE);
Graph collapsedGraph = collapsionHelper.collapseWithRuleNamePrefix(graph);
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck", ReactivityType.POSITIVE);
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.POSITIVE);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck");
collapsionHelper = new GraphCollapsionHelper(LinkFilter.NEGATIVE);
collapsedGraph = collapsionHelper.collapseWithRuleNamePrefix(graph);
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck", ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck", ReactivityType.NEGATIVE);
collapsionHelper = new GraphCollapsionHelper(LinkFilter.UNKNOWN);
collapsedGraph = collapsionHelper.collapseWithRuleNamePrefix(graph);
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck");
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.UNKNOWN);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck");
collapsionHelper = new GraphCollapsionHelper(LinkFilter.POSITIVE_NEGATIVE);
collapsedGraph = collapsionHelper.collapseWithRuleNamePrefix(graph);
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck", ReactivityType.NEGATIVE);
collapsionHelper = new GraphCollapsionHelper(LinkFilter.POSITIVE_UNKNOWN);
collapsedGraph = collapsionHelper.collapseWithRuleNamePrefix(graph);
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck", ReactivityType.POSITIVE);
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.POSITIVE, ReactivityType.UNKNOWN);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck");
collapsionHelper = new GraphCollapsionHelper(LinkFilter.NEGATIVE_UNKNOWN);
collapsedGraph = collapsionHelper.collapseWithRuleNamePrefix(graph);
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck", ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.NEGATIVE, ReactivityType.UNKNOWN);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck", ReactivityType.NEGATIVE);
collapsionHelper = new GraphCollapsionHelper(LinkFilter.ALL);
collapsedGraph = collapsionHelper.collapseWithRuleNamePrefix(graph);
assertLink(collapsedGraph, "mypkg.CustomerCheck", "mypkg.PriceCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg.PriceCheck", "mypkg.StatusCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE, ReactivityType.UNKNOWN);
assertLink(collapsedGraph, "mypkg.StatusCheck", "mypkg.PriceCheck", ReactivityType.NEGATIVE);
}
use of org.drools.impact.analysis.graph.GraphCollapsionHelper in project drools by kiegroup.
the class ExampleUsageTest method testExampleUsage.
@Test
public void testExampleUsage() throws IOException {
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.drools.impact.analysis.example", "order-process", "1.0.0");
KieFileSystem kfs = createKieFileSystemWithClassPathResourceNames(releaseId, getClass(), "/org/drools/impact/analysis/example/CustomerCheck.xls", "/org/drools/impact/analysis/example/PriceCheck.xls", "/org/drools/impact/analysis/example/StatusCheck.xls", "/org/drools/impact/analysis/example/inventory.drl");
// --- Just to confirm that this rule can run. This part is not actually required for impact analysis
Order order = new Order(1, "Guitar", 6000, 65, 5);
Product guitar = new Product("Guitar", 5500, 8);
KieSession kieSession = RuleExecutionHelper.getKieSession(kfs);
List<Order> resultList = new ArrayList<>();
kieSession.setGlobal("resultList", resultList);
kieSession.insert(order);
kieSession.insert(guitar);
kieSession.fireAllRules();
kieSession.dispose();
// ---
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(ImpactAnalysisProject.class);
ImpactAnalysisKieModule analysisKieModule = (ImpactAnalysisKieModule) kieBuilder.getKieModule();
AnalysisModel analysisModel = analysisKieModule.getAnalysisModel();
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
// whole graph
generateImage(graph, "example-whole-graph");
ImpactAnalysisHelper impactFilter = new ImpactAnalysisHelper();
// Assume you want to modify RHS of PriceCheck_11
Graph impactedSubGraph = impactFilter.filterImpactedNodes(graph, "org.drools.impact.analysis.example.PriceCheck_11");
// changed node and impacted nodes
generateImage(impactedSubGraph, "example-impacted-sub-graph");
// whole graph with impact coloring
generateImage(graph, "example-impacted-whole-graph");
// Collapse graph based on rule name prefix (= RuleSet in spreadsheet)
Graph collapsedGraph = new GraphCollapsionHelper().collapseWithRuleNamePrefix(graph);
generateImage(collapsedGraph, "example-collapsed-graph");
// You can also do impact analysis for the collapsedGraph
Graph impactedCollapsedSubGraph = impactFilter.filterImpactedNodes(collapsedGraph, "org.drools.impact.analysis.example.PriceCheck");
generateImage(impactedCollapsedSubGraph, "example-impacted-collapsed-sub-graph");
// Simple text output
System.out.println("--- toHierarchyText ---");
String hierarchyText = TextReporter.toHierarchyText(impactedSubGraph);
System.out.println(hierarchyText);
System.out.println("--- toFlatText ---");
String flatText = TextReporter.toFlatText(impactedSubGraph);
System.out.println(flatText);
// Reusing the Graph instance for another filtering is allowed. All nodes status are reset to NONE implicitly
// Backward analysis. View which rules affect StatusCheck_11
Graph impactingSubGraph = impactFilter.filterImpactingNodes(graph, "org.drools.impact.analysis.example.StatusCheck_11");
// target node and impacting nodes
generateImage(impactingSubGraph, "example-impacting-sub-graph");
}
use of org.drools.impact.analysis.graph.GraphCollapsionHelper in project drools by kiegroup.
the class GraphCollapsionTest method testSpreadsheet.
@Test
public void testSpreadsheet() throws IOException {
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.drools.impact.analysis.integrationtests", "spreadsheet-test", "1.0.0");
KieFileSystem kfs = createKieFileSystemWithClassPathResourceNames(releaseId, getClass(), "collapsion01.xls", "collapsion02.xls", "collapsion03.xls");
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(ImpactAnalysisProject.class);
ImpactAnalysisKieModule analysisKieModule = (ImpactAnalysisKieModule) kieBuilder.getKieModule();
AnalysisModel analysisModel = analysisKieModule.getAnalysisModel();
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
Graph collapsedGraph = new GraphCollapsionHelper().collapseWithRuleNamePrefix(graph);
assertEquals(3, collapsedGraph.getNodeMap().size());
assertLink(collapsedGraph, "mypkg2.CustomerCheck", "mypkg2.PriceCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg2.PriceCheck", "mypkg2.StatusCheck", ReactivityType.POSITIVE, ReactivityType.NEGATIVE);
assertLink(collapsedGraph, "mypkg2.StatusCheck", "mypkg2.PriceCheck", ReactivityType.NEGATIVE);
// --- impact analysis
// Assuming that "modify" action in PriceCheck_X is changed
// modify action in PriceCheck_X
Node changedNode = collapsedGraph.getNodeMap().get("mypkg2.PriceCheck");
ImpactAnalysisHelper impactFilter = new ImpactAnalysisHelper();
Graph impactedSubGraph = impactFilter.filterImpactedNodes(collapsedGraph, changedNode);
assertNull(impactedSubGraph.getNodeMap().get("mypkg2.CustomerCheck"));
assertEquals(Status.CHANGED, impactedSubGraph.getNodeMap().get("mypkg2.PriceCheck").getStatus());
assertEquals(Status.IMPACTED, impactedSubGraph.getNodeMap().get("mypkg2.StatusCheck").getStatus());
}
Aggregations