@spotto/manifest-rules 1.0.70-alpha.42 → 1.0.70-alpha.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +189 -189
- package/dist/index.js +484 -484
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,485 +1,485 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ruleItemReads = exports.ruleIssues = exports.failedLevel2 = exports.rulesFailed = exports.evaluateManifestRules = exports.judge = exports.renderMessage = exports.passesFilter = exports.compareCodeUnits = void 0;
|
|
4
|
-
// ---------------------------------------------------------------------------
|
|
5
|
-
// Order and equality
|
|
6
|
-
// ---------------------------------------------------------------------------
|
|
7
|
-
/** Plain code-unit order: what `<` gives two strings, on every runtime. */
|
|
8
|
-
const compareCodeUnits = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
9
|
-
exports.compareCodeUnits = compareCodeUnits;
|
|
10
|
-
const byName = (a, b) => (0, exports.compareCodeUnits)(a.name, b.name) || (0, exports.compareCodeUnits)(a.assetId, b.assetId);
|
|
11
|
-
const isBlank = (value) => value === undefined ||
|
|
12
|
-
value === '' ||
|
|
13
|
-
(Array.isArray(value) && value.length === 0);
|
|
14
|
-
/** A stored value as a key: equal keys are the same value of the same kind. */
|
|
15
|
-
const valueKey = (value) => JSON.stringify([typeof value, value]);
|
|
16
|
-
const whole = (n) => typeof n === 'number' && Number.isInteger(n) && n >= 1 ? n : 1;
|
|
17
|
-
const uniq = (names) => Array.from(new Set(names));
|
|
18
|
-
/**
|
|
19
|
-
* Does the subject pass the filter? A filter chooses; it does not judge. So
|
|
20
|
-
* a subject with no value in a name the filter reads is left out, before the
|
|
21
|
-
* expression runs, unless the filter tests that name's presence itself. A
|
|
22
|
-
* filter that cannot be read or evaluated leaves the subject out.
|
|
23
|
-
*/
|
|
24
|
-
function passesFilter(subject, where, ctx) {
|
|
25
|
-
if (!where || !where.trim())
|
|
26
|
-
return true;
|
|
27
|
-
const { engine } = ctx;
|
|
28
|
-
let names;
|
|
29
|
-
let guarded;
|
|
30
|
-
try {
|
|
31
|
-
names = engine.ReferencedFields(where);
|
|
32
|
-
guarded = new Set(engine.PresenceGuardedFields(where));
|
|
33
|
-
}
|
|
34
|
-
catch (_a) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
const fields = [];
|
|
38
|
-
for (const name of names) {
|
|
39
|
-
const held = subject.field(name);
|
|
40
|
-
if (held.fields.length) {
|
|
41
|
-
fields.push(...held.fields);
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
// An empty selection is an empty set to the engine, not a missing value.
|
|
45
|
-
if (held.checkboxes) {
|
|
46
|
-
fields.push(emptySet(name, engine));
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
// A blank the filter does not test for: the subject is not in scope.
|
|
50
|
-
if (!guarded.has(name))
|
|
51
|
-
return false;
|
|
52
|
-
// One it does test for is the author's to judge, so ISSET() and
|
|
53
|
-
// ISBLANK() must see it. Blank text is given to the engine as blank
|
|
54
|
-
// text, as a rule's expression is given it (`judge`): the engine works
|
|
55
|
-
// out both sides of `||` and `&&`, so `ISBLANK(Brand) || Brand ==
|
|
56
|
-
// 'Makita'` over a name left absent throws on its second half and the
|
|
57
|
-
// blank item the author asked for is left out. A blank number, date or
|
|
58
|
-
// yes/no stays absent: there is no value of theirs that reads as blank.
|
|
59
|
-
if (held.dataType === 'STRING') {
|
|
60
|
-
const blank = blankField(name, held.dataType, engine);
|
|
61
|
-
if (blank)
|
|
62
|
-
fields.push(blank);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
try {
|
|
66
|
-
return engine.EvaluateCondition(fields, where, ctx.now, ctx.timezone);
|
|
67
|
-
}
|
|
68
|
-
catch (_b) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
exports.passesFilter = passesFilter;
|
|
73
|
-
/** Is the item on a rule's side: of its type or beneath it, and through its filter? */
|
|
74
|
-
function onSide(item, side, ctx) {
|
|
75
|
-
if (!side)
|
|
76
|
-
return false;
|
|
77
|
-
const path = ctx.typePath(side.typeId);
|
|
78
|
-
if (!path || !item.typePath.startsWith(path))
|
|
79
|
-
return false;
|
|
80
|
-
return passesFilter(item, side.where, ctx);
|
|
81
|
-
}
|
|
82
|
-
/** The engine fields a message's placeholders need from a subject. */
|
|
83
|
-
function messageFields(subject, names, ctx) {
|
|
84
|
-
const fields = [];
|
|
85
|
-
for (const name of names) {
|
|
86
|
-
const held = subject.field(name);
|
|
87
|
-
if (held.fields.length)
|
|
88
|
-
fields.push(...held.fields);
|
|
89
|
-
else if (held.checkboxes)
|
|
90
|
-
fields.push(emptySet(name, ctx.engine));
|
|
91
|
-
else {
|
|
92
|
-
const blank = blankField(name, held.dataType, ctx.engine);
|
|
93
|
-
if (blank)
|
|
94
|
-
fields.push(blank);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return fields;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* An empty multi-select, in the engine's form: one blank entry, which the
|
|
101
|
-
* engine drops when it builds the set. So the name still resolves, to an
|
|
102
|
-
* empty set, where no entry at all would be an unknown name. It is the
|
|
103
|
-
* convention the readiness evaluation's own converter follows
|
|
104
|
-
* (`toCheckboxEngineFields`).
|
|
105
|
-
*/
|
|
106
|
-
function emptySet(name, engine) {
|
|
107
|
-
return engine.newFieldValue({
|
|
108
|
-
Name: name,
|
|
109
|
-
Type: engine.FieldType.Checkboxes,
|
|
110
|
-
StringValue: '',
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
/** What a missing text or yes/no value reads as. Nothing for the rest. */
|
|
114
|
-
function blankField(name, dataType, engine) {
|
|
115
|
-
if (dataType === 'STRING')
|
|
116
|
-
return engine.newFieldValue({
|
|
117
|
-
Name: name,
|
|
118
|
-
Type: engine.FieldType.String,
|
|
119
|
-
StringValue: '',
|
|
120
|
-
});
|
|
121
|
-
if (dataType === 'BOOLEAN')
|
|
122
|
-
return engine.newFieldValue({
|
|
123
|
-
Name: name,
|
|
124
|
-
Type: engine.FieldType.Bool,
|
|
125
|
-
BoolValue: false,
|
|
126
|
-
});
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* A message rendered over a subject, as a readiness rule's is: a missing
|
|
131
|
-
* number or date reads "(not set)", missing text reads as nothing.
|
|
132
|
-
*/
|
|
133
|
-
function renderMessage(subject, message, ctx) {
|
|
134
|
-
try {
|
|
135
|
-
const names = ctx.engine.TemplateFields(message);
|
|
136
|
-
return ctx.engine.FormatMessage(message, messageFields(subject, names, ctx), ctx.timezone).rendered;
|
|
137
|
-
}
|
|
138
|
-
catch (_a) {
|
|
139
|
-
return String(message !== null && message !== void 0 ? message : '');
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
exports.renderMessage = renderMessage;
|
|
143
|
-
/**
|
|
144
|
-
* Judge one subject with an expression that describes a problem, exactly as a
|
|
145
|
-
* readiness rule on a type is judged (`readiness/evaluate.ts`): it fires when
|
|
146
|
-
* the expression is true, when a number or a date it reads is missing and
|
|
147
|
-
* unguarded, and when it cannot be parsed or evaluated.
|
|
148
|
-
*/
|
|
149
|
-
function judge(subject, expression, message, ctx) {
|
|
150
|
-
const { engine } = ctx;
|
|
151
|
-
let exprFields;
|
|
152
|
-
let msgFields;
|
|
153
|
-
try {
|
|
154
|
-
exprFields = engine.ReferencedFields(expression);
|
|
155
|
-
msgFields = engine.TemplateFields(message);
|
|
156
|
-
}
|
|
157
|
-
catch (_a) {
|
|
158
|
-
return { fired: true, reason: 'Rule could not be parsed', values: {} };
|
|
159
|
-
}
|
|
160
|
-
const values = {};
|
|
161
|
-
for (const name of exprFields) {
|
|
162
|
-
const stored = subject.field(name).stored;
|
|
163
|
-
values[name] = stored === undefined ? null : stored;
|
|
164
|
-
}
|
|
165
|
-
const fields = [];
|
|
166
|
-
const missing = [];
|
|
167
|
-
for (const name of uniq([...exprFields, ...msgFields])) {
|
|
168
|
-
const held = subject.field(name);
|
|
169
|
-
if (held.fields.length) {
|
|
170
|
-
fields.push(...held.fields);
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
if (held.checkboxes) {
|
|
174
|
-
fields.push(emptySet(name, engine));
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
const blank = blankField(name, held.dataType, engine);
|
|
178
|
-
if (blank)
|
|
179
|
-
fields.push(blank);
|
|
180
|
-
// A number, a date or an unknown name the expression reads: it cannot be
|
|
181
|
-
// judged without the data. A name only the message reads is "(not set)".
|
|
182
|
-
else if (exprFields.includes(name))
|
|
183
|
-
missing.push(name);
|
|
184
|
-
}
|
|
185
|
-
if (missing.length) {
|
|
186
|
-
// ISSET()/ISBLANK() is the author's opt-out of failing closed.
|
|
187
|
-
let guarded;
|
|
188
|
-
try {
|
|
189
|
-
guarded = new Set(engine.PresenceGuardedFields(expression));
|
|
190
|
-
}
|
|
191
|
-
catch (_b) {
|
|
192
|
-
guarded = new Set();
|
|
193
|
-
}
|
|
194
|
-
const unguarded = missing
|
|
195
|
-
.filter((name) => !guarded.has(name))
|
|
196
|
-
.sort(exports.compareCodeUnits);
|
|
197
|
-
if (unguarded.length) {
|
|
198
|
-
return {
|
|
199
|
-
fired: true,
|
|
200
|
-
reason: `Missing data: ${unguarded.join(', ')}`,
|
|
201
|
-
missingFields: unguarded,
|
|
202
|
-
values,
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
let fired;
|
|
207
|
-
try {
|
|
208
|
-
fired = engine.EvaluateCondition(fields, expression, ctx.now, ctx.timezone);
|
|
209
|
-
}
|
|
210
|
-
catch (_c) {
|
|
211
|
-
return { fired: true, reason: 'Rule could not be evaluated', values };
|
|
212
|
-
}
|
|
213
|
-
if (!fired)
|
|
214
|
-
return { fired: false, values };
|
|
215
|
-
// Rendered over the very fields it was evaluated over, as a readiness
|
|
216
|
-
// rule's message is.
|
|
217
|
-
return {
|
|
218
|
-
fired: true,
|
|
219
|
-
reason: engine.FormatMessage(message, fields, ctx.timezone).rendered,
|
|
220
|
-
values,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
exports.judge = judge;
|
|
224
|
-
const failed = (base, level, reason) => (Object.assign(Object.assign({}, base), { triggered: true, passed: false, level,
|
|
225
|
-
reason }));
|
|
226
|
-
/**
|
|
227
|
-
* Evaluate a manifest's rules: one result per rule, in the rules' order.
|
|
228
|
-
*
|
|
229
|
-
* RELATION rules run as one pass in array order, and a `to` item one rule
|
|
230
|
-
* takes stays taken for every later rule: a battery serves one tool. Among a
|
|
231
|
-
* group's candidates a rule takes first those the fewest later RELATION rules
|
|
232
|
-
* could take, then by name, then by id (§3.2), so a general rule leaves the
|
|
233
|
-
* particular items for a narrower rule after it. A SUITABILITY rule takes
|
|
234
|
-
* nothing, so its place in the array changes no result.
|
|
235
|
-
*/
|
|
236
|
-
function evaluateManifestRules(input) {
|
|
237
|
-
const { rules, env } = input;
|
|
238
|
-
const ctx = input;
|
|
239
|
-
const items = input.items.slice().sort(byName);
|
|
240
|
-
const relations = rules
|
|
241
|
-
.map((rule, index) => ({ rule, index }))
|
|
242
|
-
.filter((entry) => entry.rule.kind === 'RELATION');
|
|
243
|
-
// How many LATER RELATION rules could take the item: of the rule's `to`
|
|
244
|
-
// type or beneath it, and through its `to` filter. Worked out once per
|
|
245
|
-
// item and rule, since a filter runs the engine.
|
|
246
|
-
const admits = new Map();
|
|
247
|
-
const laterTakers = (index, item) => relations.filter((later) => {
|
|
248
|
-
if (later.index <= index)
|
|
249
|
-
return false;
|
|
250
|
-
const key = `${later.index}|${item.assetId}`;
|
|
251
|
-
let admitted = admits.get(key);
|
|
252
|
-
if (admitted === undefined) {
|
|
253
|
-
admitted = onSide(item, later.rule.to, ctx);
|
|
254
|
-
admits.set(key, admitted);
|
|
255
|
-
}
|
|
256
|
-
return admitted;
|
|
257
|
-
}).length;
|
|
258
|
-
const taken = new Set();
|
|
259
|
-
return rules.map((rule, index) => {
|
|
260
|
-
var _a;
|
|
261
|
-
const base = Object.assign({ name: rule.name, kind: rule.kind }, (rule.section ? { section: rule.section } : {}));
|
|
262
|
-
const level = (_a = rule.level) !== null && _a !== void 0 ? _a : 2;
|
|
263
|
-
// The rules of a check come from the copy a device embedded. One that is
|
|
264
|
-
// not shaped as a rule fails, with a reason, and stops no other.
|
|
265
|
-
try {
|
|
266
|
-
switch (rule.kind) {
|
|
267
|
-
case 'READINESS':
|
|
268
|
-
return evaluateReadiness(rule, base, env, ctx);
|
|
269
|
-
case 'SUITABILITY':
|
|
270
|
-
return evaluateSuitability(rule, base, items, ctx);
|
|
271
|
-
case 'RELATION':
|
|
272
|
-
return evaluateRelation(rule, base, items, taken, ctx, (item) => laterTakers(index, item));
|
|
273
|
-
default:
|
|
274
|
-
return failed(base, level, `${base.name}: unknown kind of rule`);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
catch (_b) {
|
|
278
|
-
return failed(base, level, `${base.name}: the rule could not be evaluated`);
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
exports.evaluateManifestRules = evaluateManifestRules;
|
|
283
|
-
/** A READINESS rule: a readiness rule over the check instead of an asset. */
|
|
284
|
-
function evaluateReadiness(rule, base, env, ctx) {
|
|
285
|
-
if (!env)
|
|
286
|
-
return Object.assign(Object.assign({}, base), { evaluated: false });
|
|
287
|
-
const judgement = judge(env, rule.expression, rule.message, ctx);
|
|
288
|
-
if (!judgement.fired)
|
|
289
|
-
return Object.assign(Object.assign({}, base), { triggered: false, passed: true });
|
|
290
|
-
return Object.assign(Object.assign(Object.assign({}, base), { triggered: true, passed: false, level: rule.level, reason: judgement.reason }), (judgement.missingFields
|
|
291
|
-
? { missingFields: judgement.missingFields }
|
|
292
|
-
: {}));
|
|
293
|
-
}
|
|
294
|
-
/** A SUITABILITY rule: each item on its side, judged for this manifest. */
|
|
295
|
-
function evaluateSuitability(rule, base, items, ctx) {
|
|
296
|
-
var _a;
|
|
297
|
-
if (!rule.items || !ctx.typePath(rule.items.typeId))
|
|
298
|
-
return failed(base, rule.level, `${rule.name}: its type no longer exists`);
|
|
299
|
-
const judged = items.filter((item) => onSide(item, rule.items, ctx));
|
|
300
|
-
if (!judged.length)
|
|
301
|
-
return Object.assign(Object.assign({}, base), { triggered: false, passed: true });
|
|
302
|
-
const unsuitable = [];
|
|
303
|
-
for (const item of judged) {
|
|
304
|
-
const judgement = judge(item, rule.expression, rule.message, ctx);
|
|
305
|
-
if (!judgement.fired)
|
|
306
|
-
continue;
|
|
307
|
-
unsuitable.push(Object.assign({ assetId: item.assetId, assetName: item.name, values: judgement.values, reason: (_a = judgement.reason) !== null && _a !== void 0 ? _a : rule.message }, (judgement.missingFields
|
|
308
|
-
? { missingFields: judgement.missingFields }
|
|
309
|
-
: {})));
|
|
310
|
-
}
|
|
311
|
-
const passed = unsuitable.length === 0;
|
|
312
|
-
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, base), { triggered: true, passed }), (passed ? {} : { level: rule.level })), { judged: judged.map((item) => item.assetId) }), (passed ? {} : { unsuitable }));
|
|
313
|
-
}
|
|
314
|
-
/** A RELATION rule: what each `from` item needs alongside it. */
|
|
315
|
-
function evaluateRelation(rule, base, items, taken, ctx, laterTakers) {
|
|
316
|
-
var _a;
|
|
317
|
-
for (const side of ['from', 'to']) {
|
|
318
|
-
if (!rule[side] || !ctx.typePath(rule[side].typeId))
|
|
319
|
-
return failed(base, rule.level, `${rule.name}: its ${side} type no longer exists`);
|
|
320
|
-
}
|
|
321
|
-
const from = items.filter((item) => onSide(item, rule.from, ctx));
|
|
322
|
-
if (!from.length)
|
|
323
|
-
return Object.assign(Object.assign({}, base), { triggered: false, passed: true });
|
|
324
|
-
const match = (_a = rule.match) !== null && _a !== void 0 ? _a : [];
|
|
325
|
-
const every = whole(rule.from.every);
|
|
326
|
-
const needs = whole(rule.to.needs);
|
|
327
|
-
const unmatched = [];
|
|
328
|
-
// Group the `from` items by their `match` values. One with no value in a
|
|
329
|
-
// `match` field cannot be matched at all.
|
|
330
|
-
const groups = new Map();
|
|
331
|
-
for (const item of from) {
|
|
332
|
-
const stored = match.map((name) => item.field(name).stored);
|
|
333
|
-
const blanks = match.filter((_, i) => isBlank(stored[i]));
|
|
334
|
-
if (blanks.length) {
|
|
335
|
-
unmatched.push({
|
|
336
|
-
assetId: item.assetId,
|
|
337
|
-
assetName: item.name,
|
|
338
|
-
reason: `no ${blanks.join(' or ')} recorded`,
|
|
339
|
-
blank: true,
|
|
340
|
-
});
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
const id = JSON.stringify(stored.map(valueKey));
|
|
344
|
-
let group = groups.get(id);
|
|
345
|
-
if (!group) {
|
|
346
|
-
const key = {};
|
|
347
|
-
match.forEach((name, i) => {
|
|
348
|
-
key[name] = stored[i];
|
|
349
|
-
});
|
|
350
|
-
group = { key, items: [] };
|
|
351
|
-
groups.set(id, group);
|
|
352
|
-
}
|
|
353
|
-
group.items.push(item);
|
|
354
|
-
}
|
|
355
|
-
// The `to` side is the same for every group: worked out once. Run per
|
|
356
|
-
// group, its filter was parsed and evaluated for every item of the kit
|
|
357
|
-
// once per group, which is quadratic where `match` is on a value most
|
|
358
|
-
// items hold alone (a serial, a slot).
|
|
359
|
-
const toSide = items.filter((item) => onSide(item, rule.to, ctx));
|
|
360
|
-
const results = Array.from(groups.entries())
|
|
361
|
-
.sort((a, b) => (0, exports.compareCodeUnits)(a[0], b[0]))
|
|
362
|
-
.map(([, group], groupIndex) => {
|
|
363
|
-
const needed = Math.ceil(group.items.length / every) * needs;
|
|
364
|
-
const candidates = toSide
|
|
365
|
-
.filter((item) => !taken.has(item.assetId) &&
|
|
366
|
-
match.every((name) => {
|
|
367
|
-
const stored = item.field(name).stored;
|
|
368
|
-
return (!isBlank(stored) &&
|
|
369
|
-
valueKey(stored) === valueKey(group.key[name]));
|
|
370
|
-
}))
|
|
371
|
-
.map((item) => ({ item, later: laterTakers(item) }))
|
|
372
|
-
.sort((a, b) => a.later - b.later || byName(a.item, b.item))
|
|
373
|
-
.map((candidate) => candidate.item);
|
|
374
|
-
const took = candidates.slice(0, needed);
|
|
375
|
-
for (const item of took)
|
|
376
|
-
taken.add(item.assetId);
|
|
377
|
-
// A short group still takes what it finds. The `from` items it leaves
|
|
378
|
-
// uncovered, last in name order, are the unmatched ones.
|
|
379
|
-
const covered = Math.min(group.items.length, Math.floor(took.length / needs) * every);
|
|
380
|
-
for (const item of group.items.slice(covered)) {
|
|
381
|
-
unmatched.push({
|
|
382
|
-
assetId: item.assetId,
|
|
383
|
-
assetName: item.name,
|
|
384
|
-
reason: renderMessage(item, rule.message, ctx),
|
|
385
|
-
group: groupIndex,
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
return Object.assign({ key: group.key, from: group.items.map((item) => item.assetId), needed, taken: took.map((item) => item.assetId) }, (took.length < needed ? { short: true } : {}));
|
|
389
|
-
});
|
|
390
|
-
const passed = unmatched.length === 0;
|
|
391
|
-
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, base), { triggered: true, passed }), (passed ? {} : { level: rule.level })), { groups: results }), (passed ? {} : { unmatched }));
|
|
392
|
-
}
|
|
393
|
-
// ---------------------------------------------------------------------------
|
|
394
|
-
// What the results come to
|
|
395
|
-
// ---------------------------------------------------------------------------
|
|
396
|
-
/** How many rules failed: a check's `computed.rulesFailed`. */
|
|
397
|
-
const rulesFailed = (results = []) => results.filter((result) => result.passed === false).length;
|
|
398
|
-
exports.rulesFailed = rulesFailed;
|
|
399
|
-
/** The level-2 rules that failed, by name: what a check's verdict counts. */
|
|
400
|
-
const failedLevel2 = (results = []) => results
|
|
401
|
-
.filter((result) => result.passed === false && result.level === 2)
|
|
402
|
-
.map((result) => result.name);
|
|
403
|
-
exports.failedLevel2 = failedLevel2;
|
|
404
|
-
/**
|
|
405
|
-
* The issues failed rules raise (§5.2):
|
|
406
|
-
* - one per short group of a RELATION rule, its message rendered against the
|
|
407
|
-
* group's first unmatched item, and one per `from` item with no value in a
|
|
408
|
-
* `match` field;
|
|
409
|
-
* - one per unsuitable item, naming it;
|
|
410
|
-
* - one per triggered READINESS rule;
|
|
411
|
-
* - one for a rule that could not be applied at all.
|
|
412
|
-
*/
|
|
413
|
-
function ruleIssues(results = []) {
|
|
414
|
-
var _a, _b, _c, _d;
|
|
415
|
-
const issues = [];
|
|
416
|
-
for (const result of results) {
|
|
417
|
-
if (result.passed !== false)
|
|
418
|
-
continue;
|
|
419
|
-
const level = (_a = result.level) !== null && _a !== void 0 ? _a : 2;
|
|
420
|
-
const issue = (reason, extra = {}) => (Object.assign({ ruleName: result.name, kind: result.kind, level,
|
|
421
|
-
reason }, extra));
|
|
422
|
-
if (result.reason !== undefined) {
|
|
423
|
-
issues.push(issue(result.reason, result.missingFields ? { missingFields: result.missingFields } : {}));
|
|
424
|
-
continue;
|
|
425
|
-
}
|
|
426
|
-
for (const item of (_b = result.unsuitable) !== null && _b !== void 0 ? _b : []) {
|
|
427
|
-
issues.push(issue(`${item.assetName}: ${item.reason}`, Object.assign({ itemId: item.assetId, itemName: item.assetName }, (item.missingFields ? { missingFields: item.missingFields } : {}))));
|
|
428
|
-
}
|
|
429
|
-
for (const item of ((_c = result.unmatched) !== null && _c !== void 0 ? _c : []).filter((u) => u.blank)) {
|
|
430
|
-
issues.push(issue(`${item.assetName}: ${item.reason}`, {
|
|
431
|
-
itemId: item.assetId,
|
|
432
|
-
itemName: item.assetName,
|
|
433
|
-
}));
|
|
434
|
-
}
|
|
435
|
-
;
|
|
436
|
-
((_d = result.groups) !== null && _d !== void 0 ? _d : []).forEach((group, index) => {
|
|
437
|
-
var _a;
|
|
438
|
-
if (!group.short)
|
|
439
|
-
return;
|
|
440
|
-
const first = ((_a = result.unmatched) !== null && _a !== void 0 ? _a : []).find((u) => u.group === index);
|
|
441
|
-
if (first)
|
|
442
|
-
issues.push(issue(first.reason, {
|
|
443
|
-
itemId: first.assetId,
|
|
444
|
-
itemName: first.assetName,
|
|
445
|
-
}));
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
return issues;
|
|
449
|
-
}
|
|
450
|
-
exports.ruleIssues = ruleIssues;
|
|
451
|
-
/**
|
|
452
|
-
* The field names a rule reads from the ITEMS found: its `match` fields, the
|
|
453
|
-
* names in its filters, and a SUITABILITY rule's expression. A change to one
|
|
454
|
-
* of these on a member, or on a member's type, is what makes a kit's rules
|
|
455
|
-
* worth evaluating again. A READINESS rule reads the check, not the items,
|
|
456
|
-
* and gives none.
|
|
457
|
-
*/
|
|
458
|
-
function ruleItemReads(rule, engine) {
|
|
459
|
-
var _a, _b, _c, _d;
|
|
460
|
-
const names = new Set();
|
|
461
|
-
const read = (expression) => {
|
|
462
|
-
if (!expression)
|
|
463
|
-
return;
|
|
464
|
-
try {
|
|
465
|
-
for (const name of engine.ReferencedFields(expression))
|
|
466
|
-
names.add(name);
|
|
467
|
-
}
|
|
468
|
-
catch (_a) {
|
|
469
|
-
// Unreadable: reported where the rule is saved.
|
|
470
|
-
}
|
|
471
|
-
};
|
|
472
|
-
if (rule.kind === 'RELATION') {
|
|
473
|
-
for (const name of (_a = rule.match) !== null && _a !== void 0 ? _a : [])
|
|
474
|
-
names.add(name);
|
|
475
|
-
read((_b = rule.from) === null || _b === void 0 ? void 0 : _b.where);
|
|
476
|
-
read((_c = rule.to) === null || _c === void 0 ? void 0 : _c.where);
|
|
477
|
-
}
|
|
478
|
-
else if (rule.kind === 'SUITABILITY') {
|
|
479
|
-
read((_d = rule.items) === null || _d === void 0 ? void 0 : _d.where);
|
|
480
|
-
read(rule.expression);
|
|
481
|
-
}
|
|
482
|
-
return Array.from(names).sort(exports.compareCodeUnits);
|
|
483
|
-
}
|
|
484
|
-
exports.ruleItemReads = ruleItemReads;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ruleItemReads = exports.ruleIssues = exports.failedLevel2 = exports.rulesFailed = exports.evaluateManifestRules = exports.judge = exports.renderMessage = exports.passesFilter = exports.compareCodeUnits = void 0;
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Order and equality
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
/** Plain code-unit order: what `<` gives two strings, on every runtime. */
|
|
8
|
+
const compareCodeUnits = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
9
|
+
exports.compareCodeUnits = compareCodeUnits;
|
|
10
|
+
const byName = (a, b) => (0, exports.compareCodeUnits)(a.name, b.name) || (0, exports.compareCodeUnits)(a.assetId, b.assetId);
|
|
11
|
+
const isBlank = (value) => value === undefined ||
|
|
12
|
+
value === '' ||
|
|
13
|
+
(Array.isArray(value) && value.length === 0);
|
|
14
|
+
/** A stored value as a key: equal keys are the same value of the same kind. */
|
|
15
|
+
const valueKey = (value) => JSON.stringify([typeof value, value]);
|
|
16
|
+
const whole = (n) => typeof n === 'number' && Number.isInteger(n) && n >= 1 ? n : 1;
|
|
17
|
+
const uniq = (names) => Array.from(new Set(names));
|
|
18
|
+
/**
|
|
19
|
+
* Does the subject pass the filter? A filter chooses; it does not judge. So
|
|
20
|
+
* a subject with no value in a name the filter reads is left out, before the
|
|
21
|
+
* expression runs, unless the filter tests that name's presence itself. A
|
|
22
|
+
* filter that cannot be read or evaluated leaves the subject out.
|
|
23
|
+
*/
|
|
24
|
+
function passesFilter(subject, where, ctx) {
|
|
25
|
+
if (!where || !where.trim())
|
|
26
|
+
return true;
|
|
27
|
+
const { engine } = ctx;
|
|
28
|
+
let names;
|
|
29
|
+
let guarded;
|
|
30
|
+
try {
|
|
31
|
+
names = engine.ReferencedFields(where);
|
|
32
|
+
guarded = new Set(engine.PresenceGuardedFields(where));
|
|
33
|
+
}
|
|
34
|
+
catch (_a) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const fields = [];
|
|
38
|
+
for (const name of names) {
|
|
39
|
+
const held = subject.field(name);
|
|
40
|
+
if (held.fields.length) {
|
|
41
|
+
fields.push(...held.fields);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// An empty selection is an empty set to the engine, not a missing value.
|
|
45
|
+
if (held.checkboxes) {
|
|
46
|
+
fields.push(emptySet(name, engine));
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
// A blank the filter does not test for: the subject is not in scope.
|
|
50
|
+
if (!guarded.has(name))
|
|
51
|
+
return false;
|
|
52
|
+
// One it does test for is the author's to judge, so ISSET() and
|
|
53
|
+
// ISBLANK() must see it. Blank text is given to the engine as blank
|
|
54
|
+
// text, as a rule's expression is given it (`judge`): the engine works
|
|
55
|
+
// out both sides of `||` and `&&`, so `ISBLANK(Brand) || Brand ==
|
|
56
|
+
// 'Makita'` over a name left absent throws on its second half and the
|
|
57
|
+
// blank item the author asked for is left out. A blank number, date or
|
|
58
|
+
// yes/no stays absent: there is no value of theirs that reads as blank.
|
|
59
|
+
if (held.dataType === 'STRING') {
|
|
60
|
+
const blank = blankField(name, held.dataType, engine);
|
|
61
|
+
if (blank)
|
|
62
|
+
fields.push(blank);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
return engine.EvaluateCondition(fields, where, ctx.now, ctx.timezone);
|
|
67
|
+
}
|
|
68
|
+
catch (_b) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.passesFilter = passesFilter;
|
|
73
|
+
/** Is the item on a rule's side: of its type or beneath it, and through its filter? */
|
|
74
|
+
function onSide(item, side, ctx) {
|
|
75
|
+
if (!side)
|
|
76
|
+
return false;
|
|
77
|
+
const path = ctx.typePath(side.typeId);
|
|
78
|
+
if (!path || !item.typePath.startsWith(path))
|
|
79
|
+
return false;
|
|
80
|
+
return passesFilter(item, side.where, ctx);
|
|
81
|
+
}
|
|
82
|
+
/** The engine fields a message's placeholders need from a subject. */
|
|
83
|
+
function messageFields(subject, names, ctx) {
|
|
84
|
+
const fields = [];
|
|
85
|
+
for (const name of names) {
|
|
86
|
+
const held = subject.field(name);
|
|
87
|
+
if (held.fields.length)
|
|
88
|
+
fields.push(...held.fields);
|
|
89
|
+
else if (held.checkboxes)
|
|
90
|
+
fields.push(emptySet(name, ctx.engine));
|
|
91
|
+
else {
|
|
92
|
+
const blank = blankField(name, held.dataType, ctx.engine);
|
|
93
|
+
if (blank)
|
|
94
|
+
fields.push(blank);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return fields;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* An empty multi-select, in the engine's form: one blank entry, which the
|
|
101
|
+
* engine drops when it builds the set. So the name still resolves, to an
|
|
102
|
+
* empty set, where no entry at all would be an unknown name. It is the
|
|
103
|
+
* convention the readiness evaluation's own converter follows
|
|
104
|
+
* (`toCheckboxEngineFields`).
|
|
105
|
+
*/
|
|
106
|
+
function emptySet(name, engine) {
|
|
107
|
+
return engine.newFieldValue({
|
|
108
|
+
Name: name,
|
|
109
|
+
Type: engine.FieldType.Checkboxes,
|
|
110
|
+
StringValue: '',
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** What a missing text or yes/no value reads as. Nothing for the rest. */
|
|
114
|
+
function blankField(name, dataType, engine) {
|
|
115
|
+
if (dataType === 'STRING')
|
|
116
|
+
return engine.newFieldValue({
|
|
117
|
+
Name: name,
|
|
118
|
+
Type: engine.FieldType.String,
|
|
119
|
+
StringValue: '',
|
|
120
|
+
});
|
|
121
|
+
if (dataType === 'BOOLEAN')
|
|
122
|
+
return engine.newFieldValue({
|
|
123
|
+
Name: name,
|
|
124
|
+
Type: engine.FieldType.Bool,
|
|
125
|
+
BoolValue: false,
|
|
126
|
+
});
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A message rendered over a subject, as a readiness rule's is: a missing
|
|
131
|
+
* number or date reads "(not set)", missing text reads as nothing.
|
|
132
|
+
*/
|
|
133
|
+
function renderMessage(subject, message, ctx) {
|
|
134
|
+
try {
|
|
135
|
+
const names = ctx.engine.TemplateFields(message);
|
|
136
|
+
return ctx.engine.FormatMessage(message, messageFields(subject, names, ctx), ctx.timezone).rendered;
|
|
137
|
+
}
|
|
138
|
+
catch (_a) {
|
|
139
|
+
return String(message !== null && message !== void 0 ? message : '');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.renderMessage = renderMessage;
|
|
143
|
+
/**
|
|
144
|
+
* Judge one subject with an expression that describes a problem, exactly as a
|
|
145
|
+
* readiness rule on a type is judged (`readiness/evaluate.ts`): it fires when
|
|
146
|
+
* the expression is true, when a number or a date it reads is missing and
|
|
147
|
+
* unguarded, and when it cannot be parsed or evaluated.
|
|
148
|
+
*/
|
|
149
|
+
function judge(subject, expression, message, ctx) {
|
|
150
|
+
const { engine } = ctx;
|
|
151
|
+
let exprFields;
|
|
152
|
+
let msgFields;
|
|
153
|
+
try {
|
|
154
|
+
exprFields = engine.ReferencedFields(expression);
|
|
155
|
+
msgFields = engine.TemplateFields(message);
|
|
156
|
+
}
|
|
157
|
+
catch (_a) {
|
|
158
|
+
return { fired: true, reason: 'Rule could not be parsed', values: {} };
|
|
159
|
+
}
|
|
160
|
+
const values = {};
|
|
161
|
+
for (const name of exprFields) {
|
|
162
|
+
const stored = subject.field(name).stored;
|
|
163
|
+
values[name] = stored === undefined ? null : stored;
|
|
164
|
+
}
|
|
165
|
+
const fields = [];
|
|
166
|
+
const missing = [];
|
|
167
|
+
for (const name of uniq([...exprFields, ...msgFields])) {
|
|
168
|
+
const held = subject.field(name);
|
|
169
|
+
if (held.fields.length) {
|
|
170
|
+
fields.push(...held.fields);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (held.checkboxes) {
|
|
174
|
+
fields.push(emptySet(name, engine));
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const blank = blankField(name, held.dataType, engine);
|
|
178
|
+
if (blank)
|
|
179
|
+
fields.push(blank);
|
|
180
|
+
// A number, a date or an unknown name the expression reads: it cannot be
|
|
181
|
+
// judged without the data. A name only the message reads is "(not set)".
|
|
182
|
+
else if (exprFields.includes(name))
|
|
183
|
+
missing.push(name);
|
|
184
|
+
}
|
|
185
|
+
if (missing.length) {
|
|
186
|
+
// ISSET()/ISBLANK() is the author's opt-out of failing closed.
|
|
187
|
+
let guarded;
|
|
188
|
+
try {
|
|
189
|
+
guarded = new Set(engine.PresenceGuardedFields(expression));
|
|
190
|
+
}
|
|
191
|
+
catch (_b) {
|
|
192
|
+
guarded = new Set();
|
|
193
|
+
}
|
|
194
|
+
const unguarded = missing
|
|
195
|
+
.filter((name) => !guarded.has(name))
|
|
196
|
+
.sort(exports.compareCodeUnits);
|
|
197
|
+
if (unguarded.length) {
|
|
198
|
+
return {
|
|
199
|
+
fired: true,
|
|
200
|
+
reason: `Missing data: ${unguarded.join(', ')}`,
|
|
201
|
+
missingFields: unguarded,
|
|
202
|
+
values,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
let fired;
|
|
207
|
+
try {
|
|
208
|
+
fired = engine.EvaluateCondition(fields, expression, ctx.now, ctx.timezone);
|
|
209
|
+
}
|
|
210
|
+
catch (_c) {
|
|
211
|
+
return { fired: true, reason: 'Rule could not be evaluated', values };
|
|
212
|
+
}
|
|
213
|
+
if (!fired)
|
|
214
|
+
return { fired: false, values };
|
|
215
|
+
// Rendered over the very fields it was evaluated over, as a readiness
|
|
216
|
+
// rule's message is.
|
|
217
|
+
return {
|
|
218
|
+
fired: true,
|
|
219
|
+
reason: engine.FormatMessage(message, fields, ctx.timezone).rendered,
|
|
220
|
+
values,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
exports.judge = judge;
|
|
224
|
+
const failed = (base, level, reason) => (Object.assign(Object.assign({}, base), { triggered: true, passed: false, level,
|
|
225
|
+
reason }));
|
|
226
|
+
/**
|
|
227
|
+
* Evaluate a manifest's rules: one result per rule, in the rules' order.
|
|
228
|
+
*
|
|
229
|
+
* RELATION rules run as one pass in array order, and a `to` item one rule
|
|
230
|
+
* takes stays taken for every later rule: a battery serves one tool. Among a
|
|
231
|
+
* group's candidates a rule takes first those the fewest later RELATION rules
|
|
232
|
+
* could take, then by name, then by id (§3.2), so a general rule leaves the
|
|
233
|
+
* particular items for a narrower rule after it. A SUITABILITY rule takes
|
|
234
|
+
* nothing, so its place in the array changes no result.
|
|
235
|
+
*/
|
|
236
|
+
function evaluateManifestRules(input) {
|
|
237
|
+
const { rules, env } = input;
|
|
238
|
+
const ctx = input;
|
|
239
|
+
const items = input.items.slice().sort(byName);
|
|
240
|
+
const relations = rules
|
|
241
|
+
.map((rule, index) => ({ rule, index }))
|
|
242
|
+
.filter((entry) => entry.rule.kind === 'RELATION');
|
|
243
|
+
// How many LATER RELATION rules could take the item: of the rule's `to`
|
|
244
|
+
// type or beneath it, and through its `to` filter. Worked out once per
|
|
245
|
+
// item and rule, since a filter runs the engine.
|
|
246
|
+
const admits = new Map();
|
|
247
|
+
const laterTakers = (index, item) => relations.filter((later) => {
|
|
248
|
+
if (later.index <= index)
|
|
249
|
+
return false;
|
|
250
|
+
const key = `${later.index}|${item.assetId}`;
|
|
251
|
+
let admitted = admits.get(key);
|
|
252
|
+
if (admitted === undefined) {
|
|
253
|
+
admitted = onSide(item, later.rule.to, ctx);
|
|
254
|
+
admits.set(key, admitted);
|
|
255
|
+
}
|
|
256
|
+
return admitted;
|
|
257
|
+
}).length;
|
|
258
|
+
const taken = new Set();
|
|
259
|
+
return rules.map((rule, index) => {
|
|
260
|
+
var _a;
|
|
261
|
+
const base = Object.assign({ name: rule.name, kind: rule.kind }, (rule.section ? { section: rule.section } : {}));
|
|
262
|
+
const level = (_a = rule.level) !== null && _a !== void 0 ? _a : 2;
|
|
263
|
+
// The rules of a check come from the copy a device embedded. One that is
|
|
264
|
+
// not shaped as a rule fails, with a reason, and stops no other.
|
|
265
|
+
try {
|
|
266
|
+
switch (rule.kind) {
|
|
267
|
+
case 'READINESS':
|
|
268
|
+
return evaluateReadiness(rule, base, env, ctx);
|
|
269
|
+
case 'SUITABILITY':
|
|
270
|
+
return evaluateSuitability(rule, base, items, ctx);
|
|
271
|
+
case 'RELATION':
|
|
272
|
+
return evaluateRelation(rule, base, items, taken, ctx, (item) => laterTakers(index, item));
|
|
273
|
+
default:
|
|
274
|
+
return failed(base, level, `${base.name}: unknown kind of rule`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch (_b) {
|
|
278
|
+
return failed(base, level, `${base.name}: the rule could not be evaluated`);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
exports.evaluateManifestRules = evaluateManifestRules;
|
|
283
|
+
/** A READINESS rule: a readiness rule over the check instead of an asset. */
|
|
284
|
+
function evaluateReadiness(rule, base, env, ctx) {
|
|
285
|
+
if (!env)
|
|
286
|
+
return Object.assign(Object.assign({}, base), { evaluated: false });
|
|
287
|
+
const judgement = judge(env, rule.expression, rule.message, ctx);
|
|
288
|
+
if (!judgement.fired)
|
|
289
|
+
return Object.assign(Object.assign({}, base), { triggered: false, passed: true });
|
|
290
|
+
return Object.assign(Object.assign(Object.assign({}, base), { triggered: true, passed: false, level: rule.level, reason: judgement.reason }), (judgement.missingFields
|
|
291
|
+
? { missingFields: judgement.missingFields }
|
|
292
|
+
: {}));
|
|
293
|
+
}
|
|
294
|
+
/** A SUITABILITY rule: each item on its side, judged for this manifest. */
|
|
295
|
+
function evaluateSuitability(rule, base, items, ctx) {
|
|
296
|
+
var _a;
|
|
297
|
+
if (!rule.items || !ctx.typePath(rule.items.typeId))
|
|
298
|
+
return failed(base, rule.level, `${rule.name}: its type no longer exists`);
|
|
299
|
+
const judged = items.filter((item) => onSide(item, rule.items, ctx));
|
|
300
|
+
if (!judged.length)
|
|
301
|
+
return Object.assign(Object.assign({}, base), { triggered: false, passed: true });
|
|
302
|
+
const unsuitable = [];
|
|
303
|
+
for (const item of judged) {
|
|
304
|
+
const judgement = judge(item, rule.expression, rule.message, ctx);
|
|
305
|
+
if (!judgement.fired)
|
|
306
|
+
continue;
|
|
307
|
+
unsuitable.push(Object.assign({ assetId: item.assetId, assetName: item.name, values: judgement.values, reason: (_a = judgement.reason) !== null && _a !== void 0 ? _a : rule.message }, (judgement.missingFields
|
|
308
|
+
? { missingFields: judgement.missingFields }
|
|
309
|
+
: {})));
|
|
310
|
+
}
|
|
311
|
+
const passed = unsuitable.length === 0;
|
|
312
|
+
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, base), { triggered: true, passed }), (passed ? {} : { level: rule.level })), { judged: judged.map((item) => item.assetId) }), (passed ? {} : { unsuitable }));
|
|
313
|
+
}
|
|
314
|
+
/** A RELATION rule: what each `from` item needs alongside it. */
|
|
315
|
+
function evaluateRelation(rule, base, items, taken, ctx, laterTakers) {
|
|
316
|
+
var _a;
|
|
317
|
+
for (const side of ['from', 'to']) {
|
|
318
|
+
if (!rule[side] || !ctx.typePath(rule[side].typeId))
|
|
319
|
+
return failed(base, rule.level, `${rule.name}: its ${side} type no longer exists`);
|
|
320
|
+
}
|
|
321
|
+
const from = items.filter((item) => onSide(item, rule.from, ctx));
|
|
322
|
+
if (!from.length)
|
|
323
|
+
return Object.assign(Object.assign({}, base), { triggered: false, passed: true });
|
|
324
|
+
const match = (_a = rule.match) !== null && _a !== void 0 ? _a : [];
|
|
325
|
+
const every = whole(rule.from.every);
|
|
326
|
+
const needs = whole(rule.to.needs);
|
|
327
|
+
const unmatched = [];
|
|
328
|
+
// Group the `from` items by their `match` values. One with no value in a
|
|
329
|
+
// `match` field cannot be matched at all.
|
|
330
|
+
const groups = new Map();
|
|
331
|
+
for (const item of from) {
|
|
332
|
+
const stored = match.map((name) => item.field(name).stored);
|
|
333
|
+
const blanks = match.filter((_, i) => isBlank(stored[i]));
|
|
334
|
+
if (blanks.length) {
|
|
335
|
+
unmatched.push({
|
|
336
|
+
assetId: item.assetId,
|
|
337
|
+
assetName: item.name,
|
|
338
|
+
reason: `no ${blanks.join(' or ')} recorded`,
|
|
339
|
+
blank: true,
|
|
340
|
+
});
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const id = JSON.stringify(stored.map(valueKey));
|
|
344
|
+
let group = groups.get(id);
|
|
345
|
+
if (!group) {
|
|
346
|
+
const key = {};
|
|
347
|
+
match.forEach((name, i) => {
|
|
348
|
+
key[name] = stored[i];
|
|
349
|
+
});
|
|
350
|
+
group = { key, items: [] };
|
|
351
|
+
groups.set(id, group);
|
|
352
|
+
}
|
|
353
|
+
group.items.push(item);
|
|
354
|
+
}
|
|
355
|
+
// The `to` side is the same for every group: worked out once. Run per
|
|
356
|
+
// group, its filter was parsed and evaluated for every item of the kit
|
|
357
|
+
// once per group, which is quadratic where `match` is on a value most
|
|
358
|
+
// items hold alone (a serial, a slot).
|
|
359
|
+
const toSide = items.filter((item) => onSide(item, rule.to, ctx));
|
|
360
|
+
const results = Array.from(groups.entries())
|
|
361
|
+
.sort((a, b) => (0, exports.compareCodeUnits)(a[0], b[0]))
|
|
362
|
+
.map(([, group], groupIndex) => {
|
|
363
|
+
const needed = Math.ceil(group.items.length / every) * needs;
|
|
364
|
+
const candidates = toSide
|
|
365
|
+
.filter((item) => !taken.has(item.assetId) &&
|
|
366
|
+
match.every((name) => {
|
|
367
|
+
const stored = item.field(name).stored;
|
|
368
|
+
return (!isBlank(stored) &&
|
|
369
|
+
valueKey(stored) === valueKey(group.key[name]));
|
|
370
|
+
}))
|
|
371
|
+
.map((item) => ({ item, later: laterTakers(item) }))
|
|
372
|
+
.sort((a, b) => a.later - b.later || byName(a.item, b.item))
|
|
373
|
+
.map((candidate) => candidate.item);
|
|
374
|
+
const took = candidates.slice(0, needed);
|
|
375
|
+
for (const item of took)
|
|
376
|
+
taken.add(item.assetId);
|
|
377
|
+
// A short group still takes what it finds. The `from` items it leaves
|
|
378
|
+
// uncovered, last in name order, are the unmatched ones.
|
|
379
|
+
const covered = Math.min(group.items.length, Math.floor(took.length / needs) * every);
|
|
380
|
+
for (const item of group.items.slice(covered)) {
|
|
381
|
+
unmatched.push({
|
|
382
|
+
assetId: item.assetId,
|
|
383
|
+
assetName: item.name,
|
|
384
|
+
reason: renderMessage(item, rule.message, ctx),
|
|
385
|
+
group: groupIndex,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
return Object.assign({ key: group.key, from: group.items.map((item) => item.assetId), needed, taken: took.map((item) => item.assetId) }, (took.length < needed ? { short: true } : {}));
|
|
389
|
+
});
|
|
390
|
+
const passed = unmatched.length === 0;
|
|
391
|
+
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, base), { triggered: true, passed }), (passed ? {} : { level: rule.level })), { groups: results }), (passed ? {} : { unmatched }));
|
|
392
|
+
}
|
|
393
|
+
// ---------------------------------------------------------------------------
|
|
394
|
+
// What the results come to
|
|
395
|
+
// ---------------------------------------------------------------------------
|
|
396
|
+
/** How many rules failed: a check's `computed.rulesFailed`. */
|
|
397
|
+
const rulesFailed = (results = []) => results.filter((result) => result.passed === false).length;
|
|
398
|
+
exports.rulesFailed = rulesFailed;
|
|
399
|
+
/** The level-2 rules that failed, by name: what a check's verdict counts. */
|
|
400
|
+
const failedLevel2 = (results = []) => results
|
|
401
|
+
.filter((result) => result.passed === false && result.level === 2)
|
|
402
|
+
.map((result) => result.name);
|
|
403
|
+
exports.failedLevel2 = failedLevel2;
|
|
404
|
+
/**
|
|
405
|
+
* The issues failed rules raise (§5.2):
|
|
406
|
+
* - one per short group of a RELATION rule, its message rendered against the
|
|
407
|
+
* group's first unmatched item, and one per `from` item with no value in a
|
|
408
|
+
* `match` field;
|
|
409
|
+
* - one per unsuitable item, naming it;
|
|
410
|
+
* - one per triggered READINESS rule;
|
|
411
|
+
* - one for a rule that could not be applied at all.
|
|
412
|
+
*/
|
|
413
|
+
function ruleIssues(results = []) {
|
|
414
|
+
var _a, _b, _c, _d;
|
|
415
|
+
const issues = [];
|
|
416
|
+
for (const result of results) {
|
|
417
|
+
if (result.passed !== false)
|
|
418
|
+
continue;
|
|
419
|
+
const level = (_a = result.level) !== null && _a !== void 0 ? _a : 2;
|
|
420
|
+
const issue = (reason, extra = {}) => (Object.assign({ ruleName: result.name, kind: result.kind, level,
|
|
421
|
+
reason }, extra));
|
|
422
|
+
if (result.reason !== undefined) {
|
|
423
|
+
issues.push(issue(result.reason, result.missingFields ? { missingFields: result.missingFields } : {}));
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
for (const item of (_b = result.unsuitable) !== null && _b !== void 0 ? _b : []) {
|
|
427
|
+
issues.push(issue(`${item.assetName}: ${item.reason}`, Object.assign({ itemId: item.assetId, itemName: item.assetName }, (item.missingFields ? { missingFields: item.missingFields } : {}))));
|
|
428
|
+
}
|
|
429
|
+
for (const item of ((_c = result.unmatched) !== null && _c !== void 0 ? _c : []).filter((u) => u.blank)) {
|
|
430
|
+
issues.push(issue(`${item.assetName}: ${item.reason}`, {
|
|
431
|
+
itemId: item.assetId,
|
|
432
|
+
itemName: item.assetName,
|
|
433
|
+
}));
|
|
434
|
+
}
|
|
435
|
+
;
|
|
436
|
+
((_d = result.groups) !== null && _d !== void 0 ? _d : []).forEach((group, index) => {
|
|
437
|
+
var _a;
|
|
438
|
+
if (!group.short)
|
|
439
|
+
return;
|
|
440
|
+
const first = ((_a = result.unmatched) !== null && _a !== void 0 ? _a : []).find((u) => u.group === index);
|
|
441
|
+
if (first)
|
|
442
|
+
issues.push(issue(first.reason, {
|
|
443
|
+
itemId: first.assetId,
|
|
444
|
+
itemName: first.assetName,
|
|
445
|
+
}));
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
return issues;
|
|
449
|
+
}
|
|
450
|
+
exports.ruleIssues = ruleIssues;
|
|
451
|
+
/**
|
|
452
|
+
* The field names a rule reads from the ITEMS found: its `match` fields, the
|
|
453
|
+
* names in its filters, and a SUITABILITY rule's expression. A change to one
|
|
454
|
+
* of these on a member, or on a member's type, is what makes a kit's rules
|
|
455
|
+
* worth evaluating again. A READINESS rule reads the check, not the items,
|
|
456
|
+
* and gives none.
|
|
457
|
+
*/
|
|
458
|
+
function ruleItemReads(rule, engine) {
|
|
459
|
+
var _a, _b, _c, _d;
|
|
460
|
+
const names = new Set();
|
|
461
|
+
const read = (expression) => {
|
|
462
|
+
if (!expression)
|
|
463
|
+
return;
|
|
464
|
+
try {
|
|
465
|
+
for (const name of engine.ReferencedFields(expression))
|
|
466
|
+
names.add(name);
|
|
467
|
+
}
|
|
468
|
+
catch (_a) {
|
|
469
|
+
// Unreadable: reported where the rule is saved.
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
if (rule.kind === 'RELATION') {
|
|
473
|
+
for (const name of (_a = rule.match) !== null && _a !== void 0 ? _a : [])
|
|
474
|
+
names.add(name);
|
|
475
|
+
read((_b = rule.from) === null || _b === void 0 ? void 0 : _b.where);
|
|
476
|
+
read((_c = rule.to) === null || _c === void 0 ? void 0 : _c.where);
|
|
477
|
+
}
|
|
478
|
+
else if (rule.kind === 'SUITABILITY') {
|
|
479
|
+
read((_d = rule.items) === null || _d === void 0 ? void 0 : _d.where);
|
|
480
|
+
read(rule.expression);
|
|
481
|
+
}
|
|
482
|
+
return Array.from(names).sort(exports.compareCodeUnits);
|
|
483
|
+
}
|
|
484
|
+
exports.ruleItemReads = ruleItemReads;
|
|
485
485
|
//# sourceMappingURL=index.js.map
|