Moved from Statement nodes to Node nodes
This commit is contained in:
parent
221c9e6b23
commit
56c07550f2
2 changed files with 31 additions and 22 deletions
|
@ -1,35 +1,40 @@
|
||||||
package grafos;
|
package grafos;
|
||||||
|
|
||||||
|
import com.github.javaparser.ast.Node;
|
||||||
import com.github.javaparser.ast.stmt.EmptyStmt;
|
import com.github.javaparser.ast.stmt.EmptyStmt;
|
||||||
import com.github.javaparser.ast.stmt.Statement;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class CFG {
|
public class CFG {
|
||||||
private final List<Statement> nodes = new ArrayList<Statement>();
|
private final List<Node> nodes = new ArrayList<Node>();
|
||||||
private final Map<Statement, List<Statement>> edges = new HashMap<Statement, List<Statement>>();
|
private final Map<Node, List<Node>> edges = new HashMap<Node, List<Node>>();
|
||||||
private Statement beginNode, endNode;
|
private Node beginNode, endNode;
|
||||||
|
|
||||||
public void addNode(Statement stmt) {
|
public void addNode(Node stmt) {
|
||||||
nodes.add(stmt);
|
nodes.add(stmt);
|
||||||
System.out.println("NODO: " + stmt2str(stmt));
|
System.out.println("NODO: " + node2str(stmt));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(int begin, int end) {
|
public void connect(int begin, int end) {
|
||||||
connect(nodes.get(begin), nodes.get(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)) {
|
if (edges.containsKey(begin) && !edges.get(begin).contains(end)) {
|
||||||
edges.get(begin).add(end);
|
edges.get(begin).add(end);
|
||||||
} else {
|
} else {
|
||||||
List<Statement> dest = new ArrayList<Statement>();
|
List<Node> dest = new ArrayList<Node>();
|
||||||
dest.add(end);
|
dest.add(end);
|
||||||
edges.put(begin, dest);
|
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) {
|
if (beginNode == null) {
|
||||||
beginNode = new EmptyStmt();
|
beginNode = new EmptyStmt();
|
||||||
nodes.add(beginNode);
|
nodes.add(beginNode);
|
||||||
|
@ -37,7 +42,7 @@ public class CFG {
|
||||||
return beginNode;
|
return beginNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Statement endNode() {
|
public Node endNode() {
|
||||||
if (endNode == null) {
|
if (endNode == null) {
|
||||||
endNode = new EmptyStmt();
|
endNode = new EmptyStmt();
|
||||||
nodes.add(endNode);
|
nodes.add(endNode);
|
||||||
|
@ -47,19 +52,19 @@ public class CFG {
|
||||||
|
|
||||||
public List<String> toStringList() {
|
public List<String> toStringList() {
|
||||||
List<String> res = new LinkedList<String>();
|
List<String> res = new LinkedList<String>();
|
||||||
for (Map.Entry<Statement, List<Statement>> e : edges.entrySet()) {
|
for (Map.Entry<Node, List<Node>> e : edges.entrySet()) {
|
||||||
for (Statement dest : e.getValue()) {
|
for (Node dest : e.getValue()) {
|
||||||
res.add(edge2str(e.getKey(), dest));
|
res.add(edge2str(e.getKey(), dest));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String edge2str(Statement a, Statement b) {
|
private String edge2str(Node a, Node b) {
|
||||||
return stmt2str(a) + "->" + stmt2str(b);
|
return node2str(a) + "->" + node2str(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String stmt2str(Statement s) {
|
private String node2str(Node s) {
|
||||||
if (s == beginNode)
|
if (s == beginNode)
|
||||||
return "Start";
|
return "Start";
|
||||||
if (s == endNode)
|
if (s == endNode)
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
package grafos;
|
package grafos;
|
||||||
|
|
||||||
|
import com.github.javaparser.ast.Node;
|
||||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||||
import com.github.javaparser.ast.stmt.ExpressionStmt;
|
import com.github.javaparser.ast.stmt.ExpressionStmt;
|
||||||
import com.github.javaparser.ast.stmt.Statement;
|
|
||||||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
|
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Visitador extends VoidVisitorAdapter<CFG> {
|
public class Visitador extends VoidVisitorAdapter<CFG> {
|
||||||
//********************************************************/
|
//********************************************************/
|
||||||
//********************** Atributos ***********************/
|
//********************** Atributos ***********************/
|
||||||
//********************************************************/
|
//********************************************************/
|
||||||
|
|
||||||
private Statement prevNode;
|
private List<Node> prevNode;
|
||||||
|
|
||||||
//********************************************************/
|
//********************************************************/
|
||||||
//*********************** Metodos ************************/
|
//*********************** Metodos ************************/
|
||||||
|
@ -20,15 +23,16 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
||||||
// Este visitador añade el nodo final al CFG
|
// Este visitador añade el nodo final al CFG
|
||||||
@Override
|
@Override
|
||||||
public void visit(MethodDeclaration methodDeclaration, CFG graph) {
|
public void visit(MethodDeclaration methodDeclaration, CFG graph) {
|
||||||
prevNode = graph.beginNode();
|
prevNode = Collections.singletonList(graph.beginNode());
|
||||||
|
|
||||||
// Visitamos el método
|
// Visitamos el método
|
||||||
super.visit(methodDeclaration, graph);
|
super.visit(methodDeclaration, graph);
|
||||||
|
|
||||||
// Añadimos el nodo final al CFG
|
// Añadimos el nodo final al CFG
|
||||||
Statement end = graph.endNode();
|
Node end = graph.endNode();
|
||||||
if (prevNode != end)
|
for (Node n : prevNode)
|
||||||
graph.connect(prevNode, end);
|
if (n != end)
|
||||||
|
graph.connect(n, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visitador de expresiones
|
// Visitador de expresiones
|
||||||
|
@ -37,7 +41,7 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
||||||
public void visit(ExpressionStmt es, CFG graph) {
|
public void visit(ExpressionStmt es, CFG graph) {
|
||||||
graph.addNode(es);
|
graph.addNode(es);
|
||||||
graph.connect(prevNode, es);
|
graph.connect(prevNode, es);
|
||||||
prevNode = es;
|
prevNode = Collections.singletonList((Node) es);
|
||||||
|
|
||||||
// Seguimos visitando...
|
// Seguimos visitando...
|
||||||
super.visit(es, graph);
|
super.visit(es, graph);
|
||||||
|
|
Loading…
Reference in a new issue