@rsconcept/domain 1.0.0 → 1.1.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.
Files changed (183) hide show
  1. package/README.md +3 -3
  2. package/dist/analyzer-DlSq3Y3r.d.ts +39 -0
  3. package/dist/arguments-extractor-1acwjQNc.d.ts +38 -0
  4. package/dist/ast-C8sIpKdL.d.ts +51 -0
  5. package/dist/ast-annotations-BiMjkKvz.d.ts +16 -0
  6. package/dist/branded-ZlzIcxzu.d.ts +9 -0
  7. package/dist/calculator-C9W2jkSx.d.ts +39 -0
  8. package/dist/cctext/index.d.ts +2 -1
  9. package/dist/cctext/index.js +2 -42
  10. package/dist/cctext/language-api.d.ts +10 -12
  11. package/dist/cctext/language-api.js +157 -227
  12. package/dist/cctext/language-api.js.map +1 -1
  13. package/dist/cctext/language.d.ts +24 -22
  14. package/dist/cctext/language.js +43 -39
  15. package/dist/cctext/language.js.map +1 -1
  16. package/dist/error-E1LVq_3w.d.ts +87 -0
  17. package/dist/graph/graph.d.ts +2 -62
  18. package/dist/graph/graph.js +339 -382
  19. package/dist/graph/graph.js.map +1 -1
  20. package/dist/graph/index.d.ts +2 -1
  21. package/dist/graph/index.js +2 -384
  22. package/dist/graph-DR8rL2o3.d.ts +64 -0
  23. package/dist/hash-Y8I4c6Al.d.ts +8 -0
  24. package/dist/index-BKZ67WMa.d.ts +1 -0
  25. package/dist/index-BVVgDSdq.d.ts +1 -0
  26. package/dist/index-DmtQKWjk.d.ts +1 -0
  27. package/dist/index-_6s0AX1B.d.ts +1 -0
  28. package/dist/index.d.ts +27 -28
  29. package/dist/index.js +23 -5851
  30. package/dist/lezer-tree-iS7LpLBJ.d.ts +14 -0
  31. package/dist/library/folder-tree.d.ts +22 -20
  32. package/dist/library/folder-tree.js +108 -130
  33. package/dist/library/folder-tree.js.map +1 -1
  34. package/dist/library/index.d.ts +8 -17
  35. package/dist/library/index.js +7 -2800
  36. package/dist/library/library-api.d.ts +3 -1
  37. package/dist/library/library-api.js +9 -8
  38. package/dist/library/library-api.js.map +1 -1
  39. package/dist/library/library.d.ts +2 -56
  40. package/dist/library/library.js +23 -19
  41. package/dist/library/library.js.map +1 -1
  42. package/dist/library/oss-api.d.ts +26 -37
  43. package/dist/library/oss-api.js +257 -1096
  44. package/dist/library/oss-api.js.map +1 -1
  45. package/dist/library/oss-layout-api.d.ts +28 -28
  46. package/dist/library/oss-layout-api.js +239 -316
  47. package/dist/library/oss-layout-api.js.map +1 -1
  48. package/dist/library/oss-layout.d.ts +2 -25
  49. package/dist/library/oss-layout.js +1 -1
  50. package/dist/library/oss.d.ts +87 -89
  51. package/dist/library/oss.js +27 -26
  52. package/dist/library/oss.js.map +1 -1
  53. package/dist/library/rsengine.d.ts +100 -106
  54. package/dist/library/rsengine.js +439 -2599
  55. package/dist/library/rsengine.js.map +1 -1
  56. package/dist/library/rsform-api.d.ts +11 -16
  57. package/dist/library/rsform-api.js +313 -825
  58. package/dist/library/rsform-api.js.map +1 -1
  59. package/dist/library/rsform.d.ts +159 -167
  60. package/dist/library/rsform.js +29 -28
  61. package/dist/library/rsform.js.map +1 -1
  62. package/dist/library/rsmodel-api.d.ts +8 -15
  63. package/dist/library/rsmodel-api.js +172 -813
  64. package/dist/library/rsmodel-api.js.map +1 -1
  65. package/dist/library/rsmodel.d.ts +27 -33
  66. package/dist/library/rsmodel.js +16 -23
  67. package/dist/library/rsmodel.js.map +1 -1
  68. package/dist/library/structure-planner.d.ts +20 -26
  69. package/dist/library/structure-planner.js +106 -474
  70. package/dist/library/structure-planner.js.map +1 -1
  71. package/dist/library-CYun28Xz.d.ts +58 -0
  72. package/dist/oss-layout-3glgAqfn.d.ts +27 -0
  73. package/dist/parser-Bwd8LxJ1.d.ts +7 -0
  74. package/dist/parsing/ast.d.ts +2 -49
  75. package/dist/parsing/ast.js +68 -76
  76. package/dist/parsing/ast.js.map +1 -1
  77. package/dist/parsing/index.d.ts +3 -3
  78. package/dist/parsing/index.js +3 -141
  79. package/dist/parsing/lezer-tree.d.ts +2 -13
  80. package/dist/parsing/lezer-tree.js +50 -43
  81. package/dist/parsing/lezer-tree.js.map +1 -1
  82. package/dist/rslang/api.d.ts +9 -14
  83. package/dist/rslang/api.js +114 -827
  84. package/dist/rslang/api.js.map +1 -1
  85. package/dist/rslang/ast-annotations.d.ts +2 -18
  86. package/dist/rslang/ast-annotations.js +34 -45
  87. package/dist/rslang/ast-annotations.js.map +1 -1
  88. package/dist/rslang/error.d.ts +2 -85
  89. package/dist/rslang/error.js +88 -150
  90. package/dist/rslang/error.js.map +1 -1
  91. package/dist/rslang/eval/calculator.d.ts +2 -43
  92. package/dist/rslang/eval/calculator.js +81 -1636
  93. package/dist/rslang/eval/calculator.js.map +1 -1
  94. package/dist/rslang/eval/evaluation-cache.d.ts +22 -26
  95. package/dist/rslang/eval/evaluation-cache.js +168 -287
  96. package/dist/rslang/eval/evaluation-cache.js.map +1 -1
  97. package/dist/rslang/eval/evaluator.d.ts +59 -63
  98. package/dist/rslang/eval/evaluator.js +602 -1509
  99. package/dist/rslang/eval/evaluator.js.map +1 -1
  100. package/dist/rslang/eval/value-api.d.ts +2 -48
  101. package/dist/rslang/eval/value-api.js +2 -490
  102. package/dist/rslang/eval/value.d.ts +2 -36
  103. package/dist/rslang/eval/value.js +2 -118
  104. package/dist/rslang/index.d.ts +14 -17
  105. package/dist/rslang/index.js +12 -4314
  106. package/dist/rslang/labels.d.ts +6 -6
  107. package/dist/rslang/labels.js +139 -305
  108. package/dist/rslang/labels.js.map +1 -1
  109. package/dist/rslang/parser/expression-generator.d.ts +5 -5
  110. package/dist/rslang/parser/expression-generator.js +248 -446
  111. package/dist/rslang/parser/expression-generator.js.map +1 -1
  112. package/dist/rslang/parser/normalize.d.ts +4 -8
  113. package/dist/rslang/parser/normalize.js +286 -481
  114. package/dist/rslang/parser/normalize.js.map +1 -1
  115. package/dist/rslang/parser/parser.d.ts +2 -5
  116. package/dist/rslang/parser/parser.js +30 -21
  117. package/dist/rslang/parser/parser.js.map +1 -1
  118. package/dist/rslang/parser/parser.terms.d.ts +43 -41
  119. package/dist/rslang/parser/parser.terms.js +44 -83
  120. package/dist/rslang/parser/parser.terms.js.map +1 -1
  121. package/dist/rslang/parser/syntax-errors.d.ts +5 -8
  122. package/dist/rslang/parser/syntax-errors.js +113 -382
  123. package/dist/rslang/parser/syntax-errors.js.map +1 -1
  124. package/dist/rslang/parser/token.d.ts +2 -79
  125. package/dist/rslang/parser/token.js +81 -93
  126. package/dist/rslang/parser/token.js.map +1 -1
  127. package/dist/rslang/semantic/analyzer.d.ts +2 -39
  128. package/dist/rslang/semantic/analyzer.js +186 -2600
  129. package/dist/rslang/semantic/analyzer.js.map +1 -1
  130. package/dist/rslang/semantic/arguments-extractor.d.ts +2 -42
  131. package/dist/rslang/semantic/arguments-extractor.js +202 -361
  132. package/dist/rslang/semantic/arguments-extractor.js.map +1 -1
  133. package/dist/rslang/semantic/type-auditor.d.ts +64 -68
  134. package/dist/rslang/semantic/type-auditor.js +594 -1564
  135. package/dist/rslang/semantic/type-auditor.js.map +1 -1
  136. package/dist/rslang/semantic/typification-api.d.ts +4 -7
  137. package/dist/rslang/semantic/typification-api.js +162 -303
  138. package/dist/rslang/semantic/typification-api.js.map +1 -1
  139. package/dist/rslang/semantic/typification-parser.d.ts +2 -12
  140. package/dist/rslang/semantic/typification-parser.js +165 -219
  141. package/dist/rslang/semantic/typification-parser.js.map +1 -1
  142. package/dist/rslang/semantic/typification.d.ts +2 -119
  143. package/dist/rslang/semantic/typification.js +66 -52
  144. package/dist/rslang/semantic/typification.js.map +1 -1
  145. package/dist/rslang/semantic/value-auditor.d.ts +32 -38
  146. package/dist/rslang/semantic/value-auditor.js +206 -518
  147. package/dist/rslang/semantic/value-auditor.js.map +1 -1
  148. package/dist/rslang/semantic/value-class.d.ts +2 -10
  149. package/dist/rslang/semantic/value-class.js +8 -7
  150. package/dist/rslang/semantic/value-class.js.map +1 -1
  151. package/dist/rslang/typification-graph.d.ts +2 -33
  152. package/dist/rslang/typification-graph.js +94 -306
  153. package/dist/rslang/typification-graph.js.map +1 -1
  154. package/dist/shared/branded.d.ts +2 -7
  155. package/dist/shared/branded.js +1 -1
  156. package/dist/shared/hash.d.ts +2 -6
  157. package/dist/shared/hash.js +13 -13
  158. package/dist/shared/hash.js.map +1 -1
  159. package/dist/shared/index.d.ts +3 -2
  160. package/dist/shared/index.js +2 -18
  161. package/dist/token-DeXAmzwr.d.ts +81 -0
  162. package/dist/typification-Dk-fisgO.d.ts +120 -0
  163. package/dist/typification-graph-6HcZ-rKH.d.ts +30 -0
  164. package/dist/typification-parser-BBVx1RxP.d.ts +13 -0
  165. package/dist/value-B8UtCqaK.js +366 -0
  166. package/dist/value-B8UtCqaK.js.map +1 -0
  167. package/dist/value-CTjX6825.d.ts +33 -0
  168. package/dist/value-api-Bw-SgaYY.d.ts +49 -0
  169. package/dist/value-class-CNI-lqXJ.d.ts +12 -0
  170. package/package.json +8 -8
  171. package/src/library/rsform-api.test.ts +24 -0
  172. package/src/library/rsform-api.ts +12 -4
  173. package/dist/cctext/index.js.map +0 -1
  174. package/dist/graph/index.js.map +0 -1
  175. package/dist/index.js.map +0 -1
  176. package/dist/library/index.js.map +0 -1
  177. package/dist/library/oss-layout.js.map +0 -1
  178. package/dist/parsing/index.js.map +0 -1
  179. package/dist/rslang/eval/value-api.js.map +0 -1
  180. package/dist/rslang/eval/value.js.map +0 -1
  181. package/dist/rslang/index.js.map +0 -1
  182. package/dist/shared/branded.js.map +0 -1
  183. package/dist/shared/index.js.map +0 -1
@@ -1,1105 +1,266 @@
1
- // src/graph/graph.ts
2
- var GraphNode = class _GraphNode {
3
- /** Unique identifier of the node. */
4
- id;
5
- /** List of outgoing nodes. */
6
- outputs;
7
- /** List of incoming nodes. */
8
- inputs;
9
- constructor(id) {
10
- this.id = id;
11
- this.outputs = [];
12
- this.inputs = [];
13
- }
14
- clone() {
15
- const result = new _GraphNode(this.id);
16
- result.outputs = [...this.outputs];
17
- result.inputs = [...this.inputs];
18
- return result;
19
- }
20
- addOutput(node) {
21
- this.outputs.push(node);
22
- }
23
- addInput(node) {
24
- this.inputs.push(node);
25
- }
26
- removeInput(target) {
27
- const index = this.inputs.findIndex((node) => node === target);
28
- return index > -1 ? this.inputs.splice(index, 1)[0] : null;
29
- }
30
- removeOutput(target) {
31
- const index = this.outputs.findIndex((node) => node === target);
32
- return index > -1 ? this.outputs.splice(index, 1)[0] : null;
33
- }
34
- };
35
- var Graph = class _Graph {
36
- /** Map of nodes. */
37
- nodes = /* @__PURE__ */ new Map();
38
- constructor(arr) {
39
- if (!arr) {
40
- return;
41
- }
42
- arr.forEach((edge) => {
43
- if (edge.length === 1) {
44
- this.addNode(edge[0]);
45
- } else {
46
- this.addEdge(edge[0], edge[1]);
47
- }
48
- });
49
- }
50
- clone() {
51
- const result = new _Graph();
52
- this.nodes.forEach((node) => result.nodes.set(node.id, node.clone()));
53
- return result;
54
- }
55
- at(target) {
56
- return this.nodes.get(target);
57
- }
58
- addNode(target) {
59
- let node = this.nodes.get(target);
60
- if (!node) {
61
- node = new GraphNode(target);
62
- this.nodes.set(target, node);
63
- }
64
- return node;
65
- }
66
- hasNode(target) {
67
- return !!this.nodes.get(target);
68
- }
69
- removeNode(target) {
70
- this.nodes.forEach((node) => {
71
- node.removeInput(target);
72
- node.removeOutput(target);
73
- });
74
- this.nodes.delete(target);
75
- }
76
- foldNode(target) {
77
- const nodeToRemove = this.nodes.get(target);
78
- if (!nodeToRemove) {
79
- return;
80
- }
81
- nodeToRemove.inputs.forEach((input) => {
82
- nodeToRemove.outputs.forEach((output) => {
83
- this.addEdge(input, output);
84
- });
85
- });
86
- this.removeNode(target);
87
- }
88
- removeIsolated() {
89
- const result = [];
90
- this.nodes.forEach((node) => {
91
- if (node.outputs.length === 0 && node.inputs.length === 0) {
92
- result.push(node);
93
- this.nodes.delete(node.id);
94
- }
95
- });
96
- return result;
97
- }
98
- addEdge(source, destination) {
99
- if (this.hasEdge(source, destination)) {
100
- return;
101
- }
102
- const sourceNode = this.addNode(source);
103
- const destinationNode = this.addNode(destination);
104
- sourceNode.addOutput(destinationNode.id);
105
- destinationNode.addInput(sourceNode.id);
106
- }
107
- removeEdge(source, destination) {
108
- const sourceNode = this.nodes.get(source);
109
- const destinationNode = this.nodes.get(destination);
110
- if (sourceNode && destinationNode) {
111
- sourceNode.removeOutput(destination);
112
- destinationNode.removeInput(source);
113
- }
114
- }
115
- hasEdge(source, destination) {
116
- const sourceNode = this.nodes.get(source);
117
- if (!sourceNode) {
118
- return false;
119
- }
120
- return !!sourceNode.outputs.find((id) => id === destination);
121
- }
122
- isReachable(source, destination) {
123
- return this.expandAllOutputs([source]).includes(destination);
124
- }
125
- rootNodes() {
126
- return [...this.nodes.keys()].filter((id) => !this.nodes.get(id)?.inputs.length);
127
- }
128
- expandOutputs(origin) {
129
- const result = [];
130
- origin.forEach((id) => {
131
- const node = this.nodes.get(id);
132
- if (node) {
133
- node.outputs.forEach((child) => {
134
- if (!origin.includes(child) && !result.includes(child)) {
135
- result.push(child);
136
- }
137
- });
138
- }
139
- });
140
- return result;
141
- }
142
- expandInputs(origin) {
143
- const result = [];
144
- origin.forEach((id) => {
145
- const node = this.nodes.get(id);
146
- if (node) {
147
- node.inputs.forEach((child) => {
148
- if (!origin.includes(child) && !result.includes(child)) {
149
- result.push(child);
150
- }
151
- });
152
- }
153
- });
154
- return result;
155
- }
156
- expandAllOutputs(origin) {
157
- const result = this.expandOutputs(origin);
158
- if (result.length === 0) {
159
- return [];
160
- }
161
- const marked = /* @__PURE__ */ new Map();
162
- origin.forEach((id) => marked.set(id, true));
163
- let position = 0;
164
- while (position < result.length) {
165
- const node = this.nodes.get(result[position]);
166
- if (node && !marked.get(node.id)) {
167
- marked.set(node.id, true);
168
- node.outputs.forEach((child) => {
169
- if (!marked.get(child) && !result.find((id) => id === child)) {
170
- result.push(child);
171
- }
172
- });
173
- }
174
- position += 1;
175
- }
176
- return result;
177
- }
178
- expandAllInputs(origin) {
179
- const result = this.expandInputs(origin);
180
- if (result.length === 0) {
181
- return [];
182
- }
183
- const marked = /* @__PURE__ */ new Map();
184
- origin.forEach((id) => marked.set(id, true));
185
- let position = 0;
186
- while (position < result.length) {
187
- const node = this.nodes.get(result[position]);
188
- if (node && !marked.get(node.id)) {
189
- marked.set(node.id, true);
190
- node.inputs.forEach((child) => {
191
- if (!marked.get(child) && !result.find((id) => id === child)) {
192
- result.push(child);
193
- }
194
- });
195
- }
196
- position += 1;
197
- }
198
- return result;
199
- }
200
- maximizePart(origin) {
201
- const outputs = this.expandAllOutputs(origin);
202
- const result = [...origin];
203
- this.topologicalOrder().filter((id) => outputs.includes(id)).forEach((id) => {
204
- const node = this.nodes.get(id);
205
- if (node?.inputs.every((parent) => result.includes(parent))) {
206
- result.push(id);
207
- }
208
- });
209
- return result;
210
- }
211
- topologicalOrder() {
212
- const result = [];
213
- const marked = /* @__PURE__ */ new Set();
214
- const nodeStack = [];
215
- this.nodes.forEach((node) => {
216
- if (marked.has(node.id)) {
217
- return;
218
- }
219
- nodeStack.push(node.id);
220
- while (nodeStack.length > 0) {
221
- const item = nodeStack[nodeStack.length - 1];
222
- if (marked.has(item)) {
223
- if (!result.find((id) => id === item)) {
224
- result.push(item);
225
- }
226
- nodeStack.pop();
227
- } else {
228
- marked.add(item);
229
- const itemNode = this.nodes.get(item);
230
- if (itemNode && itemNode.outputs.length > 0) {
231
- itemNode.outputs.forEach((child) => {
232
- if (!marked.has(child)) {
233
- nodeStack.push(child);
234
- }
235
- });
236
- }
237
- }
238
- }
239
- });
240
- return result.reverse();
241
- }
242
- /**
243
- * Stably reorders the given node ids so that, for this DAG, no node appears before a transitive
244
- * successor (edges: source → dependent; {@link GraphNode.outputs} lists dependents).
245
- */
246
- sortStable(target) {
247
- if (target.length <= 1) {
248
- return [...target];
249
- }
250
- const reachable = this.buildTransitiveClosureForSort();
251
- const testSet = /* @__PURE__ */ new Set();
252
- const result = [];
253
- for (const nodeId of [...target].reverse()) {
254
- const nodeReachable = reachable.get(nodeId) ?? /* @__PURE__ */ new Set();
255
- const needMove = testSet.has(nodeId);
256
- for (const childId of nodeReachable) {
257
- testSet.add(childId);
258
- }
259
- if (!needMove) {
260
- result.push(nodeId);
261
- continue;
262
- }
263
- let inserted = false;
264
- for (let index = 0; index < result.length; index++) {
265
- const parent = result[index];
266
- const parentReachable = reachable.get(parent) ?? /* @__PURE__ */ new Set();
267
- if (nodeReachable.has(parent)) {
268
- if (parentReachable.has(nodeId)) {
269
- result.push(nodeId);
270
- } else {
271
- result.splice(index, 0, nodeId);
272
- }
273
- inserted = true;
274
- break;
275
- }
276
- }
277
- if (!inserted) {
278
- result.push(nodeId);
279
- }
280
- }
281
- result.reverse();
282
- return result;
283
- }
284
- buildTransitiveClosureForSort() {
285
- const closure = /* @__PURE__ */ new Map();
286
- for (const node of this.nodes.values()) {
287
- closure.set(node.id, new Set(node.outputs));
288
- }
289
- const order = this.topologicalOrder();
290
- for (const nodeId of [...order].reverse()) {
291
- const node = this.at(nodeId);
292
- if (!node) {
293
- continue;
294
- }
295
- const nodeClosure = closure.get(nodeId) ?? /* @__PURE__ */ new Set();
296
- for (const parentId of node.inputs) {
297
- const parentClosure = closure.get(parentId) ?? /* @__PURE__ */ new Set();
298
- for (const childId of nodeClosure) {
299
- parentClosure.add(childId);
300
- }
301
- closure.set(parentId, parentClosure);
302
- }
303
- }
304
- return closure;
305
- }
306
- transitiveReduction() {
307
- const order = this.topologicalOrder();
308
- const marked = /* @__PURE__ */ new Map();
309
- order.forEach((nodeID) => {
310
- if (marked.get(nodeID)) {
311
- return;
312
- }
313
- const stack = [];
314
- stack.push({ id: nodeID, parents: [] });
315
- while (stack.length > 0) {
316
- const item = stack.splice(0, 1)[0];
317
- const node = this.nodes.get(item.id);
318
- if (node) {
319
- node.outputs.forEach((child) => {
320
- item.parents.forEach((parent) => this.removeEdge(parent, child));
321
- stack.push({ id: child, parents: [item.id, ...item.parents] });
322
- });
323
- }
324
- marked.set(item.id, true);
325
- }
326
- });
327
- }
328
- /**
329
- * Finds a cycle in the graph.
330
- *
331
- * @returns {NodeID[] | null} The cycle if found, otherwise `null`.
332
- * Uses non-recursive DFS.
333
- */
334
- findCycle() {
335
- const visited = /* @__PURE__ */ new Set();
336
- const nodeStack = /* @__PURE__ */ new Set();
337
- const parents = /* @__PURE__ */ new Map();
338
- for (const nodeId of this.nodes.keys()) {
339
- if (visited.has(nodeId)) {
340
- continue;
341
- }
342
- const callStack = [];
343
- callStack.push({ nodeId, parentId: null });
344
- while (callStack.length > 0) {
345
- const { nodeId: nodeId2, parentId } = callStack[callStack.length - 1];
346
- if (visited.has(nodeId2)) {
347
- nodeStack.delete(nodeId2);
348
- callStack.pop();
349
- continue;
350
- }
351
- visited.add(nodeId2);
352
- nodeStack.add(nodeId2);
353
- if (parentId !== null) {
354
- parents.set(nodeId2, parentId);
355
- }
356
- const currentNode = this.nodes.get(nodeId2);
357
- for (const child of currentNode.outputs) {
358
- if (!visited.has(child)) {
359
- callStack.push({ nodeId: child, parentId: nodeId2 });
360
- continue;
361
- }
362
- if (!nodeStack.has(child)) {
363
- continue;
364
- }
365
- const cycle = [];
366
- let current = nodeId2;
367
- cycle.push(child);
368
- while (current !== child) {
369
- cycle.push(current);
370
- current = parents.get(current);
371
- }
372
- cycle.push(child);
373
- cycle.reverse();
374
- return cycle;
375
- }
376
- }
377
- }
378
- return null;
379
- }
380
- };
381
-
382
- // src/parsing/ast.ts
383
- var TOKEN_ERROR = 0;
384
-
385
- // src/rslang/parser/token.ts
386
- var TokenID = {
387
- // Global, local IDs and literals
388
- ERROR: TOKEN_ERROR,
389
- ID_LOCAL: 258,
390
- ID_GLOBAL: 259,
391
- ID_FUNCTION: 260,
392
- ID_PREDICATE: 261,
393
- ID_RADICAL: 262,
394
- LIT_INTEGER: 263,
395
- LIT_WHOLE_NUMBERS: 264,
396
- LIT_EMPTYSET: 265,
397
- // Arithmetic
398
- PLUS: 266,
399
- MINUS: 267,
400
- MULTIPLY: 268,
401
- // Integer predicate symbols
402
- GREATER: 269,
403
- LESSER: 270,
404
- GREATER_OR_EQ: 271,
405
- LESSER_OR_EQ: 272,
406
- // Equality comparison
407
- EQUAL: 273,
408
- NOTEQUAL: 274,
409
- // Logic predicate symbols
410
- QUANTOR_UNIVERSAL: 275,
411
- QUANTOR_EXISTS: 276,
412
- LOGIC_NOT: 277,
413
- LOGIC_EQUIVALENT: 278,
414
- LOGIC_IMPLICATION: 279,
415
- LOGIC_OR: 280,
416
- LOGIC_AND: 281,
417
- // Set theory predicate symbols
418
- SET_IN: 282,
419
- SET_NOT_IN: 283,
420
- SUBSET: 284,
421
- SUBSET_OR_EQ: 285,
422
- NOT_SUBSET: 286,
423
- // Set theory operators
424
- DECART: 287,
425
- SET_UNION: 288,
426
- SET_INTERSECTION: 289,
427
- SET_MINUS: 290,
428
- SET_SYMMETRIC_MINUS: 291,
429
- BOOLEAN: 292,
430
- // Structure operations
431
- BIGPR: 293,
432
- SMALLPR: 294,
433
- FILTER: 295,
434
- CARD: 296,
435
- BOOL: 297,
436
- DEBOOL: 298,
437
- REDUCE: 299,
438
- // Term constructions prefixes
439
- DECLARATIVE: 300,
440
- RECURSIVE: 301,
441
- IMPERATIVE: 302,
442
- ITERATE: 303,
443
- ASSIGN: 304,
444
- // Punctuation
445
- PUNCTUATION_DEFINE: 305,
446
- PUNCTUATION_STRUCT: 306,
447
- PUNCTUATION_PL: 307,
448
- PUNCTUATION_PR: 308,
449
- PUNCTUATION_CL: 309,
450
- PUNCTUATION_CR: 310,
451
- PUNCTUATION_SL: 311,
452
- PUNCTUATION_SR: 312,
453
- PUNCTUATION_BAR: 313,
454
- PUNCTUATION_COMMA: 314,
455
- PUNCTUATION_SEMICOLON: 315,
456
- // ======= Non-terminal tokens =========
457
- NT_ENUM_DECL: 316,
458
- NT_TUPLE: 317,
459
- NT_ENUMERATION: 318,
460
- NT_TUPLE_DECL: 319,
461
- NT_ARG_DECL: 320,
462
- NT_FUNC_DEFINITION: 321,
463
- NT_ARGUMENTS: 322,
464
- NT_FUNC_CALL: 323,
465
- NT_DECLARATIVE_EXPR: 324,
466
- NT_IMPERATIVE_EXPR: 325,
467
- NT_RECURSIVE_FULL: 326,
468
- NT_RECURSIVE_SHORT: 327,
469
- // ======= Helper tokens ========
470
- INTERRUPT: 328,
471
- END: 329
472
- };
473
-
474
- // src/rslang/api.ts
475
- var GLOBALS_REGEXP = /[XCSADFPTN]\d+/g;
476
- var TYPIFICATION_SET = /^ℬ+\([ℬ\(((X|C)\d+|Z)\)×]*\)$/g;
477
- function isSetTypification(text) {
478
- return !!text.match(TYPIFICATION_SET);
479
- }
480
- function applyAliasMapping(target, mapping) {
481
- return applyPattern(target, mapping, GLOBALS_REGEXP);
482
- }
483
- function applyTypificationMapping(target, mapping) {
484
- const modified = applyAliasMapping(target, mapping);
485
- if (modified === target) {
486
- return target;
487
- }
488
- const deleteBrackets = [];
489
- const positions = [];
490
- const booleans = [];
491
- let boolCount = 0;
492
- let stackSize = 0;
493
- for (let i = 0; i < modified.length; i++) {
494
- const char = modified[i];
495
- if (char === "\u212C") {
496
- boolCount++;
497
- continue;
498
- }
499
- if (char === "(") {
500
- stackSize++;
501
- positions.push(i);
502
- booleans.push(boolCount);
503
- }
504
- boolCount = 0;
505
- if (char === ")") {
506
- if (i < modified.length - 1 && modified[i + 1] === ")" && stackSize > 1 && positions[stackSize - 2] + booleans[stackSize - 1] + 1 === positions[stackSize - 1]) {
507
- deleteBrackets.push(i);
508
- deleteBrackets.push(positions[stackSize - 2]);
509
- }
510
- if (i === modified.length - 1 && stackSize === 1 && positions[0] === 0) {
511
- deleteBrackets.push(i);
512
- deleteBrackets.push(positions[0]);
513
- }
514
- stackSize--;
515
- positions.pop();
516
- booleans.pop();
517
- }
518
- }
519
- let result = "";
520
- for (let i = 0; i < modified.length; i++) {
521
- if (!deleteBrackets.includes(i)) {
522
- result += modified[i];
523
- }
524
- }
525
- return result;
526
- }
527
- function applyPattern(text, mapping, pattern) {
528
- if (text === "" || pattern === null) {
529
- return text;
530
- }
531
- let posInput = 0;
532
- let output = "";
533
- const patternMatches = text.matchAll(pattern);
534
- for (const segment of patternMatches) {
535
- const entity = segment[0];
536
- const start = segment.index ?? 0;
537
- if (entity in mapping) {
538
- output += text.substring(posInput, start);
539
- output += mapping[entity];
540
- posInput = start + segment[0].length;
541
- }
542
- }
543
- output += text.substring(posInput);
544
- return output;
545
- }
546
-
547
- // src/rslang/semantic/typification.ts
548
- var TypeID = {
549
- anyTypification: 1,
550
- integer: 2,
551
- basic: 3,
552
- tuple: 4,
553
- collection: 5,
554
- logic: 6,
555
- function: 7,
556
- predicate: 8
557
- };
558
- var TypeClass = {
559
- logic: 1,
560
- typification: 2,
561
- function: 3,
562
- predicate: 4
563
- };
564
- var LogicT = { typeID: TypeID.logic };
565
- var IntegerT = {
566
- typeID: TypeID.integer,
567
- isOrdered: true,
568
- isArithmetic: true,
569
- isIntegerCompatible: true
570
- };
571
- var AnyTypificationT = { typeID: TypeID.anyTypification };
572
- var EmptySetT = bool(AnyTypificationT);
573
- function bool(base) {
574
- return { typeID: TypeID.collection, base };
575
- }
576
-
577
- // src/rslang/labels.ts
578
- var INTEGER_TYPE_NAME = "Z";
579
- var ANY_TYPE_NAME = "R0";
580
- var LOGIC_TYPE_NAME = "Logic";
581
- var labelTokenRecord = {
582
- [TokenID.DECART]: "\xD7",
583
- [TokenID.PUNCTUATION_PL]: "( )",
584
- [TokenID.PUNCTUATION_SL]: "[ ]",
585
- [TokenID.QUANTOR_UNIVERSAL]: "\u2200",
586
- [TokenID.QUANTOR_EXISTS]: "\u2203",
587
- [TokenID.LOGIC_NOT]: "\xAC",
588
- [TokenID.LOGIC_AND]: "&",
589
- [TokenID.LOGIC_OR]: "\u2228",
590
- [TokenID.LOGIC_IMPLICATION]: "\u21D2",
591
- [TokenID.LOGIC_EQUIVALENT]: "\u21D4",
592
- [TokenID.LIT_EMPTYSET]: "\u2205",
593
- [TokenID.LIT_WHOLE_NUMBERS]: "Z",
594
- [TokenID.MULTIPLY]: "*",
595
- [TokenID.EQUAL]: "=",
596
- [TokenID.NOTEQUAL]: "\u2260",
597
- [TokenID.GREATER_OR_EQ]: "\u2265",
598
- [TokenID.LESSER_OR_EQ]: "\u2264",
599
- [TokenID.SET_IN]: "\u2208",
600
- [TokenID.SET_NOT_IN]: "\u2209",
601
- [TokenID.SUBSET_OR_EQ]: "\u2286",
602
- [TokenID.SUBSET]: "\u2282",
603
- [TokenID.NOT_SUBSET]: "\u2284",
604
- [TokenID.SET_INTERSECTION]: "\u2229",
605
- [TokenID.SET_UNION]: "\u222A",
606
- [TokenID.SET_MINUS]: "\\",
607
- [TokenID.SET_SYMMETRIC_MINUS]: "\u2206",
608
- [TokenID.BOOLEAN]: "\u212C()",
609
- [TokenID.NT_DECLARATIVE_EXPR]: "D{}",
610
- [TokenID.NT_IMPERATIVE_EXPR]: "I{}",
611
- [TokenID.NT_RECURSIVE_FULL]: "R{}",
612
- [TokenID.BIGPR]: "Pr1()",
613
- [TokenID.SMALLPR]: "pr1()",
614
- [TokenID.FILTER]: "Fi1[]()",
615
- [TokenID.REDUCE]: "red()",
616
- [TokenID.CARD]: "card()",
617
- [TokenID.BOOL]: "bool()",
618
- [TokenID.DEBOOL]: "debool()",
619
- [TokenID.ASSIGN]: ":=",
620
- [TokenID.ITERATE]: ":\u2208"
621
- };
622
- function labelType(type) {
623
- if (!type) {
624
- return "N/A";
625
- }
626
- switch (type.typeID) {
627
- case TypeID.anyTypification:
628
- return ANY_TYPE_NAME;
629
- case TypeID.integer:
630
- return INTEGER_TYPE_NAME;
631
- case TypeID.basic:
632
- return type.baseID;
633
- case TypeID.tuple:
634
- return type.factors.map((factor) => factor.typeID === TypeID.tuple ? `(${labelType(factor)})` : labelType(factor)).join("\xD7");
635
- case TypeID.collection:
636
- return type.base.typeID === TypeID.collection ? `\u212C${labelType(type.base)}` : `\u212C(${labelType(type.base)})`;
637
- case TypeID.logic:
638
- return LOGIC_TYPE_NAME;
639
- case TypeID.predicate:
640
- case TypeID.function:
641
- const argsText = type.args.map((arg) => labelType(arg.type)).join(", ");
642
- return `[${argsText}] \u2192 ${labelType(type.result)}`;
643
- }
644
- }
645
-
646
- // src/rslang/semantic/typification-api.ts
647
- var TypeIDToClass = {
648
- [TypeID.anyTypification]: TypeClass.typification,
649
- [TypeID.integer]: TypeClass.typification,
650
- [TypeID.basic]: TypeClass.typification,
651
- [TypeID.tuple]: TypeClass.typification,
652
- [TypeID.collection]: TypeClass.typification,
653
- [TypeID.logic]: TypeClass.logic,
654
- [TypeID.function]: TypeClass.function,
655
- [TypeID.predicate]: TypeClass.predicate
656
- };
657
- function extractBases(target) {
658
- return new Set(extractBasesImpl(target));
659
- }
660
- function extractBasesImpl(target) {
661
- switch (target.typeID) {
662
- case TypeID.basic:
663
- return [target.baseID];
664
- case TypeID.collection:
665
- return extractBasesImpl(target.base);
666
- case TypeID.tuple:
667
- return target.factors.reduce((result2, factor) => result2.concat(extractBasesImpl(factor)), []);
668
- case TypeID.function:
669
- const result = extractBasesImpl(target.result);
670
- return target.args.reduce((result2, arg) => result2.concat(extractBasesImpl(arg.type)), result);
671
- case TypeID.predicate:
672
- return target.args.reduce((result2, arg) => result2.concat(extractBasesImpl(arg.type)), []);
673
- default:
674
- return [];
675
- }
676
- }
677
-
678
- // src/library/oss.ts
679
- var NodeType = {
680
- OPERATION: 1,
681
- BLOCK: 2
682
- };
683
- var SubstitutionErrorType = {
684
- invalidIDs: 0,
685
- incorrectCst: 1,
686
- invalidClasses: 2,
687
- invalidBasic: 3,
688
- invalidConstant: 4,
689
- typificationCycle: 5,
690
- baseSubstitutionNotSet: 6,
691
- unequalTypification: 7,
692
- unequalExpressions: 8,
693
- unequalArgsCount: 9,
694
- unequalArgs: 10,
695
- invalidNominal: 11
696
- };
697
-
698
- // src/library/rsform.ts
699
- var CstType = {
700
- NOMINAL: "nominal",
701
- BASE: "basic",
702
- STRUCTURED: "structure",
703
- TERM: "term",
704
- AXIOM: "axiom",
705
- FUNCTION: "function",
706
- PREDICATE: "predicate",
707
- CONSTANT: "constant",
708
- STATEMENT: "statement"
709
- };
710
- var CstClass = {
711
- NOMINAL: "nominal",
712
- BASIC: "basic",
713
- DERIVED: "derived",
714
- STATEMENT: "statement",
715
- TEMPLATE: "template"
716
- };
717
-
718
- // src/library/oss-api.ts
719
- var STARTING_SUB_INDEX = 900;
1
+ import { Graph } from "../graph/graph.js";
2
+ import { NodeType, SubstitutionErrorType } from "./oss.js";
3
+ import { labelType } from "../rslang/labels.js";
4
+ import { extractBases } from "../rslang/semantic/typification-api.js";
5
+ import { CstClass, CstType } from "./rsform.js";
6
+ import { applyAliasMapping, applyTypificationMapping, isSetTypification } from "../rslang/api.js";
7
+ import "../graph/index.js";
8
+ //#region src/library/oss-api.ts
9
+ /**
10
+ * Module: API for OperationSystem.
11
+ */
12
+ const STARTING_SUB_INDEX = 900;
720
13
  function constructNodeID(type, itemID) {
721
- return type === NodeType.OPERATION ? "o" + String(itemID) : "b" + String(itemID);
14
+ return type === NodeType.OPERATION ? "o" + String(itemID) : "b" + String(itemID);
722
15
  }
16
+ /** Sorts library items relevant for the specified {@link OperationSchema}. */
723
17
  function sortItemsForOSS(oss, items) {
724
- const result = items.filter((item) => item.location === oss.location);
725
- for (const item of items) {
726
- if (item.visible && item.owner === oss.owner && !result.includes(item)) {
727
- result.push(item);
728
- }
729
- }
730
- for (const item of items) {
731
- if (item.visible && !result.includes(item)) {
732
- result.push(item);
733
- }
734
- }
735
- for (const item of items) {
736
- if (!result.includes(item)) {
737
- result.push(item);
738
- }
739
- }
740
- return result;
18
+ const result = items.filter((item) => item.location === oss.location);
19
+ for (const item of items) if (item.visible && item.owner === oss.owner && !result.includes(item)) result.push(item);
20
+ for (const item of items) if (item.visible && !result.includes(item)) result.push(item);
21
+ for (const item of items) if (!result.includes(item)) result.push(item);
22
+ return result;
741
23
  }
24
+ /** Validator for Substitution table. */
742
25
  var SubstitutionValidator = class {
743
- errors = [];
744
- suggestions = [];
745
- schemas;
746
- substitutions;
747
- constituents = /* @__PURE__ */ new Set();
748
- originals = /* @__PURE__ */ new Set();
749
- mapping = /* @__PURE__ */ new Map();
750
- cstByID = /* @__PURE__ */ new Map();
751
- schemaByID = /* @__PURE__ */ new Map();
752
- schemaByCst = /* @__PURE__ */ new Map();
753
- constructor(schemas, substitutions) {
754
- this.schemas = schemas;
755
- this.substitutions = substitutions;
756
- if (schemas.length === 0 || substitutions.length === 0) {
757
- return;
758
- }
759
- for (const schema of schemas) {
760
- this.schemaByID.set(schema.id, schema);
761
- this.mapping.set(schema.id, {});
762
- for (const item of schema.items) {
763
- this.cstByID.set(item.id, item);
764
- this.schemaByCst.set(item.id, schema);
765
- }
766
- }
767
- let index = STARTING_SUB_INDEX;
768
- for (const item of substitutions) {
769
- this.constituents.add(item.original);
770
- this.constituents.add(item.substitution);
771
- this.originals.add(item.original);
772
- const original = this.cstByID.get(item.original);
773
- const substitution = this.cstByID.get(item.substitution);
774
- if (!original || !substitution) {
775
- return;
776
- }
777
- index++;
778
- const newAlias = `${substitution.alias[0]}${index}`;
779
- this.mapping.get(original.schema)[original.alias] = newAlias;
780
- this.mapping.get(substitution.schema)[substitution.alias] = newAlias;
781
- }
782
- }
783
- validate() {
784
- this.errors = [];
785
- this.suggestions = [];
786
- this.calculateSuggestions();
787
- if (this.substitutions.length === 0) {
788
- return true;
789
- }
790
- if (!this.checkTypes()) {
791
- return false;
792
- }
793
- if (!this.checkCycles()) {
794
- return false;
795
- }
796
- if (!this.checkSubstitutions()) {
797
- return false;
798
- }
799
- return true;
800
- }
801
- calculateSuggestions() {
802
- const candidates = /* @__PURE__ */ new Map();
803
- const minors = /* @__PURE__ */ new Set();
804
- const schemaByCst = /* @__PURE__ */ new Map();
805
- for (const schema of this.schemas) {
806
- for (const cst of schema.items) {
807
- if (this.originals.has(cst.id)) {
808
- continue;
809
- }
810
- if (cst.cst_class === CstClass.BASIC || cst.definition_formal.length === 0) {
811
- continue;
812
- }
813
- const inputs = schema.graph.at(cst.id).inputs;
814
- if (inputs.some((id) => !this.constituents.has(id))) {
815
- continue;
816
- }
817
- if (inputs.some((id) => this.originals.has(id))) {
818
- minors.add(cst.id);
819
- }
820
- candidates.set(cst.id, applyAliasMapping(cst.definition_formal, this.mapping.get(schema.id)).replace(" ", ""));
821
- schemaByCst.set(cst.id, schema);
822
- }
823
- }
824
- for (const [key1, value1] of candidates) {
825
- for (const [key2, value2] of candidates) {
826
- if (key1 >= key2) {
827
- continue;
828
- }
829
- if (schemaByCst.get(key1) === schemaByCst.get(key2)) {
830
- continue;
831
- }
832
- if (value1 != value2) {
833
- continue;
834
- }
835
- if (minors.has(key2)) {
836
- this.suggestions.push({
837
- original: key2,
838
- substitution: key1
839
- });
840
- } else {
841
- this.suggestions.push({
842
- original: key1,
843
- substitution: key2
844
- });
845
- }
846
- }
847
- }
848
- }
849
- checkTypes() {
850
- for (const item of this.substitutions) {
851
- const original = this.cstByID.get(item.original);
852
- const substitution = this.cstByID.get(item.substitution);
853
- if (!original || !substitution) {
854
- return this.reportError(SubstitutionErrorType.invalidIDs, []);
855
- }
856
- if (substitution.analysis && original.analysis && (!original.analysis.success || !substitution.analysis.success)) {
857
- return this.reportError(SubstitutionErrorType.incorrectCst, [substitution.alias, original.alias]);
858
- }
859
- switch (substitution.cst_type) {
860
- case CstType.NOMINAL: {
861
- if (original.cst_type !== CstType.NOMINAL) {
862
- return this.reportError(SubstitutionErrorType.invalidNominal, [substitution.alias, original.alias]);
863
- }
864
- break;
865
- }
866
- case CstType.BASE: {
867
- if (original.cst_type !== CstType.BASE && original.cst_type !== CstType.CONSTANT && original.cst_type !== CstType.NOMINAL) {
868
- return this.reportError(SubstitutionErrorType.invalidBasic, [substitution.alias, original.alias]);
869
- }
870
- break;
871
- }
872
- case CstType.CONSTANT: {
873
- if (original.cst_type !== CstType.CONSTANT) {
874
- return this.reportError(SubstitutionErrorType.invalidConstant, [substitution.alias, original.alias]);
875
- }
876
- break;
877
- }
878
- case CstType.AXIOM:
879
- case CstType.STATEMENT: {
880
- if (original.cst_type !== CstType.AXIOM && original.cst_type !== CstType.STATEMENT) {
881
- return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
882
- }
883
- break;
884
- }
885
- case CstType.FUNCTION: {
886
- if (original.cst_type !== CstType.FUNCTION) {
887
- return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
888
- }
889
- break;
890
- }
891
- case CstType.PREDICATE: {
892
- if (original.cst_type !== CstType.PREDICATE) {
893
- return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
894
- }
895
- break;
896
- }
897
- case CstType.TERM:
898
- case CstType.STRUCTURED: {
899
- if (original.cst_type !== CstType.TERM && original.cst_type !== CstType.STRUCTURED && original.cst_type !== CstType.BASE) {
900
- return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
901
- }
902
- break;
903
- }
904
- }
905
- }
906
- return true;
907
- }
908
- checkCycles() {
909
- const graph = new Graph();
910
- for (const schema of this.schemas) {
911
- for (const cst of schema.items) {
912
- if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT) {
913
- graph.addNode(cst.id);
914
- }
915
- }
916
- }
917
- for (const item of this.substitutions) {
918
- const original = this.cstByID.get(item.original);
919
- const substitution = this.cstByID.get(item.substitution);
920
- for (const cst of [original, substitution]) {
921
- if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT) {
922
- continue;
923
- }
924
- graph.addNode(cst.id);
925
- if (cst.effectiveType === null) {
926
- continue;
927
- }
928
- const parents = extractBases(cst.effectiveType);
929
- if (parents.size === 0) {
930
- continue;
931
- }
932
- const schema = this.schemaByID.get(cst.schema);
933
- for (const alias of parents) {
934
- const parent = schema.cstByAlias.get(alias);
935
- if (parent) {
936
- graph.addEdge(parent.id, cst.id);
937
- }
938
- }
939
- }
940
- graph.addEdge(substitution.id, original.id);
941
- }
942
- const cycle = graph.findCycle();
943
- if (cycle !== null) {
944
- const cycleMsg = cycle.map((id) => {
945
- const cst = this.cstByID.get(id);
946
- const schema = this.schemaByID.get(cst.schema);
947
- return `[${schema.alias}]-${cst.alias}`;
948
- }).join(", ");
949
- return this.reportError(SubstitutionErrorType.typificationCycle, [cycleMsg]);
950
- }
951
- return true;
952
- }
953
- checkSubstitutions() {
954
- const baseMappings = this.prepareBaseMappings();
955
- const typeMappings = this.calculateSubstituteMappings(baseMappings);
956
- if (typeMappings === null) {
957
- return false;
958
- }
959
- for (const item of this.substitutions) {
960
- const original = this.cstByID.get(item.original);
961
- if (original.cst_type === CstType.BASE || original.cst_type === CstType.CONSTANT || original.cst_type === CstType.NOMINAL) {
962
- continue;
963
- }
964
- const substitution = this.cstByID.get(item.substitution);
965
- if (original.cst_type === substitution.cst_type && original.cst_class !== CstClass.BASIC) {
966
- if (!this.checkEqual(original, substitution)) {
967
- this.reportError(SubstitutionErrorType.unequalExpressions, [substitution.alias, original.alias]);
968
- }
969
- }
970
- if (original.analysis.success !== substitution.analysis.success) {
971
- return this.reportError(SubstitutionErrorType.unequalTypification, [substitution.alias, original.alias]);
972
- }
973
- if (!original.analysis.success || !substitution.analysis.success) {
974
- continue;
975
- }
976
- const originalType = applyTypificationMapping(
977
- applyAliasMapping(labelType(original.effectiveType), baseMappings.get(original.schema)),
978
- typeMappings
979
- );
980
- const substitutionType = applyTypificationMapping(
981
- applyAliasMapping(labelType(substitution.effectiveType), baseMappings.get(substitution.schema)),
982
- typeMappings
983
- );
984
- if (originalType !== substitutionType) {
985
- return this.reportError(SubstitutionErrorType.unequalTypification, [substitution.alias, original.alias]);
986
- }
987
- }
988
- return true;
989
- }
990
- prepareBaseMappings() {
991
- const result = /* @__PURE__ */ new Map();
992
- let baseCount = 0;
993
- let constCount = 0;
994
- for (const schema of this.schemas) {
995
- const mapping = {};
996
- for (const cst of schema.items) {
997
- if (cst.cst_type === CstType.BASE) {
998
- baseCount++;
999
- mapping[cst.alias] = `X${baseCount}`;
1000
- } else if (cst.cst_type === CstType.CONSTANT) {
1001
- constCount++;
1002
- mapping[cst.alias] = `C${constCount}`;
1003
- }
1004
- result.set(schema.id, mapping);
1005
- }
1006
- }
1007
- return result;
1008
- }
1009
- calculateSubstituteMappings(baseMappings) {
1010
- const result = {};
1011
- const processed = /* @__PURE__ */ new Set();
1012
- for (const item of this.substitutions) {
1013
- const original = this.cstByID.get(item.original);
1014
- if (original.cst_type !== CstType.BASE && original.cst_type !== CstType.CONSTANT) {
1015
- continue;
1016
- }
1017
- const originalAlias = baseMappings.get(original.schema)[original.alias];
1018
- const substitution = this.cstByID.get(item.substitution);
1019
- let substitutionText = "";
1020
- if (substitution.cst_type === original.cst_type || !substitution.effectiveType) {
1021
- substitutionText = baseMappings.get(substitution.schema)[substitution.alias];
1022
- } else {
1023
- const typeText = labelType(substitution.effectiveType);
1024
- substitutionText = applyAliasMapping(typeText, baseMappings.get(substitution.schema));
1025
- substitutionText = applyTypificationMapping(substitutionText, result);
1026
- if (!isSetTypification(substitutionText)) {
1027
- this.reportError(SubstitutionErrorType.baseSubstitutionNotSet, [substitution.alias, typeText]);
1028
- return null;
1029
- }
1030
- if (substitutionText.includes("\xD7") || substitutionText.startsWith("\u212C\u212C")) {
1031
- substitutionText = substitutionText.slice(1);
1032
- } else {
1033
- substitutionText = substitutionText.slice(2, -1);
1034
- }
1035
- }
1036
- for (const prevAlias of processed) {
1037
- result[prevAlias] = applyTypificationMapping(result[prevAlias], { [originalAlias]: substitutionText });
1038
- }
1039
- result[originalAlias] = substitutionText;
1040
- processed.add(originalAlias);
1041
- }
1042
- return result;
1043
- }
1044
- checkEqual(left, right) {
1045
- const schema1 = this.schemaByID.get(left.schema);
1046
- const inputs1 = schema1.graph.at(left.id).inputs;
1047
- if (inputs1.some((id) => !this.constituents.has(id))) {
1048
- return false;
1049
- }
1050
- const schema2 = this.schemaByID.get(right.schema);
1051
- const inputs2 = schema2.graph.at(right.id).inputs;
1052
- if (inputs2.some((id) => !this.constituents.has(id))) {
1053
- return false;
1054
- }
1055
- const expression1 = applyAliasMapping(left.definition_formal, this.mapping.get(schema1.id));
1056
- const expression2 = applyAliasMapping(right.definition_formal, this.mapping.get(schema2.id));
1057
- return expression1.replace(" ", "") === expression2.replace(" ", "");
1058
- }
1059
- reportError(errorType, params) {
1060
- this.errors.push({
1061
- errorType,
1062
- params
1063
- });
1064
- return false;
1065
- }
26
+ errors = [];
27
+ suggestions = [];
28
+ schemas;
29
+ substitutions;
30
+ constituents = /* @__PURE__ */ new Set();
31
+ originals = /* @__PURE__ */ new Set();
32
+ mapping = /* @__PURE__ */ new Map();
33
+ cstByID = /* @__PURE__ */ new Map();
34
+ schemaByID = /* @__PURE__ */ new Map();
35
+ schemaByCst = /* @__PURE__ */ new Map();
36
+ constructor(schemas, substitutions) {
37
+ this.schemas = schemas;
38
+ this.substitutions = substitutions;
39
+ if (schemas.length === 0 || substitutions.length === 0) return;
40
+ for (const schema of schemas) {
41
+ this.schemaByID.set(schema.id, schema);
42
+ this.mapping.set(schema.id, {});
43
+ for (const item of schema.items) {
44
+ this.cstByID.set(item.id, item);
45
+ this.schemaByCst.set(item.id, schema);
46
+ }
47
+ }
48
+ let index = STARTING_SUB_INDEX;
49
+ for (const item of substitutions) {
50
+ this.constituents.add(item.original);
51
+ this.constituents.add(item.substitution);
52
+ this.originals.add(item.original);
53
+ const original = this.cstByID.get(item.original);
54
+ const substitution = this.cstByID.get(item.substitution);
55
+ if (!original || !substitution) return;
56
+ index++;
57
+ const newAlias = `${substitution.alias[0]}${index}`;
58
+ this.mapping.get(original.schema)[original.alias] = newAlias;
59
+ this.mapping.get(substitution.schema)[substitution.alias] = newAlias;
60
+ }
61
+ }
62
+ validate() {
63
+ this.errors = [];
64
+ this.suggestions = [];
65
+ this.calculateSuggestions();
66
+ if (this.substitutions.length === 0) return true;
67
+ if (!this.checkTypes()) return false;
68
+ if (!this.checkCycles()) return false;
69
+ if (!this.checkSubstitutions()) return false;
70
+ return true;
71
+ }
72
+ calculateSuggestions() {
73
+ const candidates = /* @__PURE__ */ new Map();
74
+ const minors = /* @__PURE__ */ new Set();
75
+ const schemaByCst = /* @__PURE__ */ new Map();
76
+ for (const schema of this.schemas) for (const cst of schema.items) {
77
+ if (this.originals.has(cst.id)) continue;
78
+ if (cst.cst_class === CstClass.BASIC || cst.definition_formal.length === 0) continue;
79
+ const inputs = schema.graph.at(cst.id).inputs;
80
+ if (inputs.some((id) => !this.constituents.has(id))) continue;
81
+ if (inputs.some((id) => this.originals.has(id))) minors.add(cst.id);
82
+ candidates.set(cst.id, applyAliasMapping(cst.definition_formal, this.mapping.get(schema.id)).replace(" ", ""));
83
+ schemaByCst.set(cst.id, schema);
84
+ }
85
+ for (const [key1, value1] of candidates) for (const [key2, value2] of candidates) {
86
+ if (key1 >= key2) continue;
87
+ if (schemaByCst.get(key1) === schemaByCst.get(key2)) continue;
88
+ if (value1 != value2) continue;
89
+ if (minors.has(key2)) this.suggestions.push({
90
+ original: key2,
91
+ substitution: key1
92
+ });
93
+ else this.suggestions.push({
94
+ original: key1,
95
+ substitution: key2
96
+ });
97
+ }
98
+ }
99
+ checkTypes() {
100
+ for (const item of this.substitutions) {
101
+ const original = this.cstByID.get(item.original);
102
+ const substitution = this.cstByID.get(item.substitution);
103
+ if (!original || !substitution) return this.reportError(SubstitutionErrorType.invalidIDs, []);
104
+ if (substitution.analysis && original.analysis && (!original.analysis.success || !substitution.analysis.success)) return this.reportError(SubstitutionErrorType.incorrectCst, [substitution.alias, original.alias]);
105
+ switch (substitution.cst_type) {
106
+ case CstType.NOMINAL:
107
+ if (original.cst_type !== CstType.NOMINAL) return this.reportError(SubstitutionErrorType.invalidNominal, [substitution.alias, original.alias]);
108
+ break;
109
+ case CstType.BASE:
110
+ if (original.cst_type !== CstType.BASE && original.cst_type !== CstType.CONSTANT && original.cst_type !== CstType.NOMINAL) return this.reportError(SubstitutionErrorType.invalidBasic, [substitution.alias, original.alias]);
111
+ break;
112
+ case CstType.CONSTANT:
113
+ if (original.cst_type !== CstType.CONSTANT) return this.reportError(SubstitutionErrorType.invalidConstant, [substitution.alias, original.alias]);
114
+ break;
115
+ case CstType.AXIOM:
116
+ case CstType.STATEMENT:
117
+ if (original.cst_type !== CstType.AXIOM && original.cst_type !== CstType.STATEMENT) return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
118
+ break;
119
+ case CstType.FUNCTION:
120
+ if (original.cst_type !== CstType.FUNCTION) return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
121
+ break;
122
+ case CstType.PREDICATE:
123
+ if (original.cst_type !== CstType.PREDICATE) return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
124
+ break;
125
+ case CstType.TERM:
126
+ case CstType.STRUCTURED:
127
+ if (original.cst_type !== CstType.TERM && original.cst_type !== CstType.STRUCTURED && original.cst_type !== CstType.BASE) return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
128
+ break;
129
+ }
130
+ }
131
+ return true;
132
+ }
133
+ checkCycles() {
134
+ const graph = new Graph();
135
+ for (const schema of this.schemas) for (const cst of schema.items) if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT) graph.addNode(cst.id);
136
+ for (const item of this.substitutions) {
137
+ const original = this.cstByID.get(item.original);
138
+ const substitution = this.cstByID.get(item.substitution);
139
+ for (const cst of [original, substitution]) {
140
+ if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT) continue;
141
+ graph.addNode(cst.id);
142
+ if (cst.effectiveType === null) continue;
143
+ const parents = extractBases(cst.effectiveType);
144
+ if (parents.size === 0) continue;
145
+ const schema = this.schemaByID.get(cst.schema);
146
+ for (const alias of parents) {
147
+ const parent = schema.cstByAlias.get(alias);
148
+ if (parent) graph.addEdge(parent.id, cst.id);
149
+ }
150
+ }
151
+ graph.addEdge(substitution.id, original.id);
152
+ }
153
+ const cycle = graph.findCycle();
154
+ if (cycle !== null) {
155
+ const cycleMsg = cycle.map((id) => {
156
+ const cst = this.cstByID.get(id);
157
+ return `[${this.schemaByID.get(cst.schema).alias}]-${cst.alias}`;
158
+ }).join(", ");
159
+ return this.reportError(SubstitutionErrorType.typificationCycle, [cycleMsg]);
160
+ }
161
+ return true;
162
+ }
163
+ checkSubstitutions() {
164
+ const baseMappings = this.prepareBaseMappings();
165
+ const typeMappings = this.calculateSubstituteMappings(baseMappings);
166
+ if (typeMappings === null) return false;
167
+ for (const item of this.substitutions) {
168
+ const original = this.cstByID.get(item.original);
169
+ if (original.cst_type === CstType.BASE || original.cst_type === CstType.CONSTANT || original.cst_type === CstType.NOMINAL) continue;
170
+ const substitution = this.cstByID.get(item.substitution);
171
+ if (original.cst_type === substitution.cst_type && original.cst_class !== CstClass.BASIC) {
172
+ if (!this.checkEqual(original, substitution)) this.reportError(SubstitutionErrorType.unequalExpressions, [substitution.alias, original.alias]);
173
+ }
174
+ if (original.analysis.success !== substitution.analysis.success) return this.reportError(SubstitutionErrorType.unequalTypification, [substitution.alias, original.alias]);
175
+ if (!original.analysis.success || !substitution.analysis.success) continue;
176
+ if (applyTypificationMapping(applyAliasMapping(labelType(original.effectiveType), baseMappings.get(original.schema)), typeMappings) !== applyTypificationMapping(applyAliasMapping(labelType(substitution.effectiveType), baseMappings.get(substitution.schema)), typeMappings)) return this.reportError(SubstitutionErrorType.unequalTypification, [substitution.alias, original.alias]);
177
+ }
178
+ return true;
179
+ }
180
+ prepareBaseMappings() {
181
+ const result = /* @__PURE__ */ new Map();
182
+ let baseCount = 0;
183
+ let constCount = 0;
184
+ for (const schema of this.schemas) {
185
+ const mapping = {};
186
+ for (const cst of schema.items) {
187
+ if (cst.cst_type === CstType.BASE) {
188
+ baseCount++;
189
+ mapping[cst.alias] = `X${baseCount}`;
190
+ } else if (cst.cst_type === CstType.CONSTANT) {
191
+ constCount++;
192
+ mapping[cst.alias] = `C${constCount}`;
193
+ }
194
+ result.set(schema.id, mapping);
195
+ }
196
+ }
197
+ return result;
198
+ }
199
+ calculateSubstituteMappings(baseMappings) {
200
+ const result = {};
201
+ const processed = /* @__PURE__ */ new Set();
202
+ for (const item of this.substitutions) {
203
+ const original = this.cstByID.get(item.original);
204
+ if (original.cst_type !== CstType.BASE && original.cst_type !== CstType.CONSTANT) continue;
205
+ const originalAlias = baseMappings.get(original.schema)[original.alias];
206
+ const substitution = this.cstByID.get(item.substitution);
207
+ let substitutionText = "";
208
+ if (substitution.cst_type === original.cst_type || !substitution.effectiveType) substitutionText = baseMappings.get(substitution.schema)[substitution.alias];
209
+ else {
210
+ const typeText = labelType(substitution.effectiveType);
211
+ substitutionText = applyAliasMapping(typeText, baseMappings.get(substitution.schema));
212
+ substitutionText = applyTypificationMapping(substitutionText, result);
213
+ if (!isSetTypification(substitutionText)) {
214
+ this.reportError(SubstitutionErrorType.baseSubstitutionNotSet, [substitution.alias, typeText]);
215
+ return null;
216
+ }
217
+ if (substitutionText.includes("×") || substitutionText.startsWith("ℬℬ")) substitutionText = substitutionText.slice(1);
218
+ else substitutionText = substitutionText.slice(2, -1);
219
+ }
220
+ for (const prevAlias of processed) result[prevAlias] = applyTypificationMapping(result[prevAlias], { [originalAlias]: substitutionText });
221
+ result[originalAlias] = substitutionText;
222
+ processed.add(originalAlias);
223
+ }
224
+ return result;
225
+ }
226
+ checkEqual(left, right) {
227
+ const schema1 = this.schemaByID.get(left.schema);
228
+ if (schema1.graph.at(left.id).inputs.some((id) => !this.constituents.has(id))) return false;
229
+ const schema2 = this.schemaByID.get(right.schema);
230
+ if (schema2.graph.at(right.id).inputs.some((id) => !this.constituents.has(id))) return false;
231
+ const expression1 = applyAliasMapping(left.definition_formal, this.mapping.get(schema1.id));
232
+ const expression2 = applyAliasMapping(right.definition_formal, this.mapping.get(schema2.id));
233
+ return expression1.replace(" ", "") === expression2.replace(" ", "");
234
+ }
235
+ reportError(errorType, params) {
236
+ this.errors.push({
237
+ errorType,
238
+ params
239
+ });
240
+ return false;
241
+ }
1066
242
  };
243
+ /** Filter relocate candidates from gives schema. */
1067
244
  function getRelocateCandidates(source, destination, schema, oss) {
1068
- const destinationSchema = oss.operationByID.get(destination)?.result;
1069
- if (!destinationSchema) {
1070
- return [];
1071
- }
1072
- const node = oss.graph.at(source);
1073
- if (!node) {
1074
- return [];
1075
- }
1076
- if (node.outputs.includes(destination)) {
1077
- return schema.items;
1078
- }
1079
- const addedCst = schema.items.filter((item) => !item.is_inherited).map((cst) => cst.id);
1080
- const unreachableBases = [];
1081
- for (const cst of schema.items.filter((item) => item.is_inherited)) {
1082
- if (cst.parent_schema == destinationSchema) {
1083
- continue;
1084
- }
1085
- const parent = schema.inheritance.find((item) => item.child === cst.id && item.child_source === cst.schema)?.parent;
1086
- if (parent) {
1087
- const original = oss.substitutions.find((sub) => sub.substitution === parent)?.original;
1088
- if (original) {
1089
- continue;
1090
- }
1091
- }
1092
- unreachableBases.push(cst.id);
1093
- }
1094
- const unreachable = schema.graph.expandAllOutputs(unreachableBases);
1095
- return schema.items.filter(
1096
- (cst) => cst.parent_schema === destinationSchema || addedCst.includes(cst.id) && !unreachable.includes(cst.id)
1097
- );
245
+ const destinationSchema = oss.operationByID.get(destination)?.result;
246
+ if (!destinationSchema) return [];
247
+ const node = oss.graph.at(source);
248
+ if (!node) return [];
249
+ if (node.outputs.includes(destination)) return schema.items;
250
+ const addedCst = schema.items.filter((item) => !item.is_inherited).map((cst) => cst.id);
251
+ const unreachableBases = [];
252
+ for (const cst of schema.items.filter((item) => item.is_inherited)) {
253
+ if (cst.parent_schema == destinationSchema) continue;
254
+ const parent = schema.inheritance.find((item) => item.child === cst.id && item.child_source === cst.schema)?.parent;
255
+ if (parent) {
256
+ if (oss.substitutions.find((sub) => sub.substitution === parent)?.original) continue;
257
+ }
258
+ unreachableBases.push(cst.id);
259
+ }
260
+ const unreachable = schema.graph.expandAllOutputs(unreachableBases);
261
+ return schema.items.filter((cst) => cst.parent_schema === destinationSchema || addedCst.includes(cst.id) && !unreachable.includes(cst.id));
1098
262
  }
1099
- export {
1100
- SubstitutionValidator,
1101
- constructNodeID,
1102
- getRelocateCandidates,
1103
- sortItemsForOSS
1104
- };
263
+ //#endregion
264
+ export { SubstitutionValidator, constructNodeID, getRelocateCandidates, sortItemsForOSS };
265
+
1105
266
  //# sourceMappingURL=oss-api.js.map