Skip to content
PDG.java 2.16 KiB
Newer Older
Javier Costa's avatar
Javier Costa committed
package tfm.graphs;
Javier Costa's avatar
Javier Costa committed

Javier Costa's avatar
Javier Costa committed
import com.github.javaparser.ast.body.MethodDeclaration;
Javier Costa's avatar
Javier Costa committed
import tfm.arcs.Arc;
import tfm.arcs.pdg.ControlDependencyArc;
import tfm.arcs.pdg.DataDependencyArc;
Javier Costa's avatar
Javier Costa committed
import tfm.nodes.GraphNode;
import tfm.slicing.Slice;
Javier Costa's avatar
Javier Costa committed
import tfm.slicing.SlicingCriterion;
import tfm.utils.NodeNotFoundException;
Javier Costa's avatar
Javier Costa committed

Javier Costa's avatar
Javier Costa committed
import java.util.Optional;
Javier Costa's avatar
Javier Costa committed

/**
 * The <b>Program Dependence Graph</b> represents the statements of a method in
 * a graph, connecting statements according to their {@link ControlDependencyArc control}
 * and {@link DataDependencyArc data} relationships. You can build one manually or use
 * the {@link PDGBuilder PDGBuilder}.
 * The variations of the PDG are represented as child types.
public class PDG extends GraphWithRootNode<MethodDeclaration> implements Sliceable {
    private boolean built = false;
    private CFG cfg;
Javier Costa's avatar
Javier Costa committed

    public PDG() {
        this(new CFG());
Javier Costa's avatar
Javier Costa committed
    }

    public PDG(CFG cfg) {
Javier Costa's avatar
Javier Costa committed
        super();
        this.cfg = cfg;
Javier Costa's avatar
Javier Costa committed
    }

    public void addControlDependencyArc(GraphNode<?> from, GraphNode<?> to) {
        this.addEdge(from, to, new ControlDependencyArc());
Javier Costa's avatar
Javier Costa committed
    }

    public void addDataDependencyArc(GraphNode<?> from, GraphNode<?> to, String variable) {
Javier Costa's avatar
Javier Costa committed
        this.addEdge(from, to, new DataDependencyArc(variable));
Javier Costa's avatar
Javier Costa committed
    }
    public Slice slice(SlicingCriterion slicingCriterion) {
        Optional<GraphNode<?>> node = slicingCriterion.findNode(this);
        if (!node.isPresent())
Javier Costa's avatar
Javier Costa committed
            throw new NodeNotFoundException(slicingCriterion);
        Slice slice = new Slice();
        getSliceNodes(slice, node.get());
        return slice;
    protected void getSliceNodes(Slice slice, GraphNode<?> node) {
        slice.add(node);
Javier Costa's avatar
Javier Costa committed

        for (Arc arc : incomingEdgesOf(node)) {
            GraphNode<?> from = getEdgeSource(arc);
            if (slice.contains(from))
            getSliceNodes(slice, from);
Javier Costa's avatar
Javier Costa committed

    public CFG getCfg() {
        return cfg;
    }

    @Override
    public void build(MethodDeclaration method) {
        new PDGBuilder(this).createFrom(method);
        built = true;
    @Override
    public boolean isBuilt() {
        return built;
Javier Costa's avatar
Javier Costa committed
    }
Javier Costa's avatar
Javier Costa committed
}