-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathframeRule.js
515 lines (473 loc) · 17.5 KB
/
frameRule.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* eslint-disable no-continue */
/* eslint-disable no-restricted-syntax */
/* eslint-disable block-scoped-var */
/* eslint-disable no-var */
/* eslint-disable vars-on-top */
/* eslint-disable no-plusplus */
/* eslint-disable camelcase */
/* eslint-disable no-use-before-define */
const TerminusRule = require('./terminusRule');
/**
* @file Frame Rule
* @license Apache Version 2
*/
function FrameRule() {
TerminusRule.TerminusRule.call(this);
this.pattern = new FramePattern();
}
Object.setPrototypeOf(FrameRule.prototype, TerminusRule.TerminusRule.prototype);
/**
* Returns an array of rules that match the paased frame
* @param {[FrameRule]} rules - array of rules to be tested
* @param {Frame} frame - object frame, property frame or data from to be tested
* @param {function} [onmatch] - optional function to be called with args (frame, rule)
* on each match
*/
FrameRule.prototype.testRules = function (rules, frame, onmatch) {
const matched_rules = [];
if (rules && rules.length) {
for (let i = 0; i < rules.length; i++) {
const match = (!rules[i].pattern || this.patternMatchesFrame(rules[i].pattern, frame));
if (match) {
matched_rules.push(rules[i]);
if (onmatch && typeof onmatch === 'function') {
onmatch(frame, rules[i]);
}
}
}
}
return matched_rules;
};
/**
* Determines whether a given pattern matches a given frame
*/
FrameRule.prototype.patternMatchesFrame = function (pattern, frame) {
if (pattern.checkFrame) {
return pattern.checkFrame(frame);
}
const fp = new FramePattern().setPattern(pattern);
return fp.checkFrame(frame);
};
/**
* Specifies that only one from among the list of properties will match
*/
FrameRule.prototype.property = function (...prop) {
if (!prop || prop.length === 0) return this.pattern.property;
this.pattern.property = prop;
return this;
};
/**
* Specifies that only one from among the list of frame_types will match
* frame_types are: data, oneOf, document, object?
*/
FrameRule.prototype.frame_type = function (...frame_type) {
if (!frame_type || frame_type.length === 0) return this.pattern.frame_type;
this.pattern.frame_type = frame_type;
return this;
};
/**
* Specifies that only one from among the list of labels will match
*/
FrameRule.prototype.label = function (...prop) {
if (!prop || prop.length === 0) return this.pattern.label;
this.pattern.label = prop;
return this;
};
/**
* Specifies that only one from among the list of subject ids will match
*/
FrameRule.prototype.subject = function (...prop) {
if (!prop || prop.length === 0) return this.pattern.subject;
this.pattern.subject = prop;
return this;
};
/**
* Specifies that only one from among the list of subject classes will match
*/
FrameRule.prototype.subjectClass = function (...prop) {
if (!prop || prop.length === 0) return this.pattern.subjectClass;
this.pattern.subjectClass = prop;
return this;
};
/**
* Specifies that only one from among the list of range types will match
*/
FrameRule.prototype.range = function (...prop) {
if (!prop || prop.length === 0) return this.pattern.range;
this.pattern.range = prop;
return this;
};
/**
* Specifies that only one from among the list of range types will match
*/
FrameRule.prototype.value = function (...prop) {
if (!prop || prop.length === 0) return this.pattern.value;
this.pattern.value = prop;
return this;
};
/**
* Specifies that only frames of the specified depth will match the rule
*/
FrameRule.prototype.depth = function (depth) {
if (typeof depth === 'undefined') return this.pattern.depth;
this.pattern.depth = depth;
return this;
};
/**
* Specifies that only frames of the specified index will match the rule (index is the order of a
* value in the property frame)
*/
FrameRule.prototype.index = function (...index) {
if (!index || index.length === 0) return this.pattern.index;
this.pattern.index = index;
return this;
};
/**
* Specifies that only frames with the given statuses will match the rule
*/
FrameRule.prototype.status = function (...status) {
if (!status || status.length === 0) return this.pattern.status;
this.pattern.status = status;
return this;
};
/**
* Specifies that the frame will only match if its parent matches the pattern passed as par
*/
FrameRule.prototype.parent = function (par) {
if (!par) return this.pattern.parent;
this.pattern.parent = par;
return this;
};
FrameRule.prototype.children = function (...children) {
if (typeof children === 'undefined' || children.length === 0) return this.pattern.children;
if (typeof this.pattern.children === 'undefined') {
this.pattern.children = [];
}
for (let i = 0; i < children.length; i++) {
this.pattern.children.push(children[i]);
}
return this;
};
/**
* @file Frame Pattern
* A frame pattern can have the following variables
* scope : object, property, data, * - matches a specific part of the frame
* label : matches the label of a property
* frame_type: object, data, document, id, oneOf
* subject: id of the subject
* subjectClass: class of the subject
* range: type of a property (applies to property and data)
* property: property id or list of property ids (parent property if it is an object or data)
* value: value of the property
* parent: a pattern relating to the parent of this frame
* children: patterns for matching on the children of a frame
* depth: how deep are we in the document? starts from 0
* index: the index of a value in an array
* status: updated, error, new, ok,
*/
function FramePattern() {
TerminusRule.TerminusPattern.call(this);
}
Object.setPrototypeOf(FramePattern.prototype, TerminusRule.TerminusPattern.prototype);
FramePattern.prototype.setPattern = function (pattern) {
if (pattern.scope) this.scope = pattern.scope;
if (typeof pattern.literal !== 'undefined') this.literal = pattern.literal;
if (typeof pattern.type !== 'undefined') this.type = pattern.type;
if (typeof pattern.label !== 'undefined') this.label = pattern.label;
if (typeof pattern.frame_type !== 'undefined') this.frame_type = pattern.frame_type;
if (typeof pattern.subject !== 'undefined') this.subject = pattern.subject;
if (typeof pattern.subjectClass !== 'undefined') this.subjectClass = pattern.subjectClass;
if (typeof pattern.range !== 'undefined') this.range = pattern.range;
if (typeof pattern.property !== 'undefined') this.property = pattern.property;
if (typeof pattern.value !== 'undefined') this.value = pattern.value;
if (typeof pattern.parent !== 'undefined') {
let { parent } = pattern;
if (typeof parent.json !== 'function') {
parent = new FramePattern().setPattern(parent);
}
this.parent = parent;
}
if (pattern.children) {
this.children = [];
for (let i = 0; i < pattern.children.length; i++) {
let kid = pattern.children[i];
if (typeof kid.json !== 'function') {
kid = new FramePattern().setPattern(kid);
}
this.children.push(kid);
}
}
if (typeof pattern.depth !== 'undefined') this.depth = pattern.depth;
if (typeof pattern.index !== 'undefined') this.index = pattern.index;
if (typeof pattern.status !== 'undefined') this.status = pattern.status;
};
FramePattern.prototype.json = function () {
const json = {};
if (typeof this.literal !== 'undefined') json.literal = this.literal;
if (this.type) json.type = this.type;
if (this.scope) json.scope = this.scope;
if (typeof this.value !== 'undefined') json.value = this.value;
if (typeof this.label !== 'undefined') json.label = this.label;
if (typeof this.frame_type !== 'undefined') json.frame_type = this.frame_type;
if (typeof this.subject !== 'undefined') json.subject = this.subject;
if (typeof this.subjectClass !== 'undefined') json.subjectClass = this.subjectClass;
if (typeof this.range !== 'undefined') json.range = this.range;
if (typeof this.property !== 'undefined') json.property = this.property;
if (typeof this.parent !== 'undefined') json.parent = (this.parent.json ? this.parent.json() : this.parent);
if (typeof this.children !== 'undefined') {
json.children = [];
for (let i = 0; i < this.children.length; i++) {
json.children.push((this.children[i].json ? this.children[i].json() : this.children[i]));
}
}
if (typeof this.depth !== 'undefined') json.depth = this.depth;
if (typeof this.index !== 'undefined') json.index = this.index;
if (typeof this.status !== 'undefined') json.status = this.status;
return json;
};
FramePattern.prototype.checkFrame = function (frame) {
const rtype = this.getRendererType(frame);
if (!rtype) return false;
if (this.scope && (this.scope !== rtype) && (this.scope !== '*')) return false;
if (this.illegalRuleType(rtype)) return false;
if (this.frame_type && !this.checkFrameType(rtype, frame)) return false;
if (this.label && !this.checkLabel(rtype, frame)) return false;
if (this.subject && !this.checkSubject(rtype, frame)) return false;
if (this.subjectClass && !this.checkSubjectClass(rtype, frame)) return false;
if (this.property && !this.checkProperty(rtype, frame)) return false;
if (typeof this.depth !== 'undefined' && !this.checkDepth(rtype, frame)) return false;
if (this.range && !this.checkRange(rtype, frame)) return false;
if (typeof this.value !== 'undefined' && !this.checkValue(rtype, frame)) return false;
if (this.type && !this.checkType(rtype, frame)) return false;
if (typeof this.literal !== 'undefined' && !this.checkLiteral(rtype, frame)) return false;
if (this.parent && !this.checkParent(rtype, frame)) return false;
if (this.children && this.children.length && !this.checkChildren(rtype, frame)) return false;
if (this.index && !this.checkIndex(rtype, frame)) return false;
if (this.status && !this.checkStatus(rtype, frame)) return false;
return true;
};
FramePattern.prototype.prettyPrint = function () {
// starts with obj. ...
if (this.scope === '*') {
var str = 'all()';
} else {
// eslint-disable-next-line no-redeclare
var str = `${this.scope}()`;
}
if (typeof this.literal !== 'undefined') {
str += `.literal(${this.literal})`;
}
if (typeof this.type !== 'undefined') {
str += `.type(${this.unpack(this.type)})`;
}
if (typeof this.range !== 'undefined') {
str += `.range(${this.unpack(this.range)})`;
}
if (typeof this.frame_type !== 'undefined') {
str += `.frame_type(${this.unpack(this.frameType)})`;
}
if (typeof this.label !== 'undefined') {
str += `.label(${this.unpack(this.label)})`;
}
if (typeof this.subject !== 'undefined') {
str += `.subject(${this.unpack(this.subject)})`;
}
if (typeof this.subjectClass !== 'undefined') {
str += `.subjectClass(${this.unpack(this.subjectClass)})`;
}
if (typeof this.property !== 'undefined') {
str += `.property(${this.unpack(this.property)})`;
}
if (typeof this.value !== 'undefined') {
str += `.value(${this.unpack(this.value, true)})`;
}
if (typeof this.children !== 'undefined' && this.children.length > 0) {
str += '.children(\n';
const kids = this.children;
for (let i = 0; i < kids.length; i++) {
str += `View.pattern().${kids[i].prettyPrint()}`;
if (i < kids.length - 1) str += ',';
str += '\n';
}
str += ')';
}
if (typeof this.parent !== 'undefined') {
str += `.parent(View.pattern().${this.parent.prettyPrint()})`;
}
if (typeof this.depth !== 'undefined') {
str += `.depth(${this.unpack(this.depth, true)})`;
}
if (typeof this.index !== 'undefined') {
str += `.index(${this.unpack(this.index, true)})`;
}
if (typeof this.status !== 'undefined') {
str += `.status(${this.unpack(this.status)})`;
}
return str;
};
FramePattern.prototype.illegalRuleType = function (rtype) {
// data frames have no children
if (rtype === 'data' && this.children && this.children.length) return true;
// object frames have no range
if (rtype === 'object' && this.range) return true;
return false;
};
/* subject is an id or an array of ids,
/* match is positive if the renderer's subject appears in the array or is the id
*/
FramePattern.prototype.checkSubject = function (subject, frame) {
if (typeof this.subject !== 'object' || !this.subject.length) this.subject = [this.subject];
const rsubj = frame.subject();
for (let i = 0; i < this.subject.length; i++) {
if (this.IDsMatch(subject[i], rsubj)) {
return true;
}
}
return false;
};
// at least one child must match all child rules
FramePattern.prototype.checkChildren = function (rtype, frame) {
for (let i = 0; i < this.children.length; i++) {
let found = false;
if (rtype === 'object') {
for (const prop in frame.properties) {
if (this.children[i].checkFrame(frame.properties[prop])) {
found = true;
continue;
}
}
} else if (rtype === 'property') {
// eslint-disable-next-line no-undef
for (let j = 0; j <= renderer.values.length; j++) {
if (this.children[j].checkFrame(frame.values[j])) {
found = true;
continue;
}
}
}
if (!found) return false;
}
return true;
};
FramePattern.prototype.checkStatus = function (rtype, frame) {
if (typeof this.status !== 'object' || this.status.length === 0) this.status = [this.status];
for (let i = 0; i < this.status.length; i++) {
if (this.status[i] === 'updated' && !frame.isUpdated()) return false;
if (this.status[i] === 'new' && !frame.isNew()) return false;
if (this.status[i] === 'unchanged' && frame.isUpdated()) return false;
}
return true;
};
FramePattern.prototype.checkDepth = function (rtype, frame) {
return this.numberMatch(this.depth, frame.depth());
};
FramePattern.prototype.checkParent = function (rtype, frame) {
return this.parent.checkFrame(frame.parent);
};
FramePattern.prototype.checkIndex = function (rtype, frame) {
if (rtype === 'data') {
return this.index === frame.index;
}
return false;
};
FramePattern.prototype.checkProperty = function (rtype, frame) {
if (typeof this.property !== 'object' || !this.property.length) this.property = [this.property];
for (let i = 0; i < this.property.length; i++) {
if (this.propertyIDsMatch(frame.property(), this.property[i])) {
return true;
}
}
return false;
};
/**
* Checks to make sure the frame is among the specified types
*/
FramePattern.prototype.checkType = function (rtype, frame) {
if (rtype === 'object') var vs = frame.subjectClass();
// eslint-disable-next-line no-redeclare
else var vs = typeof frame.range === 'function' ? frame.range() : frame.range;
if (!Array.isArray(this.type)) this.type = [this.type];
if (this.type.indexOf(vs) === -1) return false;
return true;
};
FramePattern.prototype.checkLiteral = function (rtype, frame) {
if (rtype === 'object') return false;
if (rtype === 'property') return false;
if (rtype === 'data') return frame.isDatatypeProperty();
return true;
};
// returns true if any of the values are found
FramePattern.prototype.checkValue = function (rtype, frame) {
if (typeof this.value !== 'object' || !this.value.length) this.value = [this.value];
for (let i = 0; i < this.value.length; i++) {
if (rtype === 'data') {
if (this.valuesMatch(frame.get(), this.value[i])) {
return true;
}
} else if (rtype === 'property') {
for (let j = 0; j <= frame.values.length; j++) {
if (this.getRendererType(frame.values[i]) === 'data'
&& this.valuesMatch(frame.values[i].get(), this.value[i])) {
return true;
}
}
} else if (rtype === 'object') {
for (const prop in frame.properties) {
if (this.checkValue(this.getRendererType(frame.properties[prop]), frame.properties[prop])) {
return true;
}
}
}
}
return false;
};
FramePattern.prototype.checkRange = function (rtype, frame) {
if (typeof this.range !== 'object' || !this.range.length) this.range = [this.range];
for (let i = 0; i < this.range.length; i++) {
if (this.rangeIDsMatch(frame.range(), this.range[i])) {
return true;
}
}
return false;
};
FramePattern.prototype.checkSubjectClass = function (rtype, frame) {
if (typeof this.subjectClass !== 'object' || !this.subjectClass.length) this.subjectClass = [this.subjectClass];
const rcls = frame.subjectClass();
for (let i = 0; i < this.subjectClass.length; i++) {
if (this.classIDsMatch(this.subjectClass[i], rcls)) {
return true;
}
}
return false;
};
// eslint-disable-next-line consistent-return
FramePattern.prototype.checkFrameType = function (rtype, frame) {
if (rtype === 'object') return (this.frame_type.indexOf('object') !== -1);
if (rtype === 'data') {
if (frame.frame) {
return (this.frame_type.indexOf(frame.frame.ftype()) !== -1);
}
}
if (rtype === 'property') return false;
};
FramePattern.prototype.checkLabel = function (rtype, frame) {
if (typeof frame.getLabel !== 'function') {
// eslint-disable-next-line no-console
console.log(new Error('Rule passed to check label with broken renderer object - no getLabel'));
return false;
}
for (let i = 0; i < this.label.length; i++) {
if (this.stringMatch(this.label[i], frame.getLabel())) return true;
}
return false;
};
FramePattern.prototype.getRendererType = function (frame) {
if (frame.isProperty()) return 'property';
if (frame.isObject()) return 'object';
if (frame.isData()) return 'data';
if (frame.renderer_type) return frame.renderer_type;
// eslint-disable-next-line no-console
console.log(frame, new Error(`frame configuration passed non-renderer type ${frame.constructor.name}`));
return false;
};
module.exports = { FrameRule, FramePattern };