1
1
package com .arangodb .util ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .List ;
4
5
import java .util .Map .Entry ;
5
6
import java .util .Set ;
@@ -22,11 +23,14 @@ public class GraphQueryUtil {
22
23
private static final String OR = " || " ;
23
24
private static final String GRAPH_NAME = "graphName" ;
24
25
private static final String VERTEX_EXAMPLE = "vertexExample" ;
26
+ private static final String START_VERTEX_EXAMPLE = "startVertexExample" ;
27
+ private static final String END_VERTEX_EXAMPLE = "endVertexExample" ;
28
+ private static final String SOURCE = "source" ;
29
+ private static final String TARGET = "target" ;
25
30
26
31
public static String createEdgeQuery (
27
32
final ArangoDriver driver ,
28
33
final String graphName ,
29
- final Class <?> clazz ,
30
34
final Object vertexExample ,
31
35
final GraphEdgesOptions graphEdgesOptions ,
32
36
final MapBuilder bindVars ) throws ArangoException {
@@ -36,50 +40,21 @@ public static String createEdgeQuery(
36
40
sb .append ("FOR v,e IN " );
37
41
appendDepth (graphEdgesOptions , sb );
38
42
appendDirection (graphEdgesOptions .getDirection (), sb );
39
- sb .append (" @" );
40
- sb .append (VERTEX_EXAMPLE );
41
- bindVars .put (VERTEX_EXAMPLE , vertexExample );
43
+ appendBindVar (VERTEX_EXAMPLE , vertexExample , bindVars , sb );
42
44
} else {
43
45
final List <String > startVertexCollectionRestriction = graphEdgesOptions
44
46
.getStartVertexCollectionRestriction ();
45
47
final List <String > vertexCollections = startVertexCollectionRestriction != null
46
48
&& startVertexCollectionRestriction .size () > 0 ? startVertexCollectionRestriction
47
49
: driver .graphGetVertexCollections (graphName , true );
48
- if (vertexCollections .size () == 1 ) {
49
- sb .append ("FOR start IN `" );
50
- sb .append (vertexCollections .get (0 ));
51
- sb .append ("`" );
52
- appendFilter ("start" , vertexExample , sb );
53
- } else {
54
- sb .append ("FOR start IN UNION (" );
55
- for (String vertexCollection : vertexCollections ) {
56
- sb .append ("(FOR start IN `" );
57
- sb .append (vertexCollection );
58
- sb .append ("`" );
59
- appendFilter ("start" , vertexExample , sb );
60
- sb .append (" RETURN start)," );
61
- }
62
- // remove last ,
63
- sb .deleteCharAt (sb .length () - 1 );
64
- sb .append (")" );
65
- }
50
+ appendFor ("start" , vertexExample , sb , vertexCollections );
66
51
sb .append (" FOR v,e IN " );
67
52
appendDepth (graphEdgesOptions , sb );
68
53
appendDirection (graphEdgesOptions .getDirection (), sb );
69
54
sb .append (" start" );
70
55
}
71
- sb .append (" " );
72
56
final List <String > edgeCollectionRestriction = graphEdgesOptions .getEdgeCollectionRestriction ();
73
- if (edgeCollectionRestriction != null && edgeCollectionRestriction .size () > 0 ) {
74
- for (String edgeCollection : edgeCollectionRestriction ) {
75
- sb .append (edgeCollection );
76
- sb .append ("," );
77
- }
78
- // remove last ,
79
- sb .deleteCharAt (sb .length () - 1 );
80
- } else {
81
- appendGraphName (graphName , bindVars , sb );
82
- }
57
+ appendEdgeCollectionsOrGraph (graphName , bindVars , sb , edgeCollectionRestriction );
83
58
appendFilter ("e" , graphEdgesOptions .getEdgeExamples (), sb );
84
59
appendFilter ("v" , graphEdgesOptions .getNeighborExamples (), sb );
85
60
final Integer limit = graphEdgesOptions .getLimit ();
@@ -96,15 +71,72 @@ public static String createEdgeQuery(
96
71
return query ;
97
72
}
98
73
99
- /**
100
- * @param graphName
101
- * @param bindVars
102
- * @param sb
103
- */
74
+ private static void appendEdgeCollectionsOrGraph (
75
+ final String graphName ,
76
+ final MapBuilder bindVars ,
77
+ final StringBuilder sb ,
78
+ final List <String > edgeCollectionRestriction ) {
79
+ sb .append (" " );
80
+ if (edgeCollectionRestriction != null && edgeCollectionRestriction .size () > 0 ) {
81
+ for (String edgeCollection : edgeCollectionRestriction ) {
82
+ sb .append ("`" );
83
+ sb .append (edgeCollection );
84
+ sb .append ("`," );
85
+ }
86
+ // remove last ,
87
+ sb .deleteCharAt (sb .length () - 1 );
88
+ } else {
89
+ appendGraphName (graphName , bindVars , sb );
90
+ }
91
+ }
92
+
93
+ private static void appendBindVar (
94
+ final String param ,
95
+ final Object var ,
96
+ final MapBuilder bindVars ,
97
+ final StringBuilder sb ) {
98
+ sb .append (" @" );
99
+ sb .append (param );
100
+ bindVars .put (param , var );
101
+ }
102
+
103
+ private static void appendFor (
104
+ final String var ,
105
+ final Object vertexExample ,
106
+ final StringBuilder sb ,
107
+ final List <String > vertexCollections ) throws ArangoException {
108
+ if (vertexCollections .size () == 1 ) {
109
+ sb .append ("FOR " );
110
+ sb .append (var );
111
+ sb .append (" IN `" );
112
+ sb .append (vertexCollections .get (0 ));
113
+ sb .append ("`" );
114
+ appendFilter (var , vertexExample , sb );
115
+ } else {
116
+ sb .append ("FOR " );
117
+ sb .append (var );
118
+ sb .append (" IN UNION (" );
119
+ for (String vertexCollection : vertexCollections ) {
120
+ sb .append ("(FOR " );
121
+ sb .append (var );
122
+ sb .append (" IN `" );
123
+ sb .append (vertexCollection );
124
+ sb .append ("`" );
125
+ appendFilter (var , vertexExample , sb );
126
+ sb .append (" RETURN " );
127
+ sb .append (var );
128
+ sb .append (")," );
129
+ }
130
+ // remove last ,
131
+ sb .deleteCharAt (sb .length () - 1 );
132
+ sb .append (")" );
133
+ }
134
+ sb .append (" " );
135
+ }
136
+
104
137
private static void appendGraphName (final String graphName , final MapBuilder bindVars , final StringBuilder sb ) {
105
- sb .append ("GRAPH @" );
106
- sb .append (GRAPH_NAME );
107
- bindVars .put (GRAPH_NAME , graphName );
138
+ sb .append ("GRAPH" );
139
+ appendBindVar (GRAPH_NAME , graphName , bindVars , sb );
108
140
}
109
141
110
142
private static void appendDepth (final GraphEdgesOptions graphEdgesOptions , final StringBuilder sb ) {
@@ -171,7 +203,6 @@ private static void appendObjectinFilter(final String var, final JsonObject json
171
203
public static String createVerticesQuery (
172
204
final ArangoDriver driver ,
173
205
final String graphName ,
174
- final Class <?> clazz ,
175
206
final Object vertexExample ,
176
207
final GraphVerticesOptions graphVerticesOptions ,
177
208
final MapBuilder bindVars ) throws ArangoException {
@@ -182,37 +213,18 @@ public static String createVerticesQuery(
182
213
if (stringVertexExample ) {
183
214
sb .append ("RETURN " );
184
215
sb .append ("DOCUMENT(" );
185
- sb .append ("@" );
186
- sb .append (VERTEX_EXAMPLE );
187
- bindVars .put (VERTEX_EXAMPLE , vertexExample );
216
+ appendBindVar (VERTEX_EXAMPLE , vertexExample , bindVars , sb );
188
217
sb .append (")" );
189
218
} else {
190
219
final List <String > startVertexCollectionRestriction = graphVerticesOptions .getVertexCollectionRestriction ();
191
220
final List <String > vertexCollections = startVertexCollectionRestriction != null
192
221
&& startVertexCollectionRestriction .size () > 0 ? startVertexCollectionRestriction
193
222
: driver .graphGetVertexCollections (graphName , true );
194
- if (vertexCollections .size () == 1 ) {
195
- sb .append ("FOR start IN `" );
196
- sb .append (vertexCollections .get (0 ));
197
- sb .append ("`" );
198
- appendFilter ("start" , vertexExample , sb );
199
- } else {
200
- sb .append ("FOR start IN UNION (" );
201
- for (String vertexCollection : vertexCollections ) {
202
- sb .append ("(FOR start IN `" );
203
- sb .append (vertexCollection );
204
- sb .append ("`" );
205
- appendFilter ("start" , vertexExample , sb );
206
- sb .append (" RETURN start)," );
207
- }
208
- // remove last ,
209
- sb .deleteCharAt (sb .length () - 1 );
210
- sb .append (")" );
211
- }
223
+ appendFor ("start" , vertexExample , sb , vertexCollections );
212
224
sb .append (" RETURN start" );
213
225
}
214
226
215
- String query = sb .toString ();
227
+ final String query = sb .toString ();
216
228
return query ;
217
229
}
218
230
@@ -225,7 +237,87 @@ public static String createShortestPathQuery(
225
237
final ShortestPathOptions shortestPathOptions ,
226
238
final Class <?> vertexClass ,
227
239
final Class <?> edgeClass ,
228
- final MapBuilder bindVars ) {
229
- return null ;
240
+ final MapBuilder bindVars ) throws ArangoException {
241
+ /*
242
+ *
243
+ * final String query =
244
+ * "for i in graph_shortest_path(@graphName, @startVertexExample, @endVertexExample, @options) return i"
245
+ * ; final Map<String, Object> bindVars = mapBuilder.put("graphName",
246
+ * graphName) .put("startVertexExample",
247
+ * startVertexExample).put("endVertexExample", endVertexExample)
248
+ * .put("options", options).get();
249
+ */
250
+ final StringBuilder sb = new StringBuilder ();
251
+ final boolean notStringStartVertexExample = startVertexExample != null
252
+ && !String .class .isAssignableFrom (startVertexExample .getClass ());
253
+ boolean notStringEndVertexExample = endVertexExample != null
254
+ && !String .class .isAssignableFrom (endVertexExample .getClass ());
255
+ if (notStringStartVertexExample || notStringEndVertexExample ) {
256
+ final List <String > startVertexCollectionRestriction = shortestPathOptions
257
+ .getStartVertexCollectionRestriction ();
258
+ final List <String > endVertexCollectionRestriction = shortestPathOptions .getEndVertexCollectionRestriction ();
259
+ final boolean startVertexCollectionRestrictionNotEmpty = startVertexCollectionRestriction != null
260
+ && startVertexCollectionRestriction .size () > 0 ;
261
+ final boolean endVertexCollectionRestrictionNotEmpty = endVertexCollectionRestriction != null
262
+ && endVertexCollectionRestriction .size () > 0 ;
263
+ final List <String > vertexCollections = (!startVertexCollectionRestrictionNotEmpty
264
+ || !endVertexCollectionRestrictionNotEmpty ) ? driver .graphGetVertexCollections (graphName , true )
265
+ : new ArrayList <String >();
266
+
267
+ if (notStringStartVertexExample ) {
268
+ final List <String > tmpStartVertexCollectionRestriction = startVertexCollectionRestrictionNotEmpty
269
+ ? startVertexCollectionRestriction : vertexCollections ;
270
+ appendFor (SOURCE , startVertexExample , sb , tmpStartVertexCollectionRestriction );
271
+ }
272
+ if (notStringEndVertexExample ) {
273
+ final List <String > tmpEndVertexCollectionRestriction = endVertexCollectionRestrictionNotEmpty
274
+ ? endVertexCollectionRestriction : vertexCollections ;
275
+ appendFor (TARGET , endVertexExample , sb , tmpEndVertexCollectionRestriction );
276
+ }
277
+ if (notStringStartVertexExample && notStringEndVertexExample ) {
278
+ sb .append ("FILTER target != source " );
279
+ }
280
+ }
281
+ {// p
282
+ sb .append ("LET p = ( FOR v, e IN " );
283
+ appendDirection (shortestPathOptions .getDirection (), sb );
284
+ sb .append (" SHORTEST_PATH " );
285
+ if (notStringStartVertexExample ) {
286
+ sb .append (SOURCE );
287
+ } else {
288
+ appendBindVar (START_VERTEX_EXAMPLE , startVertexExample , bindVars , sb );
289
+ }
290
+ sb .append (" TO " );
291
+ if (notStringEndVertexExample ) {
292
+ sb .append (TARGET );
293
+ } else {
294
+ appendBindVar (END_VERTEX_EXAMPLE , endVertexExample , bindVars , sb );
295
+ }
296
+ List <String > edgeCollectionRestriction = shortestPathOptions .getEdgeCollectionRestriction ();
297
+ appendEdgeCollectionsOrGraph (graphName , bindVars , sb , edgeCollectionRestriction );
298
+
299
+ final String weight = shortestPathOptions .getWeight ();
300
+ if (weight != null ) {
301
+ sb .append (" OPTIONS {weightAttribute: @attribute, defaultWeight: @default} " );
302
+ sb .append (
303
+ " RETURN { v: v, e: e, d: IS_NULL(e) ? 0 : (IS_NUMBER(e[@attribute]) ? e[@attribute] : @default))}) " );
304
+ bindVars .put ("attribute" , weight );
305
+ final Long defaultWeight = shortestPathOptions .getDefaultWeight ();
306
+ bindVars .put ("default" , defaultWeight != null ? defaultWeight : 1 );
307
+ } else {
308
+ sb .append (" RETURN {v: v, e: e, d: IS_NULL(e) ? 0 : 1}) " );
309
+ }
310
+ }
311
+ sb .append ("FILTER LENGTH(p) > 0 " );
312
+ if (shortestPathOptions .getIncludeData () != null && !shortestPathOptions .getIncludeData ().booleanValue ()) {
313
+ sb .append (
314
+ "RETURN { vertices: p[*].v._id, edges: p[* FILTER CURRENT.e != null].e._id, distance: SUM(p[*].d)}" );
315
+ } else {
316
+ sb .append ("RETURN { vertices: p[*].v, edges: p[* FILTER CURRENT.e != null].e, distance: SUM(p[*].d)}" );
317
+ }
318
+
319
+ final String query = sb .toString ();
320
+ System .out .println (query );
321
+ return query ;
230
322
}
231
323
}
0 commit comments