Search in sources :

Example 1 with ImpactAnalysisHelper

use of org.drools.impact.analysis.graph.ImpactAnalysisHelper in project drools by kiegroup.

the class ImpactAnalysisTest method testOrderRulesBackward.

@Test
public void testOrderRulesBackward() {
    AnalysisModel analysisModel = new ModelBuilder().build(ORDER_RULES);
    ModelToGraphConverter converter = new ModelToGraphConverter();
    Graph graph = converter.toGraph(analysisModel);
    // View rules which impact R5
    ImpactAnalysisHelper impactFilter = new ImpactAnalysisHelper();
    Graph impactingSubGraph = impactFilter.filterImpactingNodes(graph, "mypkg.R5");
    assertEquals(Status.IMPACTING, impactingSubGraph.getNodeMap().get("mypkg.R1").getStatus());
    assertEquals(Status.IMPACTING, impactingSubGraph.getNodeMap().get("mypkg.R2").getStatus());
    assertEquals(Status.IMPACTING, impactingSubGraph.getNodeMap().get("mypkg.R3").getStatus());
    assertEquals(Status.IMPACTING, impactingSubGraph.getNodeMap().get("mypkg.R4").getStatus());
    assertEquals(Status.TARGET, impactingSubGraph.getNodeMap().get("mypkg.R5").getStatus());
    assertNull(impactingSubGraph.getNodeMap().get("mypkg.R6"));
    // TextReporter test
    String hierarchyText = TextReporter.toHierarchyText(impactingSubGraph);
    List<String> lines = Arrays.asList(hierarchyText.split(System.lineSeparator()));
    Assertions.assertThat(lines).containsExactlyInAnyOrder("R1[!]", INDENT + "R3[!]", INDENT + INDENT + "R5[@]", INDENT + INDENT + INDENT + "(R3)", "R2[!]", INDENT + "(R3)", "R4[!]", INDENT + "(R5)");
    String flatText = TextReporter.toFlatText(impactingSubGraph);
    List<String> lines2 = Arrays.asList(flatText.split(System.lineSeparator()));
    Assertions.assertThat(lines2).containsExactlyInAnyOrder("R1[!]", "R2[!]", "R3[!]", "R4[!]", "R5[@]");
}
Also used : ModelBuilder(org.drools.impact.analysis.parser.ModelBuilder) Graph(org.drools.impact.analysis.graph.Graph) AnalysisModel(org.drools.impact.analysis.model.AnalysisModel) ImpactAnalysisHelper(org.drools.impact.analysis.graph.ImpactAnalysisHelper) ModelToGraphConverter(org.drools.impact.analysis.graph.ModelToGraphConverter) Test(org.junit.Test)

Example 2 with ImpactAnalysisHelper

use of org.drools.impact.analysis.graph.ImpactAnalysisHelper 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());
}
Also used : ModelBuilder(org.drools.impact.analysis.parser.ModelBuilder) Graph(org.drools.impact.analysis.graph.Graph) AnalysisModel(org.drools.impact.analysis.model.AnalysisModel) Node(org.drools.impact.analysis.graph.Node) GraphCollapsionHelper(org.drools.impact.analysis.graph.GraphCollapsionHelper) ImpactAnalysisHelper(org.drools.impact.analysis.graph.ImpactAnalysisHelper) ModelToGraphConverter(org.drools.impact.analysis.graph.ModelToGraphConverter) Test(org.junit.Test)

Example 3 with ImpactAnalysisHelper

use of org.drools.impact.analysis.graph.ImpactAnalysisHelper in project drools by kiegroup.

the class LinkFilterTest method testImpactAnalysisHelper.

@Test
public void testImpactAnalysisHelper() {
    // ImpactAnalysisHelper simply returns a sub map which contains only a changed node and impacted nodes
    // So we can assertNull for non-impacted nodes. (Links are not modified)
    AnalysisModel analysisModel = new ModelBuilder().build(SIMPLE_RULE);
    // default ALL
    ModelToGraphConverter converter = new ModelToGraphConverter();
    Graph graph = converter.toGraph(analysisModel);
    ImpactAnalysisHelper impactFilter = new ImpactAnalysisHelper(LinkFilter.POSITIVE);
    Graph impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R1");
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R2", ReactivityType.POSITIVE);
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R3"));
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R4"));
    graph = converter.toGraph(analysisModel);
    impactFilter = new ImpactAnalysisHelper(LinkFilter.NEGATIVE);
    impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R1");
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R2"));
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R3", ReactivityType.NEGATIVE);
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R4"));
    graph = converter.toGraph(analysisModel);
    impactFilter = new ImpactAnalysisHelper(LinkFilter.UNKNOWN);
    impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R1");
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R2"));
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R3"));
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R4", ReactivityType.UNKNOWN);
    graph = converter.toGraph(analysisModel);
    impactFilter = new ImpactAnalysisHelper(LinkFilter.POSITIVE_NEGATIVE);
    impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R1");
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R2", ReactivityType.POSITIVE);
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R3", ReactivityType.NEGATIVE);
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R4"));
    graph = converter.toGraph(analysisModel);
    impactFilter = new ImpactAnalysisHelper(LinkFilter.POSITIVE_UNKNOWN);
    impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R1");
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R2", ReactivityType.POSITIVE);
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R3"));
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R4", ReactivityType.UNKNOWN);
    graph = converter.toGraph(analysisModel);
    impactFilter = new ImpactAnalysisHelper(LinkFilter.NEGATIVE_UNKNOWN);
    impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R1");
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R2"));
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R3", ReactivityType.NEGATIVE);
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R4", ReactivityType.UNKNOWN);
    graph = converter.toGraph(analysisModel);
    impactFilter = new ImpactAnalysisHelper(LinkFilter.ALL);
    impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R1");
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R2", ReactivityType.POSITIVE);
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R3", ReactivityType.NEGATIVE);
    assertLink(impactedSubGraph, "mypkg.R1", "mypkg.R4", ReactivityType.UNKNOWN);
}
Also used : ModelBuilder(org.drools.impact.analysis.parser.ModelBuilder) Graph(org.drools.impact.analysis.graph.Graph) AnalysisModel(org.drools.impact.analysis.model.AnalysisModel) ImpactAnalysisHelper(org.drools.impact.analysis.graph.ImpactAnalysisHelper) ModelToGraphConverter(org.drools.impact.analysis.graph.ModelToGraphConverter) Test(org.junit.Test)

Example 4 with ImpactAnalysisHelper

use of org.drools.impact.analysis.graph.ImpactAnalysisHelper in project drools by kiegroup.

the class ImpactAnalysisTest method testOrderRules.

@Test
public void testOrderRules() {
    AnalysisModel analysisModel = new ModelBuilder().build(ORDER_RULES);
    ModelToGraphConverter converter = new ModelToGraphConverter();
    Graph graph = converter.toGraph(analysisModel);
    // View rules which are impacted by R2
    ImpactAnalysisHelper impactFilter = new ImpactAnalysisHelper();
    Graph impactedSubGraph = impactFilter.filterImpactedNodes(graph, "mypkg.R2");
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R1"));
    assertEquals(Status.CHANGED, impactedSubGraph.getNodeMap().get("mypkg.R2").getStatus());
    assertEquals(Status.IMPACTED, impactedSubGraph.getNodeMap().get("mypkg.R3").getStatus());
    assertNull(impactedSubGraph.getNodeMap().get("mypkg.R4"));
    assertEquals(Status.IMPACTED, impactedSubGraph.getNodeMap().get("mypkg.R5").getStatus());
    assertEquals(Status.IMPACTED, impactedSubGraph.getNodeMap().get("mypkg.R6").getStatus());
    // TextReporter test
    String hierarchyText = TextReporter.toHierarchyText(impactedSubGraph);
    List<String> lines = Arrays.asList(hierarchyText.split(System.lineSeparator()));
    Assertions.assertThat(lines).containsExactlyInAnyOrder("R2[*]", INDENT + "R3[+]", INDENT + INDENT + "R6[+]", INDENT + INDENT + "R5[+]", INDENT + INDENT + INDENT + "(R3)");
    String flatText = TextReporter.toFlatText(impactedSubGraph);
    List<String> lines2 = Arrays.asList(flatText.split(System.lineSeparator()));
    Assertions.assertThat(lines2).containsExactlyInAnyOrder("R2[*]", "R3[+]", "R6[+]", "R5[+]");
}
Also used : ModelBuilder(org.drools.impact.analysis.parser.ModelBuilder) Graph(org.drools.impact.analysis.graph.Graph) AnalysisModel(org.drools.impact.analysis.model.AnalysisModel) ImpactAnalysisHelper(org.drools.impact.analysis.graph.ImpactAnalysisHelper) ModelToGraphConverter(org.drools.impact.analysis.graph.ModelToGraphConverter) Test(org.junit.Test)

Example 5 with ImpactAnalysisHelper

use of org.drools.impact.analysis.graph.ImpactAnalysisHelper 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");
}
Also used : Order(org.drools.impact.analysis.example.domain.Order) KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) Product(org.drools.impact.analysis.example.domain.Product) GraphCollapsionHelper(org.drools.impact.analysis.graph.GraphCollapsionHelper) KieServices(org.kie.api.KieServices) ImpactAnalysisHelper(org.drools.impact.analysis.graph.ImpactAnalysisHelper) ReleaseId(org.kie.api.builder.ReleaseId) ModelToGraphConverter(org.drools.impact.analysis.graph.ModelToGraphConverter) Graph(org.drools.impact.analysis.graph.Graph) AnalysisModel(org.drools.impact.analysis.model.AnalysisModel) ImpactAnalysisKieModule(org.drools.impact.analysis.parser.internal.ImpactAnalysisKieModule) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Aggregations

Graph (org.drools.impact.analysis.graph.Graph)6 ImpactAnalysisHelper (org.drools.impact.analysis.graph.ImpactAnalysisHelper)6 ModelToGraphConverter (org.drools.impact.analysis.graph.ModelToGraphConverter)6 AnalysisModel (org.drools.impact.analysis.model.AnalysisModel)6 Test (org.junit.Test)6 ModelBuilder (org.drools.impact.analysis.parser.ModelBuilder)4 GraphCollapsionHelper (org.drools.impact.analysis.graph.GraphCollapsionHelper)3 Node (org.drools.impact.analysis.graph.Node)2 ImpactAnalysisKieModule (org.drools.impact.analysis.parser.internal.ImpactAnalysisKieModule)2 KieServices (org.kie.api.KieServices)2 KieBuilder (org.kie.api.builder.KieBuilder)2 KieFileSystem (org.kie.api.builder.KieFileSystem)2 ReleaseId (org.kie.api.builder.ReleaseId)2 ArrayList (java.util.ArrayList)1 Order (org.drools.impact.analysis.example.domain.Order)1 Product (org.drools.impact.analysis.example.domain.Product)1 KieSession (org.kie.api.runtime.KieSession)1