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

import guru.nidi.graphviz.engine.Format;
import tfm.graphs.PDGGraph;
Javier Costa's avatar
Javier Costa committed
import tfm.nodes.GraphNode;
import tfm.utils.Logger;
import tfm.visitors.pdg.PDGBuilder;

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

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

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

        if (graph != null && graph.getCfgGraph() != null)
            cfgLog = new CFGLog(graph.getCfgGraph());
        else cfgLog = null;
    @Override
    public void visit(com.github.javaparser.ast.Node node) {
Javier Costa's avatar
Javier Costa committed
        this.graph = new PDGGraph();
        node.accept(new PDGBuilder(graph), this.graph.getRootNode());

        if (cfgLog == null) {
            cfgLog = new CFGLog(graph.getCfgGraph());
        }
    public void log() throws IOException {
        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()))
    public void generateImages(String imageName, Format format) throws IOException {
        super.generateImages(imageName + "-pdg", format);
        if (cfgLog != null)
            cfgLog.generateImages(imageName + "-cfg", format);
    public void openVisualRepresentation() throws IOException {
        super.openVisualRepresentation();

        if (cfgLog != null)
            cfgLog.openVisualRepresentation();