forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphism_apply_functions.go
496 lines (447 loc) · 15.5 KB
/
morphism_apply_functions.go
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
// Copyright 2014 The Cayley Authors. All rights reserved.
//
// 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.
package path
import (
"fmt"
"regexp"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/quad"
)
// join puts two iterators together by intersecting their result sets with an AND
// Since we're using an and iterator, it's a good idea to put the smallest result
// set first so that Next() produces fewer values to check Contains().
func join(qs graph.QuadStore, its ...graph.Iterator) graph.Iterator {
and := iterator.NewAnd(qs)
for _, it := range its {
if it == nil {
continue
}
and.AddSubIterator(it)
}
return and
}
// isMorphism represents all nodes passed in-- if there are none, this function
// acts as a passthrough for the previous iterator.
func isMorphism(nodes ...quad.Value) morphism {
return morphism{
Name: "is",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return isMorphism(nodes...), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
if len(nodes) == 0 {
// Acting as a passthrough here is equivalent to
// building a NodesAllIterator to Next() or Contains()
// from here as in previous versions.
return in, ctx
}
isNodes := qs.FixedIterator()
for _, n := range nodes {
isNodes.Add(qs.ValueOf(n))
}
// Anything with fixedIterators will usually have a much
// smaller result set, so join isNodes first here.
return join(qs, isNodes, in), ctx
},
}
}
func regexMorphism(pattern *regexp.Regexp, refs bool) morphism {
return morphism{
Name: "regex",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return regexMorphism(pattern, refs), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
it := iterator.NewRegex(in, pattern, qs)
it.AllowRefs(refs)
return it, ctx
},
}
}
// cmpMorphism is the set of nodes that passes comparison iterator with the same parameters.
func cmpMorphism(op iterator.Operator, node quad.Value) morphism {
return morphism{
Name: "cmp",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return cmpMorphism(op, node), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return iterator.NewComparison(in, op, node, qs), ctx
},
}
}
// hasMorphism is the set of nodes that is reachable via either a *Path, a
// single node.(string) or a list of nodes.([]string).
func hasMorphism(via interface{}, nodes ...quad.Value) morphism {
return morphism{
Name: "has",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return hasMorphism(via, nodes...), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return buildHas(qs, via, in, false, nodes), ctx
},
}
}
func hasReverseMorphism(via interface{}, nodes ...quad.Value) morphism {
return morphism{
Name: "hasr",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return hasMorphism(via, nodes...), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return buildHas(qs, via, in, true, nodes), ctx
},
}
}
func tagMorphism(tags ...string) morphism {
return morphism{
Name: "tag",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return tagMorphism(tags...), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
for _, t := range tags {
in.Tagger().Add(t)
}
return in, ctx
},
tags: tags,
}
}
// outMorphism iterates forward one RDF triple or via an entire path.
func outMorphism(tags []string, via ...interface{}) morphism {
return morphism{
Name: "out",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return inMorphism(tags, via...), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
path := buildViaPath(qs, via...)
return inOutIterator(path, in, false, tags, ctx), ctx
},
tags: tags,
}
}
// inMorphism iterates backwards one RDF triple or via an entire path.
func inMorphism(tags []string, via ...interface{}) morphism {
return morphism{
Name: "in",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return outMorphism(tags, via...), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
path := buildViaPath(qs, via...)
return inOutIterator(path, in, true, tags, ctx), ctx
},
tags: tags,
}
}
func bothMorphism(tags []string, via ...interface{}) morphism {
return morphism{
Name: "in",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return bothMorphism(tags, via...), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
path := buildViaPath(qs, via...)
inSide := inOutIterator(path, in, true, tags, ctx)
outSide := inOutIterator(path, in.Clone(), false, tags, ctx)
or := iterator.NewOr(inSide, outSide)
return or, ctx
},
tags: tags,
}
}
func labelContextMorphism(tags []string, via ...interface{}) morphism {
var path *Path
if len(via) == 0 {
path = nil
} else {
path = buildViaPath(nil, via...)
path = path.Tag(tags...)
}
return morphism{
Name: "label_context",
Reversal: func(ctx *pathContext) (morphism, *pathContext) {
out := ctx.copy()
ctx.labelSet = path
return labelContextMorphism(tags, via...), &out
},
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
out := ctx.copy()
out.labelSet = path
return in, &out
},
tags: tags,
}
}
// predicatesMorphism iterates to the uniqified set of predicates from
// the given set of nodes in the path.
func predicatesMorphism(isIn bool) morphism {
m := morphism{
Name: "out_predicates",
Reversal: func(ctx *pathContext) (morphism, *pathContext) {
panic("not implemented: need a function from predicates to their associated edges")
},
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
dir := quad.Subject
if isIn {
dir = quad.Object
}
lto := iterator.NewLinksTo(qs, in, dir)
hasa := iterator.NewHasA(qs, lto, quad.Predicate)
return iterator.NewUnique(hasa), ctx
},
}
if isIn {
m.Name = "in_predicates"
}
return m
}
// iteratorMorphism simply tacks the input iterator onto the chain.
func iteratorMorphism(it graph.Iterator) morphism {
return morphism{
Name: "iterator",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return iteratorMorphism(it), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return join(qs, it, in), ctx
},
}
}
// andMorphism sticks a path onto the current iterator chain.
func andMorphism(p *Path) morphism {
return morphism{
Name: "and",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return andMorphism(p), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
itR := p.BuildIteratorOn(qs)
return join(qs, in, itR), ctx
},
}
}
// orMorphism is the union, vice intersection, of a path and the current iterator.
func orMorphism(p *Path) morphism {
return morphism{
Name: "or",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return orMorphism(p), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
itR := p.BuildIteratorOn(qs)
or := iterator.NewOr(in, itR)
return or, ctx
},
}
}
func followMorphism(p *Path) morphism {
return morphism{
Name: "follow",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return followMorphism(p.Reverse()), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return p.Morphism()(qs, in), ctx
},
}
}
// exceptMorphism removes all results on p.(*Path) from the current iterators.
func exceptMorphism(p *Path) morphism {
return morphism{
Name: "except",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return exceptMorphism(p), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
subIt := p.BuildIteratorOn(qs)
allNodes := qs.NodesAllIterator()
notIn := iterator.NewNot(subIt, allNodes)
return join(qs, in, notIn), ctx
},
}
}
// uniqueMorphism removes duplicate values from current path.
func uniqueMorphism() morphism {
return morphism{
Name: "unique",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return uniqueMorphism(), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return iterator.NewUnique(in), ctx
},
}
}
func saveMorphism(via interface{}, tag string) morphism {
return morphism{
Name: "save",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return saveMorphism(via, tag), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return buildSave(qs, via, tag, in, false, false), ctx
},
tags: []string{tag},
}
}
func saveReverseMorphism(via interface{}, tag string) morphism {
return morphism{
Name: "saver",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return saveReverseMorphism(via, tag), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return buildSave(qs, via, tag, in, true, false), ctx
},
tags: []string{tag},
}
}
func saveOptionalMorphism(via interface{}, tag string) morphism {
return morphism{
Name: "saveo",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return saveOptionalMorphism(via, tag), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return buildSave(qs, via, tag, in, false, true), ctx
},
tags: []string{tag},
}
}
func saveOptionalReverseMorphism(via interface{}, tag string) morphism {
return morphism{
Name: "saveor",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return saveOptionalReverseMorphism(via, tag), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return buildSave(qs, via, tag, in, true, true), ctx
},
tags: []string{tag},
}
}
func buildHas(qs graph.QuadStore, via interface{}, in graph.Iterator, reverse bool, nodes []quad.Value) graph.Iterator {
viaIter := buildViaPath(qs, via).
BuildIterator()
ends := func() graph.Iterator {
if len(nodes) == 0 {
return qs.NodesAllIterator()
}
fixed := qs.FixedIterator()
for _, n := range nodes {
fixed.Add(qs.ValueOf(n))
}
return fixed
}()
start, goal := quad.Subject, quad.Object
if reverse {
start, goal = goal, start
}
trail := iterator.NewLinksTo(qs, viaIter, quad.Predicate)
dest := iterator.NewLinksTo(qs, ends, goal)
// If we were given nodes, intersecting with them first will
// be extremely cheap-- otherwise, it will be the most expensive
// (requiring iteration over all nodes). We have enough info to
// make this optimization now since intersections are commutative
if len(nodes) == 0 { // Where dest involves an All iterator.
route := join(qs, trail, dest)
has := iterator.NewHasA(qs, route, start)
return join(qs, in, has)
}
// This looks backwards. That's OK-- see the note above.
route := join(qs, dest, trail)
has := iterator.NewHasA(qs, route, start)
return join(qs, has, in)
}
func buildSave(
qs graph.QuadStore, via interface{},
tag string, from graph.Iterator, reverse bool, optional bool,
) graph.Iterator {
allNodes := qs.NodesAllIterator()
allNodes.Tagger().Add(tag)
start, goal := quad.Subject, quad.Object
if reverse {
start, goal = goal, start
}
viaIter := buildViaPath(qs, via).
BuildIterator()
dest := iterator.NewLinksTo(qs, allNodes, goal)
trail := iterator.NewLinksTo(qs, viaIter, quad.Predicate)
route := join(qs, trail, dest)
save := graph.Iterator(iterator.NewHasA(qs, route, start))
if optional {
save = iterator.NewOptional(save)
}
return join(qs, from, save)
}
func inOutIterator(viaPath *Path, from graph.Iterator, inIterator bool, tags []string, ctx *pathContext) graph.Iterator {
start, goal := quad.Subject, quad.Object
if inIterator {
start, goal = goal, start
}
viaIter := viaPath.BuildIterator()
for _, tag := range tags {
viaIter.Tagger().Add(tag)
}
source := iterator.NewLinksTo(viaPath.qs, from, start)
trail := iterator.NewLinksTo(viaPath.qs, viaIter, quad.Predicate)
var label graph.Iterator
if ctx != nil {
if ctx.labelSet != nil {
labeliter := ctx.labelSet.BuildIteratorOn(viaPath.qs)
label = iterator.NewLinksTo(viaPath.qs, labeliter, quad.Label)
}
}
route := join(viaPath.qs, source, trail, label)
return iterator.NewHasA(viaPath.qs, route, goal)
}
func buildViaPath(qs graph.QuadStore, via ...interface{}) *Path {
if len(via) == 0 {
return PathFromIterator(qs, qs.NodesAllIterator())
} else if len(via) == 1 {
v := via[0]
switch p := v.(type) {
case nil:
return PathFromIterator(qs, qs.NodesAllIterator())
case *Path:
if p.qs != qs {
newp := &Path{
qs: qs,
baseContext: p.baseContext,
stack: p.stack[:],
}
return newp
}
return p
case quad.Value:
return StartPath(qs, p)
}
}
nodes := make([]quad.Value, 0, len(via))
for _, v := range via {
qv, ok := quad.AsValue(v)
if !ok {
panic(fmt.Errorf("Invalid type passed to buildViaPath: %v (%T)", v, v))
}
nodes = append(nodes, qv)
}
return StartPath(qs, nodes...)
}
// skipMorphism will skip a number of values-- if there are none, this function
// acts as a passthrough for the previous iterator.
func skipMorphism(v int64) morphism {
return morphism{
Name: "skip",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return skipMorphism(v), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
if v == 0 {
// Acting as a passthrough
return in, ctx
}
return iterator.NewSkip(in, v), ctx
},
}
}
// limitMorphism will limit a number of values-- if number is negative or zero, this function
// acts as a passthrough for the previous iterator.
func limitMorphism(v int64) morphism {
return morphism{
Name: "limit",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return limitMorphism(v), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
if v <= 0 {
// Acting as a passthrough
return in, ctx
}
return iterator.NewLimit(in, v), ctx
},
}
}
// countMorphism will return count of values.
func countMorphism() morphism {
return morphism{
Name: "count",
Reversal: func(ctx *pathContext) (morphism, *pathContext) { return countMorphism(), ctx },
Apply: func(qs graph.QuadStore, in graph.Iterator, ctx *pathContext) (graph.Iterator, *pathContext) {
return iterator.NewCount(in, qs), ctx
},
}
}