-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_query.cpp
587 lines (441 loc) · 17.9 KB
/
graph_query.cpp
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "catch2/catch.hpp"
#include "shared.h"
#include "graph/graph.hpp"
#include <string>
#include <iostream>
using namespace ugly;
using namespace Catch::Matchers;
TEST_CASE( "ugly::query() basics", "[ugly::GraphQuery]" )
{
test_help::StrGraph g;
test_help::fillStrGraphWithNorse(g);
SECTION( "ugly::query() returns working query object" )
{
auto q = query(&g);
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 0);
auto r = q.run();
CHECK(r.size() == 0);
}
SECTION( "->addPipe() can add GraphQueryPipeEmpty manually" )
{
auto q = query(&g);
q->getPipeline()->addPipe(std::make_unique<GraphQueryPipeEmpty<decltype(g)>>());
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 1);
auto r = q.run();
CHECK(r.size() == 0);
}
SECTION( ".v() can add GraphQueryStepVertex (AKA `v`) with syntax (single node)" )
{
auto q = query(&g)
.v(findNode(g, "thor"));
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 1);
auto r = q.run();
CHECK(r.size() == 1);
}
SECTION( "run can be called multiple times" )
{
auto q = query(&g)
.v(findNode(g, "thor"));
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 1);
auto r0 = q.run();
CHECK(r0.size() == 1);
auto r1 = q.run();
CHECK(r1.size() == 1);
}
SECTION( ".v() can add GraphQueryStepVertex (AKA `v`) with syntax (multiple nodes)" )
{
auto q = query(&g)
.v(std::vector { findNode(g, "thor"), findNode(g, "odin"), findNode(g, "jord") });
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 1);
auto r = q.run();
CHECK(r.size() == 3);
}
SECTION( ".v() can add GraphQueryStepVertex (AKA `v`) with syntax (repeat nodes)" )
{
auto thor = findNode(g, "thor");
auto q = query(&g)
.v(std::vector { thor, thor, thor, thor, thor });
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 1);
auto r = q.run();
CHECK(r.size() == 5);
}
SECTION( "query can be used as a generator" )
{
auto q = query(&g)
.v(std::vector { findNode(g, "thor"), findNode(g, "odin") });
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 1);
CHECK(q.done() == false);
auto r0 = q.next();
CHECK(q.done() == false);
REQUIRE(r0 != nullptr);
CHECK(r0->data == "thor");
SECTION( "and run does not interrupt it" )
{
auto r = q.run();
CHECK(r.size() == 2);
}
auto r1 = q.next();
CHECK(q.done() == false);
REQUIRE(r1 != nullptr);
CHECK(r1->data == "odin");
auto r2 = q.next();
CHECK(q.done() == true);
CHECK(r2 == nullptr);
SECTION( "and calling next past done does not break it" )
{
auto r3 = q.next();
CHECK(q.done() == true);
CHECK(r3 == nullptr);
}
}
SECTION( "during a generator run, reset must be called to modify" )
{
auto q = query(&g);
q->getPipeline()->addPipe(std::make_unique<GraphQueryPipeEmpty<decltype(g)>>());
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 1);
auto r = q.next();
SECTION( "not doing so causes a recoverable exception" )
{
REQUIRE_THROWS_AS( q->getPipeline()->addPipe(std::make_unique<GraphQueryPipeEmpty<decltype(g)>>()), graph_error );
}
q->reset();
q->getPipeline()->addPipe(std::make_unique<GraphQueryPipeEmpty<decltype(g)>>());
CHECK(q->getGraph() == &g);
CHECK(q->getPipeline()->countPipes() == 2);
r = q.next();
}
}
TEST_CASE( "ugly::query() syntax traversal queries", "[ugly::GraphQuery]" )
{
test_help::StrGraph g;
test_help::fillStrGraphWithNorse(g);
SECTION( ".e() can query for specific edges." )
{
auto q = query(&g)
.v(findNode(g, "thor"))
// same as "out" (mostly)
.e( [&](auto n, auto e) { return edgeIsOutgoing(g, n, e) && e->data == "parents"; } );
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
CHECK(r.size() == 2); // Thor has 2 parents
}
SECTION( ".e() can follow edges in a specific way." )
{
auto q_mom = query(&g)
.v(findNode(g, "thor"))
// same as "out" (mostly)
.e( [&](auto n, auto e) { return edgeIsOutgoing(g, n, e) && e->data == "parents"; },
// mother is in slot 1 (size guard not needed: 2 size edges are guarnteed)
[&](auto n, auto e, auto ne) { return g.indexOfNodeInEdge(ne, e) == 1; });
auto q_dad = query(&g)
.v(findNode(g, "thor"))
// same as "out" (mostly)
.e( [&](auto n, auto e) { return edgeIsOutgoing(g, n, e) && e->data == "parents"; },
// father is in slot 2 (size guard requried)
[&](auto n, auto e, auto ne) { return g.indexOfNodeInEdge(ne, e) == 2; });
CHECK(q_mom->getPipeline()->countPipes() == 2);
CHECK(q_dad->getPipeline()->countPipes() == 2);
auto r_mom = q_mom.run();
auto r_dad = q_dad.run();
REQUIRE(r_mom.size() == 1);
REQUIRE(r_mom[0]->data == "jord"); // Thor's mom
REQUIRE(r_dad.size() == 1);
REQUIRE(r_dad[0]->data == "odin"); // Thor's dad
}
SECTION( ".in() can get nodes of incoming edges (1 arg)." )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.in( [](auto e) { return e->data == "parents"; } );
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
CHECK(r.size() == 3); // Thor has 3 children
}
SECTION( ".out() can get nodes of outgoing edges (1 arg)." )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.out( [](auto e) { return e->data == "parents"; } );
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
CHECK(r.size() == 2); // Thor has 2 parents
}
SECTION( ".filter() can filter nodes (1 arg, contrived)." )
{
auto q = query(&g)
.v(std::vector { findNode(g, "thor"), findNode(g, "odin"), findNode(g, "odr") })
.filter( [](auto n) { return n->data[0] != 'o'; } );
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
REQUIRE(r.size() == 1);
CHECK(r[0]->data == "thor");
}
SECTION( ".filter() can filter gremlins (2 arg, contrived)." )
{
auto q = query(&g)
.v(std::vector { findNode(g, "thor"), findNode(g, "odin"), findNode(g, "odr") })
.filter( [](auto n, auto r) { return r->node()->data[0] != 'o'; } );
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
REQUIRE(r.size() == 1);
CHECK(r[0]->data == "thor");
}
SECTION( ".unique() ensures unique results (contrived)" )
{
auto thor = findNode(g, "thor");
auto q = query(&g)
.v(std::vector { thor, thor, thor, thor, thor })
.unique();
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
REQUIRE(r.size() == 1); // Thor is unique
}
SECTION( ".unique() ensures unique results (large query)" )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.out( [](auto n, auto e) { return e->data == "parents"; } )
.in( [](auto n, auto e) { return e->data == "parents"; } );
CHECK(q->getPipeline()->countPipes() == 3);
auto r0 = q.run();
q->reset();
q = q.unique();
CHECK(q->getPipeline()->countPipes() == 4);
auto r1 = q.run();
REQUIRE(r0.size() > r1.size());
REQUIRE(r1.size() == 4); // Thor has 4 siblings (including himself)
}
SECTION( ".take() ensures limited results (contrived)" )
{
auto thor = findNode(g, "thor");
auto q = query(&g)
.v(std::vector { thor, thor, thor, thor, thor })
.take(3);
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
REQUIRE(r.size() == 3); // We only wanted 3 thors
q = q
.out( [](auto n, auto e) { return e->data == "parents"; } );
CHECK(q->getPipeline()->countPipes() == 3);
r = q.run();
REQUIRE(r.size() == 6); // We only wanted 3 thors * 2 parents
}
}
TEST_CASE( "ugly::query() syntax label queries", "[ugly::GraphQuery]" )
{
test_help::StrGraph g;
test_help::fillStrGraphWithNorse(g);
SECTION( ".as() can label nodes (contrived)" )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.as("me");
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
// Thor is returned
REQUIRE(r.size() == 1);
CHECK(r[0]->data == "thor");
}
SECTION( ".marge() can merge based on labels (large query)" )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.out( [](auto n, auto e) { return e->data == "parents"; } )
.as("parent")
.out( [](auto n, auto e) { return e->data == "parents"; } )
.as("grand-parent")
.merge({ "parent", "grand-parent" })
.unique();
CHECK(q->getPipeline()->countPipes() == 7);
auto r = q.run();
REQUIRE(r.size() == 6); // Thor has 2 + 4 parents-esque
}
SECTION( ".except() can filter based on labels (large query)" )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.as("me")
.out( [](auto n, auto e) { return e->data == "parents"; } )
.in( [](auto n, auto e) { return e->data == "parents"; } )
.unique()
.except("me");
CHECK(q->getPipeline()->countPipes() == 6);
auto r = q.run();
REQUIRE(r.size() == 3); // Thor has 3 siblings
}
SECTION( ".back() can allow backtracking (large query)" )
{
auto q = query(&g)
.v(findNode(g, "fjorgynn"))
.in( [](auto n, auto e) { return e->data == "parents"; } )
.as("me")
.in( [](auto n, auto e) { return e->data == "parents"; } )
.out( [](auto n, auto e) { return e->data == "parents"; } )
.out( [](auto n, auto e) { return e->data == "parents"; } )
.filter( [](auto n) { return n->data == "bestla"; } )
.back("me").unique()
;
auto r = q.run();
REQUIRE(r.size() == 1);
CHECK(r[0]->data == "frigg"); // frigg is the daughter of fjorgynn who had children with one of bestla's sons
}
}
TEST_CASE( "ugly::query() sub-queries", "[ugly::GraphQuery]" )
{
test_help::StrGraph g;
test_help::fillStrGraphWithNorse(g);
SECTION( ".optional() can replace a node with an existing subquery (contrived - empty path)" )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.optional([](auto _) { return _.out( [](auto n, auto e) { return e->data == "creator"; } ); });
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
REQUIRE(r.size() == 1);
CHECK(r[0]->data == "thor"); // thor has no creator (he has parents)
}
SECTION( ".optional() can replace a node with an existing subquery (contrived - with existing path)" )
{
auto q = query(&g)
.v(findNode(g, "thor"))
.optional([](auto _) { return _.out( [](auto n, auto e) { return e->data == "parents"; } ); });
CHECK(q->getPipeline()->countPipes() == 2);
auto r = q.run();
REQUIRE(r.size() == 2); // thor has 2 parents
}
SECTION( ".repeat_breadth() will repeat a subquery until a condition is met, breadth first (contrived - success)" )
{
std::vector<std::string> visits;
auto q = query(&g)
.v(findNode(g, "thor"))
.repeat_breadth(
[](auto _) { return _.out( [](auto e) { return e->data == "parents"; } ); },
[&](auto n) { visits.push_back(n->data); return true; },
[&](auto n, auto r)
{
// TODO simplify this
bool found = false;
g.forEdgesOnNode(n, [&](auto e)
{
if (e->data != "creator") return true;
g.forPropsOnEdge(e, [&](auto p)
{
found = p->data == "licked-into-being";
return !found;
});
return !found;
});
g.forPropsOnNode(n, [&](auto p) { return !(found = p->data == "licked-into-being"); });
return found;
});
CHECK(q->getPipeline()->countPipes() == 2); // copies the pipe expression
auto r = q.run();
REQUIRE(visits.size() == 7);
CHECK( (visits[1] == "odin" || visits[1] == "jord") ); // in breadth the second visit will be another direct parent
REQUIRE(r.size() == 1); // thor is related to someone licked into being
CHECK(r[0]->data == "buri"); // that wierdo is buri
}
SECTION( ".repeat_breadth() will repeat a subquery until a condition is met, breadth first (contrived - failure)" )
{
std::vector<std::string> visits;
auto q = query(&g)
.v(findNode(g, "frigg"))
.repeat_breadth(
[](auto _) { return _.out( [](auto e) { return e->data == "parents"; } ); },
[&](auto n) { visits.push_back(n->data); return true; },
[&](auto n, auto r)
{
// TODO simplify this
bool found = false;
g.forEdgesOnNode(n, [&](auto e)
{
if (e->data != "creator") return true;
g.forPropsOnEdge(e, [&](auto p)
{
found = p->data == "licked-into-being";
return !found;
});
return !found;
});
g.forPropsOnNode(n, [&](auto p) { return !(found = p->data == "licked-into-being"); });
return found;
});
CHECK(q->getPipeline()->countPipes() == 2); // copies the pipe expression
auto r = q.run();
CHECK(visits.size() == 2);
REQUIRE(r.size() == 0); // frigg is not related to someone licked into being
}
SECTION( ".repeat_depth() will repeat a subquery until a condition is met, depth first (contrived - success)" )
{
std::vector<std::string> visits;
auto q = query(&g)
.v(findNode(g, "thor"))
.repeat_depth(
[](auto _) { return _.out( [](auto e) { return e->data == "parents"; } ); },
[&](auto n) { visits.push_back(n->data); return true; },
[&](auto n, auto r)
{
// TODO simplify this
bool found = false;
g.forEdgesOnNode(n, [&](auto e)
{
if (e->data != "creator") return true;
g.forPropsOnEdge(e, [&](auto p)
{
found = p->data == "licked-into-being";
return !found;
});
return !found;
});
g.forPropsOnNode(n, [&](auto p) { return !(found = p->data == "licked-into-being"); });
return found;
});
CHECK(q->getPipeline()->countPipes() == 2); // copies the pipe expression
auto r = q.run();
REQUIRE(visits.size() == 7);
CHECK( (visits[1] != "odin" && visits[1] != "jord") ); // in depth the second visit will not be another direct parent
REQUIRE(r.size() == 1); // thor is related to someone licked into being
CHECK(r[0]->data == "buri"); // that wierdo is buri
}
SECTION( ".repeat_depth() will repeat a subquery until a condition is met, depth first (contrived - failure)" )
{
std::vector<std::string> visits;
auto q = query(&g)
.v(findNode(g, "frigg"))
.repeat_depth(
[](auto _) { return _.out( [](auto e) { return e->data == "parents"; } ); },
[&](auto n) { visits.push_back(n->data); return true; },
[&](auto n, auto r)
{
// TODO simplify this
bool found = false;
g.forEdgesOnNode(n, [&](auto e)
{
if (e->data != "creator") return true;
g.forPropsOnEdge(e, [&](auto p)
{
found = p->data == "licked-into-being";
return !found;
});
return !found;
});
g.forPropsOnNode(n, [&](auto p) { return !(found = p->data == "licked-into-being"); });
return found;
});
CHECK(q->getPipeline()->countPipes() == 2); // copies the pipe expression
auto r = q.run();
CHECK(visits.size() == 2);
REQUIRE(r.size() == 0); // frigg is not related to someone licked into being
}
}