Skip to content

Commit b9bdd58

Browse files
committed
Add SVG!NodesOfDirectedMultiGraph.
[Feature]
1 parent 7320cac commit b9bdd58

File tree

8 files changed

+92
-3
lines changed

8 files changed

+92
-3
lines changed

.classpath

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
<classpathentry kind="lib" path="lib/gson-2.8.6.jar"/>
1111
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
1212
<classpathentry kind="lib" path="lib/junit-4.13.jar"/>
13+
<classpathentry kind="lib" path="lib/jungrapht-layout-1.4-SNAPSHOT.jar"/>
14+
<classpathentry kind="lib" path="lib/jgrapht-core-1.5.1.jar"/>
15+
<classpathentry kind="lib" path="lib/slf4j-api-1.7.30.jar"/>
1316
<classpathentry kind="output" path="build"/>
1417
</classpath>

build.xml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</target>
2929

3030
<target name="compile" depends="download" description="compile the java module overwrites">
31-
<javac srcdir="${src}" destdir="${build}/modules" classpath="${tlc}/tla2tools.jar:${lib}/gson-2.8.6.jar"
31+
<javac srcdir="${src}" destdir="${build}/modules" classpath="${tlc}/tla2tools.jar:${lib}/gson-2.8.6.jar:${lib}/jgrapht-core-1.5.1.jar:${lib}/jungrapht-layout-1.4-SNAPSHOT.jar:${lib}/slf4j-api-1.7.30.jar"
3232
source="1.8"
3333
target="1.8"
3434
includeantruntime="false"/>
@@ -52,6 +52,21 @@
5252
<include name="**/*.class"/>
5353
</patternset>
5454
</unzip>
55+
<unzip src="lib/jgrapht-core-1.5.1.jar" dest="${build}/deps">
56+
<patternset>
57+
<include name="**/*.class"/>
58+
</patternset>
59+
</unzip>
60+
<unzip src="lib/jungrapht-layout-1.4-SNAPSHOT.jar" dest="${build}/deps">
61+
<patternset>
62+
<include name="**/*.class"/>
63+
</patternset>
64+
</unzip>
65+
<unzip src="lib/slf4j-api-1.7.30.jar" dest="${build}/deps">
66+
<patternset>
67+
<include name="**/*.class"/>
68+
</patternset>
69+
</unzip>
5570
<jar jarfile="${dist}/CommunityModules-deps-${timestamp}.jar">
5671
<fileset dir="${build}/modules/"
5772
includes="**/*.class"/>
@@ -91,6 +106,9 @@
91106
<classpath>
92107
<pathelement location="${tlc}/tla2tools.jar" />
93108
<pathelement location="${lib}/gson-2.8.6.jar" />
109+
<pathelement location="${lib}/jgrapht-core-1.5.1.jar" />
110+
<pathelement location="${lib}/jungrapht-layout-1.4-SNAPSHOT.jar" />
111+
<pathelement location="${lib}/slf4j-api-1.7.30.jar" />
94112
<!-- The jar that has just been built by the dist target. -->
95113
<pathelement location="${dist}/CommunityModules-${timestamp}.jar" />
96114
</classpath>
@@ -119,7 +137,7 @@
119137
<classpath>
120138
<pathelement location="${tlc}/tla2tools.jar" />
121139
<!-- The jar that has just been built by the dist target. -->
122-
<pathelement location="${dist}/CommunityModules-${timestamp}.jar" />
140+
<pathelement location="${dist}/CommunityModules-${timestamp}-deps.jar" />
123141
</classpath>
124142
</java>
125143
<fail message="ERROR: ShiViz test failed">

lib/jgrapht-core-1.5.1.jar

1.18 MB
Binary file not shown.
554 KB
Binary file not shown.

lib/slf4j-api-1.7.30.jar

40.5 KB
Binary file not shown.

modules/SVG.tla

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,23 @@ NodeOfRingNetwork(cx, cy, r, n, m) ==
155155
(**************************************************************************)
156156
[ x |-> 0, y |-> 0 ]
157157

158+
NodesOfDirectedMultiGraph(nodes, edges, width, height) ==
159+
(**************************************************************************)
160+
(* Example to layout a graph with the given Nodes and Edges: *)
161+
(* *)
162+
(* Nodes == {"v1", "v2", "v3"} \* v3 is not connected *)
163+
(* Edges == {<<"v1", "v2">>, <<"v2", "v1">>} *)
164+
(* Graph == NodesOfDirectedMultiGraph(Nodes, Edges, 23, 42) *)
165+
(* *)
166+
(* RN[ n \in Nodes ] == *)
167+
(* LET c == Graph[n] *)
168+
(* node == Rect(c.x, c.y, 23, 42, <<>>) *)
169+
(* IN Group(<<node>>, <<>>) *)
170+
(* *)
171+
(* For this operator's actual definition, please consult the Java module *)
172+
(* override at: *)
173+
(* modules/tlc2/overrides/SVG.java#directedMultiGraph *)
174+
(**************************************************************************)
175+
CHOOSE f \in [ nodes -> [x: Int, y: Int] ]: TRUE
176+
158177
=============================================================================

modules/tlc2/overrides/SVG.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@
2525
******************************************************************************/
2626
package tlc2.overrides;
2727

28+
import java.util.Map;
29+
import java.util.stream.Collectors;
30+
31+
import org.jgrapht.Graph;
32+
import org.jgrapht.graph.DefaultGraphType;
33+
import org.jgrapht.graph.builder.GraphTypeBuilder;
34+
import org.jgrapht.util.SupplierUtil;
35+
import org.jungrapht.visualization.layout.algorithms.TreeLayoutAlgorithm;
36+
import org.jungrapht.visualization.layout.model.LayoutModel;
37+
import org.jungrapht.visualization.layout.model.Point;
38+
import org.jungrapht.visualization.layout.model.Rectangle;
39+
2840
import tlc2.value.impl.FcnRcdValue;
2941
import tlc2.value.impl.IntValue;
3042
import tlc2.value.impl.RecordValue;
43+
import tlc2.value.impl.SetEnumValue;
3144
import tlc2.value.impl.StringValue;
3245
import tlc2.value.impl.TupleValue;
3346
import tlc2.value.impl.Value;
47+
import tlc2.value.impl.ValueVec;
3448
import util.UniqueString;
3549

3650
public final class SVG {
@@ -135,4 +149,39 @@ public static Value ringNetwork(IntValue cx, IntValue cy, IntValue r, IntValue n
135149
final int y = (int) (cy.val + r.val * Math.sin(angle));
136150
return new RecordValue(new UniqueString[] {UniqueString.of("x"), UniqueString.of("y")}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false);
137151
}
152+
153+
@TLAPlusOperator(identifier = "NodesOfDirectedMultiGraph", module = "SVG", warn = false)
154+
public static Value directedMultiGraph(final SetEnumValue nodes, final SetEnumValue edges, final IntValue width, final IntValue height) throws Exception {
155+
// https://jgrapht.org/guide/UserOverview#graph-structures
156+
final Graph<Value, Integer> graph = GraphTypeBuilder.<Value, Integer>forGraphType(DefaultGraphType.directedMultigraph())
157+
.edgeSupplier(SupplierUtil.createIntegerSupplier()).allowingSelfLoops(false).buildGraph();
158+
159+
ValueVec elems = nodes.elems;
160+
for (int i = 0; i < elems.size(); i++) {
161+
graph.addVertex(elems.elementAt(i));
162+
}
163+
elems = edges.elems;
164+
for (int i = 0; i < elems.size(); i++) {
165+
TupleValue tuple = (TupleValue) elems.elementAt(i);
166+
graph.addEdge(tuple.elems[0], tuple.elems[1]);
167+
}
168+
169+
final LayoutModel<Value> layoutModel = LayoutModel.<Value>builder().size(1000, 1000).graph(graph).build();
170+
TreeLayoutAlgorithm<Value> layoutAlgo = TreeLayoutAlgorithm.<Value>builder()
171+
.vertexBoundsFunction(v -> Rectangle.of(-5, -5, width.val, height.val)).build();
172+
layoutAlgo.visit(layoutModel);
173+
final Map<Value, Point> locations = layoutModel.getLocations();
174+
175+
return new FcnRcdValue(locations.entrySet().stream()
176+
.collect(Collectors.toMap(
177+
entry -> entry.getKey(),
178+
entry -> point2Value(entry.getValue()))));
179+
}
180+
181+
private static Value point2Value(final Point p) {
182+
final int x = ((Double) p.x).intValue();
183+
final int y = ((Double) p.y).intValue();
184+
return new RecordValue(new UniqueString[] { UniqueString.of("x"), UniqueString.of("y") },
185+
new Value[] { IntValue.gen(x), IntValue.gen(y) }, false);
186+
}
138187
}

tests/IOUtilsTests.tla

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ ASSUME PrintT("IOUtilsTests!C!d")
133133
\* Run TLC with some spec depending on CommunityModules and CM on the classpath.
134134
\* Pass an environment variable to the nested spec.
135135
ASSUME(LET ret == IOEnvExec([SOME_NESTED_VAR |-> "SOME_VAL", B |-> "23"],
136-
<<"java", "-cp", "modules:build/modules:tlc/tla2tools.jar", "tlc2.TLC",
136+
<<"java", "-cp", "modules:build/deps:build/modules:tlc/tla2tools.jar", "tlc2.TLC",
137137
"-config", "Counter.cfg", "tests/nested/CounterCM">>)
138138
IN /\ PrintT(ret)
139139
/\ ret.exitValue = 0

0 commit comments

Comments
 (0)