Commit fd62f7f3 authored by Javier Costa's avatar Javier Costa
Browse files

Graph extends from DirectedPseudograph and nodes equality is made by comparing its ids

parent 4276420b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -31,8 +31,9 @@ public class PDGLog extends GraphLog<PDG> {
        Logger.log(graph.vertexSet().stream()
                .sorted(Comparator.comparingInt(GraphNode::getId))
                .map(node ->
                        String.format("GraphNode { id: %s, declared: %s, defined: %s, used: %s }",
                        String.format("GraphNode { id: %s, instruction: %s, declared: %s, defined: %s, used: %s }",
                                node.getId(),
                                node.getInstruction(),
                                node.getDeclaredVariables(),
                                node.getDefinedVariables(),
                                node.getUsedVariables())
+2 −2
Original line number Diff line number Diff line
package tfm.graphs;

import com.github.javaparser.ast.Node;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DirectedPseudograph;
import org.jgrapht.io.DOTExporter;
import tfm.arcs.Arc;
import tfm.nodes.GraphNode;
@@ -14,7 +14,7 @@ import java.util.stream.Collectors;
/**
 *
 * */
public abstract class Graph extends DefaultDirectedGraph<GraphNode<?>, Arc> {
public abstract class Graph extends DirectedPseudograph<GraphNode<?>, Arc> {

    protected static final int DEFAULT_VERTEX_START_ID = 0;

+3 −3
Original line number Diff line number Diff line
@@ -37,10 +37,10 @@ public class CFG extends GraphWithRootNode<MethodDeclaration> {
    public Set<GraphNode<?>> findLastDefinitionsFrom(GraphNode<?> startNode, String variable) {
        if (!this.containsVertex(startNode))
            throw new NodeNotFoundException(startNode, this);
        return findLastDefinitionsFrom(new HashSet<>(), startNode, startNode, variable);
        return findLastDefinitionsFrom(new HashSet<>(), startNode.getId(), startNode, variable);
    }

    private Set<GraphNode<?>> findLastDefinitionsFrom(Set<Integer> visited, GraphNode<?> startNode, GraphNode<?> currentNode, String variable) {
    private Set<GraphNode<?>> findLastDefinitionsFrom(Set<Integer> visited, int startNode, GraphNode<?> currentNode, String variable) {
        visited.add(currentNode.getId());

        Set<GraphNode<?>> res = new HashSet<>();
@@ -50,7 +50,7 @@ public class CFG extends GraphWithRootNode<MethodDeclaration> {
                continue;
            GraphNode<?> from = getEdgeSource(arc);

            if (!Objects.equals(startNode, from) && visited.contains(from.getId())) {
            if (!Objects.equals(startNode, from.getId()) && visited.contains(from.getId())) {
                continue;
            }

+5 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import tfm.graphs.cfg.CFG;
import tfm.graphs.pdg.PDG;
import tfm.graphs.sdg.SDG;
import tfm.utils.ASTUtils;
import tfm.utils.Utils;
import tfm.variables.VariableExtractor;

@@ -117,6 +118,10 @@ public class GraphNode<N extends Node> implements Comparable<GraphNode<?>> {
                && Objects.equals(astNode, other.astNode);
    }

    public boolean equalsWithASTNodeRange(Object o) {
        return equals(o) && ASTUtils.equalsWithRange(((GraphNode<?>) o).astNode, astNode);
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getInstruction(), getAstNode());
+5 −0
Original line number Diff line number Diff line
@@ -77,4 +77,9 @@ public class ASTUtils {
                || node instanceof TryStmt
                || node instanceof CatchClause;
    }

    public static boolean equalsWithRange(Node n1, Node n2) {
        return Objects.equals(n1.getRange(), n2.getRange())
                && Objects.equals(n1, n2);
    }
}