@rsconcept/domain 1.0.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 (224) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -0
  3. package/dist/cctext/index.d.ts +1 -0
  4. package/dist/cctext/index.js +42 -0
  5. package/dist/cctext/index.js.map +1 -0
  6. package/dist/cctext/language-api.d.ts +43 -0
  7. package/dist/cctext/language-api.js +252 -0
  8. package/dist/cctext/language-api.js.map +1 -0
  9. package/dist/cctext/language.d.ts +58 -0
  10. package/dist/cctext/language.js +44 -0
  11. package/dist/cctext/language.js.map +1 -0
  12. package/dist/graph/graph.d.ts +62 -0
  13. package/dist/graph/graph.js +385 -0
  14. package/dist/graph/graph.js.map +1 -0
  15. package/dist/graph/index.d.ts +1 -0
  16. package/dist/graph/index.js +384 -0
  17. package/dist/graph/index.js.map +1 -0
  18. package/dist/index.d.ts +28 -0
  19. package/dist/index.js +5851 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/library/folder-tree.d.ts +32 -0
  22. package/dist/library/folder-tree.js +136 -0
  23. package/dist/library/folder-tree.js.map +1 -0
  24. package/dist/library/index.d.ts +17 -0
  25. package/dist/library/index.js +2800 -0
  26. package/dist/library/index.js.map +1 -0
  27. package/dist/library/library-api.d.ts +6 -0
  28. package/dist/library/library-api.js +13 -0
  29. package/dist/library/library-api.js.map +1 -0
  30. package/dist/library/library.d.ts +56 -0
  31. package/dist/library/library.js +23 -0
  32. package/dist/library/library.js.map +1 -0
  33. package/dist/library/oss-api.d.ts +47 -0
  34. package/dist/library/oss-api.js +1105 -0
  35. package/dist/library/oss-api.js.map +1 -0
  36. package/dist/library/oss-layout-api.d.ts +36 -0
  37. package/dist/library/oss-layout-api.js +330 -0
  38. package/dist/library/oss-layout-api.js.map +1 -0
  39. package/dist/library/oss-layout.d.ts +25 -0
  40. package/dist/library/oss-layout.js +1 -0
  41. package/dist/library/oss-layout.js.map +1 -0
  42. package/dist/library/oss.d.ts +136 -0
  43. package/dist/library/oss.js +30 -0
  44. package/dist/library/oss.js.map +1 -0
  45. package/dist/library/rsengine.d.ts +116 -0
  46. package/dist/library/rsengine.js +2604 -0
  47. package/dist/library/rsengine.js.map +1 -0
  48. package/dist/library/rsform-api.d.ts +74 -0
  49. package/dist/library/rsform-api.js +879 -0
  50. package/dist/library/rsform-api.js.map +1 -0
  51. package/dist/library/rsform.d.ts +206 -0
  52. package/dist/library/rsform.js +32 -0
  53. package/dist/library/rsform.js.map +1 -0
  54. package/dist/library/rsmodel-api.d.ts +43 -0
  55. package/dist/library/rsmodel-api.js +836 -0
  56. package/dist/library/rsmodel-api.js.map +1 -0
  57. package/dist/library/rsmodel.d.ts +52 -0
  58. package/dist/library/rsmodel.js +25 -0
  59. package/dist/library/rsmodel.js.map +1 -0
  60. package/dist/library/structure-planner.d.ts +33 -0
  61. package/dist/library/structure-planner.js +481 -0
  62. package/dist/library/structure-planner.js.map +1 -0
  63. package/dist/parsing/ast.d.ts +49 -0
  64. package/dist/parsing/ast.js +93 -0
  65. package/dist/parsing/ast.js.map +1 -0
  66. package/dist/parsing/index.d.ts +3 -0
  67. package/dist/parsing/index.js +141 -0
  68. package/dist/parsing/index.js.map +1 -0
  69. package/dist/parsing/lezer-tree.d.ts +13 -0
  70. package/dist/parsing/lezer-tree.js +50 -0
  71. package/dist/parsing/lezer-tree.js.map +1 -0
  72. package/dist/rslang/api.d.ts +53 -0
  73. package/dist/rslang/api.js +846 -0
  74. package/dist/rslang/api.js.map +1 -0
  75. package/dist/rslang/ast-annotations.d.ts +18 -0
  76. package/dist/rslang/ast-annotations.js +56 -0
  77. package/dist/rslang/ast-annotations.js.map +1 -0
  78. package/dist/rslang/error.d.ts +85 -0
  79. package/dist/rslang/error.js +159 -0
  80. package/dist/rslang/error.js.map +1 -0
  81. package/dist/rslang/eval/calculator.d.ts +43 -0
  82. package/dist/rslang/eval/calculator.js +1639 -0
  83. package/dist/rslang/eval/calculator.js.map +1 -0
  84. package/dist/rslang/eval/evaluation-cache.d.ts +36 -0
  85. package/dist/rslang/eval/evaluation-cache.js +310 -0
  86. package/dist/rslang/eval/evaluation-cache.js.map +1 -0
  87. package/dist/rslang/eval/evaluator.d.ts +70 -0
  88. package/dist/rslang/eval/evaluator.js +1514 -0
  89. package/dist/rslang/eval/evaluator.js.map +1 -0
  90. package/dist/rslang/eval/value-api.d.ts +48 -0
  91. package/dist/rslang/eval/value-api.js +490 -0
  92. package/dist/rslang/eval/value-api.js.map +1 -0
  93. package/dist/rslang/eval/value.d.ts +36 -0
  94. package/dist/rslang/eval/value.js +118 -0
  95. package/dist/rslang/eval/value.js.map +1 -0
  96. package/dist/rslang/index.d.ts +17 -0
  97. package/dist/rslang/index.js +4314 -0
  98. package/dist/rslang/index.js.map +1 -0
  99. package/dist/rslang/labels.d.ts +16 -0
  100. package/dist/rslang/labels.js +315 -0
  101. package/dist/rslang/labels.js.map +1 -0
  102. package/dist/rslang/parser/expression-generator.d.ts +10 -0
  103. package/dist/rslang/parser/expression-generator.js +451 -0
  104. package/dist/rslang/parser/expression-generator.js.map +1 -0
  105. package/dist/rslang/parser/normalize.d.ts +11 -0
  106. package/dist/rslang/parser/normalize.js +507 -0
  107. package/dist/rslang/parser/normalize.js.map +1 -0
  108. package/dist/rslang/parser/parser.d.ts +5 -0
  109. package/dist/rslang/parser/parser.js +24 -0
  110. package/dist/rslang/parser/parser.js.map +1 -0
  111. package/dist/rslang/parser/parser.terms.d.ts +42 -0
  112. package/dist/rslang/parser/parser.terms.js +84 -0
  113. package/dist/rslang/parser/parser.terms.js.map +1 -0
  114. package/dist/rslang/parser/syntax-errors.d.ts +11 -0
  115. package/dist/rslang/parser/syntax-errors.js +403 -0
  116. package/dist/rslang/parser/syntax-errors.js.map +1 -0
  117. package/dist/rslang/parser/token.d.ts +79 -0
  118. package/dist/rslang/parser/token.js +95 -0
  119. package/dist/rslang/parser/token.js.map +1 -0
  120. package/dist/rslang/semantic/analyzer.d.ts +39 -0
  121. package/dist/rslang/semantic/analyzer.js +2604 -0
  122. package/dist/rslang/semantic/analyzer.js.map +1 -0
  123. package/dist/rslang/semantic/arguments-extractor.d.ts +42 -0
  124. package/dist/rslang/semantic/arguments-extractor.js +366 -0
  125. package/dist/rslang/semantic/arguments-extractor.js.map +1 -0
  126. package/dist/rslang/semantic/type-auditor.d.ts +73 -0
  127. package/dist/rslang/semantic/type-auditor.js +1570 -0
  128. package/dist/rslang/semantic/type-auditor.js.map +1 -0
  129. package/dist/rslang/semantic/typification-api.d.ts +27 -0
  130. package/dist/rslang/semantic/typification-api.js +320 -0
  131. package/dist/rslang/semantic/typification-api.js.map +1 -0
  132. package/dist/rslang/semantic/typification-parser.d.ts +12 -0
  133. package/dist/rslang/semantic/typification-parser.js +226 -0
  134. package/dist/rslang/semantic/typification-parser.js.map +1 -0
  135. package/dist/rslang/semantic/typification.d.ts +119 -0
  136. package/dist/rslang/semantic/typification.js +74 -0
  137. package/dist/rslang/semantic/typification.js.map +1 -0
  138. package/dist/rslang/semantic/value-auditor.d.ts +43 -0
  139. package/dist/rslang/semantic/value-auditor.js +523 -0
  140. package/dist/rslang/semantic/value-auditor.js.map +1 -0
  141. package/dist/rslang/semantic/value-class.d.ts +10 -0
  142. package/dist/rslang/semantic/value-class.js +9 -0
  143. package/dist/rslang/semantic/value-class.js.map +1 -0
  144. package/dist/rslang/typification-graph.d.ts +33 -0
  145. package/dist/rslang/typification-graph.js +311 -0
  146. package/dist/rslang/typification-graph.js.map +1 -0
  147. package/dist/shared/branded.d.ts +7 -0
  148. package/dist/shared/branded.js +1 -0
  149. package/dist/shared/branded.js.map +1 -0
  150. package/dist/shared/hash.d.ts +6 -0
  151. package/dist/shared/hash.js +18 -0
  152. package/dist/shared/hash.js.map +1 -0
  153. package/dist/shared/index.d.ts +2 -0
  154. package/dist/shared/index.js +18 -0
  155. package/dist/shared/index.js.map +1 -0
  156. package/package.json +184 -0
  157. package/src/cctext/index.ts +9 -0
  158. package/src/cctext/language-api.test.ts +149 -0
  159. package/src/cctext/language-api.ts +285 -0
  160. package/src/cctext/language.ts +80 -0
  161. package/src/graph/graph.test.ts +392 -0
  162. package/src/graph/graph.ts +433 -0
  163. package/src/graph/index.ts +1 -0
  164. package/src/index.ts +96 -0
  165. package/src/library/folder-tree.test.ts +47 -0
  166. package/src/library/folder-tree.ts +156 -0
  167. package/src/library/index.ts +46 -0
  168. package/src/library/library-api.test.ts +32 -0
  169. package/src/library/library-api.ts +11 -0
  170. package/src/library/library.ts +61 -0
  171. package/src/library/oss-api.ts +449 -0
  172. package/src/library/oss-layout-api.ts +377 -0
  173. package/src/library/oss-layout.ts +27 -0
  174. package/src/library/oss.ts +150 -0
  175. package/src/library/rsengine.ts +593 -0
  176. package/src/library/rsform-api.ts +533 -0
  177. package/src/library/rsform.ts +228 -0
  178. package/src/library/rsmodel-api.ts +340 -0
  179. package/src/library/rsmodel.ts +50 -0
  180. package/src/library/structure-planner.ts +143 -0
  181. package/src/parsing/ast.ts +136 -0
  182. package/src/parsing/index.ts +15 -0
  183. package/src/parsing/lezer-tree.ts +69 -0
  184. package/src/rslang/api.test.ts +116 -0
  185. package/src/rslang/api.ts +183 -0
  186. package/src/rslang/ast-annotations.ts +70 -0
  187. package/src/rslang/error.ts +129 -0
  188. package/src/rslang/eval/calculator.test.ts +124 -0
  189. package/src/rslang/eval/calculator.ts +121 -0
  190. package/src/rslang/eval/evaluation-cache.ts +257 -0
  191. package/src/rslang/eval/evaluator.test.ts +352 -0
  192. package/src/rslang/eval/evaluator.ts +935 -0
  193. package/src/rslang/eval/value-api.test.ts +105 -0
  194. package/src/rslang/eval/value-api.ts +444 -0
  195. package/src/rslang/eval/value.ts +102 -0
  196. package/src/rslang/index.ts +23 -0
  197. package/src/rslang/labels.ts +191 -0
  198. package/src/rslang/parser/expression-generator.test.ts +100 -0
  199. package/src/rslang/parser/expression-generator.ts +466 -0
  200. package/src/rslang/parser/normalize.test.ts +99 -0
  201. package/src/rslang/parser/normalize.ts +462 -0
  202. package/src/rslang/parser/parser.terms.ts +42 -0
  203. package/src/rslang/parser/parser.test.ts +153 -0
  204. package/src/rslang/parser/parser.ts +20 -0
  205. package/src/rslang/parser/rslang.grammar +251 -0
  206. package/src/rslang/parser/syntax-errors.ts +209 -0
  207. package/src/rslang/parser/token.ts +106 -0
  208. package/src/rslang/semantic/analyzer.test.ts +59 -0
  209. package/src/rslang/semantic/analyzer.ts +179 -0
  210. package/src/rslang/semantic/arguments-extractor.ts +327 -0
  211. package/src/rslang/semantic/type-auditor.test.ts +326 -0
  212. package/src/rslang/semantic/type-auditor.ts +1049 -0
  213. package/src/rslang/semantic/typification-api.test.ts +46 -0
  214. package/src/rslang/semantic/typification-api.ts +321 -0
  215. package/src/rslang/semantic/typification-parser.test.ts +50 -0
  216. package/src/rslang/semantic/typification-parser.ts +220 -0
  217. package/src/rslang/semantic/typification.ts +180 -0
  218. package/src/rslang/semantic/value-auditor.test.ts +206 -0
  219. package/src/rslang/semantic/value-auditor.ts +332 -0
  220. package/src/rslang/semantic/value-class.ts +11 -0
  221. package/src/rslang/typification-graph.ts +155 -0
  222. package/src/shared/branded.ts +6 -0
  223. package/src/shared/hash.ts +17 -0
  224. package/src/shared/index.ts +2 -0
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Module: Models for formal representation for systems of concepts.
3
+ */
4
+
5
+ import { type Graph } from '../graph';
6
+ import { type AnalysisBase, type ExpressionType, type RSLangAnalyzer, type TypePath } from '../rslang';
7
+
8
+ import { type CurrentVersion, type LibraryItem, type LibraryItemReference, type VersionInfo } from './library';
9
+
10
+ /** Represents {@link Constituenta} type. */
11
+ export const CstType = {
12
+ NOMINAL: 'nominal',
13
+ BASE: 'basic',
14
+ STRUCTURED: 'structure',
15
+ TERM: 'term',
16
+ AXIOM: 'axiom',
17
+ FUNCTION: 'function',
18
+ PREDICATE: 'predicate',
19
+ CONSTANT: 'constant',
20
+ STATEMENT: 'statement'
21
+ } as const;
22
+ export type CstType = (typeof CstType)[keyof typeof CstType];
23
+
24
+ /** Represents attribution of a {@link Constituenta} to another {@link Constituenta}. */
25
+ export interface Attribution {
26
+ container: number;
27
+ attribute: number;
28
+ }
29
+
30
+ /** Represents global identifier type info. */
31
+ export interface TypeInfo {
32
+ alias: string;
33
+ type: ExpressionType;
34
+ }
35
+
36
+ /** Represents function argument value. */
37
+ export interface ArgumentValue {
38
+ alias: string;
39
+ typification: string;
40
+ value?: string;
41
+ }
42
+
43
+ /** Represents Constituenta classification in terms of system of concepts. */
44
+ export const CstClass = {
45
+ NOMINAL: 'nominal',
46
+ BASIC: 'basic',
47
+ DERIVED: 'derived',
48
+ STATEMENT: 'statement',
49
+ TEMPLATE: 'template'
50
+ } as const;
51
+ export type CstClass = (typeof CstClass)[keyof typeof CstClass];
52
+
53
+ /** Represents formal expression Status. */
54
+ export const CstStatus = {
55
+ VERIFIED: 'verified',
56
+ INCORRECT: 'incorrect',
57
+ INCALCULABLE: 'incalculable',
58
+ PROPERTY: 'property',
59
+ UNKNOWN: 'unknown'
60
+ } as const;
61
+ export type CstStatus = (typeof CstStatus)[keyof typeof CstStatus];
62
+
63
+ /** Represents substitution of {@link Constituenta}. */
64
+ export interface Substitution {
65
+ original: number;
66
+ substitution: number;
67
+ }
68
+
69
+ /** Represents word form for natural language. */
70
+ interface TermForm {
71
+ text: string;
72
+ tags: string;
73
+ }
74
+
75
+ /** Represents Constituenta. */
76
+ export interface Constituenta {
77
+ id: number;
78
+ crucial: boolean;
79
+ alias: string;
80
+ convention: string;
81
+ cst_type: CstType;
82
+ definition_formal: string;
83
+ definition_raw: string;
84
+ definition_resolved: string;
85
+ term_raw: string;
86
+ term_resolved: string;
87
+ term_forms: TermForm[];
88
+ attributes: number[];
89
+
90
+ /** Manual typification of the constituent */
91
+ typification_manual: string;
92
+
93
+ /** Controls whether base concepts expose property values or full values */
94
+ value_is_property: boolean;
95
+
96
+ homonyms: number[];
97
+ formalDuplicates: number[];
98
+ analysis: AnalysisBase;
99
+
100
+ /** Typification used for dependents, model values, and display (manual when it overrides computed). */
101
+ effectiveType: ExpressionType | null;
102
+
103
+ /**
104
+ * True when manual typification is non-empty, parses to a type, and its label differs from the
105
+ * typification inferred from the formal definition ({@link Constituenta.analysis}.type).
106
+ */
107
+ is_type_mismatch: boolean;
108
+
109
+ /** Identifier of {@link LibraryItem} containing this {@link Constituenta}. */
110
+ schema: number;
111
+
112
+ /** {@link CstClass} of this {@link Constituenta}. */
113
+ cst_class: CstClass;
114
+ /** {@link CstStatus} of this {@link Constituenta}. */
115
+ status: CstStatus;
116
+
117
+ /** Indicates if this {@link Constituenta} is a template. */
118
+ is_template: boolean;
119
+ /** Indicates if this {@link Constituenta} has a simple expression. */
120
+ is_simple_expression: boolean;
121
+
122
+ /** Index of {@link LibraryItem} that contains this cst (or inheritance parent).
123
+ * 0 - not inherited, 1 - inherited by 1st schema, 2 - inherited by 2nd schema, etc.
124
+ */
125
+ parent_schema_index: number;
126
+ /** {@link LibraryItem} that contains parent of this inherited {@link Constituenta}. */
127
+ parent_schema: number | null;
128
+ /** Indicates if this {@link Constituenta} is inherited. */
129
+ is_inherited: boolean;
130
+ /** Indicates if this {@link Constituenta} has children that are inherited. */
131
+ has_inherited_children: boolean;
132
+
133
+ /** {@link Constituenta} that spawned this one. */
134
+ spawner?: number;
135
+ /** Alias of {@link Constituenta} that spawned this one. */
136
+ spawner_alias?: string;
137
+ /** Structure path that spawned this one. */
138
+ spawner_path?: TypePath;
139
+
140
+ /** List of {@link Constituenta} that are spawned by this one. */
141
+ spawn: number[];
142
+ /** List of aliases of {@link Constituenta} that are spawned by this one. */
143
+ spawn_alias: string[];
144
+ }
145
+
146
+ /** Represents {@link RSForm} statistics. */
147
+ export interface RSFormStats {
148
+ step_complexity: number;
149
+
150
+ /** Total number of constituents in the RSForm. */
151
+ count_all: number;
152
+ /** Number of crucial constituents in the RSForm. */
153
+ count_crucial: number;
154
+
155
+ count_problematic: number;
156
+ /** Constituents that have same term. */
157
+ count_homonyms: number;
158
+ /** Constituents that have same formal definition and term. */
159
+ count_formal_duplicates: number;
160
+ /** Base constituents with no convention and alias. */
161
+ count_missing_convention: number;
162
+ /** Constituents with non-empty manual typification that disagrees with inferred typification. */
163
+ count_type_mismatch: number;
164
+
165
+ count_incorrect: number;
166
+ count_property: number;
167
+ count_incalculable: number;
168
+ count_inherited: number;
169
+
170
+ count_text_term: number;
171
+ count_definition: number;
172
+ count_convention: number;
173
+ count_comment: number;
174
+
175
+ count_base: number;
176
+ count_constant: number;
177
+ count_structured: number;
178
+ count_axiom: number;
179
+ count_term: number;
180
+ count_function: number;
181
+ count_predicate: number;
182
+ count_statement: number;
183
+ count_nominal: number;
184
+ }
185
+
186
+ /** Represents inheritance data for {@link RSForm}. */
187
+ interface InheritanceInfo {
188
+ child: number;
189
+ child_source: number;
190
+ parent: number;
191
+ parent_source: number;
192
+ }
193
+
194
+ /** Represents formal explication for set of concepts. */
195
+ export interface RSForm extends LibraryItem {
196
+ /** Whether this RSForm was produced (vs imported or inherited) */
197
+ is_produced: boolean;
198
+ /** Whether this RSForm has attribution feature */
199
+ is_attributive: boolean;
200
+ /** List of user IDs who can edit this RSForm */
201
+ editors: number[];
202
+ /** Information about the current version of this RSForm */
203
+ version: CurrentVersion;
204
+ /** Full version history of this RSForm */
205
+ versions: VersionInfo[];
206
+
207
+ /** Full list of constituents in the RSForm */
208
+ items: Constituenta[];
209
+ /** List of inheritance relationships between constituents */
210
+ inheritance: InheritanceInfo[];
211
+ /** List of constituent attribution relationships */
212
+ attribution: Attribution[];
213
+ /** References to Operational Schemes/Systems associated with the RSForm */
214
+ oss: LibraryItemReference[];
215
+ /** References to conceptual models associated with the RSForm */
216
+ models: LibraryItemReference[];
217
+
218
+ /** Analyzer instance for RS language features and validation */
219
+ analyzer: RSLangAnalyzer;
220
+ /** Graph structure representing formal dependencies between constituents */
221
+ graph: Graph;
222
+ /** Graph structure representing attribution relationships */
223
+ attribution_graph: Graph;
224
+ /** Map for quick lookup of constituents by alias */
225
+ cstByAlias: Map<string, Constituenta>;
226
+ /** Map for quick lookup of constituents by ID */
227
+ cstByID: Map<number, Constituenta>;
228
+ }
@@ -0,0 +1,340 @@
1
+ import { type ExpressionType, TypeID, type Typification, type Value } from '../rslang';
2
+ import { compare, tuple, TUPLE_ID, VALUE_TRUE, type ValuePath } from '../rslang/eval/value';
3
+ import { extractValue, isTupleValue, makeDefaultValue, setNestedValue } from '../rslang/eval/value-api';
4
+ import { type EchelonCollection } from '../rslang/semantic/typification';
5
+
6
+ import { type RSEngine } from './rsengine';
7
+ import { type Constituenta, CstType, type RSForm } from './rsform';
8
+ import { calculateSchemaStats, isBaseSet, isBasicConcept } from './rsform-api';
9
+ import { type BasicBinding, EvalStatus, type RSModelStats } from './rsmodel';
10
+
11
+ const RANDOM_INTEGER_MIN = -100;
12
+ const RANDOM_INTEGER_MAX = 100;
13
+ const DEFAULT_RANDOM_SET_ELEMENTS_COUNT = 5;
14
+
15
+ /** Evaluate if parsed data is a basic binding data. */
16
+ export function validateBasicBindingData(data: unknown): data is Record<string, string> {
17
+ if (!data || typeof data !== 'object' || Array.isArray(data)) {
18
+ return false;
19
+ }
20
+ return Object.entries(data).every(([key, value]) => typeof value === 'string' && Number.isInteger(Number(key)));
21
+ }
22
+
23
+ /** Convert parsed data to a basic binding. */
24
+ export function toBasicBinding(data: Record<string | number, string>): BasicBinding {
25
+ const result: BasicBinding = {};
26
+ for (const [k, v] of Object.entries(data)) {
27
+ result[Number(k)] = v;
28
+ }
29
+ return result;
30
+ }
31
+
32
+ /** Evaluate if parsed data is a {@link Value} data. */
33
+ export function validateValueData(data: unknown): data is Value {
34
+ if (typeof data === 'number') {
35
+ return Number.isFinite(data);
36
+ }
37
+ if (!Array.isArray(data)) {
38
+ return false;
39
+ }
40
+ return data.every(item => validateValueData(item));
41
+ }
42
+
43
+ export function generateRandomValue(
44
+ type: Typification,
45
+ basics: Map<number, BasicBinding>,
46
+ cstByAlias: Map<string, Constituenta>,
47
+ setElementsCount: number = DEFAULT_RANDOM_SET_ELEMENTS_COUNT
48
+ ): Value | null {
49
+ switch (type.typeID) {
50
+ case TypeID.integer:
51
+ return Math.floor(Math.random() * (RANDOM_INTEGER_MAX - RANDOM_INTEGER_MIN + 1)) + RANDOM_INTEGER_MIN;
52
+ case TypeID.basic: {
53
+ const cst = cstByAlias.get(type.baseID);
54
+ const binding = cst ? basics.get(cst.id) : undefined;
55
+ const ids = Object.keys(binding ?? {}).map(Number);
56
+ if (ids.length === 0) {
57
+ return null;
58
+ }
59
+ return ids[Math.floor(Math.random() * ids.length)];
60
+ }
61
+ case TypeID.tuple: {
62
+ const factors: Value[] = [];
63
+ for (const factor of type.factors) {
64
+ const sample = generateRandomValue(factor, basics, cstByAlias, setElementsCount);
65
+ if (sample === null) {
66
+ return null;
67
+ }
68
+ factors.push(sample);
69
+ }
70
+ return tuple(factors);
71
+ }
72
+ case TypeID.collection: {
73
+ const result: Value[] = [];
74
+ const randomElementsCount = Math.floor(Math.random() * (setElementsCount + 1));
75
+ for (let i = 0; i < randomElementsCount; i++) {
76
+ const sample = generateRandomValue(type.base, basics, cstByAlias, setElementsCount);
77
+ if (sample === null) {
78
+ return null;
79
+ }
80
+ result.push(sample);
81
+ }
82
+ return result;
83
+ }
84
+ case TypeID.anyTypification:
85
+ return null;
86
+ }
87
+ }
88
+
89
+ /** Calculate statistics for {@link RSModel}. */
90
+ export function calculateModelStats(schema: RSForm, engine: RSEngine, _engineGeneration?: number): RSModelStats {
91
+ const items = schema.items;
92
+ const statusByID = new Map<number, EvalStatus>();
93
+ for (const cst of items) {
94
+ statusByID.set(cst.id, engine.getCstStatus(cst.id));
95
+ }
96
+ return {
97
+ ...calculateSchemaStats(schema),
98
+ base_elements: items.reduce((sum, cst) => sum + countBaseElements(cst, engine), 0),
99
+ count_missing_base: items.reduce(
100
+ (sum, cst) => sum + (statusByID.get(cst.id) === EvalStatus.EMPTY && isBasicConcept(cst.cst_type) ? 1 : 0),
101
+ 0
102
+ ),
103
+ count_false_axioms: items.reduce(
104
+ (sum, cst) => sum + (statusByID.get(cst.id) === EvalStatus.AXIOM_FALSE ? 1 : 0),
105
+ 0
106
+ ),
107
+ count_invalid_data: items.reduce(
108
+ (sum, cst) => sum + (statusByID.get(cst.id) === EvalStatus.INVALID_DATA ? 1 : 0),
109
+ 0
110
+ ),
111
+ count_invalid_calculations: items.reduce(
112
+ (sum, cst) =>
113
+ sum +
114
+ (statusByID.get(cst.id) === EvalStatus.EVAL_FAIL || statusByID.get(cst.id) === EvalStatus.NOT_PROCESSED
115
+ ? 1
116
+ : 0),
117
+ 0
118
+ ),
119
+ count_empty_terms: items.reduce(
120
+ (sum, cst) => sum + (statusByID.get(cst.id) === EvalStatus.EMPTY && cst.cst_type === CstType.TERM ? 1 : 0),
121
+ 0
122
+ )
123
+ };
124
+ }
125
+
126
+ /** Checks whether evaluation status contributes to model status issues. */
127
+ export function isModelIssue(engine: RSEngine, cst: Constituenta): boolean {
128
+ const status = engine.getCstStatus(cst.id);
129
+ const isBasic = isBasicConcept(cst.cst_type);
130
+ return (
131
+ status === EvalStatus.EVAL_FAIL ||
132
+ status === EvalStatus.AXIOM_FALSE ||
133
+ status === EvalStatus.INVALID_DATA ||
134
+ (isBasic && status === EvalStatus.EMPTY)
135
+ );
136
+ }
137
+
138
+ /** Evaluate if {@link CstType} is interpretable. */
139
+ export function isInterpretable(type: CstType): boolean {
140
+ switch (type) {
141
+ case CstType.BASE:
142
+ case CstType.CONSTANT:
143
+ case CstType.STRUCTURED:
144
+ case CstType.AXIOM:
145
+ case CstType.TERM:
146
+ case CstType.STATEMENT:
147
+ return true;
148
+
149
+ case CstType.NOMINAL:
150
+ case CstType.FUNCTION:
151
+ case CstType.PREDICATE:
152
+ return false;
153
+ }
154
+ }
155
+
156
+ /** Evaluate if {@link CstType} is inferrable. */
157
+ export function isInferrable(type: CstType): boolean {
158
+ switch (type) {
159
+ case CstType.AXIOM:
160
+ case CstType.STATEMENT:
161
+ case CstType.TERM:
162
+ return true;
163
+
164
+ case CstType.NOMINAL:
165
+ case CstType.BASE:
166
+ case CstType.CONSTANT:
167
+ case CstType.STRUCTURED:
168
+ case CstType.FUNCTION:
169
+ case CstType.PREDICATE:
170
+ return false;
171
+ }
172
+ }
173
+
174
+ /** Infers status of a given {@link Value} and {@link CstType}. */
175
+ export function inferEvalStatus(
176
+ value: Value | null,
177
+ cstType: CstType,
178
+ wasCalculated: boolean = true,
179
+ isInvalid: boolean = false
180
+ ): EvalStatus {
181
+ if (isBaseSet(cstType) || cstType === CstType.STRUCTURED) {
182
+ if (isInvalid) {
183
+ return EvalStatus.INVALID_DATA;
184
+ }
185
+ if (value === null || (Array.isArray(value) && value.length === 0)) {
186
+ return EvalStatus.EMPTY;
187
+ }
188
+ return EvalStatus.HAS_DATA;
189
+ }
190
+ if (!isInferrable(cstType)) {
191
+ return EvalStatus.NO_EVAL;
192
+ }
193
+ if (!wasCalculated) {
194
+ return EvalStatus.NOT_PROCESSED;
195
+ }
196
+ if (value === null) {
197
+ return EvalStatus.EVAL_FAIL;
198
+ }
199
+ if (cstType === CstType.AXIOM && value !== VALUE_TRUE) {
200
+ return EvalStatus.AXIOM_FALSE;
201
+ }
202
+ if (Array.isArray(value) && value.length === 0) {
203
+ return EvalStatus.EMPTY;
204
+ }
205
+ return EvalStatus.HAS_DATA;
206
+ }
207
+
208
+ /** Tries to fix value removing invalid base elements not present in the target value.
209
+ * returns null if no fixing is possible.
210
+ * returns true if fixing was successful.
211
+ * returns false if fixing was not needed.
212
+ */
213
+ export function tryFixValue(
214
+ value: Value,
215
+ type: ExpressionType,
216
+ targetAlias: string,
217
+ targetValue: Value[]
218
+ ): boolean | null {
219
+ switch (type.typeID) {
220
+ case TypeID.integer:
221
+ return false;
222
+ case TypeID.basic:
223
+ if (type.baseID !== targetAlias) {
224
+ return false;
225
+ }
226
+ if (typeof value !== 'number') {
227
+ return null;
228
+ }
229
+ if (!targetValue.includes(value)) {
230
+ return null;
231
+ }
232
+ return false;
233
+ case TypeID.tuple: {
234
+ if (!isTupleValue(value)) {
235
+ return null;
236
+ }
237
+ let wasChanged = false;
238
+ for (let i = 0; i < type.factors.length; i++) {
239
+ const componentChanged = tryFixValue(value[i + 1], type.factors[i], targetAlias, targetValue);
240
+ if (componentChanged === null) {
241
+ return null;
242
+ }
243
+ if (componentChanged) {
244
+ wasChanged = true;
245
+ }
246
+ }
247
+ return wasChanged;
248
+ }
249
+ case TypeID.collection:
250
+ if (!Array.isArray(value) || (value.length > 1 && value[0] === TUPLE_ID)) {
251
+ return null;
252
+ }
253
+ let wasChanged = false;
254
+ const removeElements: Value[] = [];
255
+ for (const item of value) {
256
+ const elementChanged = tryFixValue(item, type.base, targetAlias, targetValue);
257
+ if (elementChanged === null || elementChanged === true) {
258
+ wasChanged = true;
259
+ if (elementChanged === null) {
260
+ removeElements.push(item);
261
+ }
262
+ }
263
+ }
264
+ for (const item of removeElements) {
265
+ const index = value.indexOf(item);
266
+ if (index !== -1) {
267
+ value.splice(index, 1);
268
+ }
269
+ }
270
+ if (wasChanged) {
271
+ value.sort((a, b) => compare(a, b));
272
+ let i = 1;
273
+ while (i < value.length) {
274
+ if (compare(value[i], value[i - 1]) === 0) {
275
+ value.splice(i, 1);
276
+ } else {
277
+ i++;
278
+ }
279
+ }
280
+ }
281
+ return wasChanged;
282
+
283
+ case TypeID.logic:
284
+ case TypeID.anyTypification:
285
+ case TypeID.predicate:
286
+ case TypeID.function:
287
+ return null;
288
+ }
289
+ }
290
+
291
+ export function updateValueElement(value: Value | null, path: ValuePath, newValue: number): Value | null {
292
+ return setNestedValue(value, path, newValue);
293
+ }
294
+
295
+ export function deleteValueElement(
296
+ value: Value | null,
297
+ path: ValuePath,
298
+ type: Typification,
299
+ target: number
300
+ ): Value | null {
301
+ if (path.length === 0 && type.typeID !== TypeID.collection) {
302
+ return null;
303
+ }
304
+
305
+ const arrayValue = extractValue(value!, path)! as Value[];
306
+ const updatedArray = [...arrayValue.slice(0, target), ...arrayValue.slice(target + 1)];
307
+ return setNestedValue(value, path, updatedArray);
308
+ }
309
+
310
+ export function addValueElement(
311
+ value: Value | null,
312
+ path: ValuePath,
313
+ type: Typification,
314
+ currentType: Typification
315
+ ): Value | null {
316
+ if (path.length === 0 && type.typeID !== TypeID.collection) {
317
+ return makeDefaultValue(type);
318
+ }
319
+
320
+ const newElem = makeDefaultValue((currentType as EchelonCollection).base);
321
+ const arrayValue = (extractValue(value!, path) as Value[] | null) ?? [];
322
+ const updatedArray = [newElem, ...arrayValue];
323
+ return setNestedValue(value, path, updatedArray);
324
+ }
325
+
326
+ // ========= Internal functions ==========
327
+
328
+ function countBaseElements(cst: Constituenta, engine: RSEngine): number {
329
+ if (!isBasicConcept(cst.cst_type)) {
330
+ return 0;
331
+ }
332
+ const value = engine.getCstValue(cst.id);
333
+ if (value === null) {
334
+ return 0;
335
+ }
336
+ if (!Array.isArray(value) || value[0] === TUPLE_ID) {
337
+ return 1;
338
+ }
339
+ return value.length;
340
+ }
@@ -0,0 +1,50 @@
1
+ import { type Value } from '../rslang';
2
+
3
+ import { type LibraryItem } from './library';
4
+ import { type RSFormStats } from './rsform';
5
+
6
+ export const TYPE_BASIC = 'basic';
7
+
8
+ /** Evaluation status enumeration. */
9
+ export const EvalStatus = {
10
+ NO_EVAL: 1, // не вычисляется
11
+ NOT_PROCESSED: 2, // Интерпретация не вычислялась
12
+ INVALID_DATA: 3, // Неверные данные
13
+ EVAL_FAIL: 4, // Ошибка при вычислении
14
+ AXIOM_FALSE: 5, // Значение аксиомы = FALSE
15
+ EMPTY: 6, // Значение пусто
16
+ HAS_DATA: 7 // Интерпретация вычислена и не пуста
17
+ } as const;
18
+ export type EvalStatus = (typeof EvalStatus)[keyof typeof EvalStatus];
19
+
20
+ /** Represents basic element binding. */
21
+ export type BasicBinding = Record<number, string>;
22
+
23
+ /** Represents {@link RSModel} basic sets binding. */
24
+ export type BasicsContext = Map<number, BasicBinding>;
25
+
26
+ /** Default value for structured data. */
27
+ export const DEFAULT_VALUE_TEXT = 'N/A';
28
+
29
+ interface RSModelData extends LibraryItem {
30
+ editors: number[];
31
+ schema: number;
32
+ items: {
33
+ id: number;
34
+ type: string;
35
+ value: Value | BasicBinding;
36
+ }[];
37
+ }
38
+
39
+ /** Represents {@link RSModel} structured data. */
40
+ export type RSModel = RSModelData;
41
+
42
+ /** Represents {@link RSModel} statistics. */
43
+ export interface RSModelStats extends RSFormStats {
44
+ count_missing_base: number;
45
+ count_false_axioms: number;
46
+ count_invalid_data: number;
47
+ count_invalid_calculations: number;
48
+ count_empty_terms: number;
49
+ base_elements: number;
50
+ }