11# graph-dsl
22
3- A groovy dsl for creating and traversing extensible graphs. The graph can be extended with plugins and traits which
4- allow developers to create a graph with the desired behavior and values for their algorithm. For project build status
5- check the [ wiki] ( https://github.com/moaxcp/graph-dsl/wiki ) .
3+ A groovy dsl for creating and traversing graphs. Graphs can be extended with plugins and traits which
4+ allow developers to create a graph with the desired behavior and values for their algorithm.
5+
6+ [ ![ Build Status] ( https://travis-ci.org/moaxcp/graph-dsl.svg?branch=master )] ( https://travis-ci.org/moaxcp/graph-dsl )
67
78# Usage
89
910``` groovy
1011#!/usr/bin/env groovy
11- @Grab(group='com.github.moaxcp', module='graph-dsl', version='0.15.0 ')
12+ @Grab(group='com.github.moaxcp', module='graph-dsl', version='latest.revision ')
1213```
1314
1415## Creating a graph
@@ -29,43 +30,62 @@ This example of a graph creates two vertices named 'step1' and 'step2' as well a
2930structure is held in a map of named Vertex objects and a set of Edge objects. Each Edge contains the names of the two
3031vertices it connects.
3132
32- There are a few rules the dsl follows as it is being processed:
33+ ## dsl policy
34+
35+ There are a few rules the dsl follows as it is being processed.
3336
34- 1 . At the end of an operation all referenced vertices are created if they don't exist.
37+ 1 . All referenced vertices are created if they don't exist.
35382 . If an edge or vertex already exists it will be reused by and operation.
3639
37- This gives the developer flexibility to create and modify the graph in many different ways.
40+ Future rules:
41+
42+ These rules will address the api leak with Vertex or Edge. When these objects are returned or in scope some properties
43+ may be changed which will break the graph. These rules will address those issues.
44+
45+ 3 . Changing name in Vertex renames the vertex in the graph
46+ 4 . Changing one or two in Edge moves the edge to different vertices. The vertices will be created if they do not exist.
47+ 5 . delegates in Vertex and Edge are read-only
48+
49+ ## extending the graph
50+
51+ Developers can use the dsl to create new algorithms based on graphs like this workflow between step1 and step2.
3852
3953``` groovy
4054def workQueue = new LinkedList()
4155graph {
42- edge (A, B) {
43- traits Mapping, Weight
44- queue = workQueue
56+ apply EdgeWeightPlugin, VertexMapPlugin
57+
58+ edge (step1, step2) {
4559 weight { queue.size() }
4660 }
4761
48- vertex A(traits:Mapping) {
62+ vertex step1 {
4963 action = {
5064 println "processing"
5165 workQueue << "work"
5266 }
5367 }
5468
55- vertex B {
56- traits Mapping
69+ vertex step2 {
5770 action = {
5871 println "done processing ${workQueue.poll()}"
5972 }
6073 }
6174}
6275```
6376
64- In this graph the edge method creates the vertices A and B as well as an edge. It also configures the edge to have a
65- queue and a weight that is the size of the queue. The vertices A and B are also configured to perform an action. A adds
66- work to the queue and B processes the work.
77+ In this script the edge method first creates step1 and step2. Then it creates and edge between the two vertices. It then
78+ configures the edge with the Mapping and Weight traits. Mapping allows the edge to have extra properties like a queue.
79+ Weight affects the behavior of traversals.
80+
81+ Next, the script finds step1 and configures it with the Mapping trait and an action closure which adds "work" to the
82+ workQueue.
83+
84+ Finally, step2 is configured with an action which processes "work".
85+
86+ ## directed graphs
6787
68- The Default behavior of a graph is that of an undirected graph . These graphs have a set of edges where only one edge
88+ The Default behavior of a graph is undirected. These graphs have a set of edges where only one edge
6989can connect any two vertices. An undirected graph can be changed to a directed graph at any time using
7090` DirectedGraphPlugin ` .
7191
@@ -77,6 +97,8 @@ graph {
7797}
7898```
7999
100+ ## traits
101+
80102Traits can be added to all edges and vertices using ` edgeTraits ` and ` vertexTraits ` .
81103
82104``` groovy
@@ -120,12 +142,13 @@ graph {
120142}
121143```
122144
145+ ` depthFirstTraversal ` provides preorder and postorder methods.
146+
123147## Functional search methods
124148
125- There are functional search methods build on the depthFirstTraversal and breadthFirstTraversal method. These methods
149+ There are functional search methods built on the depthFirstTraversal and breadthFirstTraversal method. These methods
126150follow the standard names in groovy: each, find, inject, findAll, collect. The methods can specify which type of
127- search to perform such as ` eachBfs ` or ` eachDfs ` . When a search type is not specified the methods default to depth
128- first search.
151+ search to perform such as ` eachBfs ` or ` eachDfs ` .
129152
130153``` groovy
131154eachBfs {
@@ -169,6 +192,10 @@ forward edges, and cross edges. There is also a new Graph called forest that get
169192` EdgeClassification.forrest ` contains the forrest created by tree edges. It uses the vertex and edge objects
170193from the original graph object.
171194
195+ Calling ` classifyEdges ` on an undirected graph will result in two classifications for each edge. The first classification
196+ is what the edge would be in a directed graph. The second classification is always back-edge. This is because edges in
197+ an undirected graph are considered bi-directional in ` classifyEdges ` .
198+
172199# Plugins
173200
174201Plugins provide graphs with extra functionality. They can:
@@ -277,6 +304,35 @@ If there are any issues contact me moaxcp@gmail.com.
277304
278305# Releases
279306
307+ ## 0.18.0
308+
309+ * [ #93 ] ( https://github.com/moaxcp/graph-dsl/issues/89 ) NameSpec and ConfigSpec
310+ * runnerCode can no longer be set in map parameters to vertex and edge methods
311+
312+ This release includes a major refactor in how graphs are built. Configurations for vertex and edge methods have been
313+ refactored to use NameSpec and ConfigSpec object. A NameSpec is a wrapper for a string. It represents the name of
314+ something. A ConfigSpec is a wrapper around a map and closure. It represents the configuration for something. NameSpec
315+ and ConfigSpec are a common interface for graph types to share. VertexSpec and EdgeSpec are now controlled by the graph
316+ type. Types can add new properties and methods for use in a ConfigSpec. An example of this is connectsFrom in a
317+ directed graph. ` connectsFrom ` can only be used after applying ` DirectedGraphPlugin ` . Graph types can override methods
318+ like connectsTo and connectsFrom to perform checks before adding edges. This will be important when the DAG type is
319+ added.
320+
321+ For the first time packages have been introduced to the project.
322+
323+ <dl >
324+ <dt >plugin</dt >
325+ <dd >these are plugins which add functionality not related to specific types of graphs</dd >
326+ <dt >trait</dt >
327+ <dd >these are traits which can be used on vertices and edges</dd >
328+ <dt >type</dt >
329+ <dd >these are types of graphs and related classes</dd >
330+ </dl >
331+
332+ Factories for EdgeSpec and VertexSpec are now used by Graph. This allows plugins to add new behavior to the edge and
333+ vertex methods. New attributes and methods can be added to the ConfigSpec used in these methods. DirectedGraphPlugin
334+ uses this to add connectsFrom to the map and closure in the ConfigSpec.
335+
280336## 0.17.0
281337
282338* [ #82 ] ( https://github.com/moaxcp/graph-dsl/issues/82 ) Methods that take vertex names should also take VertexNameSpec
0 commit comments