-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathExportSpec.scala
294 lines (266 loc) · 10 KB
/
ExportSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package scalax.collection
package io.dot
import scala.collection.SortedMap
import scalax.collection.OuterImplicits._
import scalax.collection.edges.{DiEdge, DiEdgeImplicits, UnDiEdge}
import scalax.collection.edges.labeled.LDiEdge
import scalax.collection.hyperedges._
import scalax.collection.immutable.{Graph, TypedGraphFactory}
import scalax.collection.io.dot.Indent._
import org.scalatest.matchers.should.Matchers
import org.scalatest.refspec.RefSpec
class ExportSpec extends RefSpec with Matchers {
import ExportSpec.Lines
def `Example at http://en.wikipedia.org/wiki/DOT_language is produced`(): Unit = {
import ExportSpec.wikipedia._
import ExportSpec.wikipedia.ExampleGraph.OuterImplicits._
val g = ExampleGraph(
"A1" ~> "A2" :+ "f",
"A2" ~> "A3" :+ "g",
"A1" ~> "B1" :+ "",
"A1" ~> "B1" :+ "",
"A2" ~> "B2" :+ "(g o f)'",
"A3" ~> "B3" :+ "",
"B1" ~> "B3" :+ "",
"B2" ~> "B3" :+ "g'"
)
val root = DotRootGraph(directed = true, id = Some(Id("Wikipedia_Example")))
val subA = DotSubGraph(ancestor = root, subgraphId = Id("A"), attrList = List(DotAttr(Id("rank"), Id("same"))))
val subB = DotSubGraph(ancestor = root, subgraphId = Id("B"), attrList = List(DotAttr(Id("rank"), Id("same"))))
def edgeTransformer(innerEdge: ExampleGraph#EdgeT): Option[(DotGraph, DotEdgeStmt)] = {
val edge = innerEdge.outer
val label = edge.label
Some(
root,
DotEdgeStmt(
NodeId(edge.source),
NodeId(edge.target),
if (label.nonEmpty) List(DotAttr(Id("label"), Id(label)))
else Nil
)
)
}
def nodeTransformer(innerNode: ExampleGraph#NodeT): Option[(DotGraph, DotNodeStmt)] =
Some(
(if (innerNode.outer.head == 'A') subA else subB, DotNodeStmt(NodeId(innerNode.toString), Seq.empty[DotAttr]))
)
val dot = g.toDot(
dotRoot = root,
edgeTransformer = edgeTransformer,
cNodeTransformer = Some(nodeTransformer),
spacing = multilineCompatibleSpacing
)
val (expected_1, expected_2) = {
val expected_header_sorted =
"""digraph Wikipedia_Example {
| A1 -> A2 [label = f]
| A1 -> B1
| A2 -> A3 [label = g]
| A2 -> B2 [label = "(g o f)'"]
| A3 -> B3
| B1 -> B3
| B2 -> B3 [label = "g'"]""".stripMargin
val expected_footer = """
|}""".stripMargin
val expected_sub_A_sorted = """
| subgraph A {
| A1
| A2
| A3
| rank = same
| }""".stripMargin
val expected_sub_B_sorted = """
| subgraph B {
| B1
| B2
| B3
| rank = same
| }""".stripMargin
(
expected_header_sorted + expected_sub_A_sorted + expected_sub_B_sorted + expected_footer,
expected_header_sorted + expected_sub_B_sorted + expected_sub_A_sorted + expected_footer
)
}
val dot_sorted = {
var group = 1
val groups = {
val unsortedMap = dot.linesWithSeparators.toList.groupBy { line =>
group match {
case 1 | 2 => if (line.contains("subgraph")) group += 1
case 3 => if (line.head == '}') group += 1
case 4 => line should have length 0
}
group
}
SortedMap(unsortedMap.toList: _*)
}
import scala.math.Ordering.fromLessThan
val groups_sorted =
for (group <- groups.valuesIterator)
yield group.sorted(
fromLessThan { (a: String, b: String) =>
val (iA, iB) = (a.indexWhere(_ != ' '), b.indexWhere(_ != ' '))
if (iA == iB)
if (a.contains("rank")) true
else a < b
else if (a(iA) == '}') false
else iA < iB
}
)
groups_sorted.flatten
}.toList.foldLeft("")((result: String, elem: String) => result + elem)
dot_sorted should (be(expected_1) or
be(expected_2))
}
def `DOT headers are covered even in edge cases`(): Unit =
Graph
.empty[String, UnDiEdge[String]]
.toDot(
dotRoot = DotRootGraph(
directed = false,
id = None,
attrList = List(
DotAttr(Id("attr_1"), Id(""""one"""")),
DotAttr(Id("attr_2"), Id("<two>"))
)
),
edgeTransformer = _ => None,
spacing = multilineCompatibleSpacing
) shouldBe
"""graph {
| attr_1 = "one"
| attr_2 = <two>
|}""".stripMargin
def `Directed hyperedges may be mapped to multiple directed DOT edges`(): Unit = {
val hg = Graph.from(OneOrMore(1) ~~> OneOrMore(2, 3) :: Nil)
val root = DotRootGraph(directed = true, id = None)
val dot = hg.toDot(
dotRoot = root,
edgeTransformer = _ => None,
hEdgeTransformer = Some { h =>
val source = h.outer.sources.head.toString
h.outer.targets.toList map (target => (root, DotEdgeStmt(NodeId(source), NodeId(target.toString))))
},
spacing = multilineCompatibleSpacing
)
Lines(dot) shouldBe Lines(
"""digraph {
| 1 -> 2
| 1 -> 3
|}""".stripMargin
)
}
def `Colons in https://www.graphviz.org/doc/info/shapes.html#record are handled correctly`(): Unit = {
import implicits._, Record._
import ExportSpec.records._
import ExportSpec.records.RecordGraph.OuterImplicits._
def struct(i: Int) = s"struct$i"
val (f0, f1, f2, here) = ("f0", "f1", "f2", "here")
val (n1, n2, n3): (Node, Node, Node) = (
Node(
struct(1),
Horizontal(Field("left", Some(f0)), Horizontal(Field("mid", Some(f1)), Field("right", Some(f2))))
),
Node(struct(2), Horizontal(Field("one", Some(f0)), Field("two", Some(f1)))),
Node(
struct(3),
Horizontal(
Field("hello\nworld"),
Horizontal(
Vertical(
Horizontal(Field("b"), Vertical(Field("c"), Horizontal(Field("d", Some(here)), Field("e")))),
Field("f")
),
Horizontal(Field("g"), Field("h"))
)
)
)
)
val g = RecordGraph(
n1 ~> n2 :+ Ports(f1, f0),
n1 ~> n3 :+ Ports(f2, here)
)
val root = DotRootGraph(
directed = true,
id = Some("structs"),
attrStmts = List(DotAttrStmt(Elem.node, List(DotAttr("shape", "record"))))
)
val dot = g.toDot(
dotRoot = root,
edgeTransformer = _.outer match {
case RecordArrow(source, target, ports) =>
def withPort(n: Node, port: String) = NodeId(n.id, port)
Some(
(root, DotEdgeStmt(withPort(source, ports.port_1), withPort(target, ports.port_2)))
)
},
cNodeTransformer = Some(_.outer match {
case Node(id, label) => Some((root, DotNodeStmt(id, List(DotAttr("label", label.toString)))))
}),
spacing = multilineCompatibleSpacing
)
Lines(dot) shouldBe Lines(
"""digraph structs {
| node [shape = record]
| struct1 [label = "<f0> left | <f1> mid | <f2> right"]
| struct1:f1 -> struct2:f0
| struct1:f2 -> struct3:here
| struct2 [label = "<f0> one | <f1> two"]
| struct3 [label = "hello\nworld | {b | {c | <here> d | e} | f} | g | h"]
|}""".stripMargin
)
}
def `doubly-nested subgraphs #69`(): Unit = {
import scalax.collection.edges.DiEdge
import scalax.collection.io.dot.implicits._
val g = Graph[Int, DiEdge](1)
val root = DotRootGraph(
directed = true,
id = Some("structs")
)
val branchDOT = DotSubGraph(root, "cluster_branch", attrList = List(DotAttr("label", "branch")))
val cSubGraph = DotSubGraph(branchDOT, "cluster_chained", attrList = List(DotAttr("label", "Chained")))
val iSubGraph = DotSubGraph(branchDOT, "cluster_unchained", attrList = List(DotAttr("label", "UnChained")))
val iNode = "inode"
val dot = g.toDot(
dotRoot = root,
edgeTransformer = _ => Some((root, DotEdgeStmt("hi", "guys"))),
cNodeTransformer = Some(_ => Some((cSubGraph, DotNodeStmt("cnode")))),
iNodeTransformer = Some(_ => Some((iSubGraph, DotNodeStmt(iNode))))
)
dot.contains(iNode) shouldBe true
}
private val multilineCompatibleSpacing = Spacing(
indent = TwoSpaces,
graphAttrSeparator = new AttrSeparator("""
|""".stripMargin) {}
)
}
private object ExportSpec {
object wikipedia {
case class LDiEdgeOfStrings(source: String, target: String, label: String) extends LDiEdge[String, String]
implicit class InfixConstructor(val e: DiEdge[String]) extends AnyVal {
def :+(label: String) = LDiEdgeOfStrings(e.source, e.target, label)
}
type ExampleGraph = Graph[String, LDiEdgeOfStrings]
object ExampleGraph extends TypedGraphFactory[String, LDiEdgeOfStrings]
}
object records {
import Record.Ports
case class Node(id: Id, label: Record.RLabel)
case class RecordArrow(source: Node, target: Node, label: Ports) extends LDiEdge[Node, Ports]
implicit class InfixConstructor(val e: DiEdge[Node]) extends AnyVal {
def :+(ports: Ports) = RecordArrow(e.source, e.target, ports)
}
type RecordGraph = Graph[Node, RecordArrow]
object RecordGraph extends TypedGraphFactory[Node, RecordArrow]
}
case class Lines(head: String, mid: Set[String], tail: String)
object Lines {
def apply(dot: String): Lines = dot.linesWithSeparators.toList match {
case head :: second :: tail =>
Lines(head, (second :: tail.init).toSet, tail.last)
case _ => throw new IllegalArgumentException("Dot string of at least three lines expected.")
}
}
}