-
Notifications
You must be signed in to change notification settings - Fork 6
Graph Usage
John Mercier edited this page Apr 6, 2017
·
15 revisions
Graph is the main object within graph-dsl.
def graph = new Graph()
The graph method is a good entry point to the dsl.
import static graph.Graph.graph
graph {
...
}
It return a graph you can use in the rest of you code.
Graph graph = graph {
...
}
Edges and vertices my be added with the edge() and vertex() methods. These methods are used to configure the edge or vertex using a map, a closure, or both. The methods always return the edge or vertex for further configuration.
vertex 'a'
vertex 'b'
vertex 'c'
edge 'a', 'b'
edge 'a', 'c'
vertex {
name = 'a'
connectsTo 'b', 'c'
}
vertex ('a') {
connectsTo 'b', 'c'
}
edge 'a', 'b'
edge 'a', 'c'
vertex 'a', 'b', 'c'
Vertices and edges can have traits. Traits may add properties and methods. There are a few traits that come with graph-dsl or you can make your own.
vertex('E') {
traits LinkedHashMap
}
edge('C', 'E') {
traits Weight
}
Traits can be configured using a closure.
vertex('E') {
traits LinkedHashMap
config {
label = 'vertex E'
value = 30
}
}
edge('E', 'F') {
traits LinkedHashMap
config {
label = "edge between $one and $two"
weight = 10
}
}
Since the edge and vertex methods return the resulting object traits can also be configured from the object. The <<
operator is overridden to make this easier.
vertex('E', traits:LinkedHashMap) << {
label = 'vertex E'
value = 30
}
edge('E', 'F', traits:LinkedHashMap) << {
label = "edge between $one and $two"
weight = 10
}