forked from apache/incubator-kie-drools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DROOLS-7600] Impact Analysis : export as JSON (apache#5643)
- Loading branch information
1 parent
f512297
commit ad65ebe
Showing
6 changed files
with
354 additions
and
0 deletions.
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
66 changes: 66 additions & 0 deletions
66
...ls-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-json/pom.xml
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,66 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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 | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
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. | ||
--> | ||
<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-graph</artifactId> | ||
<groupId>org.drools</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>drools-impact-analysis-graph-json</artifactId> | ||
|
||
<name>Drools :: Impact Analysis Graph :: JSON</name> | ||
|
||
<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> |
90 changes: 90 additions & 0 deletions
90
...is-graph-json/src/main/java/org/drools/impact/analysis/graph/json/GraphJsonGenerator.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,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; | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
...alysis-graph-json/src/test/java/org/drools/impact/analysis/graph/json/JsonOutputTest.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,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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...pact-analysis-graph/drools-impact-analysis-graph-json/src/test/resources/logback-test.xml
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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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 | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
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. | ||
--> | ||
<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> |
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