Newer
Older
package tfm.exec;
import com.github.javaparser.ast.CompilationUnit;
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;
public class PDGLog extends GraphLog<PDGGraph, PDGCFGVisitor> {
@Override
void visit(com.github.javaparser.ast.Node node) {
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
this.visitor = new PDGCFGVisitor(graph);
node.accept(this.visitor, this.graph.getRootNode());
}
@Override
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
void generatePNGs() throws IOException {
Graphviz.fromString(visitor.getCfgGraph().toGraphvizRepresentation())
.render(Format.PNG)
.toFile(new File("./out/pdg-cfg.png"));
Graphviz.fromString(graph.toGraphvizRepresentation())
.render(Format.PNG)
.toFile(new File("./out/pdg.png"));
}
@Override
void openVisualRepresentation() throws IOException {
new ProcessBuilder(Arrays.asList("xdg-open", "./out/pdg-cfg.png")).start();
new ProcessBuilder(Arrays.asList("xdg-open", "./out/pdg.png")).start();
}
}