-
Notifications
You must be signed in to change notification settings - Fork 26
/
DelegatingFramedGraph.java
445 lines (383 loc) · 16.5 KB
/
DelegatingFramedGraph.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* Copyright 2004 - 2017 Syncleus, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Part or all of this source file was forked from a third-party project, the details of which are listed below.
*
* Source Project: Totorom
* Source URL: https://github.com/BrynCooke/totorom
* Source License: Apache Public License v2.0
* When: November, 20th 2014
*/
package com.syncleus.ferma;
import java.util.Spliterators;
import java.util.function.Function;
import com.syncleus.ferma.framefactories.FrameFactory;
import com.syncleus.ferma.framefactories.DefaultFrameFactory;
import com.syncleus.ferma.typeresolvers.UntypedTypeResolver;
import com.syncleus.ferma.typeresolvers.TypeResolver;
import com.syncleus.ferma.typeresolvers.PolymorphicTypeResolver;
import com.syncleus.ferma.framefactories.annotation.AnnotationFrameFactory;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Graph;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.stream.StreamSupport;
public class DelegatingFramedGraph<G extends Graph> implements WrappedFramedGraph<G>{
private final TypeResolver defaultResolver;
private final TypeResolver untypedResolver;
private final FrameFactory builder;
private final G delegate;
@Override
public G getBaseGraph() {
return delegate;
}
/**
* Construct a framed graph.
*
* @param delegate
* The graph to wrap.
* @param builder
* The builder that will construct frames.
* @param defaultResolver
* The type defaultResolver that will decide the final frame type.
*/
public DelegatingFramedGraph(final G delegate, final FrameFactory builder, final TypeResolver defaultResolver) {
if( builder == null )
throw new IllegalArgumentException("builder can not be null");
else if( defaultResolver == null )
throw new IllegalArgumentException("defaultResolver can not be null");
else if( delegate == null )
throw new IllegalArgumentException("delegate can not be null");
this.delegate = delegate;
this.defaultResolver = defaultResolver;
this.untypedResolver = new UntypedTypeResolver();
this.builder = builder;
}
/**
* Construct an untyped framed graph without annotation support
*
* @param delegate
* The graph to wrap.
*/
public DelegatingFramedGraph(final G delegate) {
this.delegate = delegate;
this.defaultResolver = new UntypedTypeResolver();
this.untypedResolver = this.defaultResolver;
this.builder = new DefaultFrameFactory();
}
/**
* Construct a framed graph without annotation support.
*
* @param delegate
* The graph to wrap.
* @param defaultResolver
* The type defaultResolver that will decide the final frame type.
*/
public DelegatingFramedGraph(final G delegate, final TypeResolver defaultResolver) {
this(delegate, new DefaultFrameFactory(), defaultResolver);
}
/**
* Construct a framed graph with the specified typeResolution and annotation support
*
* @param delegate
* The graph to wrap.
* @param typeResolution
* True if type resolution is to be automatically handled by default, false causes explicit typing by
* @param annotationsSupported
* True if annotated classes will be supported, false otherwise.
*/
public DelegatingFramedGraph(final G delegate, final boolean typeResolution, final boolean annotationsSupported) {
this.delegate = delegate;
final ReflectionCache reflections = new ReflectionCache();
if (typeResolution) {
this.defaultResolver = new PolymorphicTypeResolver(reflections);
this.untypedResolver = new UntypedTypeResolver();
}
else {
this.defaultResolver = new UntypedTypeResolver();
this.untypedResolver = this.defaultResolver;
}
if (annotationsSupported)
this.builder = new AnnotationFrameFactory(reflections);
else
this.builder = new DefaultFrameFactory();
}
/**
* Construct a framed graph with the specified typeResolution and annotation support
*
* @param delegate
* The graph to wrap.
* @param reflections
* A RefelctionCache used to determine reflection and hierarchy of classes.
* @param typeResolution
* True if type resolution is to be automatically handled by default, false causes explicit typing by
* @param annotationsSupported
* True if annotated classes will be supported, false otherwise.
*/
public DelegatingFramedGraph(final G delegate, final ReflectionCache reflections, final boolean typeResolution, final boolean annotationsSupported) {
this.delegate = delegate;
if( reflections == null )
throw new IllegalArgumentException("reflections can not be null");
if (typeResolution) {
this.defaultResolver = new PolymorphicTypeResolver(reflections);
this.untypedResolver = new UntypedTypeResolver();
}
else {
this.defaultResolver = new UntypedTypeResolver();
this.untypedResolver = this.defaultResolver;
}
if (annotationsSupported)
this.builder = new AnnotationFrameFactory(reflections);
else
this.builder = new DefaultFrameFactory();
}
/**
* Construct a Typed framed graph with the specified type resolution and with annotation support
*
* @param delegate
* The graph to wrap.
* @param types
* The types to be consider for type resolution.
*/
public DelegatingFramedGraph(final G delegate, final Collection<? extends Class<?>> types) {
this(delegate, new ReflectionCache(types), true, true);
}
/**
* Construct a Typed framed graph with the specified type resolution and with annotation support
*
* @param delegate
* The graph to wrap.
* @param modelPackage
* The package scanned for classes to be considered for type resolution.
*/
public DelegatingFramedGraph(final G delegate, final String modelPackage) {
this(delegate, new ReflectionCache(modelPackage), true, true);
}
/**
* Construct an framed graph with the specified type resolution and with annotation support
*
* @param delegate
* The graph to wrap.
* @param typeResolution
* True if type resolution is to be automatically handled by default, false causes explicit typing by
* default.
* @param types
* The types to be consider for type resolution.
*/
public DelegatingFramedGraph(final G delegate, final boolean typeResolution, final Collection<? extends Class<?>> types) {
this.delegate = delegate;
if( types == null )
throw new IllegalArgumentException("types can not be null");
final ReflectionCache reflections = new ReflectionCache(types);
if (typeResolution) {
this.defaultResolver = new PolymorphicTypeResolver(reflections);
this.untypedResolver = new UntypedTypeResolver();
}
else {
this.defaultResolver = new UntypedTypeResolver();
this.untypedResolver = this.defaultResolver;
}
this.builder = new AnnotationFrameFactory(reflections);
}
@Override
public FrameFactory getBuilder() {
return builder;
}
/**
* Close the delegate graph.
*/
@Override
public void close() throws IOException {
try {
this.getBaseGraph().close();
}
catch(final Exception caught) {
if( caught instanceof RuntimeException )
throw (RuntimeException) caught;
else
throw new IOException("Close malfunctioned:", caught);
}
}
@Override
public <T> T frameElement(final Element e, final Class<T> kind ){
if (e == null)
return null;
final Class<? extends T> frameType = (kind == TVertex.class || kind == TEdge.class) ? kind : defaultResolver.resolve(e, kind);
final T frame = builder.create(e, frameType);
((AbstractElementFrame) frame).init(this, e);
return frame;
}
@Override
public <T> T frameNewElement(final Element e, final ClassInitializer<T> initializer) {
final T frame = frameElement(e, initializer.getInitializationType());
defaultResolver.init(e, initializer.getInitializationType());
((AbstractElementFrame) frame).init();
initializer.initalize(frame);
return frame;
}
@Override
public <T> T frameNewElement(final Element e, final Class<T> kind) {
return this.frameNewElement(e, new DefaultClassInitializer<>(kind));
}
@Override
public <T> Iterator<? extends T> frame(final Iterator<? extends Element> pipeline, final Class<T> kind) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(pipeline, 0), false).map(input -> frameElement(input, kind)).iterator();
}
@Override
public <T> T frameElementExplicit(final Element e, final Class<T> kind) {
if (e == null)
return null;
final Class<? extends T> frameType = this.untypedResolver.resolve(e, kind);
final T frame = builder.create(e, frameType);
((AbstractElementFrame) frame).init(this, e);
return frame;
}
@Override
public <T> T frameNewElementExplicit(final Element e, final ClassInitializer<T> initializer) {
final T frame = frameElement(e, initializer.getInitializationType());
this.untypedResolver.init(e, initializer.getInitializationType());
((AbstractElementFrame) frame).init();
initializer.initalize(frame);
return frame;
}
@Override
public <T> T frameNewElementExplicit(final Element e, final Class<T> kind) {
return this.frameNewElementExplicit(e, new DefaultClassInitializer<>(kind));
}
@Override
public <T> Iterator<? extends T> frameExplicit(final Iterator<? extends Element> pipeline, final Class<T> kind) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(pipeline, 0), false).map(input -> frameElementExplicit(input, kind)).iterator();
}
@Override
public <T> T addFramedVertex(final ClassInitializer<T> initializer, final Object... keyValues) {
if( keyValues != null ) {
return frameNewElement(this.getBaseGraph().addVertex(keyValues), initializer);
}
else {
return frameNewElement(this.getBaseGraph().addVertex(), initializer);
}
}
@Override
public <T> T addFramedVertex(final Class<T> kind, final Object... keyValues) {
return this.addFramedVertex(new DefaultClassInitializer<>(kind), keyValues);
}
@Override
public <T> T addFramedVertex(final Class<T> kind) {
return this.addFramedVertex(new DefaultClassInitializer<>(kind));
}
@Override
public <T> T addFramedVertexExplicit(final ClassInitializer<T> initializer) {
return frameNewElementExplicit(this.getBaseGraph().addVertex(), initializer);
}
@Override
public <T> T addFramedVertexExplicit(final Class<T> kind) {
return this.addFramedVertexExplicit(new DefaultClassInitializer<>(kind));
}
@Override
public TVertex addFramedVertex() {
return addFramedVertex(TVertex.DEFAULT_INITIALIZER);
}
@Override
public TVertex addFramedVertexExplicit() {
return addFramedVertexExplicit(TVertex.DEFAULT_INITIALIZER);
}
@Override
public <T> T addFramedEdge(final VertexFrame source, final VertexFrame destination, final String label, final ClassInitializer<T> initializer, final Object... keyValues) {
final Edge baseEdge = source.getElement().addEdge(label, destination.getElement(), keyValues);
return frameNewElement(baseEdge, initializer);
}
@Override
public <T> T addFramedEdge(final VertexFrame source, final VertexFrame destination, final String label, final Class<T> kind) {
return this.addFramedEdge(source, destination, label, new DefaultClassInitializer<>(kind));
}
@Override
public <T> T addFramedEdgeExplicit(final VertexFrame source, final VertexFrame destination, final String label, final ClassInitializer<T> initializer) {
return frameNewElementExplicit(source.getElement().addEdge(label, destination.getElement()), initializer);
}
@Override
public <T> T addFramedEdgeExplicit(final VertexFrame source, final VertexFrame destination, final String label, final Class<T> kind) {
return this.addFramedEdgeExplicit(source, destination, label, new DefaultClassInitializer<>(kind));
}
@Override
public TEdge addFramedEdge(final VertexFrame source, final VertexFrame destination, final String label) {
return addFramedEdge(source, destination, label, TEdge.DEFAULT_INITIALIZER);
}
@Override
public TEdge addFramedEdgeExplicit(final VertexFrame source, final VertexFrame destination, final String label) {
return addFramedEdgeExplicit(source, destination, label, TEdge.DEFAULT_INITIALIZER);
}
@Override
public <T extends Traversable<?, ?>> T traverse(final Function<GraphTraversalSource, GraphTraversal<?, ?>> traverser) {
return (T) new DefaultTraversable(traverser.apply(this.getBaseGraph().traversal()), this);
}
@Override
public GraphTraversalSource getRawTraversal() {
return this.getBaseGraph().traversal();
}
@Override
public <T> T getFramedVertex(final Class<T> kind, final Object id) {
return this.traverse(input -> input.V(id)).next(kind);
}
@Override
public <T> T getFramedVertexExplicit(final Class<T> kind, final Object id) {
return this.traverse(input -> input.V(id)).nextExplicit(kind);
}
@Override
public <T> Iterator<? extends T> getFramedVertices(final Class<T> kind) {
final Traversable<?, T> result = this.traverse(input -> input.V());
return result.frame(kind);
}
@Override
public <T> Iterator<? extends T> getFramedVertices(final String key, final Object value, final Class<T> kind) {
return this.traverse(input -> input.V().has(key, value)).frame(kind);
}
@Override
public <T> Iterator<? extends T> getFramedVerticesExplicit(final Class<T> kind) {
return this.traverse(input -> input.V()).frameExplicit(kind);
}
@Override
public <T> Iterator<? extends T> getFramedVerticesExplicit(final String key, final Object value, final Class<T> kind) {
return this.traverse(input -> input.V().has(key, value)).frameExplicit(kind);
}
@Override
public <T> Iterator<? extends T> getFramedEdges(final Class<T> kind) {
return this.traverse(input -> input.E()).frame(kind);
}
@Override
public <T> Iterator<? extends T> getFramedEdges(final String key, final Object value, final Class<T> kind) {
return this.traverse(input -> input.E().has(key, value)).frame(kind);
}
@Override
public <T> Iterator<? extends T> getFramedEdgesExplicit(final Class<T> kind) {
return this.traverse(input -> input.E()).frameExplicit(kind);
}
@Override
public <T> Iterator<? extends T> getFramedEdgesExplicit(final String key, final Object value, final Class<T> kind) {
return this.traverse(input -> input.E().has(key, value)).frameExplicit(kind);
}
@Override
public TypeResolver getTypeResolver() {
return this.defaultResolver;
}
@Override
public WrappedTransaction tx() {
return new DelegatingTransaction(this.getBaseGraph().tx(), this);
}
}