@polintpro/proposit-core 0.2.10 → 0.2.11

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.
Files changed (37) hide show
  1. package/dist/lib/core/{ChangeCollector.d.ts → changeCollector.d.ts} +1 -1
  2. package/dist/lib/core/{ChangeCollector.d.ts.map → changeCollector.d.ts.map} +1 -1
  3. package/dist/lib/core/{ChangeCollector.js → changeCollector.js} +1 -1
  4. package/dist/lib/core/{ChangeCollector.js.map → changeCollector.js.map} +1 -1
  5. package/package.json +2 -2
  6. package/dist/lib/core/PremiseManager.d.ts +0 -187
  7. package/dist/lib/core/PremiseManager.d.ts.map +0 -1
  8. package/dist/lib/core/PremiseManager.js +0 -873
  9. package/dist/lib/core/PremiseManager.js.map +0 -1
  10. package/dist/lib/core/evaluation/shared.d.ts +0 -20
  11. package/dist/lib/core/evaluation/shared.d.ts.map +0 -1
  12. package/dist/lib/core/evaluation/shared.js +0 -55
  13. package/dist/lib/core/evaluation/shared.js.map +0 -1
  14. package/dist/lib/core/import.d.ts +0 -14
  15. package/dist/lib/core/import.d.ts.map +0 -1
  16. package/dist/lib/core/import.js +0 -217
  17. package/dist/lib/core/import.js.map +0 -1
  18. package/dist/lib/utils.d.ts +0 -17
  19. package/dist/lib/utils.d.ts.map +0 -1
  20. package/dist/lib/utils.js +0 -33
  21. package/dist/lib/utils.js.map +0 -1
  22. /package/dist/lib/core/{ArgumentEngine.d.ts → argumentEngine.d.ts} +0 -0
  23. /package/dist/lib/core/{ArgumentEngine.d.ts.map → argumentEngine.d.ts.map} +0 -0
  24. /package/dist/lib/core/{ArgumentEngine.js → argumentEngine.js} +0 -0
  25. /package/dist/lib/core/{ArgumentEngine.js.map → argumentEngine.js.map} +0 -0
  26. /package/dist/lib/core/{ExpressionManager.d.ts → expressionManager.d.ts} +0 -0
  27. /package/dist/lib/core/{ExpressionManager.d.ts.map → expressionManager.d.ts.map} +0 -0
  28. /package/dist/lib/core/{ExpressionManager.js → expressionManager.js} +0 -0
  29. /package/dist/lib/core/{ExpressionManager.js.map → expressionManager.js.map} +0 -0
  30. /package/dist/lib/core/{PremiseEngine.d.ts → premiseEngine.d.ts} +0 -0
  31. /package/dist/lib/core/{PremiseEngine.d.ts.map → premiseEngine.d.ts.map} +0 -0
  32. /package/dist/lib/core/{PremiseEngine.js → premiseEngine.js} +0 -0
  33. /package/dist/lib/core/{PremiseEngine.js.map → premiseEngine.js.map} +0 -0
  34. /package/dist/lib/core/{VariableManager.d.ts → variableManager.d.ts} +0 -0
  35. /package/dist/lib/core/{VariableManager.d.ts.map → variableManager.d.ts.map} +0 -0
  36. /package/dist/lib/core/{VariableManager.js → variableManager.js} +0 -0
  37. /package/dist/lib/core/{VariableManager.js.map → variableManager.js.map} +0 -0
@@ -1,873 +0,0 @@
1
- import { DefaultMap } from "../utils.js";
2
- import { sortedCopyById, sortedUnique } from "../utils/collections.js";
3
- import { buildDirectionalVacuity, kleeneAnd, kleeneIff, kleeneImplies, kleeneNot, kleeneOr, makeErrorIssue, makeValidationResult, } from "./evaluation/shared.js";
4
- import { DEFAULT_CHECKSUM_CONFIG } from "../consts.js";
5
- import { ChangeCollector } from "./ChangeCollector.js";
6
- import { computeHash, entityChecksum } from "./checksum.js";
7
- import { ExpressionManager } from "./ExpressionManager.js";
8
- import { VariableManager } from "./VariableManager.js";
9
- export class PremiseManager {
10
- id;
11
- extras;
12
- rootExpressionId;
13
- variables;
14
- expressions;
15
- expressionsByVariableId;
16
- argument;
17
- checksumConfig;
18
- checksumDirty = true;
19
- cachedChecksum;
20
- constructor(id, argument, variables, extras, checksumConfig, positionConfig) {
21
- this.id = id;
22
- this.argument = argument;
23
- this.extras = extras ?? {};
24
- this.checksumConfig = checksumConfig;
25
- this.rootExpressionId = undefined;
26
- this.variables = variables;
27
- this.expressions = new ExpressionManager([], positionConfig);
28
- this.expressionsByVariableId = new DefaultMap(() => new Set());
29
- }
30
- /**
31
- * Deletes all expressions that reference the given variable ID,
32
- * including their subtrees. Operator collapse runs after each removal.
33
- * Returns all removed expressions in the changeset.
34
- */
35
- deleteExpressionsUsingVariable(variableId) {
36
- const expressionIds = this.expressionsByVariableId.get(variableId);
37
- if (expressionIds.size === 0) {
38
- return { result: [], changes: {} };
39
- }
40
- const collector = new ChangeCollector();
41
- // Copy the set since removeExpression mutates expressionsByVariableId
42
- const removed = [];
43
- for (const exprId of [...expressionIds]) {
44
- // The expression may already have been removed as part of a
45
- // prior subtree deletion or operator collapse in this loop.
46
- if (!this.expressions.getExpression(exprId))
47
- continue;
48
- const { result, changes } = this.removeExpression(exprId, true);
49
- if (result)
50
- removed.push(result);
51
- if (changes.expressions) {
52
- for (const e of changes.expressions.removed) {
53
- collector.removedExpression(e);
54
- }
55
- }
56
- }
57
- // Expressions in the collector already have checksums attached
58
- // (from removeExpression's attachChangesetChecksums).
59
- return {
60
- result: removed,
61
- changes: collector.toChangeset(),
62
- };
63
- }
64
- /**
65
- * Adds an expression to this premise's tree.
66
- *
67
- * If the expression has `parentId: null` it becomes the root; only one
68
- * root is permitted per premise. If `parentId` is non-null the parent
69
- * must already exist within this premise.
70
- *
71
- * All other structural rules (`implies`/`iff` root-only, child limits,
72
- * position uniqueness) are enforced by the underlying `ExpressionManager`.
73
- *
74
- * @throws If the premise already has a root expression and this one is also a root.
75
- * @throws If the expression's parent does not exist in this premise.
76
- * @throws If the expression is a variable reference and the variable has not been registered.
77
- * @throws If the expression does not belong to this argument.
78
- */
79
- addExpression(expression) {
80
- this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
81
- if (expression.type === "variable" &&
82
- !this.variables.hasVariable(expression.variableId)) {
83
- throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
84
- }
85
- if (expression.parentId === null) {
86
- if (this.rootExpressionId !== undefined) {
87
- throw new Error(`Premise "${this.id}" already has a root expression.`);
88
- }
89
- }
90
- else {
91
- if (!this.expressions.getExpression(expression.parentId)) {
92
- throw new Error(`Parent expression "${expression.parentId}" does not exist in this premise.`);
93
- }
94
- }
95
- const collector = new ChangeCollector();
96
- this.expressions.setCollector(collector);
97
- try {
98
- // Delegate structural validation (operator type checks, position
99
- // uniqueness, child limits) to ExpressionManager.
100
- this.expressions.addExpression(expression);
101
- if (expression.parentId === null) {
102
- this.rootExpressionId = expression.id;
103
- }
104
- if (expression.type === "variable") {
105
- this.expressionsByVariableId
106
- .get(expression.variableId)
107
- .add(expression.id);
108
- }
109
- this.markDirty();
110
- return {
111
- result: this.attachExpressionChecksum({ ...expression }),
112
- changes: this.attachChangesetChecksums(collector.toChangeset()),
113
- };
114
- }
115
- finally {
116
- this.expressions.setCollector(null);
117
- }
118
- }
119
- /**
120
- * Adds an expression as the last child of the given parent, with
121
- * position computed automatically.
122
- *
123
- * If `parentId` is `null`, the expression becomes the root.
124
- *
125
- * @throws If the premise already has a root and parentId is null.
126
- * @throws If the expression does not belong to this argument.
127
- * @throws If the expression is a variable reference and the variable has not been registered.
128
- */
129
- appendExpression(parentId, expression) {
130
- this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
131
- if (expression.type === "variable" &&
132
- !this.variables.hasVariable(expression.variableId)) {
133
- throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
134
- }
135
- if (parentId === null) {
136
- if (this.rootExpressionId !== undefined) {
137
- throw new Error(`Premise "${this.id}" already has a root expression.`);
138
- }
139
- }
140
- else {
141
- if (!this.expressions.getExpression(parentId)) {
142
- throw new Error(`Parent expression "${parentId}" does not exist in this premise.`);
143
- }
144
- }
145
- const collector = new ChangeCollector();
146
- this.expressions.setCollector(collector);
147
- try {
148
- this.expressions.appendExpression(parentId, expression);
149
- if (parentId === null) {
150
- this.syncRootExpressionId();
151
- }
152
- if (expression.type === "variable") {
153
- this.expressionsByVariableId
154
- .get(expression.variableId)
155
- .add(expression.id);
156
- }
157
- this.markDirty();
158
- const stored = this.expressions.getExpression(expression.id);
159
- return {
160
- result: this.attachExpressionChecksum({ ...stored }),
161
- changes: this.attachChangesetChecksums(collector.toChangeset()),
162
- };
163
- }
164
- finally {
165
- this.expressions.setCollector(null);
166
- }
167
- }
168
- /**
169
- * Adds an expression immediately before or after an existing sibling,
170
- * with position computed automatically.
171
- *
172
- * @throws If the sibling does not exist in this premise.
173
- * @throws If the expression does not belong to this argument.
174
- * @throws If the expression is a variable reference and the variable has not been registered.
175
- */
176
- addExpressionRelative(siblingId, relativePosition, expression) {
177
- this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
178
- if (expression.type === "variable" &&
179
- !this.variables.hasVariable(expression.variableId)) {
180
- throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
181
- }
182
- if (!this.expressions.getExpression(siblingId)) {
183
- throw new Error(`Expression "${siblingId}" not found in this premise.`);
184
- }
185
- const collector = new ChangeCollector();
186
- this.expressions.setCollector(collector);
187
- try {
188
- this.expressions.addExpressionRelative(siblingId, relativePosition, expression);
189
- if (expression.type === "variable") {
190
- this.expressionsByVariableId
191
- .get(expression.variableId)
192
- .add(expression.id);
193
- }
194
- this.markDirty();
195
- const stored = this.expressions.getExpression(expression.id);
196
- return {
197
- result: this.attachExpressionChecksum({ ...stored }),
198
- changes: this.attachChangesetChecksums(collector.toChangeset()),
199
- };
200
- }
201
- finally {
202
- this.expressions.setCollector(null);
203
- }
204
- }
205
- /**
206
- * Updates mutable fields of an existing expression in this premise.
207
- *
208
- * Only `position`, `variableId`, and `operator` may be updated. Structural
209
- * fields (`id`, `parentId`, `type`, `argumentId`, `argumentVersion`,
210
- * `checksum`) are forbidden — enforced by the underlying
211
- * `ExpressionManager`.
212
- *
213
- * If `variableId` changes, the internal `expressionsByVariableId` index is
214
- * updated so that cascade deletion (`deleteExpressionsUsingVariable`) stays
215
- * correct.
216
- *
217
- * @throws If the expression does not exist in this premise.
218
- * @throws If `variableId` references a non-existent variable.
219
- */
220
- updateExpression(expressionId, updates) {
221
- const existing = this.expressions.getExpression(expressionId);
222
- if (!existing) {
223
- throw new Error(`Expression "${expressionId}" not found in premise "${this.id}".`);
224
- }
225
- if (updates.variableId !== undefined) {
226
- if (!this.variables.hasVariable(updates.variableId)) {
227
- throw new Error(`Variable expression "${expressionId}" references non-existent variable "${updates.variableId}".`);
228
- }
229
- }
230
- const collector = new ChangeCollector();
231
- this.expressions.setCollector(collector);
232
- try {
233
- const oldVariableId = existing.type === "variable" ? existing.variableId : undefined;
234
- const updated = this.expressions.updateExpression(expressionId, updates);
235
- if (updates.variableId !== undefined &&
236
- oldVariableId !== undefined &&
237
- oldVariableId !== updates.variableId) {
238
- this.expressionsByVariableId
239
- .get(oldVariableId)
240
- ?.delete(expressionId);
241
- this.expressionsByVariableId
242
- .get(updates.variableId)
243
- .add(expressionId);
244
- }
245
- const changeset = collector.toChangeset();
246
- if (changeset.expressions !== undefined) {
247
- this.markDirty();
248
- }
249
- return {
250
- result: this.attachExpressionChecksum({
251
- ...updated,
252
- }),
253
- changes: this.attachChangesetChecksums(changeset),
254
- };
255
- }
256
- finally {
257
- this.expressions.setCollector(null);
258
- }
259
- }
260
- /**
261
- * Removes an expression and its entire descendant subtree, then collapses
262
- * any ancestor operators with fewer than two children (same semantics as
263
- * before). Returns the removed root expression, or `undefined` if not
264
- * found.
265
- *
266
- * `rootExpressionId` is recomputed after every removal because operator
267
- * collapse can silently promote a new expression into the root slot.
268
- */
269
- removeExpression(expressionId, deleteSubtree) {
270
- // Snapshot the expression before removal (for result).
271
- const snapshot = this.expressions.getExpression(expressionId);
272
- const collector = new ChangeCollector();
273
- this.expressions.setCollector(collector);
274
- try {
275
- if (!snapshot) {
276
- return {
277
- result: undefined,
278
- changes: collector.toChangeset(),
279
- };
280
- }
281
- if (deleteSubtree) {
282
- // Snapshot the subtree before deletion so we can clean up
283
- // expressionsByVariableId for cascade-deleted descendants — they are
284
- // not individually surfaced by ExpressionManager.removeExpression.
285
- const subtree = this.collectSubtree(expressionId);
286
- this.expressions.removeExpression(expressionId, true);
287
- for (const expr of subtree) {
288
- if (expr.type === "variable") {
289
- this.expressionsByVariableId
290
- .get(expr.variableId)
291
- ?.delete(expr.id);
292
- }
293
- }
294
- }
295
- else {
296
- // Only clean up expressionsByVariableId for the removed
297
- // expression itself — children survive promotion.
298
- if (snapshot.type === "variable") {
299
- this.expressionsByVariableId
300
- .get(snapshot.variableId)
301
- ?.delete(snapshot.id);
302
- }
303
- this.expressions.removeExpression(expressionId, false);
304
- }
305
- this.syncRootExpressionId();
306
- this.markDirty();
307
- return {
308
- result: this.attachExpressionChecksum({ ...snapshot }),
309
- changes: this.attachChangesetChecksums(collector.toChangeset()),
310
- };
311
- }
312
- finally {
313
- this.expressions.setCollector(null);
314
- }
315
- }
316
- /**
317
- * Splices a new expression between existing nodes in the tree. The new
318
- * expression inherits the tree slot of the anchor node
319
- * (`leftNodeId ?? rightNodeId`).
320
- *
321
- * `rootExpressionId` is recomputed after every insertion because the
322
- * anchor may have been the root.
323
- *
324
- * See `ArgumentEngine.insertExpression` for the full contract; the same
325
- * rules apply here.
326
- *
327
- * @throws If the expression does not belong to this argument.
328
- * @throws If the expression is a variable reference and the variable has not been registered.
329
- */
330
- insertExpression(expression, leftNodeId, rightNodeId) {
331
- this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
332
- if (expression.type === "variable" &&
333
- !this.variables.hasVariable(expression.variableId)) {
334
- throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
335
- }
336
- const collector = new ChangeCollector();
337
- this.expressions.setCollector(collector);
338
- try {
339
- this.expressions.insertExpression(expression, leftNodeId, rightNodeId);
340
- if (expression.type === "variable") {
341
- this.expressionsByVariableId
342
- .get(expression.variableId)
343
- .add(expression.id);
344
- }
345
- this.syncRootExpressionId();
346
- this.markDirty();
347
- const stored = this.expressions.getExpression(expression.id);
348
- return {
349
- result: this.attachExpressionChecksum({ ...stored }),
350
- changes: this.attachChangesetChecksums(collector.toChangeset()),
351
- };
352
- }
353
- finally {
354
- this.expressions.setCollector(null);
355
- }
356
- }
357
- /**
358
- * Returns an expression by ID, or `undefined` if not found in this
359
- * premise.
360
- */
361
- getExpression(id) {
362
- const expr = this.expressions.getExpression(id);
363
- if (!expr)
364
- return undefined;
365
- return this.attachExpressionChecksum(expr);
366
- }
367
- getId() {
368
- return this.id;
369
- }
370
- getExtras() {
371
- return { ...this.extras };
372
- }
373
- setExtras(extras) {
374
- this.extras = { ...extras };
375
- this.markDirty();
376
- return { result: { ...this.extras }, changes: {} };
377
- }
378
- getRootExpressionId() {
379
- return this.rootExpressionId;
380
- }
381
- getRootExpression() {
382
- if (this.rootExpressionId === undefined) {
383
- return undefined;
384
- }
385
- const expr = this.expressions.getExpression(this.rootExpressionId);
386
- if (!expr)
387
- return undefined;
388
- return this.attachExpressionChecksum(expr);
389
- }
390
- /**
391
- * Returns all argument-level variables (from the shared VariableManager)
392
- * sorted by ID. Since the VariableManager is shared across all premises,
393
- * this returns every registered variable — not just those referenced by
394
- * expressions in this premise.
395
- */
396
- getVariables() {
397
- return sortedCopyById(this.variables.toArray());
398
- }
399
- getExpressions() {
400
- const fields = this.checksumConfig?.expressionFields ??
401
- DEFAULT_CHECKSUM_CONFIG.expressionFields;
402
- return sortedCopyById(this.expressions.toArray().map((e) => ({
403
- ...e,
404
- checksum: entityChecksum(e, fields),
405
- })));
406
- }
407
- getChildExpressions(parentId) {
408
- return this.expressions
409
- .getChildExpressions(parentId)
410
- .map((expr) => this.attachExpressionChecksum(expr));
411
- }
412
- /**
413
- * Returns `true` if the root expression is an `implies` or `iff` operator,
414
- * meaning this premise expresses a logical inference relationship.
415
- */
416
- isInference() {
417
- const root = this.getRootExpression();
418
- return (root?.type === "operator" &&
419
- (root.operator === "implies" || root.operator === "iff"));
420
- }
421
- /**
422
- * Returns `true` if this premise does not have an inference operator at its
423
- * root (i.e. it is a constraint premise). Equivalent to `!isInference()`.
424
- */
425
- isConstraint() {
426
- return !this.isInference();
427
- }
428
- validateEvaluability() {
429
- const issues = [];
430
- const roots = this.expressions.getChildExpressions(null);
431
- if (this.expressions.toArray().length === 0) {
432
- issues.push(makeErrorIssue({
433
- code: "PREMISE_EMPTY",
434
- message: `Premise "${this.id}" has no expressions to evaluate.`,
435
- premiseId: this.id,
436
- }));
437
- return makeValidationResult(issues);
438
- }
439
- if (roots.length === 0) {
440
- issues.push(makeErrorIssue({
441
- code: "PREMISE_ROOT_MISSING",
442
- message: `Premise "${this.id}" has expressions but no root expression.`,
443
- premiseId: this.id,
444
- }));
445
- }
446
- if (this.rootExpressionId === undefined) {
447
- issues.push(makeErrorIssue({
448
- code: "PREMISE_ROOT_MISSING",
449
- message: `Premise "${this.id}" does not have rootExpressionId set.`,
450
- premiseId: this.id,
451
- }));
452
- }
453
- else if (!this.expressions.getExpression(this.rootExpressionId)) {
454
- issues.push(makeErrorIssue({
455
- code: "PREMISE_ROOT_MISMATCH",
456
- message: `Premise "${this.id}" rootExpressionId "${this.rootExpressionId}" does not exist.`,
457
- premiseId: this.id,
458
- expressionId: this.rootExpressionId,
459
- }));
460
- }
461
- else if (roots[0] && roots[0].id !== this.rootExpressionId) {
462
- issues.push(makeErrorIssue({
463
- code: "PREMISE_ROOT_MISMATCH",
464
- message: `Premise "${this.id}" rootExpressionId "${this.rootExpressionId}" does not match actual root "${roots[0].id}".`,
465
- premiseId: this.id,
466
- expressionId: this.rootExpressionId,
467
- }));
468
- }
469
- for (const expr of this.expressions.toArray()) {
470
- if (expr.type === "variable" &&
471
- !this.variables.hasVariable(expr.variableId)) {
472
- issues.push(makeErrorIssue({
473
- code: "EXPR_VARIABLE_UNDECLARED",
474
- message: `Expression "${expr.id}" references undeclared variable "${expr.variableId}".`,
475
- premiseId: this.id,
476
- expressionId: expr.id,
477
- variableId: expr.variableId,
478
- }));
479
- }
480
- if (expr.type !== "operator" && expr.type !== "formula") {
481
- continue;
482
- }
483
- const children = this.expressions.getChildExpressions(expr.id);
484
- if (expr.type === "formula") {
485
- if (children.length !== 1) {
486
- issues.push(makeErrorIssue({
487
- code: "EXPR_CHILD_COUNT_INVALID",
488
- message: `Formula expression "${expr.id}" must have exactly 1 child; found ${children.length}.`,
489
- premiseId: this.id,
490
- expressionId: expr.id,
491
- }));
492
- }
493
- continue;
494
- }
495
- if (expr.operator === "not" && children.length !== 1) {
496
- issues.push(makeErrorIssue({
497
- code: "EXPR_CHILD_COUNT_INVALID",
498
- message: `Operator "${expr.id}" (not) must have exactly 1 child; found ${children.length}.`,
499
- premiseId: this.id,
500
- expressionId: expr.id,
501
- }));
502
- }
503
- if ((expr.operator === "implies" || expr.operator === "iff") &&
504
- children.length !== 2) {
505
- issues.push(makeErrorIssue({
506
- code: "EXPR_CHILD_COUNT_INVALID",
507
- message: `Operator "${expr.id}" (${expr.operator}) must have exactly 2 children; found ${children.length}.`,
508
- premiseId: this.id,
509
- expressionId: expr.id,
510
- }));
511
- }
512
- if ((expr.operator === "and" || expr.operator === "or") &&
513
- children.length < 2) {
514
- issues.push(makeErrorIssue({
515
- code: "EXPR_CHILD_COUNT_INVALID",
516
- message: `Operator "${expr.id}" (${expr.operator}) must have at least 2 children; found ${children.length}.`,
517
- premiseId: this.id,
518
- expressionId: expr.id,
519
- }));
520
- }
521
- if (expr.operator === "implies" || expr.operator === "iff") {
522
- const childPositions = new Set(children.map((child) => child.position));
523
- if (!childPositions.has(0) || !childPositions.has(1)) {
524
- issues.push(makeErrorIssue({
525
- code: "EXPR_BINARY_POSITIONS_INVALID",
526
- message: `Operator "${expr.id}" (${expr.operator}) must have children at positions 0 and 1.`,
527
- premiseId: this.id,
528
- expressionId: expr.id,
529
- }));
530
- }
531
- }
532
- }
533
- return makeValidationResult(issues);
534
- }
535
- /**
536
- * Evaluates the premise under a three-valued expression assignment.
537
- *
538
- * Variable values are looked up in `assignment.variables` using Kleene
539
- * three-valued logic (`null` = unknown). Missing variables default to `null`.
540
- * Expressions listed in `assignment.rejectedExpressionIds` evaluate to
541
- * `false` and their children are not evaluated.
542
- *
543
- * For inference premises (`implies`/`iff`), an `inferenceDiagnostic` is
544
- * computed with three-valued fields unless the root is rejected.
545
- */
546
- evaluate(assignment, options) {
547
- const validation = this.validateEvaluability();
548
- if (!validation.ok) {
549
- throw new Error(`Premise "${this.id}" is not evaluable: ${validation.issues
550
- .map((issue) => issue.code)
551
- .join(", ")}`);
552
- }
553
- const rootExpressionId = this.rootExpressionId;
554
- const referencedVariableIds = sortedUnique(this.expressions
555
- .toArray()
556
- .filter((expr) => expr.type === "variable")
557
- .map((expr) => expr.variableId));
558
- if (options?.strictUnknownKeys || options?.requireExactCoverage) {
559
- const knownVariableIds = new Set(referencedVariableIds);
560
- const unknownKeys = Object.keys(assignment.variables).filter((variableId) => !knownVariableIds.has(variableId));
561
- if (unknownKeys.length > 0) {
562
- throw new Error(`Assignment contains unknown variable IDs for premise "${this.id}": ${unknownKeys.join(", ")}`);
563
- }
564
- }
565
- const expressionValues = {};
566
- const evaluateExpression = (expressionId) => {
567
- const expression = this.expressions.getExpression(expressionId);
568
- if (!expression) {
569
- throw new Error(`Expression "${expressionId}" was not found.`);
570
- }
571
- if (assignment.rejectedExpressionIds.includes(expression.id)) {
572
- expressionValues[expression.id] = false;
573
- return false;
574
- }
575
- if (expression.type === "variable") {
576
- const value = assignment.variables[expression.variableId] ?? null;
577
- expressionValues[expression.id] = value;
578
- return value;
579
- }
580
- const children = this.expressions.getChildExpressions(expression.id);
581
- let value;
582
- if (expression.type === "formula") {
583
- value = evaluateExpression(children[0].id);
584
- expressionValues[expression.id] = value;
585
- return value;
586
- }
587
- switch (expression.operator) {
588
- case "not":
589
- value = kleeneNot(evaluateExpression(children[0].id));
590
- break;
591
- case "and":
592
- value = children.reduce((acc, child) => kleeneAnd(acc, evaluateExpression(child.id)), true);
593
- break;
594
- case "or":
595
- value = children.reduce((acc, child) => kleeneOr(acc, evaluateExpression(child.id)), false);
596
- break;
597
- case "implies": {
598
- const left = children.find((child) => child.position === 0);
599
- const right = children.find((child) => child.position === 1);
600
- value = kleeneImplies(evaluateExpression(left.id), evaluateExpression(right.id));
601
- break;
602
- }
603
- case "iff": {
604
- const left = children.find((child) => child.position === 0);
605
- const right = children.find((child) => child.position === 1);
606
- value = kleeneIff(evaluateExpression(left.id), evaluateExpression(right.id));
607
- break;
608
- }
609
- }
610
- expressionValues[expression.id] = value;
611
- return value;
612
- };
613
- const rootValue = evaluateExpression(rootExpressionId);
614
- const variableValues = {};
615
- for (const variableId of referencedVariableIds) {
616
- variableValues[variableId] =
617
- assignment.variables[variableId] ?? null;
618
- }
619
- let inferenceDiagnostic;
620
- if (this.isInference() &&
621
- !assignment.rejectedExpressionIds.includes(rootExpressionId)) {
622
- const root = this.expressions.getExpression(rootExpressionId);
623
- if (root?.type === "operator") {
624
- const children = this.expressions.getChildExpressions(root.id);
625
- const left = children.find((child) => child.position === 0);
626
- const right = children.find((child) => child.position === 1);
627
- if (left && right) {
628
- const leftValue = expressionValues[left.id];
629
- const rightValue = expressionValues[right.id];
630
- if (root.operator === "implies") {
631
- inferenceDiagnostic = {
632
- kind: "implies",
633
- premiseId: this.id,
634
- rootExpressionId,
635
- leftValue,
636
- rightValue,
637
- rootValue,
638
- antecedentTrue: leftValue,
639
- consequentTrue: rightValue,
640
- isVacuouslyTrue: kleeneNot(leftValue),
641
- fired: leftValue,
642
- firedAndHeld: kleeneAnd(leftValue, rightValue),
643
- };
644
- }
645
- else if (root.operator === "iff") {
646
- const leftToRight = buildDirectionalVacuity(leftValue, rightValue);
647
- const rightToLeft = buildDirectionalVacuity(rightValue, leftValue);
648
- inferenceDiagnostic = {
649
- kind: "iff",
650
- premiseId: this.id,
651
- rootExpressionId,
652
- leftValue,
653
- rightValue,
654
- rootValue,
655
- leftToRight,
656
- rightToLeft,
657
- bothSidesTrue: kleeneAnd(leftValue, rightValue),
658
- bothSidesFalse: kleeneAnd(kleeneNot(leftValue), kleeneNot(rightValue)),
659
- };
660
- }
661
- }
662
- }
663
- }
664
- return {
665
- premiseId: this.id,
666
- premiseType: this.isInference() ? "inference" : "constraint",
667
- rootExpressionId,
668
- rootValue,
669
- expressionValues,
670
- variableValues,
671
- inferenceDiagnostic,
672
- };
673
- }
674
- /**
675
- * Returns a human-readable string of this premise's expression tree using
676
- * standard logical notation (∧ ∨ ¬ → ↔). Missing operands are rendered
677
- * as `(?)`. Returns an empty string when the premise has no expressions.
678
- */
679
- toDisplayString() {
680
- if (this.rootExpressionId === undefined) {
681
- return "";
682
- }
683
- return this.renderExpression(this.rootExpressionId);
684
- }
685
- /**
686
- * Returns the set of variable IDs referenced by expressions in this premise.
687
- * Only variables that appear in `type: "variable"` expression nodes are
688
- * included — not all variables in the shared VariableManager.
689
- */
690
- getReferencedVariableIds() {
691
- const ids = new Set();
692
- for (const expr of this.expressions.toArray()) {
693
- if (expr.type === "variable") {
694
- ids.add(expr.variableId);
695
- }
696
- }
697
- return ids;
698
- }
699
- /**
700
- * Returns a serialisable snapshot of this premise conforming to
701
- * `TCorePremise`. `variables` contains only the variables that are actually
702
- * referenced by expressions in this premise.
703
- */
704
- toData() {
705
- const expressions = this.getExpressions();
706
- const referencedVariableIds = new Set();
707
- for (const expr of expressions) {
708
- if (expr.type === "variable") {
709
- referencedVariableIds.add(expr.variableId);
710
- }
711
- }
712
- const variables = Array.from(referencedVariableIds).sort();
713
- return {
714
- ...this.extras,
715
- id: this.id,
716
- rootExpressionId: this.rootExpressionId,
717
- variables,
718
- expressions,
719
- checksum: this.checksum(),
720
- };
721
- }
722
- /**
723
- * Returns a premise-level checksum combining all entity checksums.
724
- * Computed lazily -- only recalculated when state has changed.
725
- */
726
- checksum() {
727
- if (this.checksumDirty || this.cachedChecksum === undefined) {
728
- this.cachedChecksum = this.computeChecksum();
729
- this.checksumDirty = false;
730
- }
731
- return this.cachedChecksum;
732
- }
733
- // -------------------------------------------------------------------------
734
- // Private helpers
735
- // -------------------------------------------------------------------------
736
- attachExpressionChecksum(expr) {
737
- const fields = this.checksumConfig?.expressionFields ??
738
- DEFAULT_CHECKSUM_CONFIG.expressionFields;
739
- return {
740
- ...expr,
741
- checksum: entityChecksum(expr, fields),
742
- };
743
- }
744
- attachVariableChecksum(v) {
745
- const fields = this.checksumConfig?.variableFields ??
746
- DEFAULT_CHECKSUM_CONFIG.variableFields;
747
- return {
748
- ...v,
749
- checksum: entityChecksum(v, fields),
750
- };
751
- }
752
- attachChangesetChecksums(changes) {
753
- const result = {
754
- ...changes,
755
- };
756
- if (changes.expressions) {
757
- result.expressions = {
758
- added: changes.expressions.added.map((e) => this.attachExpressionChecksum(e)),
759
- modified: changes.expressions.modified.map((e) => this.attachExpressionChecksum(e)),
760
- removed: changes.expressions.removed.map((e) => this.attachExpressionChecksum(e)),
761
- };
762
- }
763
- // Variables in the changeset should already have checksums
764
- // (from ArgumentEngine.addVariable), but cast for type safety
765
- if (changes.variables) {
766
- result.variables =
767
- changes.variables;
768
- }
769
- return result;
770
- }
771
- computeChecksum() {
772
- const config = this.checksumConfig;
773
- const parts = [];
774
- // Premise metadata
775
- parts.push(entityChecksum({
776
- id: this.id,
777
- rootExpressionId: this.rootExpressionId,
778
- }, config?.premiseFields ?? DEFAULT_CHECKSUM_CONFIG.premiseFields));
779
- // Variable checksums (sorted by ID for determinism)
780
- for (const v of this.getVariables()) {
781
- parts.push(entityChecksum(v, config?.variableFields ??
782
- DEFAULT_CHECKSUM_CONFIG.variableFields));
783
- }
784
- // Expression checksums (sorted by ID for determinism)
785
- for (const e of this.getExpressions()) {
786
- parts.push(entityChecksum(e, config?.expressionFields ??
787
- DEFAULT_CHECKSUM_CONFIG.expressionFields));
788
- }
789
- return computeHash(parts.join(":"));
790
- }
791
- /** Invalidate the cached checksum so the next call recomputes it. */
792
- markDirty() {
793
- this.checksumDirty = true;
794
- }
795
- /**
796
- * Re-reads the single root from ExpressionManager after any operation
797
- * that may have caused operator collapse to silently change the root.
798
- */
799
- syncRootExpressionId() {
800
- const roots = this.expressions.getChildExpressions(null);
801
- this.rootExpressionId = roots[0]?.id;
802
- }
803
- collectSubtree(rootId) {
804
- const result = [];
805
- const stack = [rootId];
806
- while (stack.length > 0) {
807
- const id = stack.pop();
808
- const expr = this.expressions.getExpression(id);
809
- if (!expr)
810
- continue;
811
- result.push(expr);
812
- for (const child of this.expressions.getChildExpressions(id)) {
813
- stack.push(child.id);
814
- }
815
- }
816
- return result;
817
- }
818
- assertBelongsToArgument(argumentId, argumentVersion) {
819
- if (argumentId !== this.argument.id) {
820
- throw new Error(`Entity argumentId "${argumentId}" does not match engine argument ID "${this.argument.id}".`);
821
- }
822
- if (argumentVersion !== this.argument.version) {
823
- throw new Error(`Entity argumentVersion "${argumentVersion}" does not match engine argument version "${this.argument.version}".`);
824
- }
825
- }
826
- renderExpression(expressionId) {
827
- const expression = this.expressions.getExpression(expressionId);
828
- if (!expression) {
829
- throw new Error(`Expression "${expressionId}" was not found.`);
830
- }
831
- if (expression.type === "variable") {
832
- const variable = this.variables.getVariable(expression.variableId);
833
- if (!variable) {
834
- throw new Error(`Variable "${expression.variableId}" for expression "${expressionId}" was not found.`);
835
- }
836
- return variable.symbol;
837
- }
838
- if (expression.type === "formula") {
839
- const children = this.expressions.getChildExpressions(expression.id);
840
- if (children.length === 0) {
841
- return "(?)";
842
- }
843
- return `(${this.renderExpression(children[0].id)})`;
844
- }
845
- const children = this.expressions.getChildExpressions(expression.id);
846
- if (expression.operator === "not") {
847
- if (children.length === 0) {
848
- return `${this.operatorSymbol(expression.operator)} (?)`;
849
- }
850
- return `${this.operatorSymbol(expression.operator)}(${this.renderExpression(children[0].id)})`;
851
- }
852
- if (children.length === 0) {
853
- return "(?)";
854
- }
855
- const renderedChildren = children.map((child) => this.renderExpression(child.id));
856
- return `(${renderedChildren.join(` ${this.operatorSymbol(expression.operator)} `)})`;
857
- }
858
- operatorSymbol(operator) {
859
- switch (operator) {
860
- case "and":
861
- return "∧";
862
- case "or":
863
- return "∨";
864
- case "implies":
865
- return "→";
866
- case "iff":
867
- return "↔";
868
- case "not":
869
- return "¬";
870
- }
871
- }
872
- }
873
- //# sourceMappingURL=PremiseManager.js.map