/
GraphLoader.groovy
140 lines (116 loc) · 5.33 KB
/
GraphLoader.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.tinkerpop.gremlin.groovy.loaders
import com.tinkerpop.blueprints.pgm.Edge
import com.tinkerpop.blueprints.pgm.Graph
import com.tinkerpop.blueprints.pgm.Vertex
import com.tinkerpop.blueprints.pgm.util.io.gml.GMLReader
import com.tinkerpop.blueprints.pgm.util.io.gml.GMLWriter
import com.tinkerpop.blueprints.pgm.util.io.graphml.GraphMLReader
import com.tinkerpop.blueprints.pgm.util.io.graphml.GraphMLWriter
import com.tinkerpop.blueprints.pgm.util.io.graphson.GraphSONReader
import com.tinkerpop.blueprints.pgm.util.io.graphson.GraphSONWriter
import com.tinkerpop.gremlin.groovy.Gremlin
import com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline
import java.util.Map.Entry
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
class GraphLoader {
public static void load() {
Graph.metaClass.propertyMissing = {final String name ->
if (Gremlin.isStep(name)) {
return new GremlinGroovyPipeline(delegate)."$name"();
} else {
throw new MissingPropertyException(name, delegate.getClass());
}
}
Graph.metaClass.methodMissing = {final String name, final def args ->
if (Gremlin.isStep(name)) {
return new GremlinGroovyPipeline(delegate)."$name"(* args);
} else {
throw new MissingMethodException(name, delegate.getClass());
}
}
Graph.metaClass.v = {final Object... ids ->
if (ids.length == 1)
return ((Graph) delegate).getVertex(ids[0]);
else {
final Graph g = (Graph) delegate;
final List vertices = new ArrayList();
ids.each {vertices.add(g.getVertex(it))};
return vertices;
}
}
Graph.metaClass.e = {final Object... ids ->
if (ids.length == 1)
return ((Graph) delegate).getEdge(ids[0]);
else {
final Graph g = (Graph) delegate;
final List edges = new ArrayList();
ids.each {edges.add(g.getEdge(it))};
return edges;
}
}
Graph.metaClass.addVertex = {->
return ((Graph) delegate).addVertex(null);
}
Graph.metaClass.addVertex = {final Object id, final Map<String, Object> properties ->
final Vertex vertex = ((Graph) delegate).addVertex(id);
for (final Entry<String, Object> entry: properties.entrySet()) {
vertex.setProperty(entry.getKey(), entry.getValue());
}
return vertex;
}
Graph.metaClass.addVertex = {final Map<String, Object> properties ->
return ((Graph) delegate).addVertex(null, properties);
}
Graph.metaClass.addEdge = {final Object id, final Vertex outVertex, final Vertex inVertex, final String label, final Map<String, Object> properties ->
final Edge edge = ((Graph) delegate).addEdge(id, outVertex, inVertex, label);
for (final Entry<String, Object> entry: properties.entrySet()) {
edge.setProperty(entry.getKey(), entry.getValue());
}
return edge;
}
Graph.metaClass.addEdge = {final Vertex outVertex, final Vertex inVertex, final String label, final Map<String, Object> properties ->
return ((Graph) delegate).addEdge(null, outVertex, inVertex, label, properties);
}
Graph.metaClass.addEdge = {final Vertex outVertex, final Vertex inVertex, final String label ->
return ((Graph) delegate).addEdge(null, outVertex, inVertex, label);
}
// GRAPHML
Graph.metaClass.loadGraphML = {final def fileObject ->
try {
GraphMLReader.inputGraph((Graph) delegate, new URL(fileObject).openStream());
} catch (MalformedURLException e) {
GraphMLReader.inputGraph((Graph) delegate, new FileInputStream(fileObject))
}
}
Graph.metaClass.saveGraphML = {final def fileObject ->
GraphMLWriter.outputGraph((Graph) delegate, new FileOutputStream(fileObject))
}
// GRAPHSON
Graph.metaClass.loadGraphSON = {final def fileObject ->
try {
GraphSONReader.inputGraph((Graph) delegate, new URL(fileObject).openStream());
} catch (MalformedURLException e) {
GraphSONReader.inputGraph((Graph) delegate, new FileInputStream(fileObject))
}
}
Graph.metaClass.saveGraphSON = {final def fileObject ->
GraphSONWriter.outputGraph((Graph) delegate, new FileOutputStream(fileObject), true)
}
Graph.metaClass.saveGraphSON = {final def fileObject, final boolean showTypes ->
GraphSONWriter.outputGraph((Graph) delegate, new FileOutputStream(fileObject), showTypes)
}
// GML
Graph.metaClass.loadGML = {final def fileObject ->
try {
GMLReader.inputGraph((Graph) delegate, new URL(fileObject).openStream());
} catch (MalformedURLException e) {
GMLReader.inputGraph((Graph) delegate, new FileInputStream(fileObject))
}
}
Graph.metaClass.saveGML = {final def fileObject ->
GMLWriter.outputGraph((Graph) delegate, new FileOutputStream(fileObject));
}
}
}