Moved from Statement nodes to Node nodes

This commit is contained in:
Carlos Galindo 2019-03-26 22:41:38 +01:00
parent 221c9e6b23
commit 56c07550f2
Signed by untrusted user who does not match committer: kauron
GPG key ID: 83E68706DEE119A3
2 changed files with 31 additions and 22 deletions

View file

@ -1,35 +1,40 @@
package grafos;
import com.github.javaparser.ast.Node;
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;
private final List<Node> nodes = new ArrayList<Node>();
private final Map<Node, List<Node>> edges = new HashMap<Node, List<Node>>();
private Node beginNode, endNode;
public void addNode(Statement stmt) {
public void addNode(Node stmt) {
nodes.add(stmt);
System.out.println("NODO: " + stmt2str(stmt));
System.out.println("NODO: " + node2str(stmt));
}
public void connect(int begin, int end) {
connect(nodes.get(begin), nodes.get(end));
}
public void connect(Statement begin, Statement end) {
public void connect(Node begin, Node end) {
if (edges.containsKey(begin) && !edges.get(begin).contains(end)) {
edges.get(begin).add(end);
} else {
List<Statement> dest = new ArrayList<Statement>();
List<Node> dest = new ArrayList<Node>();
dest.add(end);
edges.put(begin, dest);
}
}
public Statement beginNode() {
public void connect(Collection<Node> begin, Node end) {
for (Node n : begin)
connect(n, end);
}
public Node beginNode() {
if (beginNode == null) {
beginNode = new EmptyStmt();
nodes.add(beginNode);
@ -37,7 +42,7 @@ public class CFG {
return beginNode;
}
public Statement endNode() {
public Node endNode() {
if (endNode == null) {
endNode = new EmptyStmt();
nodes.add(endNode);
@ -47,19 +52,19 @@ public class CFG {
public List<String> toStringList() {
List<String> res = new LinkedList<String>();
for (Map.Entry<Statement, List<Statement>> e : edges.entrySet()) {
for (Statement dest : e.getValue()) {
for (Map.Entry<Node, List<Node>> e : edges.entrySet()) {
for (Node 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 edge2str(Node a, Node b) {
return node2str(a) + "->" + node2str(b);
}
private String stmt2str(Statement s) {
private String node2str(Node s) {
if (s == beginNode)
return "Start";
if (s == endNode)

View file

@ -1,16 +1,19 @@
package grafos;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.util.Collections;
import java.util.List;
public class Visitador extends VoidVisitorAdapter<CFG> {
//********************************************************/
//********************** Atributos ***********************/
//********************************************************/
private Statement prevNode;
private List<Node> prevNode;
//********************************************************/
//*********************** Metodos ************************/
@ -20,15 +23,16 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
// Este visitador añade el nodo final al CFG
@Override
public void visit(MethodDeclaration methodDeclaration, CFG graph) {
prevNode = graph.beginNode();
prevNode = Collections.singletonList(graph.beginNode());
// Visitamos el método
super.visit(methodDeclaration, graph);
// Añadimos el nodo final al CFG
Statement end = graph.endNode();
if (prevNode != end)
graph.connect(prevNode, end);
Node end = graph.endNode();
for (Node n : prevNode)
if (n != end)
graph.connect(n, end);
}
// Visitador de expresiones
@ -37,7 +41,7 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
public void visit(ExpressionStmt es, CFG graph) {
graph.addNode(es);
graph.connect(prevNode, es);
prevNode = es;
prevNode = Collections.singletonList((Node) es);
// Seguimos visitando...
super.visit(es, graph);