-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwoqlRule.js
312 lines (284 loc) · 9.57 KB
/
woqlRule.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
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable valid-typeof */
/* eslint-disable no-continue */
/* eslint-disable no-plusplus */
/* eslint-disable no-use-before-define */
const TerminusRule = require('./terminusRule');
const UTILS = require('../utils');
/**
* @file WOQL Rules
* @license Apache Version 2
* WOQL Rules support pattern matching on results of WOQL Queries
*/
function WOQLRule() {
TerminusRule.TerminusRule.call(this);
this.pattern = new WOQLPattern();
}
Object.setPrototypeOf(WOQLRule.prototype, TerminusRule.TerminusRule.prototype);
/**
* Specifies that the rule only applies to specific variables returned by the WOQL query
* @param {[String]} - array of strings, each representing a variable name, variable prefixes
* ("v:") are added automatically if absent
*/
WOQLRule.prototype.setVariables = function (vars) {
if (vars && vars.length) {
this.pattern.variables = UTILS.removeNamespacesFromVariables(vars);
this.current_variable = this.pattern.variables[this.pattern.variables.length - 1];
}
return this;
};
/**
* Shorthand to the above using spread operator
*/
WOQLRule.prototype.vars = function (...varlist) {
return this.setVariables(varlist);
};
/**
* Specifies that the rule applies to a specific variable, variable prefix ("v:")
* is added automatically if absent
*/
WOQLRule.prototype.v = function (v) {
if (v) {
this.current_variable = UTILS.removeNamespaceFromVariable(v);
return this;
}
return this.current_variable;
};
/**
* Specifies that the rule applies to a specific edge, a source -> target pair of variables
*/
WOQLRule.prototype.edge = function (source, target) {
this.scope('edge');
if (source) {
const vs = UTILS.removeNamespaceFromVariable(source);
this.setVariables([vs]);
this.pattern.source = vs;
}
if (target) {
const vs = UTILS.removeNamespaceFromVariable(target);
if (!source) this.setVariables([vs]);
this.pattern.target = vs;
}
return this;
};
/**
* Specifies that the rule applies to a specific edge, a source -> target pair of variables
*/
WOQLRule.prototype.rownum = function (rownum) {
if (typeof rownum === 'undefined') return this.pattern.rownum;
this.pattern.rownum = rownum;
return this;
};
/**
* Specifies that the value of a variable must be one of the values contained in the list
* @param {[String|Number]} list - parameters are any atomic value (string | number) -
* the rule will match only cells that have one of these values
*/
WOQLRule.prototype.in = function (...list) {
if (this.current_variable) {
if (!this.pattern.constraints) this.pattern.constraints = {};
if (!this.pattern.constraints[this.current_variable]) {
this.pattern.constraints[this.current_variable] = [];
}
this.pattern.constraints[this.current_variable].push(list);
}
return this;
};
/**
* Specifies a filter function to apply to each element - only those values that
* return true when this filter is invoked will be matched
* @param {function} tester - test function that will be used to filter values
*/
WOQLRule.prototype.filter = function (tester) {
if (this.current_variable) {
if (!this.pattern.constraints) this.pattern.constraints = {};
if (!this.pattern.constraints[this.current_variable]) {
this.pattern.constraints[this.current_variable] = [];
}
this.pattern.constraints[this.current_variable].push(tester);
}
return this;
};
WOQLRule.prototype.matchRow = function (rules, row, rownum, action) {
const matches = [];
for (let i = 0; i < rules.length; i++) {
if (action && this.rule && typeof this.rule[action] === 'undefined') continue;
if (rules[i].pattern.matchRow(row, rownum)) {
matches.push(rules[i]);
}
}
return matches;
};
WOQLRule.prototype.matchCell = function (rules, row, key, rownum, action) {
const matches = [];
for (let i = 0; i < rules.length; i++) {
if (action && this.rule && typeof this.rule[action] === 'undefined') continue;
if (rules[i].pattern.matchCell(row, key, rownum)) {
matches.push(rules[i]);
}
}
return matches;
};
/**
*
* @param {array} rules
* @param {string} key - column name
* @param {string} ruleName - the rule name like header, width etc...
* @returns {array}
*/
WOQLRule.prototype.matchColumn = function (rules, key, ruleName) {
const matches = [];
for (let i = 0; i < rules.length; i++) {
if (ruleName && this.rule && typeof this.rule[ruleName] === undefined) continue;
const ruleRow = rules[i];
/*
* if it is not the rule that I'm looking for
*/
if (ruleName && ruleRow.rule[ruleName] === undefined) {
continue;
}
if (ruleRow.pattern.matchColumn(key)) {
matches.push(ruleRow);
}
}
return matches;
};
WOQLRule.prototype.matchNode = function (rules, row, key, nid, action) {
const matches = [];
for (let i = 0; i < rules.length; i++) {
if (action && this.rule && typeof this.rule[action] === 'undefined') continue;
if (rules[i].pattern.matchNode(row, key, nid)) {
matches.push(rules[i]);
}
}
return matches;
};
WOQLRule.prototype.matchPair = function (rules, row, keya, keyb, action) {
const matches = [];
for (let i = 0; i < rules.length; i++) {
if (action && this.rule && typeof this.rule[action] === 'undefined') continue;
if (rules[i].pattern.matchPair(row, keya, keyb)) {
matches.push(rules[i]);
}
}
return matches;
};
// alias
WOQLRule.prototype.matchEdge = WOQLRule.prototype.matchPair;
/**
* Object to encapsulate the matching of woql result patterns - inherits from TerminusRule
* @param {Object} pattern
*/
function WOQLPattern(pattern) {
TerminusRule.TerminusPattern.call(this, pattern);
}
Object.setPrototypeOf(WOQLPattern.prototype, TerminusRule.TerminusPattern.prototype);
WOQLPattern.prototype.prettyPrint = function () {
// starts with obj. ...
let str = `${this.scope}('`;
if (this.variables) {
str += this.variables.join("', '");
}
str += "')";
if (typeof this.literal !== 'undefined') {
str += `.literal(${this.literal})`;
}
if (typeof this.type !== 'undefined') {
str += `.type(${this.unpack(this.type)})`;
}
if (typeof this.value !== 'undefined') {
str += `.value(${this.unpack(this.value, true)})`;
}
for (const v in this.constraints) {
str += `.v('${v}')`;
for (let i = 0; i < this.constraints[v].length; i++) {
if (typeof this.constraints[v][i] === 'function') {
str += `.filter(${this.constraints[v][i]})`;
} else {
// eslint-disable-next-line no-undef
str += `.in(${json.unpack(this.constraints[v][i])})`;
}
}
}
return str;
};
/**
* @param {String|Object} data
* @param {String|[String, String]} [key] - a variable or an array of two variables
* (in the case of edge scope) which constitutes the key being tested
* @param {String} [scope] - the scope in which the test is being carried out (row, column, cell)
*/
WOQLPattern.prototype.matchRow = function (row, rownum) {
if (typeof this.rownum !== 'undefined' && typeof rownum !== 'undefined') {
if (!this.numberMatch(this.rownum, rownum)) return false;
}
if (this.scope && this.scope !== 'row') return false;
if (!this.testVariableConstraints(row)) return false;
return true;
};
WOQLPattern.prototype.matchCell = function (row, key, rownum) {
if (typeof this.rownum !== 'undefined' && typeof rownum !== 'undefined') {
if (!this.numberMatch(this.rownum, rownum)) return false;
}
if (!this.testBasics('column', row[key])) return false;
if (this.variables && this.variables.length && this.variables.indexOf(key) === -1) return false;
if (!this.testVariableConstraints(row)) return false;
return true;
};
WOQLPattern.prototype.matchNode = function (row, key) {
if (!this.testBasics('node', row[key])) return false;
if (this.variables && this.variables.length && this.variables.indexOf(key) === -1) return false;
if (!this.testVariableConstraints(row)) return false;
return true;
};
WOQLPattern.prototype.matchColumn = function (key) {
if (this.scope && this.scope !== 'column') return false;
if (this.variables && this.variables.length && this.variables.indexOf(key) === -1) return false;
return true;
};
WOQLPattern.prototype.matchPair = function (row, keya, keyb) {
if (this.scope && this.scope !== 'edge') return false;
if (this.source && this.source !== keya) return false;
if (this.target && this.target !== keyb) return false;
if (!this.testVariableConstraints(row)) return false;
return true;
};
WOQLPattern.prototype.testVariableConstraints = function (row) {
for (const k in this.constraints) {
if (!this.testVariableConstraint(k, row[k])) return false;
}
return true;
};
WOQLPattern.prototype.testVariableConstraint = function (name, val) {
if (!this.constraints[name]) return true;
for (let i = 0; i < this.constraints[name].length; i++) {
if (!this.testValue(val, this.constraints[name][i])) {
return false;
}
}
return true;
};
WOQLPattern.prototype.setPattern = function (pattern) {
for (const key in pattern) {
this[key] = pattern[key];
}
};
WOQLPattern.prototype.json = function () {
const json = {};
if (this.scope) {
json.scope = this.scope;
}
if (this.value) {
json.value = this.value;
}
if (this.rownum) json.rownum = this.rownum;
if (this.variables) json.variables = this.variables;
if (this.literal) json.literal = this.literal;
if (this.type) json.type = this.type;
if (this.constraints) json.constraints = this.constraints;
if (this.source) json.source = this.source;
if (this.target) json.target = this.target;
return json;
};
module.exports = { WOQLRule, WOQLPattern };