use of org.drools.impact.analysis.graph.ModelToGraphConverter in project drools by kiegroup.
the class PropertyTest method testPropertyInFunction.
@Test
public void testPropertyInFunction() {
String str = "package mypkg;\n" + "import " + Person.class.getCanonicalName() + ";" + "rule R1 when\n" + " $p : Person()\n" + "then\n" + " modify ($p) {setAge(42)};" + "end\n" + "rule R2 when\n" + " $p : Person(calculateToMonth(age) > 480)\n" + "then\n" + "end\n";
// runRule(str, new Person("John"));
AnalysisModel analysisModel = new ModelBuilder().build(str);
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
assertLink(graph, "mypkg.R1", "mypkg.R2", ReactivityType.UNKNOWN);
}
use of org.drools.impact.analysis.graph.ModelToGraphConverter in project drools by kiegroup.
the class TypeTest method testLiteralsInequality.
private void testLiteralsInequality(String propName, String value1, String value2, String thresholdValue, String dialect) {
String setter = "set" + StringUtils.ucFirst(propName);
String str = "package mypkg;\n" + "import " + PropHolder.class.getCanonicalName() + ";\n" + "dialect \"" + dialect + "\"\n" + "rule R1 when\n" + " $p : PropHolder(id == 0)\n" + "then\n" + " modify ($p) {" + setter + "(" + value1 + ")};\n" + "end\n" + "rule R2 when\n" + " $p : PropHolder(id == 1)\n" + "then\n" + " modify ($p) {" + setter + "(" + value2 + ")};\n" + "end\n" + "rule R3 when\n" + " $p : PropHolder(" + propName + " < " + thresholdValue + ")\n" + "then\n" + "end\n" + "rule R4 when\n" + " $p : PropHolder(" + propName + " > " + thresholdValue + ")\n" + "then\n" + "end\n";
// PropHolder holder = new PropHolder();
// holder.setId(0);
// runRule(str, holder);
AnalysisModel analysisModel = new ModelBuilder().build(str);
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
assertLink(graph, "mypkg.R1", "mypkg.R3", ReactivityType.POSITIVE);
assertLink(graph, "mypkg.R1", "mypkg.R4", ReactivityType.NEGATIVE);
assertLink(graph, "mypkg.R2", "mypkg.R3", ReactivityType.NEGATIVE);
assertLink(graph, "mypkg.R2", "mypkg.R4", ReactivityType.POSITIVE);
}
use of org.drools.impact.analysis.graph.ModelToGraphConverter in project drools by kiegroup.
the class TypeTest method testLiteralsEquality.
private void testLiteralsEquality(String propName, String value1, String value2, String dialect) {
String setter = "set" + StringUtils.ucFirst(propName);
String str = "package mypkg;\n" + "import " + BigDecimal.class.getCanonicalName() + ";\n" + "import " + BigInteger.class.getCanonicalName() + ";\n" + "import " + PropHolder.class.getCanonicalName() + ";\n" + "dialect \"" + dialect + "\"\n" + "rule R1 when\n" + " $p : PropHolder(id == 0)\n" + "then\n" + " modify ($p) {" + setter + "(" + value1 + ")};\n" + "end\n" + "rule R2 when\n" + " $p : PropHolder(id == 1)\n" + "then\n" + " modify ($p) {" + setter + "(" + value2 + ")};\n" + "end\n" + "rule R3 when\n" + " $p : PropHolder(" + propName + " == " + value1 + ")\n" + "then\n" + "end\n" + "rule R4 when\n" + " $p : PropHolder(" + propName + " == " + value2 + ")\n" + "then\n" + "end\n";
// PropHolder holder = new PropHolder();
// holder.setId(0);
// runRule(str, holder);
AnalysisModel analysisModel = new ModelBuilder().build(str);
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
assertLink(graph, "mypkg.R1", "mypkg.R3", ReactivityType.POSITIVE);
assertLink(graph, "mypkg.R1", "mypkg.R4", ReactivityType.NEGATIVE);
assertLink(graph, "mypkg.R2", "mypkg.R3", ReactivityType.NEGATIVE);
assertLink(graph, "mypkg.R2", "mypkg.R4", ReactivityType.POSITIVE);
}
use of org.drools.impact.analysis.graph.ModelToGraphConverter 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.ModelToGraphConverter in project drools by kiegroup.
the class BasicGraphTest method testBlackBoxMethod.
@Test
public void testBlackBoxMethod() {
String str = "package mypkg;\n" + "import " + Person.class.getCanonicalName() + ";" + "rule R1 when\n" + " $p : Person(name == \"Mario\")\n" + "then\n" + " modify($p) { setAge( 18 ) };" + "end\n" + "rule R2 when\n" + " $p : Person(blackBoxMethod())\n" + "then\n" + "end\n";
AnalysisModel analysisModel = new ModelBuilder().build(str);
ModelToGraphConverter converter = new ModelToGraphConverter();
Graph graph = converter.toGraph(analysisModel);
assertLink(graph, "mypkg.R1", "mypkg.R2", ReactivityType.UNKNOWN);
}
Aggregations