Skip to content

Commit ec3d8d3

Browse files
author
Carlos Galindo
committed
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.
1 parent 3770560 commit ec3d8d3

17 files changed

+131
-367
lines changed

src/main/java/tfm/arcs/Arc.java

+44-24
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,79 @@
11
package tfm.arcs;
22

33
import org.jgrapht.graph.DefaultEdge;
4+
import org.jgrapht.io.Attribute;
5+
import tfm.arcs.cfg.ControlFlowArc;
6+
import tfm.arcs.pdg.ControlDependencyArc;
7+
import tfm.arcs.pdg.DataDependencyArc;
48
import tfm.nodes.GraphNode;
59

10+
import java.util.HashMap;
11+
import java.util.Map;
612
import java.util.Objects;
7-
import java.util.Optional;
813

914
public abstract class Arc extends DefaultEdge {
10-
11-
private String variable;
12-
1315
public Arc() {
1416

1517
}
1618

17-
public Arc(String variable) {
18-
this.variable = variable;
19+
public final boolean isControlFlowArc() {
20+
return this instanceof ControlFlowArc;
1921
}
2022

21-
public abstract boolean isControlFlowArrow();
23+
public final ControlFlowArc asControlFlowArc() {
24+
if (isControlFlowArc())
25+
return (ControlFlowArc) this;
26+
throw new UnsupportedOperationException("Not a ControlFlowArc");
27+
}
2228

23-
public abstract boolean isControlDependencyArrow();
29+
public final boolean isControlDependencyArc() {
30+
return this instanceof ControlDependencyArc;
31+
}
2432

25-
public abstract boolean isDataDependencyArrow();
33+
public final ControlDependencyArc asControlDependencyArc() {
34+
if (isControlDependencyArc())
35+
return (ControlDependencyArc) this;
36+
throw new UnsupportedOperationException("Not a ControlDependencyArc");
37+
}
2638

27-
public Optional<String> getVariable() {
28-
return Optional.ofNullable(this.variable);
39+
public final boolean isDataDependencyArc() {
40+
return this instanceof DataDependencyArc;
41+
}
42+
43+
public final DataDependencyArc asDataDependencyArc() {
44+
if (isDataDependencyArc())
45+
return (DataDependencyArc) this;
46+
throw new UnsupportedOperationException("Not a DataDependencyArc");
2947
}
3048

3149
@Override
3250
public String toString() {
33-
return toGraphvizRepresentation();
51+
return String.format("%s{%d -> %d}", getClass().getName(),
52+
((GraphNode<?>) getSource()).getId(), ((GraphNode<?>) getTarget()).getId());
3453
}
3554

36-
public String toGraphvizRepresentation() {
37-
GraphNode<?> from = (GraphNode<?>) getSource();
38-
GraphNode<?> to = (GraphNode<?>) getTarget();
55+
public String getLabel() {
56+
return "";
57+
}
3958

40-
return String.format("%s -> %s",
41-
from.getId(),
42-
to.getId()
43-
);
59+
public Map<String, Attribute> getDotAttributes() {
60+
return new HashMap<>();
4461
}
4562

4663
@Override
4764
public boolean equals(Object o) {
48-
if (this == o) {
65+
if (this == o)
4966
return true;
50-
}
51-
52-
return Objects.equals(variable, ((Arc) o).variable);
67+
if (o == null)
68+
return false;
69+
if (!o.getClass().equals(this.getClass()))
70+
return false;
71+
return Objects.equals(getSource(), ((Arc) o).getSource()) &&
72+
Objects.equals(getTarget(), ((Arc) o).getTarget());
5373
}
5474

5575
@Override
5676
public int hashCode() {
57-
return Objects.hash(variable, getSource(), getTarget());
77+
return Objects.hash(getClass(), getSource(), getTarget());
5878
}
5979
}

src/main/java/tfm/arcs/cfg/ControlFlowArc.java

-22
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,6 @@
33
import tfm.arcs.Arc;
44

55
public class ControlFlowArc extends Arc {
6-
76
public ControlFlowArc() {
87
}
9-
10-
@Override
11-
public boolean isControlFlowArrow() {
12-
return true;
13-
}
14-
15-
@Override
16-
public boolean isControlDependencyArrow() {
17-
return false;
18-
}
19-
20-
@Override
21-
public boolean isDataDependencyArrow() {
22-
return false;
23-
}
24-
25-
@Override
26-
public String toString() {
27-
return String.format("ControlFlowArc{%s}", super.toString());
28-
}
29-
308
}

src/main/java/tfm/arcs/pdg/ControlDependencyArc.java

-25
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,6 @@
33
import tfm.arcs.Arc;
44

55
public class ControlDependencyArc extends Arc {
6-
7-
public ControlDependencyArc(String variable) {
8-
super(variable);
9-
}
10-
116
public ControlDependencyArc() {
127
}
13-
14-
@Override
15-
public boolean isControlFlowArrow() {
16-
return false;
17-
}
18-
19-
@Override
20-
public boolean isControlDependencyArrow() {
21-
return true;
22-
}
23-
24-
@Override
25-
public boolean isDataDependencyArrow() {
26-
return false;
27-
}
28-
29-
@Override
30-
public String toString() {
31-
return String.format("ControlDependencyArc{%s}", super.toString());
32-
}
338
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11
package tfm.arcs.pdg;
22

3+
import org.jgrapht.io.Attribute;
4+
import org.jgrapht.io.DefaultAttribute;
35
import tfm.arcs.Arc;
4-
public class DataDependencyArc extends Arc {
56

6-
public DataDependencyArc() {
7-
}
7+
import java.util.Map;
88

9-
public DataDependencyArc(String variable) {
10-
super(variable);
11-
}
12-
13-
@Override
14-
public boolean isControlFlowArrow() {
15-
return false;
16-
}
17-
18-
@Override
19-
public boolean isControlDependencyArrow() {
20-
return false;
21-
}
9+
public class DataDependencyArc extends Arc {
10+
private final String variable;
2211

23-
@Override
24-
public boolean isDataDependencyArrow() {
25-
return true;
12+
public DataDependencyArc(String variable) {
13+
super();
14+
this.variable = variable;
2615
}
2716

2817
@Override
29-
public String toString() {
30-
return String.format("DataDependencyArc{%s}", super.toString());
18+
public String getLabel() {
19+
return variable;
3120
}
3221

3322
@Override
34-
public String toGraphvizRepresentation() {
35-
return String.format("%s [style=dashed, color=red%s];",
36-
super.toGraphvizRepresentation(),
37-
getVariable().map(variable -> String.format(", label=\"%s\"", variable)).orElse("")
38-
);
23+
public Map<String, Attribute> getDotAttributes() {
24+
Map<String, Attribute> map = super.getDotAttributes();
25+
map.put("style", DefaultAttribute.createAttribute("dashed"));
26+
map.put("color", DefaultAttribute.createAttribute("red"));
27+
return map;
3928
}
4029
}
4130

src/main/java/tfm/exec/GraphLog.java

+13-27
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
package tfm.exec;
22

33
import com.github.javaparser.ast.Node;
4-
import org.jgrapht.io.ComponentNameProvider;
5-
import org.jgrapht.io.DOTExporter;
6-
import org.jgrapht.io.ExportException;
7-
import org.jgrapht.io.GraphExporter;
8-
import tfm.arcs.Arc;
94
import tfm.graphs.Graph;
10-
import tfm.nodes.GraphNode;
115
import tfm.utils.FileUtil;
126
import tfm.utils.Logger;
137

14-
import java.io.File;
15-
import java.io.FileWriter;
16-
import java.io.IOException;
17-
import java.io.Writer;
8+
import java.io.*;
189

1910
public abstract class GraphLog<G extends Graph> {
2011
public enum Format {
@@ -64,8 +55,11 @@ public void log() throws IOException {
6455
"* GRAPHVIZ *\n" +
6556
"****************************"
6657
);
67-
Logger.log(graph.toGraphvizRepresentation());
68-
Logger.log();
58+
try (StringWriter stringWriter = new StringWriter()) {
59+
graph.getDOTExporter().exportGraph(graph, stringWriter);
60+
stringWriter.append('\n');
61+
Logger.log(stringWriter.toString());
62+
}
6963
}
7064

7165
public void generateImages() throws IOException {
@@ -81,29 +75,21 @@ public void generateImages(String imageName, Format format) throws IOException {
8175
this.format = format;
8276
generated = true;
8377
File tmpDot = File.createTempFile("graph-source-", ".dot");
84-
tmpDot.deleteOnExit();
8578

79+
// Graph -> DOT -> file
8680
try (Writer w = new FileWriter(tmpDot)) {
87-
// w.write(graph.toGraphvizRepresentation());
88-
89-
// JGraphT DOT export
90-
GraphExporter<GraphNode<?>, Arc> exporter = new DOTExporter<>(
91-
component -> String.valueOf(component.getId()),
92-
GraphNode::getInstruction,
93-
component -> component.getVariable().orElse(""));
94-
95-
exporter.exportGraph(graph, w);
96-
} catch (ExportException e) {
97-
e.printStackTrace();
81+
graph.getDOTExporter().exportGraph(graph, w);
9882
}
83+
// Execute dot
9984
ProcessBuilder pb = new ProcessBuilder("dot",
10085
tmpDot.getAbsolutePath(), "-T" + format.getExt(),
10186
"-o", getImageFile().getAbsolutePath());
10287
try {
10388
int result = pb.start().waitFor();
104-
if (result != 0) {
105-
Logger.log("Image generation failed");
106-
}
89+
if (result == 0)
90+
tmpDot.deleteOnExit();
91+
else
92+
Logger.log("Image generation failed, try running \"" + pb.toString() + "\" on your terminal.");
10793
} catch (InterruptedException e) {
10894
Logger.log("Image generation failed\n" + e.getMessage());
10995
}

src/main/java/tfm/exec/Main.java

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public static void main(String[] args) throws IOException {
4545

4646
long tt = tf - t0;
4747

48-
graphLog.graph.modifyNode(12, node -> {
49-
node.setInstruction("hahhdh");
50-
});
51-
5248
graphLog.log();
5349
graphLog.openVisualRepresentation();
5450

src/main/java/tfm/exec/PDGLog.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void log() throws IOException {
4141
super.log();
4242

4343
Logger.log("Nodes with variable info");
44-
Logger.log(graph.getNodes().stream()
44+
Logger.log(graph.vertexSet().stream()
4545
.sorted(Comparator.comparingInt(GraphNode::getId))
4646
.map(node ->
4747
String.format("GraphNode { id: %s, declared: %s, defined: %s, used: %s }",

0 commit comments

Comments
 (0)