Skip to content

Commit c459d83

Browse files
committed
Remove graph IO features from sdg-core.
1 parent 7bca102 commit c459d83

28 files changed

+158
-247
lines changed

sdg-cli/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@
5858
<artifactId>sdg-core</artifactId>
5959
<version>${parent.version}</version>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.jgrapht</groupId>
63+
<artifactId>jgrapht-io</artifactId>
64+
<version>1.5.0</version>
65+
</dependency>
6166
</dependencies>
6267
</project>

sdg-cli/src/main/java/es/upv/mist/slicing/cli/CFGLog.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package es.upv.mist.slicing.cli;
22

3+
import es.upv.mist.slicing.arcs.Arc;
34
import es.upv.mist.slicing.graphs.cfg.CFG;
45

56
public class CFGLog extends GraphLog<CFG> {
@@ -10,4 +11,12 @@ public CFGLog() {
1011
public CFGLog(CFG graph) {
1112
super(graph);
1213
}
14+
15+
@Override
16+
protected DOTAttributes edgeAttributes(Arc arc) {
17+
DOTAttributes res = super.edgeAttributes(arc);
18+
if (arc.isNonExecutableControlFlowArc())
19+
res.add("style", "dashed");
20+
return res;
21+
}
1322
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package es.upv.mist.slicing.cli;
2+
3+
import org.jgrapht.nio.Attribute;
4+
5+
import java.util.*;
6+
7+
import static org.jgrapht.nio.DefaultAttribute.createAttribute;
8+
9+
public class DOTAttributes {
10+
protected Map<String, Set<String>> map = new HashMap<>();
11+
12+
/** Set the value of a property to the given value. */
13+
public void set(String key, String value) {
14+
Set<String> set = new HashSet<>();
15+
set.add(value);
16+
map.put(key, set);
17+
}
18+
19+
/** Add the given value to the list of values of the given property. */
20+
public void add(String key, String value) {
21+
map.computeIfAbsent(key, k -> new HashSet<>())
22+
.add(value);
23+
}
24+
25+
/** Generate the map of attributes required by DOTExporter. */
26+
public Map<String, Attribute> build() {
27+
Map<String, Attribute> map = new HashMap<>();
28+
for (var entry : this.map.entrySet()) {
29+
Optional<String> string = entry.getValue().stream().reduce((a, b) -> a + "," + b);
30+
string.ifPresent(s -> map.put(entry.getKey(), createAttribute(s)));
31+
}
32+
return map;
33+
}
34+
}

sdg-cli/src/main/java/es/upv/mist/slicing/cli/GraphLog.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void log() throws IOException {
5656
"****************************"
5757
);
5858
try (StringWriter stringWriter = new StringWriter()) {
59-
getDOTExporter(graph).exportGraph(graph, stringWriter);
59+
getDOTExporter().exportGraph(graph, stringWriter);
6060
stringWriter.append('\n');
6161
Logger.log(stringWriter.toString());
6262
}
@@ -80,7 +80,7 @@ public void generateImages(String imageName, String format) throws IOException {
8080

8181
// Graph -> DOT -> file
8282
try (Writer w = new FileWriter(tmpDot)) {
83-
getDOTExporter(graph).exportGraph(graph, w);
83+
getDOTExporter().exportGraph(graph, w);
8484
}
8585
// Execute dot
8686
ProcessBuilder pb = new ProcessBuilder("dot",
@@ -130,7 +130,23 @@ public File getImageFile() {
130130
return new File(outputDir, imageName + "." + format);
131131
}
132132

133-
protected DOTExporter<GraphNode<?>, Arc> getDOTExporter(G graph) {
134-
return graph.getDOTExporter();
133+
protected DOTExporter<GraphNode<?>, Arc> getDOTExporter() {
134+
DOTExporter<GraphNode<?>, Arc> exporter = new DOTExporter<>();
135+
exporter.setVertexIdProvider(node -> String.valueOf(node.getId()));
136+
exporter.setVertexAttributeProvider(v -> vertexAttributes(v).build());
137+
exporter.setEdgeAttributeProvider(v -> edgeAttributes(v).build());
138+
return exporter;
139+
}
140+
141+
protected DOTAttributes vertexAttributes(GraphNode<?> node) {
142+
DOTAttributes res = new DOTAttributes();
143+
res.set("label", node.getLongLabel());
144+
if (node.isImplicitInstruction())
145+
res.add("style", "dashed");
146+
return res;
147+
}
148+
149+
protected DOTAttributes edgeAttributes(Arc arc) {
150+
return new DOTAttributes();
135151
}
136152
}

sdg-cli/src/main/java/es/upv/mist/slicing/cli/PDGLog.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package es.upv.mist.slicing.cli;
22

3+
import es.upv.mist.slicing.arcs.Arc;
4+
import es.upv.mist.slicing.arcs.pdg.ConditionalControlDependencyArc;
5+
import es.upv.mist.slicing.arcs.pdg.FlowDependencyArc;
6+
import es.upv.mist.slicing.arcs.pdg.ObjectFlowDependencyArc;
7+
import es.upv.mist.slicing.arcs.pdg.StructuralArc;
8+
import es.upv.mist.slicing.arcs.sdg.InterproceduralArc;
39
import es.upv.mist.slicing.graphs.pdg.PDG;
410

511
import java.io.IOException;
@@ -33,4 +39,27 @@ public void openVisualRepresentation() throws IOException {
3339
if (cfgLog != null)
3440
cfgLog.openVisualRepresentation();
3541
}
42+
43+
@Override
44+
protected DOTAttributes edgeAttributes(Arc arc) {
45+
return pdgEdgeAttributes(arc);
46+
}
47+
48+
public static DOTAttributes pdgEdgeAttributes(Arc arc) {
49+
DOTAttributes res = new DOTAttributes();
50+
res.set("label", arc.getLabel());
51+
if (arc.isDataDependencyArc()
52+
|| arc instanceof FlowDependencyArc
53+
|| arc instanceof ObjectFlowDependencyArc)
54+
res.set("color", "red");
55+
if (arc instanceof StructuralArc)
56+
res.add("style", "dashed");
57+
if (arc.isObjectFlow() && !(arc instanceof InterproceduralArc))
58+
res.add("style", "dashed");
59+
if (arc instanceof ConditionalControlDependencyArc.CC1)
60+
res.add("color", "orange");
61+
if (arc instanceof ConditionalControlDependencyArc.CC2)
62+
res.add("color", "green");
63+
return res;
64+
}
3665
}

sdg-cli/src/main/java/es/upv/mist/slicing/cli/PHPSlice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void slice() throws ParseException, IOException {
109109
sdg.build(units);
110110

111111
SlicingCriterion sc = graph -> Set.of(graph.findNodeBy(n -> n.getId() == (long) 0).orElseThrow());
112-
Slice slice = new Slice();
112+
Slice slice = new Slice(Set.of());
113113
if (scId != 0) {
114114
// Slice the SDG
115115
sc = new FileLineSlicingCriterion(scFile, scId);
@@ -132,7 +132,7 @@ public void slice() throws ParseException, IOException {
132132
File imageDir = new File(outputDir, "images");
133133
imageDir.mkdir();
134134
// Output the sliced graph to the output directory
135-
SDGLog sdgLog = new SlicedSDGLog(sdg, slice, sc);
135+
SDGLog sdgLog = new SDGLog(sdg, slice);
136136
sdgLog.setDirectory(outputDir);
137137
sdgLog.generateImages("graph", "svg");
138138
for (CFG cfg : sdg.getCFGs()) {
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
11
package es.upv.mist.slicing.cli;
22

3+
import es.upv.mist.slicing.arcs.Arc;
4+
import es.upv.mist.slicing.arcs.sdg.InterproceduralArc;
35
import es.upv.mist.slicing.graphs.sdg.SDG;
6+
import es.upv.mist.slicing.nodes.GraphNode;
7+
import es.upv.mist.slicing.slicing.Slice;
48

59
public class SDGLog extends GraphLog<SDG> {
6-
public SDGLog() {
7-
super();
8-
}
10+
protected final Slice slice;
911

1012
public SDGLog(SDG graph) {
13+
this(graph, null);
14+
}
15+
16+
public SDGLog(SDG graph, Slice slice) {
1117
super(graph);
18+
this.slice = slice;
19+
}
20+
21+
@Override
22+
protected DOTAttributes vertexAttributes(GraphNode<?> node) {
23+
DOTAttributes res = super.vertexAttributes(node);
24+
if (slice != null) {
25+
if (slice.contains(node))
26+
res.add("style", "filled");
27+
if (slice.getCriterion().contains(node)) {
28+
res.add("style", "filled");
29+
res.set("fillcolor", "lightblue");
30+
}
31+
}
32+
return res;
33+
}
34+
35+
@Override
36+
protected DOTAttributes edgeAttributes(Arc arc) {
37+
DOTAttributes res = PDGLog.pdgEdgeAttributes(arc);
38+
if (arc instanceof InterproceduralArc || arc.isSummaryArc())
39+
res.set("penwidth", "3");
40+
if (arc.isObjectFlow() && arc instanceof InterproceduralArc)
41+
res.add("style", "dotted");
42+
if (arc.isInterproceduralInputArc())
43+
res.set("color", "darkgreen");
44+
if (arc.isInterproceduralOutputArc())
45+
res.set("color", "blue");
46+
if (arc.isSummaryArc())
47+
res.set("color", "#a01210");
48+
return res;
1249
}
1350
}

sdg-cli/src/main/java/es/upv/mist/slicing/cli/SlicedSDGLog.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

sdg-core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@
4747
<artifactId>jgrapht-core</artifactId>
4848
<version>1.5.0</version>
4949
</dependency>
50-
<dependency>
51-
<groupId>org.jgrapht</groupId>
52-
<artifactId>jgrapht-io</artifactId>
53-
<version>1.5.0</version>
54-
</dependency>
5550
<dependency>
5651
<groupId>org.junit.jupiter</groupId>
5752
<artifactId>junit-jupiter</artifactId>

sdg-core/src/main/java/es/upv/mist/slicing/arcs/Arc.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
import es.upv.mist.slicing.graphs.pdg.PDG;
1212
import es.upv.mist.slicing.graphs.sdg.SDG;
1313
import es.upv.mist.slicing.nodes.GraphNode;
14-
import es.upv.mist.slicing.utils.Utils;
1514
import org.jgrapht.graph.DefaultEdge;
16-
import org.jgrapht.nio.Attribute;
1715

18-
import java.util.Map;
1916
import java.util.Objects;
2017

2118
/** The root class from which all arcs in the {@link CFG CFG}, {@link PDG PDG}
@@ -141,11 +138,6 @@ public String toString() {
141138
((GraphNode<?>) super.getSource()).getId(), ((GraphNode<?>) super.getTarget()).getId());
142139
}
143140

144-
/** A map of DOT attributes that define the style of this arc. */
145-
public Map<String, Attribute> getDotAttributes() {
146-
return Utils.dotLabel(getLabel());
147-
}
148-
149141
@Override
150142
public boolean equals(Object o) {
151143
if (this == o)

0 commit comments

Comments
 (0)