From 31840e6e8284faa0a0fceda0bb6dec7e69fdec6b Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Tue, 24 Dec 2019 15:09:23 +0100 Subject: [PATCH 1/2] Generalized image generation and opening --- src/main/java/tfm/exec/CFGLog.java | 23 ++--------- src/main/java/tfm/exec/GraphLog.java | 36 ++++++++++++++--- src/main/java/tfm/exec/Main.java | 3 -- src/main/java/tfm/exec/PDGLog.java | 57 ++++++++++----------------- src/main/java/tfm/exec/SDGLog.java | 17 -------- src/main/java/tfm/slicing/Slice.java | 2 +- src/main/java/tfm/utils/FileUtil.java | 35 ++++++++++++++++ 7 files changed, 91 insertions(+), 82 deletions(-) create mode 100644 src/main/java/tfm/utils/FileUtil.java diff --git a/src/main/java/tfm/exec/CFGLog.java b/src/main/java/tfm/exec/CFGLog.java index f538229..ebd6b2f 100644 --- a/src/main/java/tfm/exec/CFGLog.java +++ b/src/main/java/tfm/exec/CFGLog.java @@ -4,6 +4,7 @@ import com.github.javaparser.ast.Node; import guru.nidi.graphviz.engine.Format; import guru.nidi.graphviz.engine.Graphviz; import tfm.graphs.CFGGraph; +import tfm.utils.FileUtil; import tfm.visitors.cfg.CFGBuilder; import java.io.File; @@ -13,34 +14,16 @@ import java.util.Arrays; public class CFGLog extends GraphLog { public CFGLog() { - + super(); } public CFGLog(CFGGraph graph) { - this.graph = graph; + super(graph); } @Override public void visit(Node node) { this.graph = new CFGGraph(); - node.accept(new CFGBuilder(graph), null); } - - @Override - public void generatePNGs() throws IOException { - this.generatePNGs("cfg"); - } - - @Override - public void generatePNGs(String pngName) throws IOException { - Graphviz.fromString(graph.toGraphvizRepresentation()) - .render(Format.PNG) - .toFile(new File("./out/" + pngName + ".png")); - } - - @Override - public void openVisualRepresentation() throws IOException { - new ProcessBuilder(Arrays.asList("xdg-open", "./out/cfg.png")).start(); - } } diff --git a/src/main/java/tfm/exec/GraphLog.java b/src/main/java/tfm/exec/GraphLog.java index 5f8d9cc..5a9db09 100644 --- a/src/main/java/tfm/exec/GraphLog.java +++ b/src/main/java/tfm/exec/GraphLog.java @@ -1,9 +1,13 @@ package tfm.exec; import com.github.javaparser.ast.Node; +import guru.nidi.graphviz.engine.Format; +import guru.nidi.graphviz.engine.Graphviz; import tfm.graphs.Graph; +import tfm.utils.FileUtil; import tfm.utils.Logger; +import java.io.File; import java.io.IOException; public abstract class GraphLog { @@ -14,10 +18,12 @@ public abstract class GraphLog { G graph; - protected String pngName; + protected String imageName; + protected Format format; + protected boolean generated = false; public GraphLog() { - + this(null); } public GraphLog(G graph) { @@ -43,9 +49,29 @@ public abstract class GraphLog { Logger.log(); } - public abstract void generatePNGs() throws IOException; + public void generateImages() throws IOException { + generateImages("graph"); + } + + public void generateImages(String imageName) throws IOException { + generateImages(imageName, Format.PNG); + } + + public void generateImages(String imageName, Format format) throws IOException { + this.imageName = imageName; + this.format = format; + generated = true; + Graphviz.fromString(graph.toGraphvizRepresentation()) + .render(format) + .toFile(getImageFile()); + } - public abstract void generatePNGs(String pngName) throws IOException; + public void openVisualRepresentation() throws IOException { + if (!generated) generateImages(); + FileUtil.open(getImageFile()); + } - public abstract void openVisualRepresentation() throws IOException; + protected File getImageFile() { + return new File("./out/" + imageName + "." + format.name()); + } } diff --git a/src/main/java/tfm/exec/Main.java b/src/main/java/tfm/exec/Main.java index 2cfa7ed..9532105 100644 --- a/src/main/java/tfm/exec/Main.java +++ b/src/main/java/tfm/exec/Main.java @@ -46,9 +46,6 @@ public class Main { long tt = tf - t0; graphLog.log(); - - graphLog.generatePNGs(); - graphLog.openVisualRepresentation(); Logger.log(); diff --git a/src/main/java/tfm/exec/PDGLog.java b/src/main/java/tfm/exec/PDGLog.java index 4eb4c00..719104e 100644 --- a/src/main/java/tfm/exec/PDGLog.java +++ b/src/main/java/tfm/exec/PDGLog.java @@ -1,26 +1,27 @@ package tfm.exec; import guru.nidi.graphviz.engine.Format; -import guru.nidi.graphviz.engine.Graphviz; import tfm.graphs.PDGGraph; import tfm.nodes.GraphNode; import tfm.utils.Logger; import tfm.visitors.pdg.PDGBuilder; -import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.Comparator; import java.util.stream.Collectors; public class PDGLog extends GraphLog { + private final CFGLog cfgLog; public PDGLog() { - super(); + this(null); } public PDGLog(PDGGraph pdgGraph) { super(pdgGraph); + if (graph.getCfgGraph() != null) + cfgLog = new CFGLog(graph.getCfgGraph()); + else cfgLog = null; } @Override @@ -35,45 +36,29 @@ public class PDGLog extends GraphLog { super.log(); Logger.log("Nodes with variable info"); - Logger.log( - graph.getNodes().stream() - .sorted(Comparator.comparingInt(GraphNode::getId)) - .map(node -> - String.format("GraphNode { id: %s, declared: %s, defined: %s, used: %s }", - node.getId(), - node.getDeclaredVariables(), - node.getDefinedVariables(), - node.getUsedVariables()) - ).collect(Collectors.joining(System.lineSeparator())) + Logger.log(graph.getNodes().stream() + .sorted(Comparator.comparingInt(GraphNode::getId)) + .map(node -> + String.format("GraphNode { id: %s, declared: %s, defined: %s, used: %s }", + node.getId(), + node.getDeclaredVariables(), + node.getDefinedVariables(), + node.getUsedVariables()) + ).collect(Collectors.joining(System.lineSeparator())) ); } @Override - public void generatePNGs() throws IOException { - this.generatePNGs("program"); - } - - @Override - public void generatePNGs(String pngName) throws IOException { - this.pngName = pngName; - - if (graph.getCfgGraph() != null) { - Graphviz.fromString(this.graph.getCfgGraph().toGraphvizRepresentation()) - .render(Format.PNG) - .toFile(new File("./out/" + pngName + "-cfg.png")); - } - - Graphviz.fromString(graph.toGraphvizRepresentation()) - .render(Format.PNG) - .toFile(new File("./out/" + pngName + "-pdg.png")); + public void generateImages(String imageName, Format format) throws IOException { + super.generateImages(imageName + "-pdg", format); + if (cfgLog != null) + cfgLog.generateImages(imageName + "-cfg", format); } @Override public void openVisualRepresentation() throws IOException { - if (this.graph.getCfgGraph() != null) { - new ProcessBuilder(Arrays.asList("xdg-open", "./out/" + pngName + "-cfg.png")).start(); - } - - new ProcessBuilder(Arrays.asList("xdg-open", "./out/" + pngName + "-pdg.png")).start(); + if (cfgLog != null) + cfgLog.openVisualRepresentation(); + super.openVisualRepresentation(); } } diff --git a/src/main/java/tfm/exec/SDGLog.java b/src/main/java/tfm/exec/SDGLog.java index 575f052..b88de54 100644 --- a/src/main/java/tfm/exec/SDGLog.java +++ b/src/main/java/tfm/exec/SDGLog.java @@ -11,24 +11,7 @@ public class SDGLog extends GraphLog { @Override public void visit(Node node) { this.graph = new SDGGraph(); - SDGBuilder sdgBuilder = new SDGBuilder(this.graph); - node.accept(sdgBuilder, null); } - - @Override - public void generatePNGs() throws IOException { - - } - - @Override - public void generatePNGs(String pngName) throws IOException { - - } - - @Override - public void openVisualRepresentation() throws IOException { - - } } diff --git a/src/main/java/tfm/slicing/Slice.java b/src/main/java/tfm/slicing/Slice.java index 082dd90..dca9284 100644 --- a/src/main/java/tfm/slicing/Slice.java +++ b/src/main/java/tfm/slicing/Slice.java @@ -32,7 +32,7 @@ public class Slice { PDGLog pdgLog = new PDGLog(sliced); pdgLog.log(); - pdgLog.generatePNGs(PROGRAM_NAME + "-sliced"); + pdgLog.generateImages(PROGRAM_NAME + "-sliced"); pdgLog.openVisualRepresentation(); PDGValidator.printPDGProgram("Slice" + PROGRAM_NAME, sliced); diff --git a/src/main/java/tfm/utils/FileUtil.java b/src/main/java/tfm/utils/FileUtil.java new file mode 100644 index 0000000..cefd5af --- /dev/null +++ b/src/main/java/tfm/utils/FileUtil.java @@ -0,0 +1,35 @@ +package tfm.utils; + +import java.awt.*; +import java.io.File; +import java.io.IOException; + +public class FileUtil { + public static void open(String path) throws IOException { + open(new File(path)); + } + + public static void open(File file) throws IOException { + if (Desktop.isDesktopSupported()) { + Desktop.getDesktop().open(file); + return; + } + // Alternative manual opening of the file + String os = System.getProperty("os.name").toLowerCase(); + String cmd = null; + if (os.contains("win")) { + cmd = ""; + } else if (os.contains("mac")) { + cmd = "open"; + } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) { + cmd = "xdg-open"; + } + + if (cmd != null) { + new ProcessBuilder(cmd, file.getAbsolutePath()).start(); + } else { + Logger.format("Warning: cannot open file %s in your system (%s)", + file.getName(), os); + } + } +} -- GitLab From 9e2f8822958d1d0694b52f08f3866e4a425fc8b3 Mon Sep 17 00:00:00 2001 From: Javier Costa Date: Sat, 4 Jan 2020 16:57:06 +0100 Subject: [PATCH 2/2] Fix NullPointerException when creating an empty PDGLog --- src/main/java/tfm/exec/PDGLog.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/tfm/exec/PDGLog.java b/src/main/java/tfm/exec/PDGLog.java index 719104e..a3efaec 100644 --- a/src/main/java/tfm/exec/PDGLog.java +++ b/src/main/java/tfm/exec/PDGLog.java @@ -11,7 +11,8 @@ import java.util.Comparator; import java.util.stream.Collectors; public class PDGLog extends GraphLog { - private final CFGLog cfgLog; + + private CFGLog cfgLog; public PDGLog() { this(null); @@ -19,7 +20,8 @@ public class PDGLog extends GraphLog { public PDGLog(PDGGraph pdgGraph) { super(pdgGraph); - if (graph.getCfgGraph() != null) + + if (graph != null && graph.getCfgGraph() != null) cfgLog = new CFGLog(graph.getCfgGraph()); else cfgLog = null; } @@ -29,6 +31,10 @@ public class PDGLog extends GraphLog { this.graph = new PDGGraph(); node.accept(new PDGBuilder(graph), this.graph.getRootNode()); + + if (cfgLog == null) { + cfgLog = new CFGLog(graph.getCfgGraph()); + } } @Override @@ -57,8 +63,9 @@ public class PDGLog extends GraphLog { @Override public void openVisualRepresentation() throws IOException { + super.openVisualRepresentation(); + if (cfgLog != null) cfgLog.openVisualRepresentation(); - super.openVisualRepresentation(); } } -- GitLab