Skip to content

Commit 9c2b599

Browse files
committed
kraphviz: DSL funs only available inside graph block
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent 77568ec commit 9c2b599

File tree

3 files changed

+94
-69
lines changed

3 files changed

+94
-69
lines changed

graphviz-kotlin/src/main/kotlin/guru/nidi/graphviz/Kraphviz.kt

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,84 +17,24 @@
1717

1818
package guru.nidi.graphviz
1919

20-
import guru.nidi.graphviz.attribute.*
21-
import guru.nidi.graphviz.attribute.Attributes.attr
2220
import guru.nidi.graphviz.engine.Graphviz
2321
import guru.nidi.graphviz.model.*
2422
import guru.nidi.graphviz.model.Factory.*
2523

26-
fun graph(name: String = "", strict: Boolean = false, directed: Boolean = false, cluster: Boolean = false, config: () -> Unit = { }) =
24+
fun graph(name: String = "", strict: Boolean = false, directed: Boolean = false, cluster: Boolean = false,
25+
config: KraphvizContext.() -> Unit = { }): MutableGraph =
2726
mutGraph(name).apply {
2827
isStrict = strict
2928
isDirected = directed
3029
isCluster = cluster
31-
use { _, _ ->
32-
config()
30+
use { _, ctx ->
31+
config(KraphvizContext(ctx))
3332
}
3433
}
3534

36-
infix fun String.eq(value: Any): Attributes<ForAll> = attr(this, value)
37-
38-
interface AttributeContainer<F : For> {
39-
operator fun get(vararg attrs: Attributes<out F>)
40-
}
41-
42-
val edge = object : AttributeContainer<ForLink> {
43-
override fun get(vararg attrs: Attributes<out ForLink>) {
44-
val linkAttrs = CreationContext.get().linkAttrs()
45-
attrs.forEach { linkAttrs.add(it) }
46-
}
47-
}
48-
49-
val node = object : AttributeContainer<ForNode> {
50-
override fun get(vararg attrs: Attributes<out ForNode>) {
51-
val nodeAttrs = CreationContext.get().nodeAttrs()
52-
attrs.forEach { nodeAttrs.add(it) }
53-
}
54-
}
55-
56-
val graph = object : AttributeContainer<ForGraph> {
57-
override fun get(vararg attrs: Attributes<out ForGraph>) {
58-
val graphAttrs = CreationContext.get().graphAttrs()
59-
attrs.forEach { graphAttrs.add(it) }
60-
}
61-
}
62-
63-
operator fun MutableNode.minus(target: LinkTarget) = addLink(target).links().last()!!
64-
operator fun MutableNode.minus(node: String) = this - mutNode(node)
65-
operator fun MutableNode.div(record: String) = port(record)
66-
operator fun MutableNode.div(compass: Compass) = port(compass)
67-
operator fun MutableNode.get(vararg attrs: Attributes<out ForNode>) = add(*attrs)
68-
69-
operator fun PortNode.minus(target: LinkTarget) = links().run {
70-
add(between(port(), target))
71-
last()!!
72-
}
73-
74-
operator fun PortNode.minus(node: String) = this - mutNode(node)
75-
operator fun PortNode.div(record: String) = port(record)
76-
operator fun PortNode.div(compass: Compass) = port(compass)
77-
operator fun PortNode.get(vararg attrs: Attributes<out ForNode>): PortNode {
78-
(node() as MutableNode).add(*attrs)
79-
return this
80-
}
81-
82-
operator fun Link.minus(target: LinkTarget): Link {
83-
val source = to().asLinkSource()
84-
return source.links().run {
85-
add(source.linkTo(target))
86-
last()!!
87-
}
88-
}
89-
90-
operator fun Link.minus(node: String) = this - mutNode(node)
91-
operator fun Link.get(vararg attrs: Attributes<out ForLink>) = add(Attributes.attrs(*attrs))
92-
93-
operator fun String.unaryMinus() = mutNode(this, true)
94-
operator fun String.minus(target: LinkTarget) = -this - target
95-
operator fun String.minus(node: String) = -this - node
96-
operator fun String.div(record: String) = -this / record
97-
operator fun String.div(compass: Compass) = -this / compass
98-
operator fun String.get(vararg attrs: Attributes<out ForNode>) = (-this).add(*attrs)
35+
operator fun MutableGraph.invoke(config: KraphvizContext.() -> Unit): MutableGraph =
36+
reuse { _, ctx ->
37+
config(KraphvizContext(ctx))
38+
}
9939

100-
fun MutableGraph.toGraphviz() = Graphviz.fromGraph(this)
40+
fun MutableGraph.toGraphviz(): Graphviz = Graphviz.fromGraph(this)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright © 2015 Stefan Niederhauser ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package guru.nidi.graphviz
17+
18+
import guru.nidi.graphviz.attribute.*
19+
import guru.nidi.graphviz.model.*
20+
21+
class KraphvizContext(private val ctx: CreationContext) {
22+
interface AttributeContainer<F : For> {
23+
operator fun get(vararg attrs: Attributes<out F>)
24+
}
25+
26+
val edge = object : AttributeContainer<ForLink> {
27+
override fun get(vararg attrs: Attributes<out ForLink>) {
28+
attrs.forEach { ctx.linkAttrs().add(it) }
29+
}
30+
}
31+
32+
val node = object : AttributeContainer<ForNode> {
33+
override fun get(vararg attrs: Attributes<out ForNode>) {
34+
attrs.forEach { ctx.nodeAttrs().add(it) }
35+
}
36+
}
37+
38+
val graph = object : AttributeContainer<ForGraph> {
39+
override fun get(vararg attrs: Attributes<out ForGraph>) {
40+
attrs.forEach { ctx.graphAttrs().add(it) }
41+
}
42+
}
43+
44+
infix fun String.eq(value: Any): Attributes<ForAll> = Attributes.attr(this, value)
45+
46+
operator fun MutableNode.minus(target: LinkTarget): Link = addLink(target).links().last()!!
47+
operator fun MutableNode.minus(node: String): Link = this - Factory.mutNode(node)
48+
operator fun MutableNode.div(record: String): PortNode = port(record)
49+
operator fun MutableNode.div(compass: Compass): PortNode = port(compass)
50+
operator fun MutableNode.get(vararg attrs: Attributes<out ForNode>): MutableNode = add(*attrs)
51+
52+
operator fun PortNode.minus(target: LinkTarget): Link = links().run {
53+
add(Factory.between(port(), target))
54+
last()!!
55+
}
56+
57+
operator fun PortNode.minus(node: String): Link = this - Factory.mutNode(node)
58+
operator fun PortNode.div(record: String): PortNode = port(record)
59+
operator fun PortNode.div(compass: Compass): PortNode = port(compass)
60+
operator fun PortNode.get(vararg attrs: Attributes<out ForNode>): PortNode {
61+
(node() as MutableNode).add(*attrs)
62+
return this
63+
}
64+
65+
operator fun Link.minus(target: LinkTarget): Link {
66+
val source = to().asLinkSource()
67+
return source.links().run {
68+
add(source.linkTo(target))
69+
last()!!
70+
}
71+
}
72+
73+
operator fun Link.minus(node: String): Link = this - Factory.mutNode(node)
74+
operator fun Link.get(vararg attrs: Attributes<out ForLink>): Link = add(Attributes.attrs(*attrs))
75+
76+
operator fun String.unaryMinus(): MutableNode = Factory.mutNode(this, true)
77+
operator fun String.minus(target: LinkTarget): Link = -this - target
78+
operator fun String.minus(node: String): Link = -this - node
79+
operator fun String.div(record: String): PortNode = -this / record
80+
operator fun String.div(compass: Compass): PortNode = -this / compass
81+
operator fun String.get(vararg attrs: Attributes<out ForNode>): MutableNode = (-this).add(*attrs)
82+
}

graphviz-kotlin/src/test/kotlin/guru/nidi/graphviz/KraphvizTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ class KraphvizTest {
3434
edge[Arrow.TEE]
3535
node[GREEN]
3636
graph[GREY.background()]
37+
}
3738

39+
g {
3840
"e" - "f"
3941

4042
("a"[RED] - "b")[Arrow.VEE]
4143
("c" / "rec" / SOUTH)[BLUE] - "d" / WEST
4244
}
45+
4346
val h = mutGraph().setDirected(true)
4447
.graphAttrs().add(GREY.background())
4548
.add(node("e").with(GREEN).link(Link.to(node("f").with(GREEN)).with(RED, Arrow.TEE)))

0 commit comments

Comments
 (0)