Repository navigation
Expand file tree
/
Copy pathGrapoi.js
More file actions
260 lines (223 loc) · 8.32 KB
/
Copy pathGrapoi.js
File metadata and controls
260 lines (223 loc) · 8.32 KB
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
import { baseDataset, baseTerm } from './lib/base.js'
import { rebaseDataset } from './lib/rebase.js'
import { replaceDataset } from './lib/replace.js'
import sortByScore from './lib/sortByScore.js'
import toPathArray from './lib/toPathArray.js'
import toTerm from './lib/toTerm.js'
import toTermArray from './lib/toTermArray.js'
import Path from './Path.js'
import PathList from './PathList.js'
/**
* A graph pointer object
* @extends PathList
*/
class Grapoi extends 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} term Term for the pointers
* @param {Term[]} terms Terms for the pointers
* @param {Term} graph Graph for the pointers
* @param {Term[]} graphs Graphs for graph pointers
*/
constructor ({ dataset, factory, ptrs, term, terms, graph, graphs }) {
if (term || terms) {
terms = terms || term
}
if (graph || graphs) {
graphs = graphs || graph
}
if (!ptrs && terms) {
ptrs = toPathArray(terms, { dataset, factory, graph: graphs })
}
super({ dataset, factory, ptrs, graphs })
}
_toTerm (value) {
return toTerm(value, { factory: this.factory })
}
_toTermArray (values) {
return toTermArray(values, { factory: this.factory })
}
/**
* Add quad(s) with the current terms as the object
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [subjects] Subjects of the quads
* @param {function} [callback] Function called for each subject as a pointer argument
* @returns {Grapoi} this
*/
addIn (predicates, subjects, callback) {
if (typeof subjects === 'function') {
callback = subjects
subjects = null
}
if (!subjects) {
subjects = [this.factory.blankNode()]
}
return super.addIn(this._toTermArray(predicates), this._toTermArray(subjects), callback)
}
/**
* Add list(s) with the given items
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the lists
* @param {Grapoi|Grapoi[]|Term|Term[]} [items] List items
* @returns {Grapoi} this
*/
addList (predicates, items) {
return super.addList(this._toTermArray(predicates), this._toTermArray(items))
}
/**
* Add quad(s) with the current terms as the subject
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [objects] Objects of the quads
* @param {function} [callback] Function called for each subject as a pointer argument
* @returns {Grapoi} this
*/
addOut (predicates, objects, callback) {
if (typeof objects === 'function') {
callback = objects
objects = null
}
if (!objects) {
objects = [this.factory.blankNode()]
}
return super.addOut(this._toTermArray(predicates), this._toTermArray(objects), callback)
}
/**
* Base all terms with a relative IRI with the given base.
* @param {Grapoi|Grapoi[]|Term|Term[]} base Base of the terms
* @returns {Constructor} Instance with a single pointer with the term based
*/
base (base) {
if (!base) {
throw new Error('base parameter is required')
}
base = this._toTerm(base)
for (const ptr of this.ptrs) {
baseDataset(base, { factory: this.factory })(ptr.dataset)
}
return this.node(baseTerm(base, { factory: this.factory })(this.term))
}
/**
* Use the given score function on all pointers and return the pointer with the best score.
* @param {function} score Score function
* @returns {Constructor} Instance with a single pointer with the best score
*/
best (score) {
return this.score(score, { limit: 1 })
}
/**
* Delete quad(s) with the current terms as the object.
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [subjects] Subjects of the quads
* @returns {Grapoi} this
*/
deleteIn (predicates, subjects) {
return super.deleteIn(this._toTermArray(predicates), this._toTermArray(subjects))
}
/**
* Delete list(s).
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the lists
* @returns {Grapoi} this
*/
deleteList (predicates) {
return super.deleteList(this._toTermArray(predicates))
}
/**
* Delete quad(s) with the current terms as the subject.
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [objects] Objects of the quads
* @returns {Constructor} this
*/
deleteOut (predicates, objects) {
return super.deleteOut(this._toTermArray(predicates), this._toTermArray(objects))
}
/**
* Filter the pointers based on matching quad(s) with the current terms as the object.
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [subjects] Subjects of the quads
* @returns {Constructor} Instance that contains only the filtered pointers
*/
hasIn (predicates, subjects) {
return super.hasIn(this._toTermArray(predicates), this._toTermArray(subjects))
}
/**
* Filter the pointers based on matching quad(s) with the current terms as the subject.
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [objects] Objects of the quads
* @returns {Constructor} Instance that contains only the filtered pointers
*/
hasOut (predicates, objects) {
return super.hasOut(this._toTermArray(predicates), this._toTermArray(objects))
}
/**
* Traverse the graph with the current terms as the object.
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [subjects] Subjects of the quads
* @returns {Constructor} Instance with pointers of the traversed target terms
*/
in (predicates, subjects) {
return super.in(this._toTermArray(predicates), this._toTermArray(subjects))
}
/**
* Traverse the graph with the current terms as the subject.
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Predicates of the quads
* @param {Grapoi|Grapoi[]|Term|Term[]} [objects] Objects of the quads
* @returns {Constructor} Instance with pointers of the traversed target terms
*/
out (predicates, objects) {
return super.out(this._toTermArray(predicates), this._toTermArray(objects))
}
/**
* Jump to random terms.
* @param {Grapoi|Grapoi[]|Term|Term[]} predicates Terms for the new pointers
* @returns {Constructor} Instance with pointers of the selected terms
*/
node (terms = null) {
return super.node(this._toTermArray(terms))
}
/**
* Rebase all terms of the current pointers with a new base.
* @param {Grapoi|Grapoi[]|Term|Term[]} base New base of the terms
* @returns {Constructor} Instance with a single pointer with the new base as the term
*/
rebase (base) {
if (!base) {
throw new Error('base parameter is required')
}
base = this._toTerm(base)
for (const ptr of this.ptrs) {
rebaseDataset(ptr.term, base, { factory: this.factory })(ptr.dataset)
}
return this.node(base)
}
/**
* Replace all terms of the current pointers with another term.
* @param {Grapoi|Grapoi[]|Term|Term[]} replacement Term used as replacement
* @returns {Constructor} Instance with a single pointer with the replacement as the term
*/
replace (replacement) {
if (!replacement) {
throw new Error('replacement parameter is required')
}
replacement = this._toTerm(replacement)
for (const ptr of this.ptrs) {
replaceDataset(ptr.term, replacement, { factory: this.factory })(ptr.dataset)
}
return this.node(replacement)
}
/**
* Score the pointers and sort them by score value.
* @param {Function} score @rdfjs/score compatible score function
* @param {Number} [limit] Limit for the result pointers
* @param {Number} [offset] Offset for the result pointers
* @returns {Constructor} Instance of the scored pointers, sorted and sliced.
*/
score (score, { limit = Infinity, offset = 0 } = {}) {
const ptrs = sortByScore(score(this))
.slice(offset, offset + limit)
.map(ptr => new Path({ ...ptr, factory: this.factory }))
return this.clone({ ptrs })
}
}
export default Grapoi