|
1 | 1 | package com.ideacrest.cycledetector; |
2 | 2 |
|
| 3 | +import com.mxgraph.layout.mxCircleLayout; |
| 4 | +import com.mxgraph.layout.mxIGraphLayout; |
| 5 | +import com.mxgraph.util.mxCellRenderer; |
3 | 6 | import java.awt.Color; |
4 | 7 | import java.awt.image.BufferedImage; |
5 | 8 | import java.io.File; |
6 | 9 | import java.io.IOException; |
7 | 10 | import java.util.HashMap; |
8 | 11 | import java.util.Map; |
9 | | - |
10 | 12 | import javax.imageio.ImageIO; |
11 | | - |
12 | 13 | import org.jgrapht.Graph; |
13 | 14 | import org.jgrapht.alg.cycle.CycleDetector; |
14 | 15 | import org.jgrapht.ext.JGraphXAdapter; |
15 | 16 | import org.jgrapht.graph.AsSubgraph; |
16 | 17 | import org.jgrapht.graph.DefaultEdge; |
17 | 18 |
|
18 | | -import com.mxgraph.layout.mxCircleLayout; |
19 | | -import com.mxgraph.layout.mxIGraphLayout; |
20 | | -import com.mxgraph.util.mxCellRenderer; |
21 | | - |
22 | 19 | public class CircularReferenceChecker { |
23 | 20 |
|
24 | | - /** |
25 | | - * Detects cycles in the classReferencesGraph parameter |
26 | | - * and stores the cycles of a class as a subgraph in a Map |
27 | | - * |
28 | | - * @param classReferencesGraph |
29 | | - * @return a Map of Class and its Cycle Graph |
30 | | - */ |
31 | | - public Map<String,AsSubgraph<String, DefaultEdge>> detectCyles(Graph<String, DefaultEdge> classReferencesGraph) { |
32 | | - Map<String, AsSubgraph<String, DefaultEdge>> cyclesForEveryVertexMap = new HashMap<>(); |
33 | | - CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<>(classReferencesGraph); |
34 | | - cycleDetector.findCycles().forEach(v -> { |
35 | | - AsSubgraph<String, DefaultEdge> subGraph = new AsSubgraph<>(classReferencesGraph, |
36 | | - cycleDetector.findCyclesContainingVertex(v)); |
37 | | - cyclesForEveryVertexMap.put(v,subGraph); |
38 | | - }); |
39 | | - return cyclesForEveryVertexMap; |
40 | | - } |
41 | | - |
42 | | - /** |
43 | | - * Given graph and image name, use jgrapht to create .png file of graph in given outputDirectory. |
44 | | - * Create outputDirectory if it does not exist. |
45 | | - * |
46 | | - * @param outputDirectoryPath |
47 | | - * @param subGraph |
48 | | - * @param imageName |
49 | | - * @throws IOException |
50 | | - */ |
51 | | - public void createImage(String outputDirectoryPath, Graph<String, DefaultEdge> subGraph, String imageName) throws IOException { |
52 | | - new File(outputDirectoryPath).mkdirs(); |
53 | | - File imgFile = new File(outputDirectoryPath+"/graph" + imageName + ".png"); |
54 | | - if(imgFile.createNewFile()) { |
55 | | - JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<>(subGraph); |
56 | | - mxIGraphLayout layout = new mxCircleLayout(graphAdapter); |
57 | | - layout.execute(graphAdapter.getDefaultParent()); |
58 | | - |
59 | | - BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null); |
60 | | - if (image != null) { |
61 | | - ImageIO.write(image, "PNG", imgFile); |
62 | | - } |
63 | | - } |
64 | | - } |
| 21 | + /** |
| 22 | + * Detects cycles in the classReferencesGraph parameter |
| 23 | + * and stores the cycles of a class as a subgraph in a Map |
| 24 | + * |
| 25 | + * @param classReferencesGraph |
| 26 | + * @return a Map of Class and its Cycle Graph |
| 27 | + */ |
| 28 | + public Map<String, AsSubgraph<String, DefaultEdge>> detectCyles(Graph<String, DefaultEdge> classReferencesGraph) { |
| 29 | + Map<String, AsSubgraph<String, DefaultEdge>> cyclesForEveryVertexMap = new HashMap<>(); |
| 30 | + CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<>(classReferencesGraph); |
| 31 | + cycleDetector.findCycles().forEach(v -> { |
| 32 | + AsSubgraph<String, DefaultEdge> subGraph = |
| 33 | + new AsSubgraph<>(classReferencesGraph, cycleDetector.findCyclesContainingVertex(v)); |
| 34 | + cyclesForEveryVertexMap.put(v, subGraph); |
| 35 | + }); |
| 36 | + return cyclesForEveryVertexMap; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Given graph and image name, use jgrapht to create .png file of graph in given outputDirectory. |
| 41 | + * Create outputDirectory if it does not exist. |
| 42 | + * |
| 43 | + * @param outputDirectoryPath |
| 44 | + * @param subGraph |
| 45 | + * @param imageName |
| 46 | + * @throws IOException |
| 47 | + */ |
| 48 | + public void createImage(String outputDirectoryPath, Graph<String, DefaultEdge> subGraph, String imageName) |
| 49 | + throws IOException { |
| 50 | + new File(outputDirectoryPath).mkdirs(); |
| 51 | + File imgFile = new File(outputDirectoryPath + "/graph" + imageName + ".png"); |
| 52 | + if (imgFile.createNewFile()) { |
| 53 | + JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<>(subGraph); |
| 54 | + mxIGraphLayout layout = new mxCircleLayout(graphAdapter); |
| 55 | + layout.execute(graphAdapter.getDefaultParent()); |
| 56 | + |
| 57 | + BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null); |
| 58 | + if (image != null) { |
| 59 | + ImageIO.write(image, "PNG", imgFile); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
65 | 63 | } |
0 commit comments