This is an automated email from the ASF dual-hosted git repository.
mariofusco pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/main by this push:
new 9a3128e47f [DROOLS-7600] Impact Analysis : export as JSON (#5643)
9a3128e47f is described below
commit 9a3128e47f13d7c0e85fe8ff00e573ec769d2f9d
Author: Toshiya Kobayashi <[email protected]>
AuthorDate: Thu Jan 18 00:15:06 2024 +0900
[DROOLS-7600] Impact Analysis : export as JSON (#5643)
---
bom/drools-bom/pom.xml | 11 ++
.../pom.xml | 40 ++++--
.../analysis/graph/json/GraphJsonGenerator.java | 90 +++++++++++++
.../impact/analysis/graph/json/JsonOutputTest.java | 147 +++++++++++++++++++++
.../src/test/resources/logback-test.xml} | 39 +++---
.../drools-impact-analysis-graph/pom.xml | 1 +
6 files changed, 299 insertions(+), 29 deletions(-)
diff --git a/bom/drools-bom/pom.xml b/bom/drools-bom/pom.xml
index f718a910cf..51378329dd 100644
--- a/bom/drools-bom/pom.xml
+++ b/bom/drools-bom/pom.xml
@@ -884,6 +884,17 @@
<version>${project.version}</version>
<classifier>sources</classifier>
</dependency>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-impact-analysis-graph-json</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-impact-analysis-graph-json</artifactId>
+ <version>${project.version}</version>
+ <classifier>sources</classifier>
+ </dependency>
<dependency>
<groupId>org.drools</groupId>
diff --git a/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/pom.xml
similarity index 54%
copy from drools-impact-analysis/drools-impact-analysis-graph/pom.xml
copy to
drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/pom.xml
index 589f0965d1..32de869fa0 100644
--- a/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
+++
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/pom.xml
@@ -23,20 +23,44 @@
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-impact-analysis</artifactId>
+ <artifactId>drools-impact-analysis-graph</artifactId>
<groupId>org.drools</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>drools-impact-analysis-graph</artifactId>
- <packaging>pom</packaging>
+ <artifactId>drools-impact-analysis-graph-json</artifactId>
- <name>Drools :: Impact Analysis Graph</name>
+ <name>Drools :: Impact Analysis Graph :: JSON</name>
- <modules>
- <module>drools-impact-analysis-graph-common</module>
- <module>drools-impact-analysis-graph-graphviz</module>
- </modules>
+ <properties>
+ <java.module.name>org.drools.impact.analysis.graph.json</java.module.name>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-impact-analysis-graph-common</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-impact-analysis-parser</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</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>
diff --git
a/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/main/java/org/drools/impact/analysis/graph/json/GraphJsonGenerator.java
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/main/java/org/drools/impact/analysis/graph/json/GraphJsonGenerator.java
new file mode 100644
index 0000000000..e578b434e1
--- /dev/null
+++
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/main/java/org/drools/impact/analysis/graph/json/GraphJsonGenerator.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.impact.analysis.graph.json;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.drools.impact.analysis.graph.Graph;
+import org.drools.impact.analysis.graph.Link;
+import org.drools.impact.analysis.graph.Node;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class GraphJsonGenerator {
+
+ private static final Logger logger =
LoggerFactory.getLogger(GraphJsonGenerator.class);
+
+ private GraphJsonGenerator() {
+ // Creating instances of this class is not allowed.
+ }
+
+ public static String generateJson(Graph graph) {
+ Map<String, List<Map<String, String>>> map = convertToMap(graph);
+ ObjectMapper objectMapper = new ObjectMapper();
+ try {
+ return objectMapper.writeValueAsString(map);
+ } catch (JsonProcessingException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ private static Map<String, List<Map<String, String>>> convertToMap(Graph
graph) {
+ Map<String, List<Map<String, String>>> map = new HashMap<>();
+ List<Map<String, String>> nodes = new ArrayList<>();
+ List<Map<String, String>> edges = new ArrayList<>();
+ map.put("NODES", nodes);
+ map.put("EDGES", edges);
+ for (Node node : graph.getNodeMap().values()) {
+ Map<String, String> nodeMap = new HashMap<>();
+ nodeMap.put("id", node.getId());
+ nodeMap.put("type", "node");
+ nodeMap.put("label", node.getRuleName());
+ nodes.add(nodeMap);
+
+ for (Link outgoingLink : node.getOutgoingLinks()) {
+ Map<String, String> edgeMap = new HashMap<>();
+ edgeMap.put("id", "edge-" + node.getId() + "-" +
outgoingLink.getTarget().getId());
+ edgeMap.put("type", "edge");
+ edgeMap.put("source", node.getId());
+ edgeMap.put("target", outgoingLink.getTarget().getId());
+ switch (outgoingLink.getReactivityType()) {
+ case POSITIVE:
+ edgeMap.put("edgeStyle", "solid");
+ break;
+ case NEGATIVE:
+ edgeMap.put("edgeStyle", "dashed");
+ break;
+ case UNKNOWN:
+ edgeMap.put("edgeStyle", "dotted");
+ break;
+ default:
+ logger.warn("Unexpected reactivity type: {}",
outgoingLink.getReactivityType());
+ edgeMap.put("edgeStyle", "solid");
+ }
+ edges.add(edgeMap);
+ }
+ }
+ return map;
+ }
+}
diff --git
a/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/test/java/org/drools/impact/analysis/graph/json/JsonOutputTest.java
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/test/java/org/drools/impact/analysis/graph/json/JsonOutputTest.java
new file mode 100644
index 0000000000..1cd927715c
--- /dev/null
+++
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/test/java/org/drools/impact/analysis/graph/json/JsonOutputTest.java
@@ -0,0 +1,147 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.impact.analysis.graph.json;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.drools.impact.analysis.graph.Graph;
+import org.drools.impact.analysis.graph.Node;
+import org.drools.impact.analysis.graph.ReactivityType;
+import org.drools.impact.analysis.model.Rule;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * This class is to test from drools-impact-analysis-graph-common Graph to
json output.
+ * <p>
+ * If you want to test from DRL (involving parser), use
drools-impact-analysis-itests
+ */
+public class JsonOutputTest {
+
+ private static final String EXPECTED_JSON =
+ """
+ {
+ "NODES":[
+ {
+ "id":"org.example.rule1",
+ "label":"rule1",
+ "type":"node"
+ },
+ {
+ "id":"org.example.rule5",
+ "label":"rule5",
+ "type":"node"
+ },
+ {
+ "id":"org.example.rule4",
+ "label":"rule4",
+ "type":"node"
+ },
+ {
+ "id":"org.example.rule3",
+ "label":"rule3",
+ "type":"node"
+ },
+ {
+ "id":"org.example.rule2",
+ "label":"rule2",
+ "type":"node"
+ }
+ ],
+ "EDGES":[
+ {
+ "id":"edge-org.example.rule1-org.example.rule2",
+ "source":"org.example.rule1",
+ "type":"edge",
+ "edgeStyle":"solid",
+ "target":"org.example.rule2"
+ },
+ {
+ "id":"edge-org.example.rule1-org.example.rule3",
+ "source":"org.example.rule1",
+ "type":"edge",
+ "edgeStyle":"dashed",
+ "target":"org.example.rule3"
+ },
+ {
+ "id":"edge-org.example.rule3-org.example.rule5",
+ "source":"org.example.rule3",
+ "type":"edge",
+ "edgeStyle":"solid",
+ "target":"org.example.rule5"
+ },
+ {
+ "id":"edge-org.example.rule2-org.example.rule4",
+ "source":"org.example.rule2",
+ "type":"edge",
+ "edgeStyle":"dotted",
+ "target":"org.example.rule4"
+ }
+ ]
+ }
+ """;
+
+ @Test
+ public void generate_simpleGraph() throws JsonProcessingException {
+ Graph graph = createSimpleGraph();
+ String actualJson = GraphJsonGenerator.generateJson(graph);
+ assertJson(actualJson, EXPECTED_JSON);
+ }
+
+ private static void assertJson(String actualJson, String expectedJson)
throws JsonProcessingException {
+ ObjectMapper objectMapper = new ObjectMapper();
+ Map<String, List<Map<String, String>>> actualMap =
objectMapper.readValue(actualJson, Map.class);
+ Map<String, List<Map<String, String>>> expectedMap =
objectMapper.readValue(expectedJson, Map.class);
+
+ List<Map<String, String>> actualNodes = actualMap.get("NODES");
+ List<Map<String, String>> expectedNodes = expectedMap.get("NODES");
+
assertThat(actualNodes).containsExactlyInAnyOrderElementsOf(expectedNodes);
+
+ List<Map<String, String>> actualEdges = actualMap.get("EDGES");
+ List<Map<String, String>> expectedEdges = expectedMap.get("EDGES");
+
assertThat(actualEdges).containsExactlyInAnyOrderElementsOf(expectedEdges);
+ }
+
+ private Graph createSimpleGraph() {
+ Node node1 = new Node(new Rule("org.example", "rule1", "example"));
+ Node node2 = new Node(new Rule("org.example", "rule2", "example"));
+ Node node3 = new Node(new Rule("org.example", "rule3", "example"));
+ Node node4 = new Node(new Rule("org.example", "rule4", "example"));
+ Node node5 = new Node(new Rule("org.example", "rule5", "example"));
+
+ Node.linkNodes(node1, node2, ReactivityType.POSITIVE);
+ Node.linkNodes(node1, node3, ReactivityType.NEGATIVE);
+ Node.linkNodes(node2, node4, ReactivityType.UNKNOWN);
+ Node.linkNodes(node3, node5, ReactivityType.POSITIVE);
+
+ Map<String, Node> nodeMap = new HashMap<>();
+ nodeMap.put(node1.getFqdn(), node1);
+ nodeMap.put(node2.getFqdn(), node2);
+ nodeMap.put(node3.getFqdn(), node3);
+ nodeMap.put(node4.getFqdn(), node4);
+ nodeMap.put(node5.getFqdn(), node5);
+
+ return new Graph(nodeMap);
+ }
+}
diff --git a/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/test/resources/logback-test.xml
similarity index 56%
copy from drools-impact-analysis/drools-impact-analysis-graph/pom.xml
copy to
drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/test/resources/logback-test.xml
index 589f0965d1..32ecf9c8d4 100644
--- a/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
+++
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/src/test/resources/logback-test.xml
@@ -19,24 +19,21 @@
under the License.
-->
-<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-impact-analysis</artifactId>
- <groupId>org.drools</groupId>
- <version>999-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>drools-impact-analysis-graph</artifactId>
- <packaging>pom</packaging>
-
- <name>Drools :: Impact Analysis Graph</name>
-
- <modules>
- <module>drools-impact-analysis-graph-common</module>
- <module>drools-impact-analysis-graph-graphviz</module>
- </modules>
-
-</project>
+<configuration>
+
+ <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %class{36}
%msg%n</pattern>
+ </encoder>
+ </appender>
+
+ <logger name="org.kie" level="warn"/>
+ <logger name="org.drools" level="warn"/>
+<!-- <logger name="org.drools.impact.analysis.parser" level="debug"/> -->
+ <logger name="org.drools.impact.analysis" level="info"/>
+
+ <root level="warn">
+ <appender-ref ref="consoleAppender" />
+ </root>
+
+</configuration>
diff --git a/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
b/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
index 589f0965d1..5e220a4c42 100644
--- a/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
+++ b/drools-impact-analysis/drools-impact-analysis-graph/pom.xml
@@ -37,6 +37,7 @@
<modules>
<module>drools-impact-analysis-graph-common</module>
<module>drools-impact-analysis-graph-graphviz</module>
+ <module>drools-impact-analysis-graph-json</module>
</modules>
</project>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]