Commit 8f07088a authored by Carlos Galindo's avatar Carlos Galindo
Browse files

Fixes DOT generation implementation.

* Adds API `asXArc` and `isXArc` to Arc.
* Adds Sliceable interface for graphs.
* Removed old DOT methods (toGraphvizRepresentation)
* Removed representation-related methods from graphs.
* Removed unnecessary methods.
parent 37705607
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -47,5 +47,11 @@
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
 No newline at end of file
+44 −24
Original line number Diff line number Diff line
package tfm.arcs;

import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.io.Attribute;
import tfm.arcs.cfg.ControlFlowArc;
import tfm.arcs.pdg.ControlDependencyArc;
import tfm.arcs.pdg.DataDependencyArc;
import tfm.nodes.GraphNode;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public abstract class Arc extends DefaultEdge {

    private String variable;

    public Arc() {

    }

    public Arc(String variable) {
        this.variable = variable;
    public final boolean isControlFlowArc() {
        return this instanceof ControlFlowArc;
    }

    public abstract boolean isControlFlowArrow();
    public final ControlFlowArc asControlFlowArc() {
        if (isControlFlowArc())
            return (ControlFlowArc) this;
        throw new UnsupportedOperationException("Not a ControlFlowArc");
    }

    public final boolean isControlDependencyArc() {
        return this instanceof ControlDependencyArc;
    }

    public abstract boolean isControlDependencyArrow();
    public final ControlDependencyArc asControlDependencyArc() {
        if (isControlDependencyArc())
            return (ControlDependencyArc) this;
        throw new UnsupportedOperationException("Not a ControlDependencyArc");
    }

    public abstract boolean isDataDependencyArrow();
    public final boolean isDataDependencyArc() {
        return this instanceof DataDependencyArc;
    }

    public Optional<String> getVariable() {
        return Optional.ofNullable(this.variable);
    public final DataDependencyArc asDataDependencyArc() {
        if (isDataDependencyArc())
            return (DataDependencyArc) this;
        throw new UnsupportedOperationException("Not a DataDependencyArc");
    }

    @Override
    public String toString() {
        return toGraphvizRepresentation();
        return String.format("%s{%d -> %d}", getClass().getName(),
                ((GraphNode<?>) getSource()).getId(), ((GraphNode<?>) getTarget()).getId());
    }

    public String toGraphvizRepresentation() {
        GraphNode<?> from = (GraphNode<?>) getSource();
        GraphNode<?> to = (GraphNode<?>) getTarget();
    public String getLabel() {
        return "";
    }

        return String.format("%s -> %s",
                from.getId(),
                to.getId()
        );
    public Map<String, Attribute> getDotAttributes() {
        return new HashMap<>();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
        if (this == o)
            return true;
        }

        return Objects.equals(variable, ((Arc) o).variable);
        if (o == null)
            return false;
        if (!o.getClass().equals(this.getClass()))
            return false;
        return Objects.equals(getSource(), ((Arc) o).getSource()) &&
                Objects.equals(getTarget(), ((Arc) o).getTarget());
    }

    @Override
    public int hashCode() {
        return Objects.hash(variable, getSource(), getTarget());
        return Objects.hash(getClass(), getSource(), getTarget());
    }
}
+0 −22
Original line number Diff line number Diff line
@@ -3,28 +3,6 @@ package tfm.arcs.cfg;
import tfm.arcs.Arc;

public class ControlFlowArc extends Arc {

    public ControlFlowArc() {
    }

    @Override
    public boolean isControlFlowArrow() {
        return true;
    }

    @Override
    public boolean isControlDependencyArrow() {
        return false;
    }

    @Override
    public boolean isDataDependencyArrow() {
        return false;
    }

    @Override
    public String toString() {
        return String.format("ControlFlowArc{%s}", super.toString());
    }

}
+0 −25
Original line number Diff line number Diff line
@@ -3,31 +3,6 @@ package tfm.arcs.pdg;
import tfm.arcs.Arc;

public class ControlDependencyArc extends Arc {

    public ControlDependencyArc(String variable) {
        super(variable);
    }

    public ControlDependencyArc() {
    }

    @Override
    public boolean isControlFlowArrow() {
        return false;
    }

    @Override
    public boolean isControlDependencyArrow() {
        return true;
    }

    @Override
    public boolean isDataDependencyArrow() {
        return false;
    }

    @Override
    public String toString() {
        return String.format("ControlDependencyArc{%s}", super.toString());
    }
}
+15 −26
Original line number Diff line number Diff line
package tfm.arcs.pdg;

import org.jgrapht.io.Attribute;
import org.jgrapht.io.DefaultAttribute;
import tfm.arcs.Arc;
public class DataDependencyArc extends Arc {

    public DataDependencyArc() {
    }
import java.util.Map;

    public DataDependencyArc(String variable) {
        super(variable);
    }

    @Override
    public boolean isControlFlowArrow() {
        return false;
    }

    @Override
    public boolean isControlDependencyArrow() {
        return false;
    }
public class DataDependencyArc extends Arc {
    private final String variable;

    @Override
    public boolean isDataDependencyArrow() {
        return true;
    public DataDependencyArc(String variable) {
        super();
        this.variable = variable;
    }

    @Override
    public String toString() {
        return String.format("DataDependencyArc{%s}", super.toString());
    public String getLabel() {
        return variable;
    }

    @Override
    public String toGraphvizRepresentation() {
        return String.format("%s [style=dashed, color=red%s];",
                super.toGraphvizRepresentation(),
                getVariable().map(variable -> String.format(", label=\"%s\"", variable)).orElse("")
        );
    public Map<String, Attribute> getDotAttributes() {
        Map<String, Attribute> map = super.getDotAttributes();
        map.put("style", DefaultAttribute.createAttribute("dashed"));
        map.put("color", DefaultAttribute.createAttribute("red"));
        return map;
    }
}
Loading