Commit c459d834 authored by Carlos Galindo's avatar Carlos Galindo
Browse files

Remove graph IO features from sdg-core.

parent 7bca1023
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -58,5 +58,10 @@
            <artifactId>sdg-core</artifactId>
            <version>${parent.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jgrapht</groupId>
            <artifactId>jgrapht-io</artifactId>
            <version>1.5.0</version>
        </dependency>
    </dependencies>
</project>
+9 −0
Original line number Diff line number Diff line
package es.upv.mist.slicing.cli;

import es.upv.mist.slicing.arcs.Arc;
import es.upv.mist.slicing.graphs.cfg.CFG;

public class CFGLog extends GraphLog<CFG> {
@@ -10,4 +11,12 @@ public class CFGLog extends GraphLog<CFG> {
    public CFGLog(CFG graph) {
        super(graph);
    }

    @Override
    protected DOTAttributes edgeAttributes(Arc arc) {
        DOTAttributes res = super.edgeAttributes(arc);
        if (arc.isNonExecutableControlFlowArc())
            res.add("style", "dashed");
        return res;
    }
}
+34 −0
Original line number Diff line number Diff line
package es.upv.mist.slicing.cli;

import org.jgrapht.nio.Attribute;

import java.util.*;

import static org.jgrapht.nio.DefaultAttribute.createAttribute;

public class DOTAttributes {
    protected Map<String, Set<String>> map = new HashMap<>();

    /** Set the value of a property to the given value. */
    public void set(String key, String value) {
        Set<String> set = new HashSet<>();
        set.add(value);
        map.put(key, set);
    }

    /** Add the given value to the list of values of the given property. */
    public void add(String key, String value) {
        map.computeIfAbsent(key, k -> new HashSet<>())
                .add(value);
    }

    /** Generate the map of attributes required by DOTExporter. */
    public Map<String, Attribute> build() {
        Map<String, Attribute> map = new HashMap<>();
        for (var entry : this.map.entrySet()) {
            Optional<String> string = entry.getValue().stream().reduce((a, b) -> a + "," + b);
            string.ifPresent(s -> map.put(entry.getKey(), createAttribute(s)));
        }
        return map;
    }
}
+20 −4
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ public abstract class GraphLog<G extends Graph> {
                "****************************"
        );
        try (StringWriter stringWriter = new StringWriter()) {
            getDOTExporter(graph).exportGraph(graph, stringWriter);
            getDOTExporter().exportGraph(graph, stringWriter);
            stringWriter.append('\n');
            Logger.log(stringWriter.toString());
        }
@@ -80,7 +80,7 @@ public abstract class GraphLog<G extends Graph> {

        // Graph -> DOT -> file
        try (Writer w = new FileWriter(tmpDot)) {
            getDOTExporter(graph).exportGraph(graph, w);
            getDOTExporter().exportGraph(graph, w);
        }
        // Execute dot
        ProcessBuilder pb = new ProcessBuilder("dot",
@@ -130,7 +130,23 @@ public abstract class GraphLog<G extends Graph> {
        return new File(outputDir, imageName + "." + format);
    }

    protected DOTExporter<GraphNode<?>, Arc> getDOTExporter(G graph) {
        return graph.getDOTExporter();
    protected DOTExporter<GraphNode<?>, Arc> getDOTExporter() {
        DOTExporter<GraphNode<?>, Arc> exporter = new DOTExporter<>();
        exporter.setVertexIdProvider(node -> String.valueOf(node.getId()));
        exporter.setVertexAttributeProvider(v -> vertexAttributes(v).build());
        exporter.setEdgeAttributeProvider(v -> edgeAttributes(v).build());
        return exporter;
    }

    protected DOTAttributes vertexAttributes(GraphNode<?> node) {
        DOTAttributes res = new DOTAttributes();
        res.set("label", node.getLongLabel());
        if (node.isImplicitInstruction())
            res.add("style", "dashed");
        return res;
    }

    protected DOTAttributes edgeAttributes(Arc arc) {
        return new DOTAttributes();
    }
}
+29 −0
Original line number Diff line number Diff line
package es.upv.mist.slicing.cli;

import es.upv.mist.slicing.arcs.Arc;
import es.upv.mist.slicing.arcs.pdg.ConditionalControlDependencyArc;
import es.upv.mist.slicing.arcs.pdg.FlowDependencyArc;
import es.upv.mist.slicing.arcs.pdg.ObjectFlowDependencyArc;
import es.upv.mist.slicing.arcs.pdg.StructuralArc;
import es.upv.mist.slicing.arcs.sdg.InterproceduralArc;
import es.upv.mist.slicing.graphs.pdg.PDG;

import java.io.IOException;
@@ -33,4 +39,27 @@ public class PDGLog extends GraphLog<PDG> {
        if (cfgLog != null)
            cfgLog.openVisualRepresentation();
    }

    @Override
    protected DOTAttributes edgeAttributes(Arc arc) {
        return pdgEdgeAttributes(arc);
    }

    public static DOTAttributes pdgEdgeAttributes(Arc arc) {
        DOTAttributes res = new DOTAttributes();
        res.set("label", arc.getLabel());
        if (arc.isDataDependencyArc()
                || arc instanceof FlowDependencyArc
                || arc instanceof ObjectFlowDependencyArc)
            res.set("color", "red");
        if (arc instanceof StructuralArc)
            res.add("style", "dashed");
        if (arc.isObjectFlow() && !(arc instanceof InterproceduralArc))
            res.add("style", "dashed");
        if (arc instanceof ConditionalControlDependencyArc.CC1)
            res.add("color", "orange");
        if (arc instanceof ConditionalControlDependencyArc.CC2)
            res.add("color", "green");
        return res;
    }
}
Loading