Newer
Older
import com.github.javaparser.ast.stmt.EmptyStmt;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class CFGGraph extends GraphWithRootNode {
protected GraphNode<?> buildRootNode() {
return new GraphNode<>(getNextVertexId(), "Start", new EmptyStmt());
public void addControlFlowEdge(GraphNode<?> from, GraphNode<?> to) {
super.addEdge(from, to, new ControlFlowArc());
@Override
public String toGraphvizRepresentation() {
String lineSep = System.lineSeparator();
.map(GraphNode::toGraphvizRepresentation)
getArcs().stream()
.sorted(Comparator.comparingInt(arc -> this.getEdgeSource(arc).getId()))
.map(Arc::toGraphvizRepresentation)
.collect(Collectors.joining(lineSep));
return "digraph g{" + lineSep +
public Graph slice(SlicingCriterion slicingCriterion) {
public Set<GraphNode<?>> findLastDefinitionsFrom(GraphNode<?> startNode, String variable) {
// Logger.log("=======================================================");
// Logger.log("Starting from " + startNode);
// Logger.log("Looking for variable " + variable);
// Logger.log(cfgGraph.toString());
if (!this.contains(startNode)) {
throw new NodeNotFoundException(startNode, this);
}
return findLastDefinitionsFrom(new HashSet<>(), startNode, startNode, variable);
}
private Set<GraphNode<?>> findLastDefinitionsFrom(Set<Integer> visited, GraphNode<?> startNode, GraphNode<?> currentNode, String variable) {
visited.add(currentNode.getId());
// Logger.log("On " + currentNode);
Set<GraphNode<?>> res = new HashSet<>();
ControlFlowArc controlFlowArc = (ControlFlowArc) arc;
GraphNode<?> from = this.getEdgeSource(controlFlowArc);
// Logger.log("Arrow from node: " + from);
if (!Objects.equals(startNode, from) && visited.contains(from.getId())) {
// Logger.log("It's already visited. Continuing...");
continue;
}
if (from.getDefinedVariables().contains(variable)) {
// Logger.log("Contains defined variable: " + variable);
res.add(from);
} else {
// Logger.log("Doesn't contain the variable, searching inside it");
res.addAll(findLastDefinitionsFrom(visited, startNode, from, variable));
}
}
// Logger.format("Done with node %s", currentNode.getId());
return res;
}