@orkestrel/reason 0.0.3 → 0.0.5

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.
@@ -105,7 +105,7 @@ var ReasonError = class extends Error {
105
105
  super(message);
106
106
  this.name = "ReasonError";
107
107
  this.code = code;
108
- this.context = context;
108
+ if (context !== void 0) this.context = context;
109
109
  }
110
110
  };
111
111
  /**
@@ -949,7 +949,7 @@ function rule(id, premises, conclusion, overrides) {
949
949
  * transform stays exact-record valid; the transformer then applies its
950
950
  * per-operation default (`1` for `multiply` / `divide` / `power`, `0` otherwise).
951
951
  *
952
- * @param operation - The math operation to apply
952
+ * @param operator - The math operation to apply
953
953
  * @param operand - The operand (ignored by the unary operations)
954
954
  * @returns A fresh transform
955
955
  *
@@ -961,9 +961,9 @@ function rule(id, premises, conclusion, overrides) {
961
961
  * transform('round') // { operation: 'round' }
962
962
  * ```
963
963
  */
964
- function transform(operation, operand) {
965
- return operand === void 0 ? { operation } : {
966
- operation,
964
+ function transform(operator, operand) {
965
+ return operand === void 0 ? { operation: operator } : {
966
+ operation: operator,
967
967
  operand
968
968
  };
969
969
  }
@@ -1051,7 +1051,7 @@ function constant(value) {
1051
1051
  * operation('abs', variable('x')) // unary — no right operand
1052
1052
  * ```
1053
1053
  */
1054
- var operation = (operator, left, right) => {
1054
+ function operation(operator, left, right) {
1055
1055
  return right === void 0 ? {
1056
1056
  form: "operation",
1057
1057
  operator,
@@ -1062,7 +1062,7 @@ var operation = (operator, left, right) => {
1062
1062
  left,
1063
1063
  right
1064
1064
  };
1065
- };
1065
+ }
1066
1066
  /**
1067
1067
  * Build an {@link Equation} — `left = right`, solved for `target`.
1068
1068
  *
@@ -2101,7 +2101,10 @@ function extractAtoms(expression) {
2101
2101
  continue;
2102
2102
  }
2103
2103
  const operands = node.operands;
2104
- for (let index = operands.length - 1; index >= 0; index--) if (index in operands) stack.push(operands[index]);
2104
+ for (let index = operands.length - 1; index >= 0; index--) {
2105
+ const operand = operands[index];
2106
+ if (operand !== void 0) stack.push(operand);
2107
+ }
2105
2108
  }
2106
2109
  return atoms;
2107
2110
  }
@@ -3429,10 +3432,7 @@ var Reason = class {
3429
3432
  constructor(options) {
3430
3433
  this.#bail = options?.bail ?? true;
3431
3434
  this.#validate = options?.validate ?? false;
3432
- this.#emitter = new _orkestrel_emitter.Emitter({
3433
- on: options?.on,
3434
- error: options?.error
3435
- });
3435
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
3436
3436
  for (const reasoner of options?.reasoners ?? []) this.#reasoners.set(reasoner.reasoning, reasoner);
3437
3437
  }
3438
3438
  get emitter() {
@@ -4104,34 +4104,7 @@ var LogicalReasoner = class {
4104
4104
  if (typeof rule !== "object" || rule === null) continue;
4105
4105
  if (rule.enabled === false) trace.push(`Skipped rule "${rule.id}" (disabled)`);
4106
4106
  }
4107
- const prove = (rule, depth, visited) => {
4108
- if (depth > maxDepth) return false;
4109
- if (visited.has(rule.id)) return false;
4110
- const nextVisited = new Set(visited);
4111
- nextVisited.add(rule.id);
4112
- const currentSubject = {
4113
- ...subject,
4114
- ...derived
4115
- };
4116
- const premiseResults = [];
4117
- for (const premise of rule.premises) premiseResults.push(this.#establish(premise, currentSubject, sortedRules, depth + 1, nextVisited, derived, subject, trace, ruleResults, maxDepth));
4118
- const allMet = premiseResults.every(Boolean);
4119
- ruleResults.set(rule.id, {
4120
- id: rule.id,
4121
- applied: allMet,
4122
- premises: premiseResults,
4123
- conclusion: allMet
4124
- });
4125
- if (allMet) {
4126
- const conclusions = extractConclusions(rule.conclusion);
4127
- for (const [key, value] of Object.entries(conclusions)) if (!equalValues(derived[key], value)) {
4128
- derived[key] = value;
4129
- trace.push(`Rule "${rule.id}" derived: ${key}=${String(value)} (backward, depth ${depth})`);
4130
- }
4131
- } else trace.push(`Rule "${rule.id}": does not hold (backward, depth ${depth})`);
4132
- return allMet;
4133
- };
4134
- for (const rule of sortedRules) prove(rule, 0, /* @__PURE__ */ new Set());
4107
+ for (const rule of sortedRules) this.#proveRule(rule, 0, /* @__PURE__ */ new Set(), sortedRules, derived, subject, trace, ruleResults, maxDepth);
4135
4108
  const finalResults = [];
4136
4109
  for (const rule of sortedRules) {
4137
4110
  const existing = ruleResults.get(rule.id);
@@ -4146,6 +4119,33 @@ var LogicalReasoner = class {
4146
4119
  rules: finalResults
4147
4120
  };
4148
4121
  }
4122
+ #proveRule(rule, depth, visited, rules, derived, subject, trace, ruleResults, maxDepth) {
4123
+ if (depth > maxDepth) return false;
4124
+ if (visited.has(rule.id)) return false;
4125
+ const nextVisited = new Set(visited);
4126
+ nextVisited.add(rule.id);
4127
+ const currentSubject = {
4128
+ ...subject,
4129
+ ...derived
4130
+ };
4131
+ const premiseResults = [];
4132
+ for (const premise of rule.premises) premiseResults.push(this.#establish(premise, currentSubject, rules, depth + 1, nextVisited, derived, subject, trace, ruleResults, maxDepth));
4133
+ const allMet = premiseResults.every(Boolean);
4134
+ ruleResults.set(rule.id, {
4135
+ id: rule.id,
4136
+ applied: allMet,
4137
+ premises: premiseResults,
4138
+ conclusion: allMet
4139
+ });
4140
+ if (allMet) {
4141
+ const conclusions = extractConclusions(rule.conclusion);
4142
+ for (const [key, value] of Object.entries(conclusions)) if (!equalValues(derived[key], value)) {
4143
+ derived[key] = value;
4144
+ trace.push(`Rule "${rule.id}" derived: ${key}=${String(value)} (backward, depth ${depth})`);
4145
+ }
4146
+ } else trace.push(`Rule "${rule.id}": does not hold (backward, depth ${depth})`);
4147
+ return allMet;
4148
+ }
4149
4149
  #establish(expression, currentSubject, rules, depth, visited, derived, subject, trace, ruleResults, maxDepth) {
4150
4150
  if (this.#evaluateExpression(expression, currentSubject)) return true;
4151
4151
  return this.#proveExpression(expression, rules, depth, visited, derived, subject, trace, ruleResults, maxDepth);
@@ -4533,7 +4533,7 @@ var InferentialReasoner = class {
4533
4533
  return {
4534
4534
  reasoning: "inferential",
4535
4535
  derived: result.derived,
4536
- proof: result.proof,
4536
+ ...result.proof === void 0 ? {} : { proof: result.proof },
4537
4537
  success: errors.length === 0,
4538
4538
  trace,
4539
4539
  errors
@@ -4722,10 +4722,7 @@ var EquationManager = class {
4722
4722
  #destroyed = false;
4723
4723
  constructor(options) {
4724
4724
  this.#equations = options?.equations ?? [];
4725
- this.#emitter = new _orkestrel_emitter.Emitter({
4726
- on: options?.on,
4727
- error: options?.error
4728
- });
4725
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
4729
4726
  }
4730
4727
  get emitter() {
4731
4728
  return this.#emitter;
@@ -4792,10 +4789,7 @@ var FactManager = class {
4792
4789
  #destroyed = false;
4793
4790
  constructor(options) {
4794
4791
  this.#facts = options?.facts ?? [];
4795
- this.#emitter = new _orkestrel_emitter.Emitter({
4796
- on: options?.on,
4797
- error: options?.error
4798
- });
4792
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
4799
4793
  }
4800
4794
  get emitter() {
4801
4795
  return this.#emitter;
@@ -4864,10 +4858,7 @@ var FactorManager = class {
4864
4858
  #destroyed = false;
4865
4859
  constructor(groups, options) {
4866
4860
  this.#groups = groups;
4867
- this.#emitter = new _orkestrel_emitter.Emitter({
4868
- on: options?.on,
4869
- error: options?.error
4870
- });
4861
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
4871
4862
  }
4872
4863
  get emitter() {
4873
4864
  return this.#emitter;
@@ -4936,10 +4927,7 @@ var GroupManager = class {
4936
4927
  #destroyed = false;
4937
4928
  constructor(options) {
4938
4929
  this.#groups = options?.groups ?? [];
4939
- this.#emitter = new _orkestrel_emitter.Emitter({
4940
- on: options?.on,
4941
- error: options?.error
4942
- });
4930
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
4943
4931
  }
4944
4932
  get emitter() {
4945
4933
  return this.#emitter;
@@ -5006,10 +4994,7 @@ var InferenceManager = class {
5006
4994
  #destroyed = false;
5007
4995
  constructor(options) {
5008
4996
  this.#inferences = options?.inferences ?? [];
5009
- this.#emitter = new _orkestrel_emitter.Emitter({
5010
- on: options?.on,
5011
- error: options?.error
5012
- });
4997
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
5013
4998
  }
5014
4999
  get emitter() {
5015
5000
  return this.#emitter;
@@ -5076,10 +5061,7 @@ var RuleManager = class {
5076
5061
  #destroyed = false;
5077
5062
  constructor(options) {
5078
5063
  this.#rules = options?.rules ?? [];
5079
- this.#emitter = new _orkestrel_emitter.Emitter({
5080
- on: options?.on,
5081
- error: options?.error
5082
- });
5064
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
5083
5065
  }
5084
5066
  get emitter() {
5085
5067
  return this.#emitter;
@@ -5148,10 +5130,7 @@ var VariableManager = class {
5148
5130
  #destroyed = false;
5149
5131
  constructor(options) {
5150
5132
  this.#variables = options?.variables ?? {};
5151
- this.#emitter = new _orkestrel_emitter.Emitter({
5152
- on: options?.on,
5153
- error: options?.error
5154
- });
5133
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
5155
5134
  }
5156
5135
  get emitter() {
5157
5136
  return this.#emitter;
@@ -5233,10 +5212,7 @@ var DefinitionBuilder = class {
5233
5212
  id: this.#id
5234
5213
  };
5235
5214
  this.#envelope = this.#strip(seeded);
5236
- this.#emitter = new _orkestrel_emitter.Emitter({
5237
- on: options?.on,
5238
- error: options?.error
5239
- });
5215
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
5240
5216
  this.groups = options?.groups ?? new GroupManager({ groups: seeded.reasoning === "quantitative" ? seeded.groups : [] });
5241
5217
  this.factors = options?.factors ?? new FactorManager(this.groups);
5242
5218
  this.rules = options?.rules ?? new RuleManager({ rules: seeded.reasoning === "logical" ? seeded.rules : [] });
@@ -5423,10 +5399,7 @@ var SubjectBuilder = class {
5423
5399
  const { id: _id, ...rest } = seed;
5424
5400
  this.#subject = rest;
5425
5401
  }
5426
- this.#emitter = new _orkestrel_emitter.Emitter({
5427
- on: options?.on,
5428
- error: options?.error
5429
- });
5402
+ this.#emitter = new _orkestrel_emitter.Emitter(options);
5430
5403
  }
5431
5404
  get id() {
5432
5405
  return this.#id;