Skip to content

Graph Usage

John Mercier edited this page Apr 6, 2017 · 15 revisions

Create a graph

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 {
    ...
}

Create vertices and edges

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(String) and edge(String, String)

vertex 'a'
vertex 'b'
vertex 'c'
edge 'a', 'b'
edge 'a', 'c'

vertex(Closure)

vertex {
    name = 'a'
    connectsTo 'b', 'c'
}

vertex(String, Closure)

vertex ('a') {
    connectsTo 'b', 'c'
}

edge(String, String)

edge 'a', 'b'
edge 'a', 'c'

vertex(String...)

vertex 'a', 'b', 'c'

Traits

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
}

Configuring traits

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
}