Updated getAllLineNumbers function, was moving into cfg it returns a List<Integer>

This commit is contained in:
Nicolas Amaya
2025-12-14 16:24:46 -07:00
parent 9437b5e88d
commit c70db58e20
2 changed files with 59 additions and 44 deletions

View File

@@ -43,7 +43,7 @@ public class PDGTool {
// Step 3: Show results // Step 3: Show results
System.out.println("\n=== Available Lines ==="); System.out.println("\n=== Available Lines ===");
List<Integer> lines = cfgBuilder.getAllLineNumbers(); List<Integer> lines = cfg.getAllLineNumbers();
System.out.println("Lines with statements: " + lines); System.out.println("Lines with statements: " + lines);
// If specific line requested, compute impact // If specific line requested, compute impact
@@ -59,7 +59,7 @@ public class PDGTool {
} }
private static void computeImpact(ControlFlowGraph cfg, PDG pdg, private static void computeImpact(ControlFlowGraph cfg, PDG pdg,
CFGBuilder cfgBuilder, int targetLine) { CFGBuilder cfgBuilder, int targetLine) {
System.out.println("\n=== Impact Analysis for Line " + targetLine + " ==="); System.out.println("\n=== Impact Analysis for Line " + targetLine + " ===");
// Find nodes at target line // Find nodes at target line

View File

@@ -49,7 +49,7 @@ public class ControlFlowGraph {
* {@link Node#equals(Object)} method. * {@link Node#equals(Object)} method.
* *
* @param n * @param n
* The node to look for. * The node to look for.
* @return true if this CFG contains the indicated node; otherwise, false. * @return true if this CFG contains the indicated node; otherwise, false.
*/ */
public boolean containsNode(Node n) { public boolean containsNode(Node n) {
@@ -63,12 +63,12 @@ public class ControlFlowGraph {
* Constructor * Constructor
* *
* @param name * @param name
* The name of the graph. Cannot be null or empty. * The name of the graph. Cannot be null or empty.
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the name is null or empty. * If the name is null or empty.
*/ */
public ControlFlowGraph(String name) { public ControlFlowGraph(String name) {
if(name == null || name.length() < 1) if (name == null || name.length() < 1)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
this.name = name; this.name = name;
@@ -97,12 +97,23 @@ public class ControlFlowGraph {
public List<Node> nodes() { public List<Node> nodes() {
ArrayList<Node> result = new ArrayList<>(); ArrayList<Node> result = new ArrayList<>();
for(List<Node> list : nodes.values()) for (List<Node> list : nodes.values())
result.addAll(list); result.addAll(list);
return Collections.unmodifiableList(result); return Collections.unmodifiableList(result);
} }
public List<Integer> getAllLineNumbers() {
List<Integer> lineNumbers = new ArrayList<>();
for (List<Node> listNodes : this.nodes.values()) {
for (Node n : listNodes) {
lineNumbers.add(n.getLineNumber());
}
}
return lineNumbers;
}
/** /**
* Accesses the set of edges defined on this graph. * Accesses the set of edges defined on this graph.
* *
@@ -116,13 +127,13 @@ public class ControlFlowGraph {
* Accesses the first node with label as its label. * Accesses the first node with label as its label.
* *
* @param label * @param label
* The label to locate. Must not be null. * The label to locate. Must not be null.
* @return The first node with the indicated label, or null if nonesuch exists. * @return The first node with the indicated label, or null if nonesuch exists.
*/ */
public Node findNode(String label) { public Node findNode(String label) {
List<Node> list = nodes.get(label); List<Node> list = nodes.get(label);
if(list != null && list.size() > 0) if (list != null && list.size() > 0)
return list.get(0); return list.get(0);
return null; return null;
@@ -132,13 +143,13 @@ public class ControlFlowGraph {
* Accesses the first node with label as its label. * Accesses the first node with label as its label.
* *
* @param label * @param label
* The label to locate. Must not be null. * The label to locate. Must not be null.
* @return The first node with the indicated label, or null if nonesuch exists. * @return The first node with the indicated label, or null if nonesuch exists.
*/ */
public List<Node> findNodes(String label) { public List<Node> findNodes(String label) {
List<Node> list = nodes.get(label); List<Node> list = nodes.get(label);
if(list != null) if (list != null)
return Collections.unmodifiableList(list); return Collections.unmodifiableList(list);
return null; return null;
@@ -148,20 +159,20 @@ public class ControlFlowGraph {
* Builds a node on this graph with the indicated label. * Builds a node on this graph with the indicated label.
* *
* @param label * @param label
* A label for the new node that may be used in identifying it. * A label for the new node that may be used in identifying it.
* Cannot be null or empty. * Cannot be null or empty.
* @return The newly created node. * @return The newly created node.
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the label is null or empty. * If the label is null or empty.
*/ */
public Node buildNode(String label) { public Node buildNode(String label) {
if(label == null || label.length() < 1) if (label == null || label.length() < 1)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
Node node = new Node(label); Node node = new Node(label);
List<Node> list; List<Node> list;
if(nodes.containsKey(label)) if (nodes.containsKey(label))
list = nodes.get(label); list = nodes.get(label);
else { else {
list = new ArrayList<>(); list = new ArrayList<>();
@@ -178,19 +189,20 @@ public class ControlFlowGraph {
* Builds an edge on this graph with the indicated label. * Builds an edge on this graph with the indicated label.
* *
* @param source * @param source
* The source node. Cannot be null. * The source node. Cannot be null.
* @param target * @param target
* The target node. Cannot be null. * The target node. Cannot be null.
* @param label * @param label
* A label for the new edge that may be used in identifying it. * A label for the new edge that may be used in identifying it.
* Cannot be null or empty. * Cannot be null or empty.
* @return The newly created edge. * @return The newly created edge.
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the label is null or empty or either of the indicated nodes is * If the label is null or empty or either of
* null. * the indicated nodes is
* null.
*/ */
public Edge buildEdge(Node source, Node target, EdgeLabel label) { public Edge buildEdge(Node source, Node target, EdgeLabel label) {
if(source == null || label == null) if (source == null || label == null)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
Edge edge = new Edge(source, target, label); Edge edge = new Edge(source, target, label);
@@ -199,7 +211,7 @@ public class ControlFlowGraph {
edge.setGraph(this); edge.setGraph(this);
source.addOutEdge(edge); source.addOutEdge(edge);
if(target != null) if (target != null)
target.addInEdge(edge); target.addInEdge(edge);
return edge; return edge;
@@ -209,22 +221,25 @@ public class ControlFlowGraph {
* Builds an edge on this graph with the indicated label. * Builds an edge on this graph with the indicated label.
* *
* @param source * @param source
* The source node. Cannot be null. * The source node. Cannot be null.
* @param target * @param target
* The target node. Cannot be null. * The target node. Cannot be null.
* @param label * @param label
* A label for the new edge that may be used in identifying it. * A label for the new edge that may be used in identifying
* Cannot be null or empty. * it.
* Cannot be null or empty.
* @param extendedLabel * @param extendedLabel
* An extension to the label for the new edge that may be used in * An extension to the label for the new edge that may be
* identifying it. * used in
* identifying it.
* @return The newly created edge. * @return The newly created edge.
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the label is null or empty or either of the indicated nodes is * If the label is null or empty or either of
* null. * the indicated nodes is
* null.
*/ */
public Edge buildEdge(Node source, Node target, EdgeLabel label, String extendedLabel) { public Edge buildEdge(Node source, Node target, EdgeLabel label, String extendedLabel) {
if(source == null || label == null) if (source == null || label == null)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
Edge edge = new Edge(source, target, label, extendedLabel); Edge edge = new Edge(source, target, label, extendedLabel);
@@ -233,7 +248,7 @@ public class ControlFlowGraph {
edge.setGraph(this); edge.setGraph(this);
source.addOutEdge(edge); source.addOutEdge(edge);
if(target != null) if (target != null)
target.addInEdge(edge); target.addInEdge(edge);
return edge; return edge;
@@ -241,10 +256,10 @@ public class ControlFlowGraph {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof ControlFlowGraph)) if (!(obj instanceof ControlFlowGraph))
return false; return false;
ControlFlowGraph other = (ControlFlowGraph)obj; ControlFlowGraph other = (ControlFlowGraph) obj;
return areEqual(nodes, other.nodes) && areEqual(edges, other.edges); return areEqual(nodes, other.nodes) && areEqual(edges, other.edges);
} }
@@ -253,28 +268,28 @@ public class ControlFlowGraph {
List<Node> list1 = new ArrayList<>(); List<Node> list1 = new ArrayList<>();
List<Node> list2 = new ArrayList<>(); List<Node> list2 = new ArrayList<>();
for(List<Node> nodeList : nodes1.values()) for (List<Node> nodeList : nodes1.values())
list1.addAll(nodeList); list1.addAll(nodeList);
for(List<Node> nodeList : nodes2.values()) for (List<Node> nodeList : nodes2.values())
list2.addAll(nodeList); list2.addAll(nodeList);
if(list1.size() != list2.size()) if (list1.size() != list2.size())
return false; return false;
for(Node n : list1) for (Node n : list1)
if(!list2.contains(n)) if (!list2.contains(n))
return false; return false;
return true; return true;
} }
private static boolean areEqual(List<Edge> edges1, List<Edge> edges2) { private static boolean areEqual(List<Edge> edges1, List<Edge> edges2) {
if(edges1.size() != edges2.size()) if (edges1.size() != edges2.size())
return false; return false;
for(Edge edge : edges1) { for (Edge edge : edges1) {
if(!edges2.contains(edge)) if (!edges2.contains(edge))
return false; return false;
} }