@earnestjs/model 0.0.0-stage → 0.2.0
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/LICENSE +674 -0
- package/LICENSE.LESSER +165 -0
- package/README.md +298 -2
- package/dist/breadth.d.ts +125 -0
- package/dist/breadth.js +150 -0
- package/dist/declarations.d.ts +201 -0
- package/dist/declarations.js +482 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +7 -0
- package/dist/m.d.ts +10 -0
- package/dist/m.js +3 -0
- package/dist/model.d.ts +297 -0
- package/dist/model.js +262 -0
- package/dist/replay.d.ts +41 -0
- package/dist/replay.js +99 -0
- package/dist/runs.d.ts +41 -0
- package/dist/runs.js +325 -0
- package/dist/search.d.ts +130 -0
- package/dist/search.js +484 -0
- package/dist/values.d.ts +296 -0
- package/dist/values.js +695 -0
- package/package.json +51 -4
package/dist/search.js
ADDED
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { breadthFirst, stateLimit, trailTo, } from "./breadth.js";
|
|
2
|
+
import { rowsOf } from "./declarations.js";
|
|
3
|
+
import { hunter } from "./runs.js";
|
|
4
|
+
import { NullFault, DivisionFault, RemainderFault, frozen, isJsonList, keyOf, toJson, } from "./values.js";
|
|
5
|
+
/**
|
|
6
|
+
* Gives the function that names a part of a model: the name it is exported under in `exports`,
|
|
7
|
+
* or `fallback`, its sentence, when it is not exported there.
|
|
8
|
+
*/
|
|
9
|
+
export const namer = (exports = {}) => {
|
|
10
|
+
const names = new Map();
|
|
11
|
+
for (const [name, exported] of Object.entries(exports)) {
|
|
12
|
+
if (!names.has(exported)) {
|
|
13
|
+
names.set(exported, name);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return (part, fallback) => names.get(part) ?? fallback;
|
|
17
|
+
};
|
|
18
|
+
const NULL_READ = "reads a value that is null";
|
|
19
|
+
const OVERFLOW = "computes a number too large to hold";
|
|
20
|
+
const BY_ZERO = "takes a division or a remainder by zero";
|
|
21
|
+
const WRONG_KIND = "uses a value of the wrong kind";
|
|
22
|
+
/**
|
|
23
|
+
* Runs part of a model and turns a fault into a value: reading a field of `null`, or using a
|
|
24
|
+
* value as something it is not. Any other error is a mistake in the model file and is thrown.
|
|
25
|
+
*/
|
|
26
|
+
export const attempt = (run) => {
|
|
27
|
+
try {
|
|
28
|
+
return { fault: null, value: run() };
|
|
29
|
+
}
|
|
30
|
+
catch (thrown) {
|
|
31
|
+
if (thrown instanceof RemainderFault || thrown instanceof DivisionFault) {
|
|
32
|
+
return { fault: BY_ZERO, value: null };
|
|
33
|
+
}
|
|
34
|
+
if (thrown instanceof NullFault) {
|
|
35
|
+
return { fault: NULL_READ, value: null };
|
|
36
|
+
}
|
|
37
|
+
if (thrown instanceof TypeError) {
|
|
38
|
+
return { fault: /\bnull\b/.test(thrown.message) ? NULL_READ : WRONG_KIND, value: null };
|
|
39
|
+
}
|
|
40
|
+
throw thrown;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const numberFault = (json) => {
|
|
44
|
+
if (typeof json === "number") {
|
|
45
|
+
if (Number.isNaN(json)) {
|
|
46
|
+
return BY_ZERO;
|
|
47
|
+
}
|
|
48
|
+
return Math.abs(json) > Number.MAX_SAFE_INTEGER ? OVERFLOW : null;
|
|
49
|
+
}
|
|
50
|
+
if (json === null || typeof json !== "object") {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const inside = isJsonList(json) ? json : Object.values(json);
|
|
54
|
+
return inside.map(numberFault).find((fault) => fault !== null) ?? null;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Applies an action and gives the next state, or the fault that stops it. Besides the faults of
|
|
58
|
+
* `attempt`, a next state that holds `NaN`, which `%` by zero gives, or a number too large for
|
|
59
|
+
* JavaScript to hold exactly is a fault.
|
|
60
|
+
*/
|
|
61
|
+
export const nextState = (action, state, input) => {
|
|
62
|
+
const tried = attempt(() => action.changes(state, input));
|
|
63
|
+
if (tried.fault !== null) {
|
|
64
|
+
return tried;
|
|
65
|
+
}
|
|
66
|
+
const fault = numberFault(toJson(tried.value));
|
|
67
|
+
return fault === null ? tried : { fault, value: null };
|
|
68
|
+
};
|
|
69
|
+
/** Every input an action is tried with, in the checker's order: the first input varies fastest. */
|
|
70
|
+
export const inputsOf = (action) => rowsOf(action.inputs);
|
|
71
|
+
const STAYS = "nothing changes the state from here, so the run can stay in it forever";
|
|
72
|
+
class Search {
|
|
73
|
+
#fields;
|
|
74
|
+
#parts;
|
|
75
|
+
#inputs;
|
|
76
|
+
#name;
|
|
77
|
+
#firstBroken = new Map();
|
|
78
|
+
#unjudged = new Set();
|
|
79
|
+
#stuck = null;
|
|
80
|
+
#outside = null;
|
|
81
|
+
#trouble = null;
|
|
82
|
+
#isCutShort = false;
|
|
83
|
+
#starts;
|
|
84
|
+
#maxStates;
|
|
85
|
+
#seenMoves = [];
|
|
86
|
+
#keepsMoves;
|
|
87
|
+
constructor(fields, parts, options, starts) {
|
|
88
|
+
this.#fields = fields;
|
|
89
|
+
this.#parts = {
|
|
90
|
+
...parts,
|
|
91
|
+
witnesses: (parts.witnesses ?? []).filter((witness) => witness.setup === options.setup),
|
|
92
|
+
};
|
|
93
|
+
this.#inputs = new Map(parts.actions.map((action) => [action, inputsOf(action)]));
|
|
94
|
+
this.#name = namer(options.exports);
|
|
95
|
+
this.#starts = starts;
|
|
96
|
+
this.#maxStates = stateLimit(options.maxStates);
|
|
97
|
+
this.#keepsMoves = (parts.eventually ?? []).length > 0;
|
|
98
|
+
}
|
|
99
|
+
key(row) {
|
|
100
|
+
return this.#fields.map(([name]) => keyOf(row[name])).join(";");
|
|
101
|
+
}
|
|
102
|
+
expand(at) {
|
|
103
|
+
const expanded = this.#expand(at.state);
|
|
104
|
+
this.#record(at, expanded);
|
|
105
|
+
if (this.#keepsMoves) {
|
|
106
|
+
for (const { move, next } of expanded.successors) {
|
|
107
|
+
this.#seenMoves.push({ from: at, taken: move, next: this.key(next) });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return expanded.successors;
|
|
111
|
+
}
|
|
112
|
+
judge(at) {
|
|
113
|
+
this.#record(at, this.#judge(at.state));
|
|
114
|
+
}
|
|
115
|
+
#judge(state) {
|
|
116
|
+
const outside = this.#fields.find(([name, declared]) => !declared.has(state[name]));
|
|
117
|
+
const found = {
|
|
118
|
+
outside: outside === undefined ? null : outside[0],
|
|
119
|
+
broken: [],
|
|
120
|
+
unjudged: [],
|
|
121
|
+
successors: [],
|
|
122
|
+
trouble: null,
|
|
123
|
+
isCutShort: outside !== undefined,
|
|
124
|
+
isEnabled: false,
|
|
125
|
+
isStuck: false,
|
|
126
|
+
};
|
|
127
|
+
if (found.outside !== null) {
|
|
128
|
+
return found;
|
|
129
|
+
}
|
|
130
|
+
for (const rule of this.#parts.rules) {
|
|
131
|
+
const held = attempt(() => rule.holds(state));
|
|
132
|
+
if (held.fault !== null) {
|
|
133
|
+
found.trouble ??= `the rule \`${this.#name(rule.part, rule.says)}\` ${held.fault}`;
|
|
134
|
+
found.unjudged.push(rule);
|
|
135
|
+
}
|
|
136
|
+
else if (!held.value) {
|
|
137
|
+
found.broken.push(rule);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return found;
|
|
141
|
+
}
|
|
142
|
+
#expand(state) {
|
|
143
|
+
const found = this.#judge(state);
|
|
144
|
+
if (found.outside !== null) {
|
|
145
|
+
return found;
|
|
146
|
+
}
|
|
147
|
+
let canTell = true;
|
|
148
|
+
for (const action of this.#parts.actions) {
|
|
149
|
+
canTell = this.#expandAction(action, state, found) && canTell;
|
|
150
|
+
}
|
|
151
|
+
if (found.isEnabled) {
|
|
152
|
+
return found;
|
|
153
|
+
}
|
|
154
|
+
let hasEnded = false;
|
|
155
|
+
for (const end of this.#parts.ends) {
|
|
156
|
+
const ended = attempt(() => end.when(state));
|
|
157
|
+
if (ended.fault === null) {
|
|
158
|
+
hasEnded ||= ended.value;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
found.trouble ??= `the end \`${this.#name(end.part, end.says)}\` ${ended.fault}`;
|
|
162
|
+
found.isCutShort = true;
|
|
163
|
+
canTell = false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
found.isStuck = canTell && !hasEnded;
|
|
167
|
+
return found;
|
|
168
|
+
}
|
|
169
|
+
#expandAction(action, state, found) {
|
|
170
|
+
const id = this.#name(action.part, action.sentence);
|
|
171
|
+
let canTell = true;
|
|
172
|
+
for (const input of this.#inputs.get(action) ?? []) {
|
|
173
|
+
const can = attempt(() => action.when(state, input));
|
|
174
|
+
if (can.fault !== null) {
|
|
175
|
+
found.trouble ??= `the condition of the action \`${id}\` ${can.fault}`;
|
|
176
|
+
found.isCutShort = true;
|
|
177
|
+
canTell = false;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (!can.value) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
found.isEnabled = true;
|
|
184
|
+
const next = nextState(action, state, input);
|
|
185
|
+
if (next.fault === null) {
|
|
186
|
+
found.successors.push({ move: { action, input }, next: next.value });
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
found.trouble ??= `the change of the action \`${id}\` ${next.fault}`;
|
|
190
|
+
found.isCutShort = true;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return canTell;
|
|
194
|
+
}
|
|
195
|
+
#record(at, expanded) {
|
|
196
|
+
if (expanded.outside !== null) {
|
|
197
|
+
this.#outside ??= { at, field: expanded.outside };
|
|
198
|
+
}
|
|
199
|
+
for (const rule of expanded.broken) {
|
|
200
|
+
if (!this.#firstBroken.has(rule)) {
|
|
201
|
+
this.#firstBroken.set(rule, at);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
for (const rule of expanded.unjudged) {
|
|
205
|
+
this.#unjudged.add(rule);
|
|
206
|
+
}
|
|
207
|
+
if (expanded.trouble !== null) {
|
|
208
|
+
this.#trouble ??= { at, message: `${expanded.trouble} in this state` };
|
|
209
|
+
}
|
|
210
|
+
this.#isCutShort ||= expanded.isCutShort;
|
|
211
|
+
if (expanded.isStuck) {
|
|
212
|
+
this.#stuck ??= at;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
#named(row) {
|
|
216
|
+
return this.#fields.map(([name]) => ({ name, value: toJson(row[name]) }));
|
|
217
|
+
}
|
|
218
|
+
#step({ action, input }, before, after) {
|
|
219
|
+
return {
|
|
220
|
+
action: this.#name(action.part, action.sentence),
|
|
221
|
+
label: action.sentence,
|
|
222
|
+
input: action.inputs.map(([name]) => ({ name, value: toJson(input[name]) })),
|
|
223
|
+
changes: this.#fields
|
|
224
|
+
.filter(([name]) => keyOf(before[name]) !== keyOf(after[name]))
|
|
225
|
+
.map(([name]) => ({
|
|
226
|
+
field: name,
|
|
227
|
+
before: toJson(before[name]),
|
|
228
|
+
after: toJson(after[name]),
|
|
229
|
+
})),
|
|
230
|
+
state: this.#named(after),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
#pathTo(target) {
|
|
234
|
+
const { start, steps } = trailTo(target);
|
|
235
|
+
return {
|
|
236
|
+
start: this.#named(start),
|
|
237
|
+
steps: steps.map(({ move, state }, index) => this.#step(move, steps[index - 1]?.state ?? start, state)),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
#kept(visits) {
|
|
241
|
+
const byKey = new Map(visits.map((visit, index) => [this.key(visit.state), index]));
|
|
242
|
+
const byVisit = new Map(visits.map((visit, index) => [visit, index]));
|
|
243
|
+
const positions = new Map(this.#parts.actions.map((action, position) => [action, position]));
|
|
244
|
+
const kept = new Map();
|
|
245
|
+
for (const { from: visit, taken, next } of this.#seenMoves) {
|
|
246
|
+
const from = byVisit.get(visit);
|
|
247
|
+
const to = byKey.get(next);
|
|
248
|
+
const action = positions.get(taken.action);
|
|
249
|
+
if (from === undefined || to === undefined || action === undefined || from === to) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const edge = `${from},${to},${action}`;
|
|
253
|
+
if (!kept.has(edge)) {
|
|
254
|
+
kept.set(edge, { from, to, action, taken });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return [...kept.values()].sort((one, other) => one.from - other.from || one.to - other.to || one.action - other.action);
|
|
258
|
+
}
|
|
259
|
+
#lassoPath(lasso, visits, edges) {
|
|
260
|
+
const rowOf = (state) => state === undefined ? {} : (visits[state]?.state ?? {});
|
|
261
|
+
const steps = [];
|
|
262
|
+
for (const [position, to] of lasso.states.entries()) {
|
|
263
|
+
const from = lasso.states[position - 1];
|
|
264
|
+
const only = lasso.actions[position] ?? null;
|
|
265
|
+
const edge = edges.find((kept) => kept.from === from && kept.to === to && (only === null || kept.action === only));
|
|
266
|
+
if (from !== undefined && edge !== undefined) {
|
|
267
|
+
steps.push(this.#step(edge.taken, rowOf(from), rowOf(to)));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return { start: this.#named(rowOf(lasso.states[0])), steps };
|
|
271
|
+
}
|
|
272
|
+
#eventually({ visits, reason }) {
|
|
273
|
+
const rules = this.#parts.eventually ?? [];
|
|
274
|
+
const unknown = (rule) => ({
|
|
275
|
+
id: this.#name(rule.part, rule.says),
|
|
276
|
+
says: rule.says,
|
|
277
|
+
verdict: "unknown",
|
|
278
|
+
path: null,
|
|
279
|
+
kind: "eventually",
|
|
280
|
+
});
|
|
281
|
+
if (rules.length === 0 || reason !== null || this.#isCutShort) {
|
|
282
|
+
return rules.map(unknown);
|
|
283
|
+
}
|
|
284
|
+
const edges = this.#kept(visits);
|
|
285
|
+
const byVisit = new Map(visits.map((visit, index) => [visit, index]));
|
|
286
|
+
const parents = visits.map((visit) => visit.parent === null ? null : (byVisit.get(visit.parent.from) ?? null));
|
|
287
|
+
const depth = [];
|
|
288
|
+
for (const parent of parents) {
|
|
289
|
+
depth.push(parent === null ? 0 : (depth[parent] ?? 0) + 1);
|
|
290
|
+
}
|
|
291
|
+
const fair = this.#parts.actions.flatMap((action, position) => action.fair === true ? [position] : []);
|
|
292
|
+
const hunt = hunter({ states: visits.length, edges, fair, parents, depth });
|
|
293
|
+
return rules.map((rule) => {
|
|
294
|
+
const startsHere = [];
|
|
295
|
+
const reached = [];
|
|
296
|
+
for (const visit of visits) {
|
|
297
|
+
const { after } = rule;
|
|
298
|
+
const starts = after === null ? null : attempt(() => after(visit.state));
|
|
299
|
+
const reaches = attempt(() => rule.reaches(visit.state));
|
|
300
|
+
const fault = starts?.fault ?? reaches.fault;
|
|
301
|
+
if (fault !== null) {
|
|
302
|
+
const id = this.#name(rule.part, rule.says);
|
|
303
|
+
this.#trouble ??= { at: visit, message: `the rule \`${id}\` ${fault} in this state` };
|
|
304
|
+
return unknown(rule);
|
|
305
|
+
}
|
|
306
|
+
startsHere.push(starts === null ? visit.parent === null : starts.value === true);
|
|
307
|
+
reached.push(reaches.value === true);
|
|
308
|
+
}
|
|
309
|
+
const lasso = hunt(startsHere, reached);
|
|
310
|
+
if (lasso === null) {
|
|
311
|
+
return { ...unknown(rule), verdict: "holds" };
|
|
312
|
+
}
|
|
313
|
+
if (lasso === "lost") {
|
|
314
|
+
return unknown(rule);
|
|
315
|
+
}
|
|
316
|
+
const broken = {
|
|
317
|
+
...unknown(rule),
|
|
318
|
+
verdict: "broken",
|
|
319
|
+
path: this.#lassoPath(lasso, visits, edges),
|
|
320
|
+
};
|
|
321
|
+
return lasso.repeatsFrom === null
|
|
322
|
+
? { ...broken, reason: STAYS }
|
|
323
|
+
: {
|
|
324
|
+
...broken,
|
|
325
|
+
reason: `steps ${lasso.repeatsFrom + 1} to ${lasso.states.length - 1} can repeat forever`,
|
|
326
|
+
repeatsFrom: lasso.repeatsFrom,
|
|
327
|
+
};
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
#play(witness, start) {
|
|
331
|
+
let state = start;
|
|
332
|
+
const steps = [];
|
|
333
|
+
const broken = (reason) => ({
|
|
334
|
+
broken: true,
|
|
335
|
+
path: { start: this.#named(start), steps },
|
|
336
|
+
reason,
|
|
337
|
+
});
|
|
338
|
+
for (const [position, { action, input }] of witness.steps.entries()) {
|
|
339
|
+
const number = position + 1;
|
|
340
|
+
const id = this.#name(action.part, action.sentence);
|
|
341
|
+
const before = state;
|
|
342
|
+
const can = attempt(() => action.when(before, input));
|
|
343
|
+
if (can.fault !== null) {
|
|
344
|
+
return broken(`step ${number}: the condition of the action \`${id}\` ${can.fault} here`);
|
|
345
|
+
}
|
|
346
|
+
if (!can.value) {
|
|
347
|
+
return broken(`step ${number}, \`${id}\`, can't happen here`);
|
|
348
|
+
}
|
|
349
|
+
const next = nextState(action, before, input);
|
|
350
|
+
if (next.fault !== null) {
|
|
351
|
+
return broken(`step ${number}: the change of the action \`${id}\` ${next.fault} here`);
|
|
352
|
+
}
|
|
353
|
+
const after = next.value;
|
|
354
|
+
steps.push(this.#step({ action, input }, before, after));
|
|
355
|
+
state = after;
|
|
356
|
+
const outside = this.#fields.find(([name, declared]) => !declared.has(after[name]));
|
|
357
|
+
if (outside !== undefined) {
|
|
358
|
+
return broken(`step ${number}, \`${id}\`, takes the field \`${outside[0]}\` out of the values it declares`);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
const last = state;
|
|
362
|
+
const expected = attempt(() => witness.expect(last));
|
|
363
|
+
if (expected.fault !== null) {
|
|
364
|
+
return broken(`what the witness expects ${expected.fault} here`);
|
|
365
|
+
}
|
|
366
|
+
return expected.value ? { broken: false } : broken("what the witness expects is false here");
|
|
367
|
+
}
|
|
368
|
+
#witness(witness) {
|
|
369
|
+
const result = { id: this.#name(witness.part, witness.says), says: witness.says };
|
|
370
|
+
for (const [position, start] of this.#starts.entries()) {
|
|
371
|
+
if (position >= this.#maxStates) {
|
|
372
|
+
return { ...result, verdict: "unknown", path: null, kind: "witness" };
|
|
373
|
+
}
|
|
374
|
+
const played = this.#play(witness, start);
|
|
375
|
+
if (played.broken) {
|
|
376
|
+
return {
|
|
377
|
+
...result,
|
|
378
|
+
verdict: "broken",
|
|
379
|
+
path: played.path,
|
|
380
|
+
kind: "witness",
|
|
381
|
+
reason: played.reason,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return { ...result, verdict: "holds", path: null, kind: "witness" };
|
|
386
|
+
}
|
|
387
|
+
#findings() {
|
|
388
|
+
const findings = [];
|
|
389
|
+
if (this.#stuck !== null) {
|
|
390
|
+
findings.push({
|
|
391
|
+
kind: "stuck",
|
|
392
|
+
message: "no action can happen here, and the model does not mark it as an end",
|
|
393
|
+
path: this.#pathTo(this.#stuck),
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
if (this.#outside !== null) {
|
|
397
|
+
findings.push({
|
|
398
|
+
kind: "out-of-range",
|
|
399
|
+
message: `the field \`${this.#outside.field}\` leaves the values it declares`,
|
|
400
|
+
path: this.#pathTo(this.#outside.at),
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
if (this.#trouble !== null) {
|
|
404
|
+
findings.push({
|
|
405
|
+
kind: "fault",
|
|
406
|
+
message: this.#trouble.message,
|
|
407
|
+
path: this.#pathTo(this.#trouble.at),
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
return findings;
|
|
411
|
+
}
|
|
412
|
+
outcome(reach) {
|
|
413
|
+
const { visits, reason } = reach;
|
|
414
|
+
const always = this.#parts.rules.map((rule) => {
|
|
415
|
+
const first = this.#firstBroken.get(rule);
|
|
416
|
+
const isJudged = reason === null && !this.#isCutShort && !this.#unjudged.has(rule);
|
|
417
|
+
const unbroken = isJudged ? "holds" : "unknown";
|
|
418
|
+
return {
|
|
419
|
+
id: this.#name(rule.part, rule.says),
|
|
420
|
+
says: rule.says,
|
|
421
|
+
verdict: first === undefined ? unbroken : "broken",
|
|
422
|
+
path: first === undefined ? null : this.#pathTo(first),
|
|
423
|
+
};
|
|
424
|
+
});
|
|
425
|
+
const eventually = this.#eventually(reach);
|
|
426
|
+
const witnesses = (this.#parts.witnesses ?? []).map((witness) => this.#witness(witness));
|
|
427
|
+
const rules = [...always, ...eventually, ...witnesses];
|
|
428
|
+
const findings = this.#findings();
|
|
429
|
+
const hasFailed = findings.length > 0 || rules.some((rule) => rule.verdict === "broken");
|
|
430
|
+
const unfailed = reason === null ? "pass" : "unfinished";
|
|
431
|
+
return {
|
|
432
|
+
status: hasFailed ? "fail" : unfailed,
|
|
433
|
+
states: visits.length,
|
|
434
|
+
rules,
|
|
435
|
+
findings,
|
|
436
|
+
reason,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Gives the start states of `model`, or of `setup`, a setup declared on it with `m.setup`: each
|
|
442
|
+
* start state of the model with the setup's fields put in, once each, in the model's order.
|
|
443
|
+
* `asker` names the function in the error thrown for a setup of another model.
|
|
444
|
+
*/
|
|
445
|
+
export const startsOf = (model, setup, asker) => {
|
|
446
|
+
if (setup === undefined) {
|
|
447
|
+
return [...model.starts()];
|
|
448
|
+
}
|
|
449
|
+
const fields = Object.keys(model.fields);
|
|
450
|
+
const key = (row) => fields.map((name) => keyOf(row[name])).join(";");
|
|
451
|
+
const chosen = (model.runners().setups ?? []).find((each) => each.part === setup);
|
|
452
|
+
if (chosen === undefined) {
|
|
453
|
+
throw new Error(`${model.name}: the setup given to ${asker} is not one declared on this model`);
|
|
454
|
+
}
|
|
455
|
+
const seen = new Set();
|
|
456
|
+
return model
|
|
457
|
+
.starts()
|
|
458
|
+
.map((start) => frozen({ ...start, ...chosen.starts }))
|
|
459
|
+
.filter((start) => {
|
|
460
|
+
const found = key(start);
|
|
461
|
+
const isNew = !seen.has(found);
|
|
462
|
+
seen.add(found);
|
|
463
|
+
return isNew;
|
|
464
|
+
});
|
|
465
|
+
};
|
|
466
|
+
/**
|
|
467
|
+
* Searches every state a model can reach, breadth first, and gives the count of distinct
|
|
468
|
+
* states, each rule's verdict with the shortest path to a state that breaks it, and the first
|
|
469
|
+
* stuck, out-of-range and fault findings. An end is asked only in a state where no action can
|
|
470
|
+
* happen. It visits states in the same order as
|
|
471
|
+
* `earnest verify`, so the two results can be compared. Pass the model file's exports as
|
|
472
|
+
* `exports` to get the same ids.
|
|
473
|
+
*/
|
|
474
|
+
export const explore = (model, options = {}) => {
|
|
475
|
+
const maxStates = stateLimit(options.maxStates);
|
|
476
|
+
const fields = Object.entries(model.fields);
|
|
477
|
+
const parts = model.runners();
|
|
478
|
+
const key = (row) => fields.map(([name]) => keyOf(row[name])).join(";");
|
|
479
|
+
const search = (starts) => {
|
|
480
|
+
const found = new Search(fields, parts, options, starts);
|
|
481
|
+
return found.outcome(breadthFirst(starts, key, maxStates, found));
|
|
482
|
+
};
|
|
483
|
+
return search(startsOf(model, options.setup, "explore"));
|
|
484
|
+
};
|