-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-node.js
319 lines (248 loc) · 7.36 KB
/
query-node.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
/**
* A query node is a node in a query tree. A query tree can be built for any hierarchical selection
* on the databus, such as publishers, groups, artifacts and collections.
* Each node may declare a range of restrictions. Restrictions can then be overriden again by
* any child node in the hierarchy.
*
* EXAMPLE: Group node says: Select everything in English. One specific artifact child node of the
* group node then states: I don't want to select English, I will select German.
*
* A query tree can then be translated into a SPARQL query that tries to use as few statements as possible
* to fetch the desired data
*/
class QueryNode {
/**
* Creates a new QueryNode with a resource URI and a property. The property will be added to the
* query as a forced and non-overrideable restriction
* @param {*} uri
* @param {*} property
*/
constructor(uri, property) {
this.uri = uri;
this.property = property;
this.childNodes = [];
this.facetSettings = {};
}
// Set or unset a facet of the query node
setFacet(key, value, checked) {
var list = this.facetSettings[key];
if(list == undefined) {
this.facetSettings[key] = [];
list = this.facetSettings[key];
}
if(!this.isOverride(key, value, checked)) {
for(var i = 0; i < list.length; i++) {
if(list[i].value == value) {
list.splice(i, 1);
}
}
if(list.length == 0) {
delete this.facetSettings[key];
}
return;
}
for(var i in list) {
if(list[i].value == value) {
list[i].checked = checked;
return;
}
}
list.push({ value : value, checked : checked });
}
/**
* Check whether a certain facet setting is an override in the hierarchy
* @param {*} key
* @param {*} value
* @param {*} checked
*/
isOverride(key, value, checked) {
if(checked == undefined) {
var setting = QueryNode.findFacetSetting(this, key, value);
checked = setting != null ? setting.checked : false;
}
var parentSetting = QueryNode.findInheritedSetting(this.parent, key, value);
if(parentSetting == undefined) {
return checked;
}
return parentSetting.checked != checked;
}
/**
* Add a child node to this node
* @param {*} node
*/
addChild(node) {
this.childNodes.push(node);
// node.parent = this;
}
static removeChildByUri(node, uri) {
for(var i = 0; i < node.childNodes.length; i++) {
if(node.childNodes[i].uri == uri) {
node.childNodes.splice(i, 1);
return;
}
QueryNode.removeChildByUri(node.childNodes[i], uri);
}
}
static findChildByUri(node, uri) {
for(let i = 0; i < node.childNodes.length; i++) {
if(node.childNodes[i].uri === uri) {
node.childNodes[i] = QueryNode.createFrom(node.childNodes[i]);
return node.childNodes[i];
}
let result = QueryNode.findChildByUri(node.childNodes[i], uri);
if(result != null) {
return result;
}
}
return null;
}
hasFacetSetting(key, value) {
for(var i in this.facetSettings[key]) {
var setting = this.facetSettings[key][i];
if(setting.value == value) {
return true;
}
}
return false;
}
/**
* Create a settings object with all the facet settings active for this node (inluding inherited settings)
* @param {*} node
*/
createFullFacetSettings() {
var fullSettings = {};
for(var facetUri in this.facetSettings) {
fullSettings[facetUri] = JSON.parse(JSON.stringify(this.facetSettings[facetUri]));
}
var parentNode = this.parent;
while(parentNode != undefined) {
for(var facetUri in parentNode.facetSettings) {
if(fullSettings[facetUri] == undefined) {
fullSettings[facetUri] = [];
}
for(var i in parentNode.facetSettings[facetUri]) {
var parentSetting = parentNode.facetSettings[facetUri][i];
if(!this.hasFacetSetting(facetUri, parentSetting.value)) {
fullSettings[facetUri].push(JSON.parse(JSON.stringify(parentSetting)));
}
}
}
parentNode = parentNode.parent;
}
return fullSettings;
}
static serialize(queryNode) {
// QueryNode.clearParents(queryNode);
var result = JSON.stringify(queryNode);
// QueryNode.assignParents(queryNode);
return result;
}
static addChild(node, child) {
node.childNodes.push(child);
// child.parent = node;
}
static mergeAddChild(root, child) {
var existingNode = QueryNode.findChildByUri(root, child.uri);
if(existingNode == null) {
QueryNode.addChild(root, child);
return true;
}
if(child.childNodes.length == 0) {
return false;
}
for(var i in child.childNodes) {
QueryNode.mergeAddChild(existingNode, child.childNodes[i]);
}
}
/*
static clearParents(queryNode) {
queryNode.parent = null;
for(var i = 0; i < queryNode.childNodes.length; i++) {
QueryNode.clearParents(queryNode.childNodes[i]);
}
}
static assignParents(queryNode) {
for(var i = 0; i < queryNode.childNodes.length; i++) {
queryNode.childNodes[i].parent = queryNode;
QueryNode.assignParents(queryNode.childNodes[i]);
}
}
*/
static expandAll(queryNode) {
queryNode.expanded = true;
for(var i = 0; i < queryNode.childNodes.length; i++) {
QueryNode.expandAll(queryNode.childNodes[i]);
}
}
static findParentNodeRecursive(parent, node) {
if(node.uri == null) {
return null;
}
if(parent.childNodes == null || parent.childNodes.length == 0) {
return null;
}
for(var child of parent.childNodes) {
if(child.uri == node.uri) {
return parent;
}
}
for(var child of parent.childNodes) {
var recParent = QueryNode.findParentNodeRecursive(child, node);
if(recParent != null) {
return recParent;
}
}
return null;
}
/**
* Copy constructor to use the QueryNode class inside of angular components
* @param {*} obj
*/
static createFrom(obj) {
var tmpNode = new QueryNode(obj.uri, obj.property);
tmpNode.childNodes = obj.childNodes;
tmpNode.facetSettings = obj.facetSettings;
// tmpNode.parent = obj.parent;
tmpNode.files = obj.files;
return tmpNode;
}
static createSubTree(obj) {
var node = QueryNode.createFrom(obj);
node.facetSettings = node.createFullFacetSettings();
// node.parent = null;
return node;
}
/**
* Search a specific node for a certain facet setting
* @param {*} node
* @param {*} key
* @param {*} value
*/
static findFacetSetting(node, key, value) {
if(node == undefined || node.facetSettings == undefined) {
return undefined;
}
var settingsList = node.facetSettings[key];
if(settingsList == undefined) {
return undefined;
}
for(var i in settingsList) {
var setting = settingsList[i];
if(setting.value == value) {
return setting;
}
}
return undefined;
}
static findInheritedSetting(node, key, value) {
if(node == null) {
return undefined;
}
var setting = QueryNode.findFacetSetting(node, key, value);
if(setting == undefined) {
return QueryNode.findInheritedSetting(node.parent, key, value);
}
return setting;
}
}
module.exports = QueryNode;