-
Notifications
You must be signed in to change notification settings - Fork 2
/
PathList.js
409 lines (348 loc) · 10.3 KB
/
PathList.js
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
import TermSet from '@rdfjs/term-set'
import ptrIsEqual from './lib/ptrIsEqual.js'
import Path from './Path.js'
function createExtendCallback (ptrList, callback) {
if (!callback) {
return () => {}
}
return ptr => {
return callback(new ptrList.constructor({
factory: ptrList.factory,
ptrs: [ptr]
}))
}
}
/**
* List of paths
* @property {Array} ptrs All paths of this list
*/
class PathList {
/**
* Create a new instance
* @param {DatasetCore} dataset Dataset for the pointers
* @param {Environment} factory Factory for new quads
* @param {Path[]} ptrs Use existing pointers
* @param {Term[]} terms Terms for the pointers
* @param {Term[]} graphs Graphs for the pointers
*/
constructor ({ dataset, factory, ptrs, terms, graphs }) {
this.factory = factory
if (ptrs) {
this.ptrs = [...ptrs]
} else {
this.ptrs = []
for (const term of (terms || [null])) {
for (const graph of (graphs || [null])) {
this.ptrs.push(new Path({ dataset, factory, graph, term }))
}
}
}
}
/**
* Dataset of the pointer or null if there is no unique dataset.
* @returns {DatasetCore|null} Unique dataset or null
*/
get dataset () {
const datasets = new Set(this.datasets)
if (datasets.size !== 1) {
return null
}
return datasets[Symbol.iterator]().next().value
}
/**
* An array of all datasets of all pointers.
* @returns {DatasetCore[]} Array of datasets.
*/
get datasets () {
return this.ptrs.map(ptr => ptr.dataset)
}
/**
* The term of the pointers if all pointers refer to a unique term.
* @returns {Term|undefined} Term of undefined
*/
get term () {
const terms = new TermSet(this.terms)
if (terms.size !== 1) {
return undefined
}
return terms[Symbol.iterator]().next().value
}
/**
* An array of all terms of all pointers.
* @returns {Term[]} Array of all terms
*/
get terms () {
return this.ptrs.map(ptr => ptr.term)
}
/**
* The value of the pointers if all pointers refer to a unique term.
* @returns {String|undefined} Value or undefined
*/
get value () {
const term = this.term
return (term === undefined || term === null) ? undefined : term.value
}
/**
* An array of all values of all pointers.
* @returns {String[]} Array of all values
*/
get values () {
return this.ptrs.map(ptr => ptr.value)
}
/**
* Add quads with the current terms as the object
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} subjects Subjects of the quads
* @param {function} [callback] Function called for each subject as a pointer argument
* @returns {PathList} this
*/
addIn (predicates, subjects, callback) {
const extendCallback = createExtendCallback(this, callback)
for (const ptr of this.ptrs) {
ptr.addIn(predicates, subjects, extendCallback)
}
return this
}
/**
* Add lists with the given items
* @param {Term[]} predicates Predicates of the lists
* @param {Term[]} items List items
* @returns {PathList} this
*/
addList (predicates, items) {
if (this.isAny()) {
throw new Error('can\'t attach a list to an any ptr')
}
for (const ptr of this.ptrs) {
ptr.addList(predicates, items)
}
return this
}
/**
* Add quads with the current terms as the subject
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} objects Objects of the quads
* @param {function} [callback] Function called for each subject as a pointer argument
* @returns {PathList} this
*/
addOut (predicates, objects, callback) {
const extendCallback = createExtendCallback(this, callback)
for (const ptr of this.ptrs) {
ptr.addOut(predicates, objects, extendCallback)
}
return this
}
/**
* Create a new instance of the Constructor with a cloned list of pointers.
* @param args Additional arguments for the constructor
* @returns {Constructor} Cloned instance
*/
clone (args) {
return new this.constructor({ factory: this.factory, ptrs: this.ptrs, ...args })
}
/**
* Delete quads with the current terms as the object.
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} subjects Subjects of the quads
* @returns {PathList} this
*/
deleteIn (predicates, subjects) {
for (const ptr of this.ptrs) {
ptr.deleteIn(predicates, subjects)
}
return this
}
/**
* Delete lists.
* @param {Term[]} predicates Predicates of the lists
* @returns {PathList} this
*/
deleteList (predicates) {
for (const ptr of this.ptrs) {
ptr.deleteList(predicates)
}
return this
}
/**
* Delete quads with the current terms as the subject.
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} objects Objects of the quads
* @returns {PathList} this
*/
deleteOut (predicates, objects) {
for (const ptr of this.ptrs) {
ptr.deleteOut(predicates, objects)
}
return this
}
/**
* Create a new instance with a unique set of pointers.
* The path of the pointers is trimmed.
* @returns {Constructor} Instance with unique pointers
*/
distinct () {
const ptrs = this.ptrs.reduce((unique, ptr) => {
if (!unique.some(uPtr => ptrIsEqual(uPtr, ptr))) {
unique.push(ptr.trim())
}
return unique
}, [])
return this.clone({ ptrs })
}
/**
* Executes a single instruction.
* @param instruction The instruction to execute
* @returns {Constructor} Instance with the result pointers.
*/
execute (instruction) {
return this.clone({ ptrs: this.ptrs.flatMap(ptr => ptr.execute(instruction)) })
}
/**
* Executes an array of instructions.
* @param instruction The instructions to execute
* @returns {Constructor} Instance with the result pointers.
*/
executeAll (instructions) {
let output = this
for (const instruction of instructions) {
output = output.execute(instruction)
}
return output
}
/**
* Filter the pointers based on the result of the given callback function.
* @param callback
* @returns {Constructor} Instance with the filtered pointers.
*/
filter (callback) {
return this.clone({ ptrs: [...this].filter(callback).map(ptr => ptr.ptrs[0]) })
}
/**
* Filter the pointers based on matching quad(s) with the current terms as the object.
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} subjects Subjects of the quads
* @returns {Constructor} Instance that contains only the filtered pointers
*/
hasIn (predicates, subjects) {
return this.clone({ ptrs: this.ptrs.flatMap(ptr => ptr.hasIn(predicates, subjects)) })
}
/**
* Filter the pointers based on matching quad(s) with the current terms as the subject.
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} objects Objects of the quads
* @returns {Constructor} Instance that contains only the filtered pointers
*/
hasOut (predicates, objects) {
return this.clone({ ptrs: this.ptrs.flatMap(ptr => ptr.hasOut(predicates, objects)) })
}
/**
* Traverse the graph with the current terms as the object.
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} subjects Subjects of the quads
* @returns {Constructor} Instance with pointers of the traversed target terms
*/
in (predicates, subjects) {
return this.clone({ ptrs: this.ptrs.flatMap(ptr => ptr.in(predicates, subjects)) })
}
/**
* Check if any pointer is an any-pointer.
* @returns {boolean} True if any any-pointer was found
*/
isAny () {
return this.ptrs.length > 0 && this.ptrs.some(ptr => ptr.isAny())
}
/**
* Check if there is only one pointer and whether that pointer is a list.
* @returns {boolean} True if the pointer is a list
*/
isList () {
if (this.ptrs.length !== 1) {
return false
}
return this.ptrs[0].isList()
}
/**
* Create an iterator for the list if the instance is a list; otherwise, return undefined.
* @returns {Iterator<Constructor>|undefined} Iterator or undefined
*/
list () {
if (!this.isList()) {
return undefined
}
const iterator = this.ptrs[0].list()[Symbol.iterator]()
const next = () => {
const { done, value } = iterator.next()
if (done) {
return { done: true }
}
return { done: false, value: this.clone({ ptrs: [value] }) }
}
return {
[Symbol.iterator]: () => {
return { next }
}
}
}
/**
* Map each pointer using the given callback function.
* @param callback
* @returns {Array} Array of mapped results
*/
map (callback) {
return [...this].map(callback)
}
/**
* Create a new instance with pointers using the given terms.
* @param terms Array of terms for the pointers
* @returns {Constructor} Instance with pointers of the given terms
*/
node (terms) {
const dataset = this.dataset
const ptrs = [...terms].map(term => new Path({ dataset, factory: this.factory, term }))
return this.clone({ ptrs })
}
/**
* Traverse the graph with the current terms as the subject.
* @param {Term[]} predicates Predicates of the quads
* @param {Term[]} objects Objects of the quads
* @returns {Constructor} Instance with pointers of the traversed target terms
*/
out (predicates, objects) {
return this.clone({ ptrs: this.ptrs.flatMap(ptr => ptr.out(predicates, objects)) })
}
/**
* Create an iterator of all quads of all pointer paths.
* @returns {Iterator<Quad>} Iterator for the quads
*/
quads () {
const pathList = this
return {
* [Symbol.iterator] () {
for (const path of pathList.ptrs) {
for (const edge of path.edges) {
yield edge.quad
}
}
}
}
}
/**
* Trim the path of all pointers and create a new instance for the result.
* @returns {Constructor} Instance of the trimmed pointers
*/
trim () {
return this.clone({
ptrs: this.ptrs.map(ptr => ptr.trim())
})
}
/**
* Iterator for each pointer wrapped into a new instance.
* @returns {Iterator<Constructor>}} Iterator for the wrapped pointers
*/
* [Symbol.iterator] () {
for (const ptr of this.ptrs) {
yield this.clone({ ptrs: [ptr] })
}
}
}
export default PathList