Skip to content
PDGLog.java 2.4 KiB
Newer Older
package tfm.exec;

import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import tfm.graphs.PDGGraph;
import tfm.nodes.Node;
import tfm.utils.Logger;
import tfm.visitors.PDGCFGVisitor;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

Javier Costa's avatar
Javier Costa committed
public class PDGLog extends GraphLog<PDGGraph> {
    public PDGLog() {
        super();
    }

    public PDGLog(PDGGraph pdgGraph) {
        super(pdgGraph);
    }

    @Override
    public void visit(com.github.javaparser.ast.Node node) {
Javier Costa's avatar
Javier Costa committed
        this.graph = new PDGGraph();
Javier Costa's avatar
Javier Costa committed
        node.accept(new PDGCFGVisitor(graph), this.graph.getRootNode());
    public void log() throws IOException {
        super.log();

        Logger.log("Nodes with variable info");
        Logger.log(
                graph.getNodes().stream()
                        .sorted(Comparator.comparingInt(Node::getId))
                        .map(node ->
                                String.format("Node { 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 {
Javier Costa's avatar
Javier Costa committed
        this.generatePNGs("program");
    }

    @Override
    public void generatePNGs(String pngName) throws IOException {
        this.pngName = pngName;

Javier Costa's avatar
Javier Costa committed
        if (graph.getCfgGraph() != null) {
            Graphviz.fromString(this.graph.getCfgGraph().toGraphvizRepresentation())
Javier Costa's avatar
Javier Costa committed
                    .render(Format.PNG)
Javier Costa's avatar
Javier Costa committed
                    .toFile(new File("./out/" + pngName + "-cfg.png"));
Javier Costa's avatar
Javier Costa committed
        }

        Graphviz.fromString(graph.toGraphvizRepresentation())
                .render(Format.PNG)
Javier Costa's avatar
Javier Costa committed
                .toFile(new File("./out/" + pngName + "-pdg.png"));
    public void openVisualRepresentation() throws IOException {
Javier Costa's avatar
Javier Costa committed
        if (this.graph.getCfgGraph() != null) {
Javier Costa's avatar
Javier Costa committed
            new ProcessBuilder(Arrays.asList("xdg-open", "./out/" + pngName + "-cfg.png")).start();
Javier Costa's avatar
Javier Costa committed
        }

Javier Costa's avatar
Javier Costa committed
        new ProcessBuilder(Arrays.asList("xdg-open", "./out/" + pngName + "-pdg.png")).start();