-
Notifications
You must be signed in to change notification settings - Fork 786
/
Attachable.java
417 lines (384 loc) · 26.7 KB
/
Attachable.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.tinkerpop.gremlin.structure.util;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
import java.util.Iterator;
import java.util.Optional;
import java.util.function.Function;
/**
* An interface that provides methods for detached properties and elements to be re-attached to the {@link Graph}.
* There are two general ways in which they can be attached: {@link Method#get} or {@link Method#create}.
* A {@link Method#get} will find the property/element at the host location and return it.
* A {@link Method#create} will create the property/element at the host location and return it.
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
public interface Attachable<V> {
/**
* Get the raw object trying to be attached.
*
* @return the raw object to attach
*/
public V get();
/**
* Provide a way to attach an {@link Attachable} implementation to a host. Note that the context of the host
* is not defined by way of the attachment method itself that is supplied as an argument. It is up to the
* implementer to supply that context.
*
* @param method a {@link Function} that takes an {@link Attachable} and returns the "re-attached" object
* @return the return value of the {@code method}
* @throws IllegalStateException if the {@link Attachable} is not a "graph" object (i.e. host or
* attachable don't work together)
*/
public default V attach(final Function<Attachable<V>, V> method) throws IllegalStateException {
return method.apply(this);
}
/**
* A collection of general methods of attachment. Note that more efficient methods of attachment might exist
* if the user knows the source data being attached and the features of the graph that the data is being
* attached to.
*/
public static class Method {
public static <V> Function<Attachable<V>, V> get(final Host hostVertexOrGraph) {
return hostVertexOrGraph instanceof EmptyGraph ? Attachable::get : (Attachable<V> attachable) -> {
final Object base = attachable.get();
if (base instanceof Vertex) {
final Optional<Vertex> optional = hostVertexOrGraph instanceof Graph ?
Method.getVertex((Attachable<Vertex>) attachable, (Graph) hostVertexOrGraph) :
Method.getVertex((Attachable<Vertex>) attachable, (Vertex) hostVertexOrGraph);
return (V) optional.orElseThrow(() -> hostVertexOrGraph instanceof Graph ?
Attachable.Exceptions.canNotGetAttachableFromHostGraph(attachable, (Graph) hostVertexOrGraph) :
Attachable.Exceptions.canNotGetAttachableFromHostVertex(attachable, (Vertex) hostVertexOrGraph));
} else if (base instanceof Edge) {
final Optional<Edge> optional = hostVertexOrGraph instanceof Graph ?
Method.getEdge((Attachable<Edge>) attachable, (Graph) hostVertexOrGraph) :
Method.getEdge((Attachable<Edge>) attachable, (Vertex) hostVertexOrGraph);
return (V) optional.orElseThrow(() -> hostVertexOrGraph instanceof Graph ?
Attachable.Exceptions.canNotGetAttachableFromHostGraph(attachable, (Graph) hostVertexOrGraph) :
Attachable.Exceptions.canNotGetAttachableFromHostVertex(attachable, (Vertex) hostVertexOrGraph));
} else if (base instanceof VertexProperty) {
final Optional<VertexProperty> optional = hostVertexOrGraph instanceof Graph ?
Method.getVertexProperty((Attachable<VertexProperty>) attachable, (Graph) hostVertexOrGraph) :
Method.getVertexProperty((Attachable<VertexProperty>) attachable, (Vertex) hostVertexOrGraph);
return (V) optional.orElseThrow(() -> hostVertexOrGraph instanceof Graph ?
Attachable.Exceptions.canNotGetAttachableFromHostGraph(attachable, (Graph) hostVertexOrGraph) :
Attachable.Exceptions.canNotGetAttachableFromHostVertex(attachable, (Vertex) hostVertexOrGraph));
} else if (base instanceof Property) {
final Optional<Property> optional = hostVertexOrGraph instanceof Graph ?
Method.getProperty((Attachable<Property>) attachable, (Graph) hostVertexOrGraph) :
Method.getProperty((Attachable<Property>) attachable, (Vertex) hostVertexOrGraph);
return (V) optional.orElseThrow(() -> hostVertexOrGraph instanceof Graph ?
Attachable.Exceptions.canNotGetAttachableFromHostGraph(attachable, (Graph) hostVertexOrGraph) :
Attachable.Exceptions.canNotGetAttachableFromHostVertex(attachable, (Vertex) hostVertexOrGraph));
} else
throw Attachable.Exceptions.providedAttachableMustContainAGraphObject(attachable);
};
}
public static <V> Function<Attachable<V>, V> getOrCreate(final Host hostVertexOrGraph) {
return (Attachable<V> attachable) -> {
final Object base = attachable.get();
if (base instanceof Vertex) {
return (V) (hostVertexOrGraph instanceof Graph ?
Method.getVertex((Attachable<Vertex>) attachable, (Graph) hostVertexOrGraph) :
Method.getVertex((Attachable<Vertex>) attachable, (Vertex) hostVertexOrGraph))
.orElseGet(() -> hostVertexOrGraph instanceof Graph ?
Method.createVertex((Attachable<Vertex>) attachable, (Graph) hostVertexOrGraph) :
Method.createVertex((Attachable<Vertex>) attachable, (Vertex) hostVertexOrGraph));
} else if (base instanceof Edge) {
return (V) (hostVertexOrGraph instanceof Graph ?
Method.getEdge((Attachable<Edge>) attachable, (Graph) hostVertexOrGraph) :
Method.getEdge((Attachable<Edge>) attachable, (Vertex) hostVertexOrGraph))
.orElseGet(() -> hostVertexOrGraph instanceof Graph ?
Method.createEdge((Attachable<Edge>) attachable, (Graph) hostVertexOrGraph) :
Method.createEdge((Attachable<Edge>) attachable, (Vertex) hostVertexOrGraph));
} else if (base instanceof VertexProperty) {
return (V) (hostVertexOrGraph instanceof Graph ?
Method.getVertexProperty((Attachable<VertexProperty>) attachable, (Graph) hostVertexOrGraph) :
Method.getVertexProperty((Attachable<VertexProperty>) attachable, (Vertex) hostVertexOrGraph))
.orElseGet(() -> hostVertexOrGraph instanceof Graph ?
Method.createVertexProperty((Attachable<VertexProperty>) attachable, (Graph) hostVertexOrGraph) :
Method.createVertexProperty((Attachable<VertexProperty>) attachable, (Vertex) hostVertexOrGraph));
} else if (base instanceof Property) {
return (V) (hostVertexOrGraph instanceof Graph ?
Method.getProperty((Attachable<Property>) attachable, (Graph) hostVertexOrGraph) :
Method.getProperty((Attachable<Property>) attachable, (Vertex) hostVertexOrGraph))
.orElseGet(() -> hostVertexOrGraph instanceof Graph ?
Method.createProperty((Attachable<Property>) attachable, (Graph) hostVertexOrGraph) :
Method.createProperty((Attachable<Property>) attachable, (Vertex) hostVertexOrGraph));
} else
throw Attachable.Exceptions.providedAttachableMustContainAGraphObject(attachable);
};
}
public static <V> Function<Attachable<V>, V> create(final Host hostVertexOrGraph) {
return (Attachable<V> attachable) -> {
final Object base = attachable.get();
if (base instanceof Vertex) {
return hostVertexOrGraph instanceof Graph ?
(V) Method.createVertex((Attachable<Vertex>) attachable, (Graph) hostVertexOrGraph) :
(V) Method.createVertex((Attachable<Vertex>) attachable, (Vertex) hostVertexOrGraph);
} else if (base instanceof Edge) {
return hostVertexOrGraph instanceof Graph ?
(V) Method.createEdge((Attachable<Edge>) attachable, (Graph) hostVertexOrGraph) :
(V) Method.createEdge((Attachable<Edge>) attachable, (Vertex) hostVertexOrGraph);
} else if (base instanceof VertexProperty) {
return hostVertexOrGraph instanceof Graph ?
(V) Method.createVertexProperty((Attachable<VertexProperty>) attachable, (Graph) hostVertexOrGraph) :
(V) Method.createVertexProperty((Attachable<VertexProperty>) attachable, (Vertex) hostVertexOrGraph);
} else if (base instanceof Property) {
return hostVertexOrGraph instanceof Graph ?
(V) Method.createProperty((Attachable<Property>) attachable, (Graph) hostVertexOrGraph) :
(V) Method.createProperty((Attachable<Property>) attachable, (Vertex) hostVertexOrGraph);
} else
throw Attachable.Exceptions.providedAttachableMustContainAGraphObject(attachable);
};
}
///////////////////
///// GET HELPER METHODS
public static Optional<Vertex> getVertex(final Attachable<Vertex> attachableVertex, final Graph hostGraph) {
final Iterator<Vertex> vertexIterator = hostGraph.vertices(attachableVertex.get().id());
return vertexIterator.hasNext() ? Optional.of(vertexIterator.next()) : Optional.empty();
}
public static Optional<Vertex> getVertex(final Attachable<Vertex> attachableVertex, final Vertex hostVertex) {
return ElementHelper.areEqual(attachableVertex.get(), hostVertex) ? Optional.of(hostVertex) : Optional.empty();
}
public static Optional<Edge> getEdge(final Attachable<Edge> attachableEdge, final Graph hostGraph) {
final Iterator<Edge> edgeIterator = hostGraph.edges(attachableEdge.get().id());
return edgeIterator.hasNext() ? Optional.of(edgeIterator.next()) : Optional.empty();
}
public static Optional<Edge> getEdge(final Attachable<Edge> attachableEdge, final Vertex hostVertex) {
final Edge baseEdge = attachableEdge.get();
final Iterator<Edge> edgeIterator = hostVertex.edges(Direction.OUT, attachableEdge.get().label());
while (edgeIterator.hasNext()) {
final Edge edge = edgeIterator.next();
if (ElementHelper.areEqual(edge, baseEdge))
return Optional.of(edge);
}
return Optional.empty();
}
public static Optional<VertexProperty> getVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Graph hostGraph) {
final VertexProperty baseVertexProperty = attachableVertexProperty.get();
final Iterator<Vertex> vertexIterator = hostGraph.vertices(baseVertexProperty.element().id());
if (vertexIterator.hasNext()) {
final Iterator<VertexProperty<Object>> vertexPropertyIterator = vertexIterator.next().properties(baseVertexProperty.key());
while (vertexPropertyIterator.hasNext()) {
final VertexProperty vertexProperty = vertexPropertyIterator.next();
if (ElementHelper.areEqual(vertexProperty, baseVertexProperty))
return Optional.of(vertexProperty);
}
}
return Optional.empty();
}
public static Optional<VertexProperty> getVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Vertex hostVertex) {
final VertexProperty baseVertexProperty = attachableVertexProperty.get();
final Iterator<VertexProperty<Object>> vertexPropertyIterator = hostVertex.properties(baseVertexProperty.key());
while (vertexPropertyIterator.hasNext()) {
final VertexProperty vertexProperty = vertexPropertyIterator.next();
if (ElementHelper.areEqual(vertexProperty, baseVertexProperty))
return Optional.of(vertexProperty);
}
return Optional.empty();
}
public static Optional<Property> getProperty(final Attachable<Property> attachableProperty, final Graph hostGraph) {
final Property baseProperty = attachableProperty.get();
final Element propertyElement = attachableProperty.get().element();
if (propertyElement instanceof Vertex) {
return (Optional) Method.getVertexProperty((Attachable) attachableProperty, hostGraph);
} else if (propertyElement instanceof Edge) {
final Iterator<Edge> edgeIterator = hostGraph.edges(propertyElement.id());
while (edgeIterator.hasNext()) {
final Property property = edgeIterator.next().property(baseProperty.key());
if (property.isPresent() && property.value().equals(baseProperty.value()))
return Optional.of(property);
}
return Optional.empty();
} else { // vertex property
final Iterator<Vertex> vertexIterator = hostGraph.vertices(((VertexProperty) propertyElement).element().id());
if (vertexIterator.hasNext()) {
final Iterator<VertexProperty<Object>> vertexPropertyIterator = vertexIterator.next().properties();
while (vertexPropertyIterator.hasNext()) {
final VertexProperty vertexProperty = vertexPropertyIterator.next();
if (ElementHelper.areEqual(vertexProperty, baseProperty.element())) {
final Property property = vertexProperty.property(baseProperty.key());
if (property.isPresent() && property.value().equals(baseProperty.value()))
return Optional.of(property);
else
return Optional.empty();
}
}
}
return Optional.empty();
}
}
public static Optional<Property> getProperty(final Attachable<Property> attachableProperty, final Vertex hostVertex) {
final Property baseProperty = attachableProperty.get();
final Element propertyElement = attachableProperty.get().element();
if (propertyElement instanceof Vertex) {
return (Optional) Method.getVertexProperty((Attachable) attachableProperty, hostVertex);
} else if (propertyElement instanceof Edge) {
final Iterator<Edge> edgeIterator = hostVertex.edges(Direction.OUT);
while (edgeIterator.hasNext()) {
final Edge edge = edgeIterator.next();
if (ElementHelper.areEqual(edge, propertyElement)) {
final Property property = edge.property(baseProperty.key());
if (ElementHelper.areEqual(baseProperty, property))
return Optional.of(property);
}
}
return Optional.empty();
} else { // vertex property
final Iterator<VertexProperty<Object>> vertexPropertyIterator = hostVertex.properties();
while (vertexPropertyIterator.hasNext()) {
final VertexProperty vertexProperty = vertexPropertyIterator.next();
if (ElementHelper.areEqual(vertexProperty, baseProperty.element())) {
final Property property = vertexProperty.property(baseProperty.key());
if (property.isPresent() && property.value().equals(baseProperty.value()))
return Optional.of(property);
else
return Optional.empty();
}
}
return Optional.empty();
}
}
///// CREATE HELPER METHODS
public static Vertex createVertex(final Attachable<Vertex> attachableVertex, final Graph hostGraph) {
final Vertex baseVertex = attachableVertex.get();
final Vertex vertex = hostGraph.features().vertex().willAllowId(baseVertex.id()) ?
hostGraph.addVertex(T.id, baseVertex.id(), T.label, baseVertex.label()) :
hostGraph.addVertex(T.label, baseVertex.label());
baseVertex.properties().forEachRemaining(vp -> {
final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(vp.id()) ?
vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value(), T.id, vp.id()) :
vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value());
vp.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value()));
});
return vertex;
}
public static Vertex createVertex(final Attachable<Vertex> attachableVertex, final Vertex hostVertex) {
throw new IllegalStateException("It is not possible to create a vertex at a host vertex");
}
public static Edge createEdge(final Attachable<Edge> attachableEdge, final Graph hostGraph) {
final Edge baseEdge = attachableEdge.get();
Iterator<Vertex> vertices = hostGraph.vertices(baseEdge.outVertex().id());
final Vertex outV = vertices.hasNext() ? vertices.next() : hostGraph.features().vertex().willAllowId(baseEdge.outVertex().id()) ? hostGraph.addVertex(T.id, baseEdge.outVertex().id()) : hostGraph.addVertex();
vertices = hostGraph.vertices(baseEdge.inVertex().id());
final Vertex inV = vertices.hasNext() ? vertices.next() : hostGraph.features().vertex().willAllowId(baseEdge.inVertex().id()) ? hostGraph.addVertex(T.id, baseEdge.inVertex().id()) : hostGraph.addVertex();
if (ElementHelper.areEqual(outV, inV)) {
final Iterator<Edge> itty = outV.edges(Direction.OUT, baseEdge.label());
while (itty.hasNext()) {
final Edge e = itty.next();
if (ElementHelper.areEqual(baseEdge, e))
return e;
}
}
final Edge e = hostGraph.features().edge().willAllowId(baseEdge.id()) ? outV.addEdge(baseEdge.label(), inV, T.id, baseEdge.id()) : outV.addEdge(baseEdge.label(), inV);
baseEdge.properties().forEachRemaining(p -> e.property(p.key(), p.value()));
return e;
}
public static Edge createEdge(final Attachable<Edge> attachableEdge, final Vertex hostVertex) {
return Method.createEdge(attachableEdge, hostVertex.graph()); // TODO (make local to vertex)
}
public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Graph hostGraph) {
final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get();
final Iterator<Vertex> vertexIterator = hostGraph.vertices(baseVertexProperty.element().id());
if (vertexIterator.hasNext()) {
final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(baseVertexProperty.id()) ?
vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) :
vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value());
baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value()));
return vertexProperty;
}
throw new IllegalStateException("Could not find vertex to create the attachable vertex property on");
}
public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Vertex hostVertex) {
final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get();
final VertexProperty vertexProperty = hostVertex.graph().features().vertex().properties().willAllowId(baseVertexProperty.id()) ?
hostVertex.property(hostVertex.graph().features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) :
hostVertex.property(hostVertex.graph().features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value());
baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value()));
return vertexProperty;
}
public static Property createProperty(final Attachable<Property> attachableProperty, final Graph hostGraph) {
final Property baseProperty = attachableProperty.get();
final Element baseElement = baseProperty.element();
if (baseElement instanceof Vertex) {
return Method.createVertexProperty((Attachable) attachableProperty, hostGraph);
} else if (baseElement instanceof Edge) {
final Iterator<Edge> edgeIterator = hostGraph.edges(baseElement.id());
if (edgeIterator.hasNext())
return edgeIterator.next().property(baseProperty.key(), baseProperty.value());
throw new IllegalStateException("Could not find edge to create the attachable property on");
} else { // vertex property
final Iterator<Vertex> vertexIterator = hostGraph.vertices(((VertexProperty) baseElement).element().id());
if (vertexIterator.hasNext()) {
final Vertex vertex = vertexIterator.next();
final Iterator<VertexProperty<Object>> vertexPropertyIterator = vertex.properties(((VertexProperty) baseElement).key());
while (vertexPropertyIterator.hasNext()) {
final VertexProperty<Object> vp = vertexPropertyIterator.next();
if (ElementHelper.areEqual(vp, baseElement))
return vp.property(baseProperty.key(), baseProperty.value());
}
}
throw new IllegalStateException("Could not find vertex property to create the attachable property on");
}
}
public static Property createProperty(final Attachable<Property> attachableProperty, final Vertex hostVertex) {
final Property baseProperty = attachableProperty.get();
final Element baseElement = baseProperty.element();
if (baseElement instanceof Vertex) {
return Method.createVertexProperty((Attachable) attachableProperty, hostVertex);
} else if (baseElement instanceof Edge) {
final Iterator<Edge> edgeIterator = hostVertex.edges(Direction.OUT);
if (edgeIterator.hasNext())
return edgeIterator.next().property(baseProperty.key(), baseProperty.value());
throw new IllegalStateException("Could not find edge to create the property on");
} else { // vertex property
final Iterator<VertexProperty<Object>> vertexPropertyIterator = hostVertex.properties(((VertexProperty) baseElement).key());
while (vertexPropertyIterator.hasNext()) {
final VertexProperty<Object> vp = vertexPropertyIterator.next();
if (ElementHelper.areEqual(vp, baseElement))
return vp.property(baseProperty.key(), baseProperty.value());
}
throw new IllegalStateException("Could not find vertex property to create the attachable property on");
}
}
}
public static class Exceptions {
private Exceptions() {
}
public static IllegalStateException canNotGetAttachableFromHostVertex(final Attachable<?> attachable, final Vertex hostVertex) {
return new IllegalStateException("Can not get the attachable from the host vertex: " + attachable + "-/->" + hostVertex);
}
public static IllegalStateException canNotGetAttachableFromHostGraph(final Attachable<?> attachable, final Graph hostGraph) {
return new IllegalStateException("Can not get the attachable from the host vertex: " + attachable + "-/->" + hostGraph);
}
public static IllegalArgumentException providedAttachableMustContainAGraphObject(final Attachable<?> attachable) {
return new IllegalArgumentException("The provided attachable must contain a graph object: " + attachable);
}
}
}