Newer
Older
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.stmt.Statement;
import tfm.nodes.Node;
import java.util.Comparator;
public class CFGGraph extends Graph<CFGNode> {
setRootVertex(new CFGNode(getNextVertexId(), getRootNodeData(), new EmptyStmt()));
public CFGNode addNode(String instruction, Statement statement) {
CFGNode vertex = new CFGNode(getNextVertexId(), instruction, statement);
protected String getRootNodeData() {
return "Start";
}
public void addControlFlowEdge(CFGNode from, CFGNode to) {
@Override
public String toGraphvizRepresentation() {
String lineSep = System.lineSeparator();
String nodes = getNodes().stream()
.sorted(Comparator.comparingInt(Node::getId))
.map(node -> String.format("%s [label=\"%s: %s\"]", node.getId(), node.getId(), node.getData()))
.collect(Collectors.joining(lineSep));
String arrows =
getArrows().stream()
.sorted(Comparator.comparingInt(arrow -> ((Node) arrow.getFrom()).getId()))
.map(arrow -> ((Arc) arrow).toGraphvizRepresentation())
.collect(Collectors.joining(lineSep));
return "digraph g{" + lineSep +