simple-graph-query 2.4.0 → 2.5.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.
@@ -51022,8 +51022,7 @@ function normalizeBinaryResult(result) {
51022
51022
  }
51023
51023
  return ids;
51024
51024
  }
51025
- function evaluateExpression(node, datum, normalizer) {
51026
- const evaluator = new index_1.SimpleGraphQueryEvaluator(datum);
51025
+ function evaluateExpression(node, evaluator, normalizer) {
51027
51026
  const expression = nodeToString(node);
51028
51027
  const result = evaluator.evaluateExpression(expression);
51029
51028
  return normalizer(result);
@@ -51055,15 +51054,52 @@ function setsEqual(a, b) {
51055
51054
  }
51056
51055
  return true;
51057
51056
  }
51057
+ function classifyIdentifier(name, datums) {
51058
+ if (name === "univ" || name === "iden") {
51059
+ return "builtin";
51060
+ }
51061
+ for (const datum of datums) {
51062
+ if (datum.getRelations().some((relation) => relation.name === name)) {
51063
+ return "relation";
51064
+ }
51065
+ if (datum.getTypes().some((type) => type.id === name)) {
51066
+ return "type";
51067
+ }
51068
+ }
51069
+ return "other";
51070
+ }
51058
51071
  function buildBaseNodes(datums) {
51059
51072
  const baseNames = intersectNames(datums);
51060
51073
  // Always include standard top-level identifiers when present in the language
51061
51074
  ["univ", "iden"].forEach((builtin) => baseNames.add(builtin));
51062
- return Array.from(baseNames).map((name) => ({ kind: "identifier", name }));
51075
+ const orderedNames = Array.from(baseNames).sort((left, right) => {
51076
+ const priority = {
51077
+ relation: 0,
51078
+ type: 1,
51079
+ builtin: 2,
51080
+ other: 3,
51081
+ };
51082
+ const leftPriority = priority[classifyIdentifier(left, datums)];
51083
+ const rightPriority = priority[classifyIdentifier(right, datums)];
51084
+ if (leftPriority !== rightPriority) {
51085
+ return leftPriority - rightPriority;
51086
+ }
51087
+ return left.localeCompare(right);
51088
+ });
51089
+ return orderedNames.map((name) => ({ kind: "identifier", name }));
51090
+ }
51091
+ function getOrCreateEvaluator(datum, cache) {
51092
+ const existing = cache.get(datum);
51093
+ if (existing) {
51094
+ return existing;
51095
+ }
51096
+ const evaluator = new index_1.SimpleGraphQueryEvaluator(datum);
51097
+ cache.set(datum, evaluator);
51098
+ return evaluator;
51063
51099
  }
51064
51100
  function matchesTargets(node, examples, normalizer) {
51065
51101
  for (const example of examples) {
51066
- const result = evaluateExpression(node, example.datum, normalizer);
51102
+ const result = evaluateExpression(node, example.evaluator, normalizer);
51067
51103
  if (!result)
51068
51104
  return false;
51069
51105
  if (!setsEqual(result, example.target)) {
@@ -51079,11 +51115,21 @@ function synthesizeExpressionNode(examples, normalizer, maxDepth = 3) {
51079
51115
  if (examples.length === 0) {
51080
51116
  throw new SelectorSynthesisError("No examples provided for synthesis");
51081
51117
  }
51082
- const datums = examples.map((example) => example.datum);
51118
+ const evaluatorCache = new Map();
51119
+ const evaluatedExamples = examples.map((example) => ({
51120
+ ...example,
51121
+ evaluator: getOrCreateEvaluator(example.datum, evaluatorCache),
51122
+ }));
51123
+ const datums = evaluatedExamples.map((example) => example.datum);
51083
51124
  const baseNodes = buildBaseNodes(datums);
51084
51125
  if (baseNodes.length === 0) {
51085
51126
  throw new SelectorSynthesisError("No shared identifiers available across provided data instances");
51086
51127
  }
51128
+ for (const node of baseNodes) {
51129
+ if (matchesTargets(node, evaluatedExamples, normalizer)) {
51130
+ return node;
51131
+ }
51132
+ }
51087
51133
  const queue = [];
51088
51134
  const queued = new Set();
51089
51135
  const visited = new Set();
@@ -51104,7 +51150,7 @@ function synthesizeExpressionNode(examples, normalizer, maxDepth = 3) {
51104
51150
  continue;
51105
51151
  }
51106
51152
  visited.add(key);
51107
- if (matchesTargets(current.node, examples, normalizer)) {
51153
+ if (matchesTargets(current.node, evaluatedExamples, normalizer)) {
51108
51154
  return current.node;
51109
51155
  }
51110
51156
  if (current.depth >= maxDepth) {
@@ -51141,8 +51187,8 @@ function synthesizeBinaryRelation(examples, maxDepth = 3) {
51141
51187
  const node = synthesizeExpressionNode(synthesisExamples, normalizeBinaryResult, maxDepth);
51142
51188
  return nodeToString(node);
51143
51189
  }
51144
- function buildWhyNode(node, datum, normalizer) {
51145
- const result = evaluateExpression(node, datum, normalizer);
51190
+ function buildWhyNode(node, evaluator, normalizer) {
51191
+ const result = evaluateExpression(node, evaluator, normalizer);
51146
51192
  const base = {
51147
51193
  kind: node.kind,
51148
51194
  expression: nodeToString(node),
@@ -51152,7 +51198,7 @@ function buildWhyNode(node, datum, normalizer) {
51152
51198
  case "identifier":
51153
51199
  return base;
51154
51200
  case "closure":
51155
- return { ...base, children: [buildWhyNode(node.child, datum, normalizer)] };
51201
+ return { ...base, children: [buildWhyNode(node.child, evaluator, normalizer)] };
51156
51202
  case "join":
51157
51203
  case "union":
51158
51204
  case "intersection":
@@ -51160,8 +51206,8 @@ function buildWhyNode(node, datum, normalizer) {
51160
51206
  return {
51161
51207
  ...base,
51162
51208
  children: [
51163
- buildWhyNode(node.left, datum, normalizer),
51164
- buildWhyNode(node.right, datum, normalizer),
51209
+ buildWhyNode(node.left, evaluator, normalizer),
51210
+ buildWhyNode(node.right, evaluator, normalizer),
51165
51211
  ],
51166
51212
  };
51167
51213
  default:
@@ -51175,12 +51221,16 @@ function synthesizeSelectorWithWhy(examples, maxDepth = 3) {
51175
51221
  }));
51176
51222
  const node = synthesizeExpressionNode(synthesisExamples, normalizeUnaryResult, maxDepth);
51177
51223
  const expression = nodeToString(node);
51178
- const explanationExamples = synthesisExamples.map((example) => ({
51179
- datum: example.datum,
51180
- target: example.target,
51181
- result: evaluateExpression(node, example.datum, normalizeUnaryResult),
51182
- why: buildWhyNode(node, example.datum, normalizeUnaryResult),
51183
- }));
51224
+ const evaluatorCache = new Map();
51225
+ const explanationExamples = synthesisExamples.map((example) => {
51226
+ const evaluator = getOrCreateEvaluator(example.datum, evaluatorCache);
51227
+ return {
51228
+ datum: example.datum,
51229
+ target: example.target,
51230
+ result: evaluateExpression(node, evaluator, normalizeUnaryResult),
51231
+ why: buildWhyNode(node, evaluator, normalizeUnaryResult),
51232
+ };
51233
+ });
51184
51234
  return { expression, examples: explanationExamples };
51185
51235
  }
51186
51236
  function synthesizeBinaryRelationWithWhy(examples, maxDepth = 3) {
@@ -51190,12 +51240,16 @@ function synthesizeBinaryRelationWithWhy(examples, maxDepth = 3) {
51190
51240
  });
51191
51241
  const node = synthesizeExpressionNode(synthesisExamples, normalizeBinaryResult, maxDepth);
51192
51242
  const expression = nodeToString(node);
51193
- const explanationExamples = synthesisExamples.map((example) => ({
51194
- datum: example.datum,
51195
- target: example.target,
51196
- result: evaluateExpression(node, example.datum, normalizeBinaryResult),
51197
- why: buildWhyNode(node, example.datum, normalizeBinaryResult),
51198
- }));
51243
+ const evaluatorCache = new Map();
51244
+ const explanationExamples = synthesisExamples.map((example) => {
51245
+ const evaluator = getOrCreateEvaluator(example.datum, evaluatorCache);
51246
+ return {
51247
+ datum: example.datum,
51248
+ target: example.target,
51249
+ result: evaluateExpression(node, evaluator, normalizeBinaryResult),
51250
+ why: buildWhyNode(node, evaluator, normalizeBinaryResult),
51251
+ };
51252
+ });
51199
51253
  return { expression, examples: explanationExamples };
51200
51254
  }
51201
51255
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple-graph-query",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "TypeScript evaluator for Forge expressions with browser-compatible UMD bundle",
5
5
  "main": "dist/simple-graph-query.bundle.js",
6
6
  "types": "dist/index.d.ts",