Moved from List<String> to CFG object
This commit is contained in:
parent
0e5667582b
commit
221c9e6b23
3 changed files with 95 additions and 82 deletions
70
src/main/java/grafos/CFG.java
Normal file
70
src/main/java/grafos/CFG.java
Normal file
|
@ -0,0 +1,70 @@
|
|||
package grafos;
|
||||
|
||||
import com.github.javaparser.ast.stmt.EmptyStmt;
|
||||
import com.github.javaparser.ast.stmt.Statement;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class CFG {
|
||||
private final List<Statement> nodes = new ArrayList<Statement>();
|
||||
private final Map<Statement, List<Statement>> edges = new HashMap<Statement, List<Statement>>();
|
||||
private Statement beginNode, endNode;
|
||||
|
||||
public void addNode(Statement stmt) {
|
||||
nodes.add(stmt);
|
||||
System.out.println("NODO: " + stmt2str(stmt));
|
||||
}
|
||||
|
||||
public void connect(int begin, int end) {
|
||||
connect(nodes.get(begin), nodes.get(end));
|
||||
}
|
||||
|
||||
public void connect(Statement begin, Statement end) {
|
||||
if (edges.containsKey(begin) && !edges.get(begin).contains(end)) {
|
||||
edges.get(begin).add(end);
|
||||
} else {
|
||||
List<Statement> dest = new ArrayList<Statement>();
|
||||
dest.add(end);
|
||||
edges.put(begin, dest);
|
||||
}
|
||||
}
|
||||
|
||||
public Statement beginNode() {
|
||||
if (beginNode == null) {
|
||||
beginNode = new EmptyStmt();
|
||||
nodes.add(beginNode);
|
||||
}
|
||||
return beginNode;
|
||||
}
|
||||
|
||||
public Statement endNode() {
|
||||
if (endNode == null) {
|
||||
endNode = new EmptyStmt();
|
||||
nodes.add(endNode);
|
||||
}
|
||||
return endNode;
|
||||
}
|
||||
|
||||
public List<String> toStringList() {
|
||||
List<String> res = new LinkedList<String>();
|
||||
for (Map.Entry<Statement, List<Statement>> e : edges.entrySet()) {
|
||||
for (Statement dest : e.getValue()) {
|
||||
res.add(edge2str(e.getKey(), dest));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private String edge2str(Statement a, Statement b) {
|
||||
return stmt2str(a) + "->" + stmt2str(b);
|
||||
}
|
||||
|
||||
private String stmt2str(Statement s) {
|
||||
if (s == beginNode)
|
||||
return "Start";
|
||||
if (s == endNode)
|
||||
return "Stop";
|
||||
int index = nodes.indexOf(s);
|
||||
return "\"(" + index + ") " + s.toString().replace("\"", "\\\"") + "\"";
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue