-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConstraintManager.js
229 lines (175 loc) · 6.03 KB
/
ConstraintManager.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
var forEach = require('lodash.foreach');
var uniq = require('lodash.uniq');
var Class = require('../ext/Class');
var ExprUtils = require('../sparql/ExprUtils');
var ElementsAndExprs = require('./ElementsAndExprs');
var ArrayUtils = require('../util/ArrayUtils');
/**
* TODO Possibly rename to constraint list
*
* A constraint manager is a container for ConstraintSpec objects.
*
* @param cefRegistry
* A Map<String, ConstraintElementFactory>
*/
var ConstraintManager = Class.create({
classLabel: 'jassa.facete.ConstraintManager',
initialize: function(constraints) {
this.constraints = constraints || [];
},
clear: function() {
ArrayUtils.clear(this.constraints);
},
isEmpty: function() {
var result = this.constraints.length === 0;
return result;
},
/**
* Returns a new constraintManager with a new array of the original
* constraints
*/
shallowClone: function() {
var result = new ConstraintManager(this.constraints.slice(0));
return result;
},
/**
* Yields all constraints having at least one variable bound to the
* exact path
*
* Note: In general, a constraint may make use of multiple paths
*/
getConstraintsByPath: function(path) {
var result = [];
var constraints = this.constraints;
//for(var i = 0; i < constraints.length; ++i) {
constraints.forEach(function(constraint) {
var paths = constraint.getDeclaredPaths();
var isPath = paths.some(function(p) {
var tmp = p.equals(path);
return tmp;
});
if(isPath) {
result.push(constraint);
}
});
return result;
},
getConstrainedSteps: function(path) {
// console.log("getConstrainedSteps: ", path);
// checkNotNull(path);
var tmp = [];
var steps = path.getSteps();
var constraints = this.constraints;
for(var i = 0; i < constraints.length; ++i) {
var constraint = constraints[i];
// console.log(" Constraint: " + constraint);
var paths = constraint.getDeclaredPaths();
// console.log(" Paths: " + paths.length + " - " + paths);
for(var j = 0; j < paths.length; ++j) {
var p = paths[j];
var pSteps = p.getSteps();
var delta = pSteps.length - steps.length;
// console.log(" Compare: " + delta, p, path);
var startsWith = p.startsWith(path);
// console.log(" Startswith: " + startsWith);
if(delta == 1 && startsWith) {
var step = pSteps[pSteps.length - 1];
tmp.push(step);
}
}
}
var result = uniq(tmp, function(step) { return '' + step; });
// console.log("Constraint result", constraints.length,
// result.length);
return result;
},
getConstraints: function() {
return this.constraints;
},
addConstraint: function(constraint) {
this.constraints.push(constraint);
},
addConstraints: function(constraints) {
var self = this;
constraints.forEach(function(constraint) {
self.addConstraint(constraint);
});
},
// Fcuking hack because of legacy code and the lack of a standard
// collection library...
// TODO Make the constraints a hash set (or a list set)
removeConstraint: function(constraint) {
var result = false;
var cs = this.constraints;
var n = [];
for(var i = 0; i < cs.length; ++i) {
var c = cs[i];
if(!c.equals(constraint)) {
n.push(c);
} else {
result = true;
}
}
this.constraints = n;
return result;
},
toggleConstraint: function(constraint) {
var wasRemoved = this.removeConstraint(constraint);
if(!wasRemoved) {
this.addConstraint(constraint);
}
},
createElementsAndExprs: function(facetNode, excludePath) {
// var triples = [];
var elements = [];
var resultExprs = [];
var pathToExprs = {};
var self = this;
this.constraints.forEach(function(constraint) {
var paths = constraint.getDeclaredPaths();
var pathId = paths.join(' ');
// _(paths).reduce(
// function(memo, path) {
// return memo + ' ' + path;
// }, '');
// Check if any of the paths is excluded
if(excludePath) {
var skip = paths.some(function(path) {
// console.log("Path.equals", excludePath, path);
var tmp = excludePath.equals(path);
return tmp;
});
if(skip) {
return;
}
}
paths.forEach(function(path) {
var fn = facetNode.forPath(path);
var tmpElements = fn.getElements();
elements.push.apply(elements, tmpElements);
});
var ci = constraint.createElementsAndExprs(facetNode);
var ciElements = ci.getElements();
var ciExprs = ci.getExprs();
if(ciElements) {
elements.push.apply(elements, ciElements);
}
if(ciExprs && ciExprs.length > 0) {
var exprs = pathToExprs[pathId];
if(!exprs) {
exprs = [];
pathToExprs[pathId] = exprs;
}
var andExpr = ExprUtils.andify(ciExprs);
exprs.push(andExpr);
}
});
forEach(pathToExprs, function(exprs) {
var orExpr = ExprUtils.orify(exprs);
resultExprs.push(orExpr);
});
var result = new ElementsAndExprs(elements, resultExprs);
return result;
}
});
module.exports = ConstraintManager;