-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFacetNode.js
356 lines (264 loc) · 7.69 KB
/
FacetNode.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
var Class = require('../ext/Class');
var NodeFactory = require('../rdf/NodeFactory');
var ElementTriplesBlock = require('../sparql/element/ElementTriplesBlock');
var GenSym = require('../sparql/GenSym');
var VarUtils = require('../sparql/VarUtils');
var Path = require('./Path');
var Step = require('./Step');
var VarNode = require('./VarNode');
var HashMap = require('../util/collection/HashMap');
/**
* This class only has the purpose of allocating variables
* and generating elements.
*
* The purpose is NOT TO DECIDE on which elements should be created.
*
*
* @param parent
* @param root
* @param generator
* @returns {ns.FacetNode}
*/
var FacetNode = Class.create({
initialize: function(varNode, step, parent, root) {
this.parent = parent;
this.root = root;
if(!this.root) {
if(this.parent) {
this.root = parent.root;
}
else {
this.root = this;
}
}
this.varNode = varNode;
/**
* The step for how this node can be reached from the parent
* Null for the root
*/
this.step = step;
this._isActive = true; // Nodes can be disabled; in this case no triples/constraints will be generated
//this.idToChild = {};
this.idToChild = new HashMap();
//this.idToConstraint = {};
},
hashCode: function() {
var result = this.idToChild.hashCode() * 3; // * (this.step ? this.step.hashCode() : 7);
return result;
},
equals: function(that) {
throw new Error('Should not be called');
},
getRootNode: function() {
return this.root;
},
isRoot: function() {
var result = this.parent ? false : true;
return result;
},
/*
getVariableName: function() {
return this.varNode.getVariableName();
},*/
getVar: function() {
var varName = this.varNode.getVariableName();
var result = NodeFactory.createVar(varName);
return result;
},
getVariable: function() {
if(!this.warningShown) {
//console.log('[WARN] Deprecated. Use .getVar() instead');
this.warningShown = true;
}
return this.getVar();
},
getStep: function() {
return this.step;
},
getParent: function() {
return this.parent;
},
getPath: function() {
var steps = [];
var tmp = this;
while(tmp != this.root) {
steps.push(tmp.getStep());
tmp = tmp.getParent();
}
steps.reverse();
var result = new Path(steps);
return result;
},
forPath: function(path) {
var steps = path.getSteps();
var result = this;
steps.forEach(function(step) {
// TODO Allow steps back
result = result.forStep(step);
});
return result;
},
// getIdStr: function() {
// // TODO concat this id with those of all parents
// },
getSteps: function() {
return this.steps;
},
isActiveDirect: function() {
return this._isActive;
},
/**
* Returns an array having at most one element.
*
*
*/
getElements: function() {
var result = [];
var triples = this.getTriples();
if(triples.length > 0) {
var element = new ElementTriplesBlock(triples);
result.push(element);
}
return result;
},
/**
* Get triples for the path starting from the root node until this node
*
* @returns {Array}
*/
getTriples: function() {
var result = [];
this.getTriples2(result);
return result;
},
getTriples2: function(result) {
this.createDirectTriples2(result);
if(this.parent) {
this.parent.getTriples2(result);
}
return result;
},
/*
createTriples2: function(result) {
},*/
createDirectTriples: function() {
var result = [];
this.createDirectTriples2(result);
return result;
},
/**
* Create the element for moving from the parent to this node
*
* TODO Cache the element, as the generator might allocate new vars on the next call
*/
createDirectTriples2: function(result) {
if(this.step) {
var sourceVar = this.parent.getVariable();
var targetVar = this.getVariable();
var tmp = this.step.createElement(sourceVar, targetVar, this.generator);
// FIXME
var triples = tmp.getTriples();
result.push.apply(result, triples);
//console.log('Created element', result);
}
return result;
/*
if(step instanceof ns.Step) {
result = ns.FacetUtils.createTriplesStepProperty(step, startVar, endVar);
} else if(step instanceof ns.StepFacet) {
result = ns.FacetUtils.createTriplesStepFacets(generator, step, startVar, endVar);
} else {
console.error('Should not happen: Step is ', step);
}
*/
},
isActive: function() {
if(!this._isActive) {
return false;
}
if(this.parent) {
return this.parent.isActive();
}
return true;
},
attachToParent: function() {
if(!this.parent) {
return;
}
this.parent[this.id] = this;
this.parent.attachToParent();
},
/**
* Convenience method, uses forStep
*
* @param propertyUri
* @param isInverse
* @returns
*/
forProperty: function(propertyUri, isInverse) {
var step = new Step(propertyUri, isInverse);
var result = this.forStep(step);
return result;
},
forStep: function(step) {
//console.log('Step is', step);
//var stepId = '' + JSON.stringify(step);
var child = this.idToChild.get(step);//[stepId];
if(!child) {
var subVarNode = this.varNode.forStep(step);
child = new FacetNode(subVarNode, step, this, this.root);
/*
child = {
step: step,
child: facet
};*/
//Unless we change something
// we do not add the node to the parent
this.idToChild.put(step, child);
}
return child;
},
/**
*
*
* @returns the new root node.
*/
copyExclude: function() {
// Result is a new root node
var result = new FacetNode();
console.log('Now root:' , result);
this.root.copyExcludeRec(result, this);
return result;
},
copyExcludeRec: function(targetNode, excludeNode) {
console.log('Copy:', this, targetNode);
if(this === excludeNode) {
return;
}
this.copyTo(targetNode);
this.steps.forEach(function(s) {
var childStep = s.step;
var childNode = s.child;
console.log('child:', childStep, childNode);
if(childNode === excludeNode) {
return;
}
var newChildNode = targetNode.forStep(childStep);
console.log('New child:', newChildNode);
childNode.copyExcludeRec(newChildNode, excludeNode);
});
//return result;
}
});
/**
* Use this instead of the constructor
*
*/
FacetNode.createRoot = function(v, generator) {
var varName = v ? v.getName() : VarUtils.s.getName();
generator = generator ? generator : new GenSym('fv');
var varNode = new VarNode(varName, generator);
var result = new FacetNode(varNode);
return result;
};
module.exports = FacetNode;