Skip to content

Commit e8b5056

Browse files
committed
Merge branch 'release/0.19.0'
2 parents ff0d433 + 35cae85 commit e8b5056

17 files changed

Lines changed: 162 additions & 33 deletions

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@ If there are any issues contact me moaxcp@gmail.com.
304304

305305
# Releases
306306

307+
## 0.19.0
308+
309+
* [#91](https://github.com/moaxcp/graph-dsl/issues/91) Plugins should be called Type
310+
311+
This change adds names for types of graphs and plugins. Instead of the developers needing to import and use classes
312+
they can simply use a name.
313+
314+
```
315+
type 'directed-graph'
316+
plugin 'edge-map'
317+
```
318+
307319
## 0.18.0
308320

309321
* [#93](https://github.com/moaxcp/graph-dsl/issues/89) NameSpec and ConfigSpec

src/main/groovy/graph/Graph.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package graph
33
import graph.plugin.Plugin
44
import graph.type.EdgeSpec
55
import graph.type.EdgeSpecFactory
6+
import graph.type.Type
67
import graph.type.VertexSpec
78
import graph.type.VertexSpecFactory
89
import graph.type.DefaultVertexFactory
@@ -34,6 +35,7 @@ class Graph {
3435
private Set<? extends Edge> edges = [] as LinkedHashSet<? extends Edge>
3536
private final Set<Class> edgeTraitsSet = [] as LinkedHashSet<Class>
3637
private final Set<? extends Plugin> plugins = [] as LinkedHashSet<? extends Plugin>
38+
private final Set<? extends Type> types = [] as LinkedHashSet<? extends Type>
3739
@PackageScope
3840
EdgeFactory edgeFactory = new UnDirectedEdgeFactory()
3941
@PackageScope
@@ -170,6 +172,10 @@ class Graph {
170172
Collections.unmodifiableSet(plugins)
171173
}
172174

175+
Set<? extends Type> getTypes() {
176+
Collections.unmodifiableSet(types)
177+
}
178+
173179
/**
174180
* Creates and applies a {@link Plugin} to this graph.
175181
* @param pluginClass - the {@link Plugin} to create and apply to this graph.
@@ -186,6 +192,30 @@ class Graph {
186192
plugin.apply(this)
187193
}
188194

195+
void apply(String pluginName) {
196+
Properties properties = new Properties()
197+
properties.load(getClass().getResourceAsStream("/META-INF/graph-plugins/${pluginName}.properties"))
198+
apply(Class.forName((String) properties.'implementation-class'))
199+
}
200+
201+
void type(Class typeClass) {
202+
if (types.contains(typeClass)) {
203+
throw new IllegalArgumentException("$typeClass.name is already applied.")
204+
}
205+
if (!typeClass.interfaces.contains(Type)) {
206+
throw new IllegalArgumentException("$typeClass.name does not implement Plugin")
207+
}
208+
types << typeClass
209+
Type type = typeClass.newInstance()
210+
type.apply(this)
211+
}
212+
213+
void type(String typeName) {
214+
Properties properties = new Properties()
215+
properties.load(getClass().getResourceAsStream("/META-INF/graph-types/${typeName}.properties"))
216+
type(Class.forName((String) properties.'implementation-class'))
217+
}
218+
189219
/**
190220
* Applies trait to all vertices and all future vertices.
191221
* @param traits to add to vertices and all future vertices
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package graph.type
2+
3+
import graph.Graph
4+
5+
interface Type {
6+
void apply(Graph graph)
7+
}

src/main/groovy/graph/type/directed/DirectedGraphPlugin.groovy renamed to src/main/groovy/graph/type/directed/DirectedGraphType.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package graph.type.directed
33
import graph.Edge
44
import graph.Graph
55
import graph.plugin.Plugin
6+
import graph.type.Type
67

78
/**
89
* This plugin changes the behavior of a {@link Graph} to that of a directed graph.
@@ -47,7 +48,7 @@ import graph.plugin.Plugin
4748
* <dd>traverses the graph in reverse-post-order</dd>
4849
* </dl>
4950
*/
50-
class DirectedGraphPlugin implements Plugin {
51+
class DirectedGraphType implements Type {
5152

5253
/**
5354
* Applies the plugin to a {@link graph.Graph}.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=graph.plugin.EdgeMapPlugin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=graph.plugin.EdgeWeightPlugin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=graph.plugin.VertexMapPlugin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=graph.type.directed.DirectedGraphType

src/test/groovy/dsl/ConnectsToConnectsFromSpec.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dsl
22

33
import graph.Graph
4-
import graph.type.directed.DirectedGraphPlugin
4+
import graph.type.directed.DirectedGraphType
55
import spock.lang.Specification
66

77
import static graph.Graph.graph
@@ -11,7 +11,7 @@ class ConnectsToConnectsFromSpec extends Specification {
1111
def 'use connectsFrom with a VertexNameSpec'() {
1212
given:
1313
Graph graph = graph {
14-
apply DirectedGraphPlugin
14+
type DirectedGraphType
1515
vertex A {
1616
connectsFrom B
1717
}
@@ -27,7 +27,7 @@ class ConnectsToConnectsFromSpec extends Specification {
2727
def 'use connectsFrom with two VertexNameSpec'() {
2828
given:
2929
Graph graph = graph {
30-
apply DirectedGraphPlugin
30+
type DirectedGraphType
3131
vertex A {
3232
connectsFrom B, C
3333
}
@@ -79,7 +79,7 @@ class ConnectsToConnectsFromSpec extends Specification {
7979
def 'use connectsFrom with a VertexSpec'() {
8080
given:
8181
Graph graph = graph {
82-
apply DirectedGraphPlugin
82+
type DirectedGraphType
8383
vertex A {
8484
connectsFrom B {}
8585
}
@@ -95,7 +95,7 @@ class ConnectsToConnectsFromSpec extends Specification {
9595
def 'use nested connectsFrom with a VertexSpec'() {
9696
given:
9797
Graph graph = graph {
98-
apply DirectedGraphPlugin
98+
type DirectedGraphType
9999
vertex A {
100100
connectsFrom B {
101101
connectsFrom C
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dsl
2+
3+
import graph.Graph
4+
import graph.plugin.EdgeMapPlugin
5+
import spock.lang.Specification
6+
7+
import static graph.Graph.graph
8+
9+
class PluginMethods extends Specification {
10+
11+
def 'can apply plugin with Class'() {
12+
given:
13+
Graph graph = graph {
14+
apply EdgeMapPlugin
15+
}
16+
17+
expect:
18+
graph.plugins.contains(EdgeMapPlugin)
19+
}
20+
21+
def 'can apply plugin with String'() {
22+
given:
23+
Graph graph = graph {
24+
apply 'edge-map'
25+
}
26+
27+
expect:
28+
graph.plugins.contains(EdgeMapPlugin)
29+
}
30+
}

0 commit comments

Comments
 (0)