Commit 80f538b3 authored by Javier Costa's avatar Javier Costa
Browse files

Refactor: GraphNode

parent fa157fc4
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ import java.util.Objects;
public abstract class Arc<D extends ArcData> extends edg.graphlib.Arrow<String, D> {

    @SuppressWarnings("unchecked")
    public Arc(GraphNode from, GraphNode to) {
    public Arc(GraphNode<?> from, GraphNode<?> to) {
        super((edg.graphlib.Vertex<String, D>) from, (edg.graphlib.Vertex<String, D>) to);
    }

@@ -37,12 +37,12 @@ public abstract class Arc<D extends ArcData> extends edg.graphlib.Arrow<String,
        );
    }

    public GraphNode getFromNode() {
        return (GraphNode) super.getFrom();
    public GraphNode<?> getFromNode() {
        return (GraphNode<?>) super.getFrom();
    }

    public GraphNode getToNode() {
        return (GraphNode) super.getTo();
    public GraphNode<?> getToNode() {
        return (GraphNode<?>) super.getTo();
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ import tfm.utils.Logger;

import java.io.IOException;

public abstract class GraphLog<G extends Graph<?>> {
public abstract class GraphLog<G extends Graph> {

    static final String CFG = "cfg";
    static final String PDG = "pdg";
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ import java.util.Optional;

public class Main {

    public static final String PROGRAM = Utils.PROGRAMS_FOLDER + "cfg/Eval_1.java";
    public static final String PROGRAM = Utils.PROGRAMS_FOLDER + "pdg/Example1.java";
    public static final String METHOD = "";
    public static final String GRAPH = GraphLog.CFG;

+56 −8
Original line number Diff line number Diff line
@@ -2,27 +2,29 @@ package tfm.graphs;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.stmt.Statement;
import edg.graphlib.Arrow;
import tfm.arcs.Arc;
import tfm.arcs.cfg.ControlFlowArc;
import tfm.nodes.CFGNode;
import tfm.nodes.GraphNode;
import tfm.slicing.SlicingCriterion;
import tfm.utils.NodeNotFoundException;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class CFGGraph extends Graph<CFGNode<?>> {
public class CFGGraph extends Graph {

    public CFGGraph() {
        super();
        setRootVertex(new CFGNode<>(getNextVertexId(), getRootNodeData(), new EmptyStmt()));
        setRootVertex(new GraphNode<>(getNextVertexId(), getRootNodeData(), new EmptyStmt()));
    }

    @Override
    public <ASTNode extends Node> CFGNode<ASTNode> addNode(String instruction, ASTNode node) {
        CFGNode<ASTNode> vertex = new CFGNode<>(getNextVertexId(), instruction, node);
    public <ASTNode extends Node> GraphNode<ASTNode> addNode(String instruction, ASTNode node) {
        GraphNode<ASTNode> vertex = new GraphNode<>(getNextVertexId(), instruction, node);
        this.addVertex(vertex);

        return vertex;
@@ -34,7 +36,7 @@ public class CFGGraph extends Graph<CFGNode<?>> {
    }

    @SuppressWarnings("unchecked")
    public void addControlFlowEdge(CFGNode from, CFGNode to) {
    public void addControlFlowEdge(GraphNode from, GraphNode to) {
        super.addEdge((Arrow) new ControlFlowArc(from, to));
    }

@@ -60,7 +62,53 @@ public class CFGGraph extends Graph<CFGNode<?>> {
    }

    @Override
    public Graph<CFGNode<?>> slice(SlicingCriterion slicingCriterion) {
    public Graph slice(SlicingCriterion slicingCriterion) {
        return this;
    }

    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<>();

        for (Arc<?> arc : currentNode.getIncomingArcs()) {
            ControlFlowArc controlFlowArc = (ControlFlowArc) arc;

            GraphNode<?> from = controlFlowArc.getFromNode();

//            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;
    }
}
+23 −20
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ import java.util.stream.Collectors;
/**
 * A graphlib Graph without cost and data in arcs
 * */
public abstract class Graph<NodeType extends GraphNode<?>> extends edg.graphlib.Graph<String, ArcData> {
public abstract class Graph extends edg.graphlib.Graph<String, ArcData> {

    private int nextVertexId = 0;

@@ -45,10 +45,10 @@ public abstract class Graph<NodeType extends GraphNode<?>> extends edg.graphlib.
        super();
    }

    @Override
    /*
    /**
      Library fix: if a node had an edge to itself resulted in 2 outgoing nodes instead of 1 outgoing and 1 incoming
     */
    @Override
    public boolean addEdge(Arrow<String, ArcData> arrow) {
        Vertex<String, ArcData> from = arrow.getFrom();
        Vertex<String, ArcData> to = arrow.getTo();
@@ -82,32 +82,30 @@ public abstract class Graph<NodeType extends GraphNode<?>> extends edg.graphlib.
    }

    @SuppressWarnings("unchecked")
    public NodeType getRootNode() {
        return (NodeType) super.getRootVertex();
    public GraphNode<?> getRootNode() {
        return (GraphNode<?>) super.getRootVertex();
    }

    public abstract <ASTNode extends Node> NodeType addNode(String instruction, ASTNode node);
    public abstract <ASTNode extends Node> GraphNode<ASTNode> addNode(String instruction, ASTNode node);

    public <ASTNode extends Node> Optional<NodeType> findNodeByASTNode(ASTNode astNode) {
    @SuppressWarnings("unchecked")
    public <ASTNode extends Node> Optional<GraphNode<ASTNode>> findNodeByASTNode(ASTNode astNode) {
        return getNodes().stream()
                .filter(node -> Objects.equals(node.getAstNode(), astNode))
                .findFirst();
                .findFirst()
                .map(node -> (GraphNode<ASTNode>) node);
    }

    public Optional<NodeType> findNodeById(int id) {
        return findNodeById(String.valueOf(id));
    }

    public Optional<NodeType> findNodeById(String id) {
    public Optional<GraphNode<?>> findNodeById(int id) {
        return getNodes().stream()
                .filter(node -> Objects.equals(node.getName(), id))
                .filter(node -> Objects.equals(node.getId(), id))
                .findFirst();
    }

    @SuppressWarnings("unchecked")
    public Set<NodeType> getNodes() {
    public Set<GraphNode<?>> getNodes() {
        return getVerticies().stream()
                .map(vertex -> (NodeType) vertex)
                .map(vertex -> (GraphNode<?>) vertex)
                .collect(Collectors.toSet());
    }

@@ -130,7 +128,12 @@ public abstract class Graph<NodeType extends GraphNode<?>> extends edg.graphlib.
        return nextVertexId++;
    }

    public abstract Graph<NodeType> slice(SlicingCriterion slicingCriterion);
    public boolean contains(GraphNode<?> graphNode) {
        return getNodes().stream()
                .anyMatch(node -> Objects.equals(node, graphNode));
    }

    public abstract Graph slice(SlicingCriterion slicingCriterion);

    /**
     * Deprecated for incorrect behaviour. Use removeNode instead
@@ -144,11 +147,11 @@ public abstract class Graph<NodeType extends GraphNode<?>> extends edg.graphlib.
    public void removeNode(GraphNode<?> node) {
        verticies.remove(node);

        edges.removeAll(node.getOutgoingArrows());
        edges.removeAll(node.getIncomingArrows());
        edges.removeAll(node.getOutgoingArcs());
        edges.removeAll(node.getIncomingArcs());
    }

    public List<NodeType> findDeclarationsOfVariable(String variable) {
    public List<GraphNode<?>> findDeclarationsOfVariable(String variable) {
        return getNodes().stream()
                .filter(node -> node.getDeclaredVariables().contains(variable))
                .collect(Collectors.toList());
Loading