Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions circular-reference-detector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-ext</artifactId>
<version>1.3.0</version>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.hjug.app;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -38,44 +37,36 @@ public void launchApp(String[] args) {
printCommandUsage();
} else {
String srcDirectoryPath = args[0];
String outputDirectoryPath = args[1];
JavaProjectParser javaProjectParser = new JavaProjectParser();
try {
Graph<String, DefaultWeightedEdge> classReferencesGraph =
javaProjectParser.getClassReferences(srcDirectoryPath);
detectAndStoreCyclesInDirectory(outputDirectoryPath, classReferencesGraph);
detectAndStoreCyclesInDirectory(classReferencesGraph);
} catch (Exception e) {
e.printStackTrace();
}
}
}

private void detectAndStoreCyclesInDirectory(
String outputDirectoryPath, Graph<String, DefaultWeightedEdge> classReferencesGraph) {
private void detectAndStoreCyclesInDirectory(Graph<String, DefaultWeightedEdge> classReferencesGraph) {
CircularReferenceChecker circularReferenceChecker = new CircularReferenceChecker();
Map<String, AsSubgraph<String, DefaultWeightedEdge>> cyclesForEveryVertexMap =
circularReferenceChecker.detectCycles(classReferencesGraph);
cyclesForEveryVertexMap.forEach((vertex, subGraph) -> {
try {
int vertexCount = subGraph.vertexSet().size();
int edgeCount = subGraph.edgeSet().size();
if (vertexCount > 1 && edgeCount > 1 && !isDuplicateSubGraph(subGraph, vertex)) {
circularReferenceChecker.createImage(outputDirectoryPath, subGraph, vertex);
renderedSubGraphs.put(vertex, subGraph);
System.out.println(
"Vertex: " + vertex + " vertex count: " + vertexCount + " edge count: " + edgeCount);
GusfieldGomoryHuCutTree<String, DefaultWeightedEdge> gusfieldGomoryHuCutTree =
new GusfieldGomoryHuCutTree<>(new AsUndirectedGraph<>(subGraph));
double minCut = gusfieldGomoryHuCutTree.calculateMinCut();
System.out.println("Min cut weight: " + minCut);
Set<DefaultWeightedEdge> minCutEdges = gusfieldGomoryHuCutTree.getCutEdges();
System.out.println("Minimum Cut Edges:");
for (DefaultWeightedEdge minCutEdge : minCutEdges) {
System.out.println(minCutEdge);
}
int vertexCount = subGraph.vertexSet().size();
int edgeCount = subGraph.edgeSet().size();
if (vertexCount > 1 && edgeCount > 1 && !isDuplicateSubGraph(subGraph, vertex)) {
renderedSubGraphs.put(vertex, subGraph);
System.out.println("Vertex: " + vertex + " vertex count: " + vertexCount + " edge count: " + edgeCount);
GusfieldGomoryHuCutTree<String, DefaultWeightedEdge> gusfieldGomoryHuCutTree =
new GusfieldGomoryHuCutTree<>(new AsUndirectedGraph<>(subGraph));
double minCut = gusfieldGomoryHuCutTree.calculateMinCut();
System.out.println("Min cut weight: " + minCut);
Set<DefaultWeightedEdge> minCutEdges = gusfieldGomoryHuCutTree.getCutEdges();
System.out.println("Minimum Cut Edges:");
for (DefaultWeightedEdge minCutEdge : minCutEdges) {
System.out.println(minCutEdge);
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package org.hjug.cycledetector;

import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.util.mxCellRenderer;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import org.jgrapht.Graph;
import org.jgrapht.alg.cycle.CycleDetector;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.AsSubgraph;
import org.jgrapht.graph.DefaultWeightedEdge;

Expand All @@ -36,29 +27,4 @@ public Map<String, AsSubgraph<String, DefaultWeightedEdge>> detectCycles(
});
return cyclesForEveryVertexMap;
}

/**
* Given graph and image name, use jgrapht to create .png file of graph in given outputDirectory.
* Create outputDirectory if it does not exist.
*
* @param outputDirectoryPath
* @param subGraph
* @param imageName
* @throws IOException
*/
public void createImage(String outputDirectoryPath, Graph<String, DefaultWeightedEdge> subGraph, String imageName)
throws IOException {
new File(outputDirectoryPath).mkdirs();
File imgFile = new File(outputDirectoryPath + "/graph" + imageName + ".png");
if (imgFile.createNewFile()) {
JGraphXAdapter<String, DefaultWeightedEdge> graphAdapter = new JGraphXAdapter<>(subGraph);
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
layout.execute(graphAdapter.getDefaultParent());

BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
if (image != null) {
ImageIO.write(image, "PNG", imgFile);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.jgrapht.Graph;
import org.jgrapht.graph.AsSubgraph;
Expand Down Expand Up @@ -34,16 +33,14 @@ public void detectCyclesTest() {

@DisplayName("Create graph image in given outputDirectory")
@Test
public void createImageTest() throws IOException {
public void createImageTest() {
Graph<String, DefaultWeightedEdge> classReferencesGraph = new DefaultDirectedGraph<>(DefaultWeightedEdge.class);
classReferencesGraph.addVertex("A");
classReferencesGraph.addVertex("B");
classReferencesGraph.addVertex("C");
classReferencesGraph.addEdge("A", "B");
classReferencesGraph.addEdge("B", "C");
classReferencesGraph.addEdge("C", "A");
sutCircularReferenceChecker.createImage(
"src/test/resources/testOutputDirectory", classReferencesGraph, "testGraph");
File newGraphImage = new File("src/test/resources/testOutputDirectory/graphtestGraph.png");
assertTrue(newGraphImage.exists() && !newGraphImage.isDirectory());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void close() throws Exception {
gitLogReader.close();
}

public List<RankedCycle> runCycleAnalysis(String outputDirectoryPath, boolean renderImages) {
public List<RankedCycle> runCycleAnalysis() {
StaticJavaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.BLEEDING_EDGE);
List<RankedCycle> rankedCycles = new ArrayList<>();
try {
Expand All @@ -84,15 +84,6 @@ public List<RankedCycle> runCycleAnalysis(String outputDirectoryPath, boolean re
double minCut = 0;
Set<DefaultWeightedEdge> minCutEdges;
if (vertexCount > 1 && edgeCount > 1 && !isDuplicateSubGraph(subGraph, vertex)) {
if (renderImages) {
try {
circularReferenceChecker.createImage(
outputDirectoryPath + "/refactorFirst/cycles", subGraph, vertex);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

renderedSubGraphs.put(vertex, subGraph);
log.info("Vertex: " + vertex + " vertex count: " + vertexCount + " edge count: " + edgeCount);
GusfieldGomoryHuCutTree<String, DefaultWeightedEdge> gusfieldGomoryHuCutTree =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public void executeReport(Locale locale) throws MavenReportException {
}

public List<RankedCycle> runCycleAnalysis(CostBenefitCalculator costBenefitCalculator, String outputDirectory) {
return costBenefitCalculator.runCycleAnalysis(outputDirectory, true);
return costBenefitCalculator.runCycleAnalysis();
}

private void renderCycles(
Expand Down
102 changes: 76 additions & 26 deletions report/src/main/java/org/hjug/refactorfirst/report/HtmlReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.hjug.cbc.RankedCycle;
import org.hjug.cbc.RankedDisharmony;
import org.hjug.gdg.GraphDataGenerator;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;

@Slf4j
public class HtmlReport extends SimpleHtmlReport {
Expand Down Expand Up @@ -39,14 +41,13 @@ public class HtmlReport extends SimpleHtmlReport {

@Override
public void printHead(StringBuilder stringBuilder) {
stringBuilder.append("<link rel=\"stylesheet\" href=\"./css/maven-base.css\" />\n"
+ " <link rel=\"stylesheet\" href=\"./css/maven-theme.css\" />\n"
+ " <link rel=\"stylesheet\" href=\"./css/site.css\" />\n"
+ " <link rel=\"stylesheet\" href=\"./css/print.css\" media=\"print\" />\n"
+ "<script async defer src=\"https://buttons.github.io/buttons.js\"></script>"
stringBuilder.append("<script async defer src=\"https://buttons.github.io/buttons.js\"></script>"
+ "<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\">"
+ "</script><script type=\"text/javascript\" src=\"./gchart.js\"></script>"
+ "<script type=\"text/javascript\" src=\"./gchart2.js\"></script>"
+ "</script><script type=\"text/javascript\" src=\"./gchart.js\"></script>\n"
+ "<script type=\"text/javascript\" src=\"./gchart2.js\"></script>\n"
+ "<script src=\"https://d3js.org/d3.v5.min.js\"></script>\n"
+ "<script src=\"https://unpkg.com/[email protected]/build/d3-graphviz.min.js\"></script>\n"
+ "<script src=\"https://unpkg.com/@hpcc-js/[email protected]/dist/index.min.js\"></script>\n"
+ " </head>\n");
}

Expand All @@ -57,24 +58,24 @@ public void printTitle(String projectName, String projectVersion, StringBuilder
.append(projectName)
.append(" ")
.append(projectVersion)
.append(" </title>");
.append(" </title>\n");
}

@Override
void renderGithubButtons(StringBuilder stringBuilder) {
stringBuilder.append("<div align=\"center\">");
stringBuilder.append("Show RefactorFirst some &#10084;&#65039;");
stringBuilder.append("<br/>");
stringBuilder.append("<div align=\"center\">\n");
stringBuilder.append("Show RefactorFirst some &#10084;&#65039;\n");
stringBuilder.append("<br/>\n");
stringBuilder.append(
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst\" data-icon=\"octicon-star\" data-size=\"large\" data-show-count=\"true\" aria-label=\"Star refactorfirst/refactorfirst on GitHub\">Star</a>");
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst\" data-icon=\"octicon-star\" data-size=\"large\" data-show-count=\"true\" aria-label=\"Star refactorfirst/refactorfirst on GitHub\">Star</a>\n");
stringBuilder.append(
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst/fork\" data-icon=\"octicon-repo-forked\" data-size=\"large\" data-show-count=\"true\" aria-label=\"Fork refactorfirst/refactorfirst on GitHub\">Fork</a>");
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst/fork\" data-icon=\"octicon-repo-forked\" data-size=\"large\" data-show-count=\"true\" aria-label=\"Fork refactorfirst/refactorfirst on GitHub\">Fork</a>\n");
stringBuilder.append(
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst/subscription\" data-icon=\"octicon-eye\" data-size=\"large\" data-show-count=\"true\" aria-label=\"Watch refactorfirst/refactorfirst on GitHub\">Watch</a>");
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst/subscription\" data-icon=\"octicon-eye\" data-size=\"large\" data-show-count=\"true\" aria-label=\"Watch refactorfirst/refactorfirst on GitHub\">Watch</a>\n");
stringBuilder.append(
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst/issues\" data-icon=\"octicon-issue-opened\" data-size=\"large\" data-show-count=\"false\" aria-label=\"Issue refactorfirst/refactorfirst on GitHub\">Issue</a>");
"<a class=\"github-button\" href=\"https://github.com/refactorfirst/refactorfirst/issues\" data-icon=\"octicon-issue-opened\" data-size=\"large\" data-show-count=\"false\" aria-label=\"Issue refactorfirst/refactorfirst on GitHub\">Issue</a>\n");
stringBuilder.append(
"<a class=\"github-button\" href=\"https://github.com/sponsors/jimbethancourt\" data-icon=\"octicon-heart\" data-size=\"large\" aria-label=\"Sponsor @jimbethancourt on GitHub\">Sponsor</a>");
"<a class=\"github-button\" href=\"https://github.com/sponsors/jimbethancourt\" data-icon=\"octicon-heart\" data-size=\"large\" aria-label=\"Sponsor @jimbethancourt on GitHub\">Sponsor</a>\n");
stringBuilder.append("</div>");
}

Expand Down Expand Up @@ -156,7 +157,7 @@ void renderGodClassChart(
int maxGodClassPriority,
StringBuilder stringBuilder) {
writeGodClassGchartJs(rankedGodClassDisharmonies, maxGodClassPriority - 1, outputDirectory);
stringBuilder.append("<div id=\"series_chart_div\" align=\"center\"></div>");
stringBuilder.append("<div id=\"series_chart_div\" align=\"center\"></div>\n");
renderGithubButtons(stringBuilder);
stringBuilder.append(GOD_CLASS_CHART_LEGEND);
}
Expand All @@ -168,23 +169,72 @@ void renderCBOChart(
int maxCboPriority,
StringBuilder stringBuilder) {
writeGCBOGchartJs(rankedCBODisharmonies, maxCboPriority - 1, outputDirectory);
stringBuilder.append("<div id=\"series_chart_div_2\" align=\"center\"></div>");
stringBuilder.append("<div id=\"series_chart_div_2\" align=\"center\"></div>\n");
renderGithubButtons(stringBuilder);
stringBuilder.append(COUPLING_BETWEEN_OBJECT_CHART_LEGEND);
}

@Override
public List<RankedCycle> runCycleAnalysis(CostBenefitCalculator costBenefitCalculator, String outputDirectory) {
return costBenefitCalculator.runCycleAnalysis(outputDirectory, true);
return costBenefitCalculator.runCycleAnalysis();
}

@Override
public void renderCycleImage(String cycleName, StringBuilder stringBuilder, String outputDirectory) {
stringBuilder.append("<div align=\"center\">");
stringBuilder.append("<img src=\"./refactorFirst/cycles/graph" + cycleName
+ ".png\" width=\"1000\" height=\"1000\" alt=\"Cycle " + cycleName + "\">");
stringBuilder.append("</div>");
stringBuilder.append("<br/>");
stringBuilder.append("<br/>");
public void renderCycleImage(
Graph<String, DefaultWeightedEdge> classGraph, RankedCycle cycle, StringBuilder stringBuilder) {
String dot = buildDot(classGraph, cycle);

stringBuilder.append("<div align=\"center\" id=\"" + cycle.getCycleName() + "\"></div>\n");
stringBuilder.append("<script>\n");
stringBuilder.append("d3.select(\"#" + cycle.getCycleName() + "\")\n");
stringBuilder.append(".graphviz()\n");
stringBuilder.append(".renderDot(" + dot + ");\n");
stringBuilder.append("</script>\n");
}

String buildDot(Graph<String, DefaultWeightedEdge> classGraph, RankedCycle cycle) {
StringBuilder dot = new StringBuilder();

dot.append("'strict digraph G {\\n' +\n");

// render vertices
// e.g DownloadManager;
for (String vertex : cycle.getVertexSet()) {
dot.append("'");
dot.append(vertex);
dot.append(";\\n' +\n");
}

for (DefaultWeightedEdge edge : cycle.getEdgeSet()) {
// 'DownloadManager -> Download [ label="1" color="red" ];'

// render edge
String[] vertexes =
edge.toString().replace("(", "").replace(")", "").split(":");

String start = vertexes[0].trim();
String end = vertexes[1].trim();

dot.append("'");
dot.append(start);
dot.append(" -> ");
dot.append(end);

// render edge attributes
dot.append(" [ ");
dot.append("label = \"");
dot.append((int) classGraph.getEdgeWeight(edge));
dot.append("\"");

if (cycle.getMinCutEdges().contains(edge)) {
dot.append(" color = \"red\"");
}

dot.append(" ];\\n' +\n");
}

dot.append("'}'");

return dot.toString();
}
}
Loading
Loading