From 56c07550f25dc2055600a2aceec69b8133ee307f Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Tue, 26 Mar 2019 22:41:38 +0100 Subject: [PATCH] Moved from Statement nodes to Node nodes --- src/main/java/grafos/CFG.java | 35 ++++++++++++++++------------- src/main/java/grafos/Visitador.java | 18 +++++++++------ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/main/java/grafos/CFG.java b/src/main/java/grafos/CFG.java index b6c08d7..24dc65c 100644 --- a/src/main/java/grafos/CFG.java +++ b/src/main/java/grafos/CFG.java @@ -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 nodes = new ArrayList(); - private final Map> edges = new HashMap>(); - private Statement beginNode, endNode; + private final List nodes = new ArrayList(); + private final Map> edges = new HashMap>(); + 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 dest = new ArrayList(); + List dest = new ArrayList(); dest.add(end); edges.put(begin, dest); } } - public Statement beginNode() { + public void connect(Collection 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 toStringList() { List res = new LinkedList(); - for (Map.Entry> e : edges.entrySet()) { - for (Statement dest : e.getValue()) { + for (Map.Entry> 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) diff --git a/src/main/java/grafos/Visitador.java b/src/main/java/grafos/Visitador.java index fa0e995..8508b7c 100755 --- a/src/main/java/grafos/Visitador.java +++ b/src/main/java/grafos/Visitador.java @@ -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 { //********************************************************/ //********************** Atributos ***********************/ //********************************************************/ - private Statement prevNode; + private List prevNode; //********************************************************/ //*********************** Metodos ************************/ @@ -20,15 +23,16 @@ public class Visitador extends VoidVisitorAdapter { // 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 { 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);