Unverified Commit 51bba5db authored by Javier Costa's avatar Javier Costa Committed by GitHub
Browse files

Merge pull request #2 from jacosro/1-open-images

Generalized image generation and opening
parents 4879ae97 9e2f8822
Loading
Loading
Loading
Loading
+3 −20
Original line number Diff line number Diff line
@@ -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<CFGGraph> {

    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();
    }
}
+31 −5
Original line number Diff line number Diff line
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<G extends Graph> {
@@ -14,10 +18,12 @@ public abstract class GraphLog<G extends Graph> {

    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<G extends Graph> {
        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());
    }
}
+0 −3
Original line number Diff line number Diff line
@@ -46,9 +46,6 @@ public class Main {
        long tt = tf - t0;

        graphLog.log();

        graphLog.generatePNGs();

        graphLog.openVisualRepresentation();

        Logger.log();
+27 −35
Original line number Diff line number Diff line
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<PDGGraph> {

    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<PDGGraph> {
        this.graph = new PDGGraph();

        node.accept(new PDGBuilder(graph), this.graph.getRootNode());

        if (cfgLog == null) {
            cfgLog = new CFGLog(graph.getCfgGraph());
        }
    }

    @Override
@@ -35,8 +42,7 @@ public class PDGLog extends GraphLog<PDGGraph> {
        super.log();

        Logger.log("Nodes with variable info");
        Logger.log(
                graph.getNodes().stream()
        Logger.log(graph.getNodes().stream()
                .sorted(Comparator.comparingInt(GraphNode::getId))
                .map(node ->
                        String.format("GraphNode { id: %s, declared: %s, defined: %s, used: %s }",
@@ -49,31 +55,17 @@ public class PDGLog extends GraphLog<PDGGraph> {
    }

    @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();
    }
}
+0 −17
Original line number Diff line number Diff line
@@ -11,24 +11,7 @@ public class SDGLog extends GraphLog<SDGGraph> {
    @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 {

    }
}
Loading