diff --git a/src/main/java/tfm/exec/CFGLog.java b/src/main/java/tfm/exec/CFGLog.java index f5382290b1134ec2c2a21e37e1334c5d1bd399d1..ebd6b2fe63835fe0c50d36d502fee1d3dc111849 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 5f8d9cc37341f65b10903d1388a1987bf1ca2891..5a9db095ed437c27e14ccc64547c27d6aaf0b23c 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 2cfa7edc587a8f541224aa4cc637a8547dee1cc0..9532105800151a0c67f1c4322b80bb0711468341 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 4eb4c0088c3076151eca3ee3f11d9ce9881ec771..a3efaecb2043b63ae124cd863b1d99f1c4d038b6 100644 --- a/src/main/java/tfm/exec/PDGLog.java +++ b/src/main/java/tfm/exec/PDGLog.java @@ -1,26 +1,29 @@ 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 CFGLog cfgLog; + public PDGLog() { - super(); + this(null); } public PDGLog(PDGGraph pdgGraph) { super(pdgGraph); + + if (graph != null && graph.getCfgGraph() != null) + cfgLog = new CFGLog(graph.getCfgGraph()); + else cfgLog = null; } @Override @@ -28,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 @@ -35,45 +42,30 @@ 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(); - } + super.openVisualRepresentation(); - new ProcessBuilder(Arrays.asList("xdg-open", "./out/" + pngName + "-pdg.png")).start(); + if (cfgLog != null) + cfgLog.openVisualRepresentation(); } } diff --git a/src/main/java/tfm/exec/SDGLog.java b/src/main/java/tfm/exec/SDGLog.java index 575f0525b7261548f1ce7dc7cac7974e9f60c89d..b88de5468eabb9f24dc36c4af1265714eae365a5 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 082dd901768c45fd3f198e34a704baad24116d03..dca9284bf4c8539eac8f54f6e235f3cdd6b833f9 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 0000000000000000000000000000000000000000..cefd5af3cf3e1e7db650d1896b897fb261ba76e4 --- /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); + } + } +}