@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.
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/cctext/index.d.ts +1 -0
- package/dist/cctext/index.js +42 -0
- package/dist/cctext/index.js.map +1 -0
- package/dist/cctext/language-api.d.ts +43 -0
- package/dist/cctext/language-api.js +252 -0
- package/dist/cctext/language-api.js.map +1 -0
- package/dist/cctext/language.d.ts +58 -0
- package/dist/cctext/language.js +44 -0
- package/dist/cctext/language.js.map +1 -0
- package/dist/graph/graph.d.ts +62 -0
- package/dist/graph/graph.js +385 -0
- package/dist/graph/graph.js.map +1 -0
- package/dist/graph/index.d.ts +1 -0
- package/dist/graph/index.js +384 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +5851 -0
- package/dist/index.js.map +1 -0
- package/dist/library/folder-tree.d.ts +32 -0
- package/dist/library/folder-tree.js +136 -0
- package/dist/library/folder-tree.js.map +1 -0
- package/dist/library/index.d.ts +17 -0
- package/dist/library/index.js +2800 -0
- package/dist/library/index.js.map +1 -0
- package/dist/library/library-api.d.ts +6 -0
- package/dist/library/library-api.js +13 -0
- package/dist/library/library-api.js.map +1 -0
- package/dist/library/library.d.ts +56 -0
- package/dist/library/library.js +23 -0
- package/dist/library/library.js.map +1 -0
- package/dist/library/oss-api.d.ts +47 -0
- package/dist/library/oss-api.js +1105 -0
- package/dist/library/oss-api.js.map +1 -0
- package/dist/library/oss-layout-api.d.ts +36 -0
- package/dist/library/oss-layout-api.js +330 -0
- package/dist/library/oss-layout-api.js.map +1 -0
- package/dist/library/oss-layout.d.ts +25 -0
- package/dist/library/oss-layout.js +1 -0
- package/dist/library/oss-layout.js.map +1 -0
- package/dist/library/oss.d.ts +136 -0
- package/dist/library/oss.js +30 -0
- package/dist/library/oss.js.map +1 -0
- package/dist/library/rsengine.d.ts +116 -0
- package/dist/library/rsengine.js +2604 -0
- package/dist/library/rsengine.js.map +1 -0
- package/dist/library/rsform-api.d.ts +74 -0
- package/dist/library/rsform-api.js +879 -0
- package/dist/library/rsform-api.js.map +1 -0
- package/dist/library/rsform.d.ts +206 -0
- package/dist/library/rsform.js +32 -0
- package/dist/library/rsform.js.map +1 -0
- package/dist/library/rsmodel-api.d.ts +43 -0
- package/dist/library/rsmodel-api.js +836 -0
- package/dist/library/rsmodel-api.js.map +1 -0
- package/dist/library/rsmodel.d.ts +52 -0
- package/dist/library/rsmodel.js +25 -0
- package/dist/library/rsmodel.js.map +1 -0
- package/dist/library/structure-planner.d.ts +33 -0
- package/dist/library/structure-planner.js +481 -0
- package/dist/library/structure-planner.js.map +1 -0
- package/dist/parsing/ast.d.ts +49 -0
- package/dist/parsing/ast.js +93 -0
- package/dist/parsing/ast.js.map +1 -0
- package/dist/parsing/index.d.ts +3 -0
- package/dist/parsing/index.js +141 -0
- package/dist/parsing/index.js.map +1 -0
- package/dist/parsing/lezer-tree.d.ts +13 -0
- package/dist/parsing/lezer-tree.js +50 -0
- package/dist/parsing/lezer-tree.js.map +1 -0
- package/dist/rslang/api.d.ts +53 -0
- package/dist/rslang/api.js +846 -0
- package/dist/rslang/api.js.map +1 -0
- package/dist/rslang/ast-annotations.d.ts +18 -0
- package/dist/rslang/ast-annotations.js +56 -0
- package/dist/rslang/ast-annotations.js.map +1 -0
- package/dist/rslang/error.d.ts +85 -0
- package/dist/rslang/error.js +159 -0
- package/dist/rslang/error.js.map +1 -0
- package/dist/rslang/eval/calculator.d.ts +43 -0
- package/dist/rslang/eval/calculator.js +1639 -0
- package/dist/rslang/eval/calculator.js.map +1 -0
- package/dist/rslang/eval/evaluation-cache.d.ts +36 -0
- package/dist/rslang/eval/evaluation-cache.js +310 -0
- package/dist/rslang/eval/evaluation-cache.js.map +1 -0
- package/dist/rslang/eval/evaluator.d.ts +70 -0
- package/dist/rslang/eval/evaluator.js +1514 -0
- package/dist/rslang/eval/evaluator.js.map +1 -0
- package/dist/rslang/eval/value-api.d.ts +48 -0
- package/dist/rslang/eval/value-api.js +490 -0
- package/dist/rslang/eval/value-api.js.map +1 -0
- package/dist/rslang/eval/value.d.ts +36 -0
- package/dist/rslang/eval/value.js +118 -0
- package/dist/rslang/eval/value.js.map +1 -0
- package/dist/rslang/index.d.ts +17 -0
- package/dist/rslang/index.js +4314 -0
- package/dist/rslang/index.js.map +1 -0
- package/dist/rslang/labels.d.ts +16 -0
- package/dist/rslang/labels.js +315 -0
- package/dist/rslang/labels.js.map +1 -0
- package/dist/rslang/parser/expression-generator.d.ts +10 -0
- package/dist/rslang/parser/expression-generator.js +451 -0
- package/dist/rslang/parser/expression-generator.js.map +1 -0
- package/dist/rslang/parser/normalize.d.ts +11 -0
- package/dist/rslang/parser/normalize.js +507 -0
- package/dist/rslang/parser/normalize.js.map +1 -0
- package/dist/rslang/parser/parser.d.ts +5 -0
- package/dist/rslang/parser/parser.js +24 -0
- package/dist/rslang/parser/parser.js.map +1 -0
- package/dist/rslang/parser/parser.terms.d.ts +42 -0
- package/dist/rslang/parser/parser.terms.js +84 -0
- package/dist/rslang/parser/parser.terms.js.map +1 -0
- package/dist/rslang/parser/syntax-errors.d.ts +11 -0
- package/dist/rslang/parser/syntax-errors.js +403 -0
- package/dist/rslang/parser/syntax-errors.js.map +1 -0
- package/dist/rslang/parser/token.d.ts +79 -0
- package/dist/rslang/parser/token.js +95 -0
- package/dist/rslang/parser/token.js.map +1 -0
- package/dist/rslang/semantic/analyzer.d.ts +39 -0
- package/dist/rslang/semantic/analyzer.js +2604 -0
- package/dist/rslang/semantic/analyzer.js.map +1 -0
- package/dist/rslang/semantic/arguments-extractor.d.ts +42 -0
- package/dist/rslang/semantic/arguments-extractor.js +366 -0
- package/dist/rslang/semantic/arguments-extractor.js.map +1 -0
- package/dist/rslang/semantic/type-auditor.d.ts +73 -0
- package/dist/rslang/semantic/type-auditor.js +1570 -0
- package/dist/rslang/semantic/type-auditor.js.map +1 -0
- package/dist/rslang/semantic/typification-api.d.ts +27 -0
- package/dist/rslang/semantic/typification-api.js +320 -0
- package/dist/rslang/semantic/typification-api.js.map +1 -0
- package/dist/rslang/semantic/typification-parser.d.ts +12 -0
- package/dist/rslang/semantic/typification-parser.js +226 -0
- package/dist/rslang/semantic/typification-parser.js.map +1 -0
- package/dist/rslang/semantic/typification.d.ts +119 -0
- package/dist/rslang/semantic/typification.js +74 -0
- package/dist/rslang/semantic/typification.js.map +1 -0
- package/dist/rslang/semantic/value-auditor.d.ts +43 -0
- package/dist/rslang/semantic/value-auditor.js +523 -0
- package/dist/rslang/semantic/value-auditor.js.map +1 -0
- package/dist/rslang/semantic/value-class.d.ts +10 -0
- package/dist/rslang/semantic/value-class.js +9 -0
- package/dist/rslang/semantic/value-class.js.map +1 -0
- package/dist/rslang/typification-graph.d.ts +33 -0
- package/dist/rslang/typification-graph.js +311 -0
- package/dist/rslang/typification-graph.js.map +1 -0
- package/dist/shared/branded.d.ts +7 -0
- package/dist/shared/branded.js +1 -0
- package/dist/shared/branded.js.map +1 -0
- package/dist/shared/hash.d.ts +6 -0
- package/dist/shared/hash.js +18 -0
- package/dist/shared/hash.js.map +1 -0
- package/dist/shared/index.d.ts +2 -0
- package/dist/shared/index.js +18 -0
- package/dist/shared/index.js.map +1 -0
- package/package.json +184 -0
- package/src/cctext/index.ts +9 -0
- package/src/cctext/language-api.test.ts +149 -0
- package/src/cctext/language-api.ts +285 -0
- package/src/cctext/language.ts +80 -0
- package/src/graph/graph.test.ts +392 -0
- package/src/graph/graph.ts +433 -0
- package/src/graph/index.ts +1 -0
- package/src/index.ts +96 -0
- package/src/library/folder-tree.test.ts +47 -0
- package/src/library/folder-tree.ts +156 -0
- package/src/library/index.ts +46 -0
- package/src/library/library-api.test.ts +32 -0
- package/src/library/library-api.ts +11 -0
- package/src/library/library.ts +61 -0
- package/src/library/oss-api.ts +449 -0
- package/src/library/oss-layout-api.ts +377 -0
- package/src/library/oss-layout.ts +27 -0
- package/src/library/oss.ts +150 -0
- package/src/library/rsengine.ts +593 -0
- package/src/library/rsform-api.ts +533 -0
- package/src/library/rsform.ts +228 -0
- package/src/library/rsmodel-api.ts +340 -0
- package/src/library/rsmodel.ts +50 -0
- package/src/library/structure-planner.ts +143 -0
- package/src/parsing/ast.ts +136 -0
- package/src/parsing/index.ts +15 -0
- package/src/parsing/lezer-tree.ts +69 -0
- package/src/rslang/api.test.ts +116 -0
- package/src/rslang/api.ts +183 -0
- package/src/rslang/ast-annotations.ts +70 -0
- package/src/rslang/error.ts +129 -0
- package/src/rslang/eval/calculator.test.ts +124 -0
- package/src/rslang/eval/calculator.ts +121 -0
- package/src/rslang/eval/evaluation-cache.ts +257 -0
- package/src/rslang/eval/evaluator.test.ts +352 -0
- package/src/rslang/eval/evaluator.ts +935 -0
- package/src/rslang/eval/value-api.test.ts +105 -0
- package/src/rslang/eval/value-api.ts +444 -0
- package/src/rslang/eval/value.ts +102 -0
- package/src/rslang/index.ts +23 -0
- package/src/rslang/labels.ts +191 -0
- package/src/rslang/parser/expression-generator.test.ts +100 -0
- package/src/rslang/parser/expression-generator.ts +466 -0
- package/src/rslang/parser/normalize.test.ts +99 -0
- package/src/rslang/parser/normalize.ts +462 -0
- package/src/rslang/parser/parser.terms.ts +42 -0
- package/src/rslang/parser/parser.test.ts +153 -0
- package/src/rslang/parser/parser.ts +20 -0
- package/src/rslang/parser/rslang.grammar +251 -0
- package/src/rslang/parser/syntax-errors.ts +209 -0
- package/src/rslang/parser/token.ts +106 -0
- package/src/rslang/semantic/analyzer.test.ts +59 -0
- package/src/rslang/semantic/analyzer.ts +179 -0
- package/src/rslang/semantic/arguments-extractor.ts +327 -0
- package/src/rslang/semantic/type-auditor.test.ts +326 -0
- package/src/rslang/semantic/type-auditor.ts +1049 -0
- package/src/rslang/semantic/typification-api.test.ts +46 -0
- package/src/rslang/semantic/typification-api.ts +321 -0
- package/src/rslang/semantic/typification-parser.test.ts +50 -0
- package/src/rslang/semantic/typification-parser.ts +220 -0
- package/src/rslang/semantic/typification.ts +180 -0
- package/src/rslang/semantic/value-auditor.test.ts +206 -0
- package/src/rslang/semantic/value-auditor.ts +332 -0
- package/src/rslang/semantic/value-class.ts +11 -0
- package/src/rslang/typification-graph.ts +155 -0
- package/src/shared/branded.ts +6 -0
- package/src/shared/hash.ts +17 -0
- package/src/shared/index.ts +2 -0
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module: API for formal representation for systems of concepts.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type AnalysisFull,
|
|
7
|
+
RSErrorCode,
|
|
8
|
+
TypeClass,
|
|
9
|
+
TypeID,
|
|
10
|
+
type TypePath,
|
|
11
|
+
type Typification,
|
|
12
|
+
ValueClass
|
|
13
|
+
} from '../rslang';
|
|
14
|
+
import { basic, bool, constant, type EchelonFunctional, isTypification } from '../rslang/semantic/typification';
|
|
15
|
+
import { applyPath } from '../rslang/semantic/typification-api';
|
|
16
|
+
|
|
17
|
+
import { type LibraryItem } from './library';
|
|
18
|
+
import {
|
|
19
|
+
type ArgumentValue,
|
|
20
|
+
type Constituenta,
|
|
21
|
+
CstClass,
|
|
22
|
+
CstStatus,
|
|
23
|
+
CstType,
|
|
24
|
+
type RSForm,
|
|
25
|
+
type RSFormStats
|
|
26
|
+
} from './rsform';
|
|
27
|
+
|
|
28
|
+
/** Record of {@link CstType} prefixes. */
|
|
29
|
+
const CST_TYPE_PREFIX: Record<CstType, string> = {
|
|
30
|
+
[CstType.NOMINAL]: 'N',
|
|
31
|
+
[CstType.BASE]: 'X',
|
|
32
|
+
[CstType.CONSTANT]: 'C',
|
|
33
|
+
[CstType.STRUCTURED]: 'S',
|
|
34
|
+
[CstType.AXIOM]: 'A',
|
|
35
|
+
[CstType.TERM]: 'D',
|
|
36
|
+
[CstType.FUNCTION]: 'F',
|
|
37
|
+
[CstType.PREDICATE]: 'P',
|
|
38
|
+
[CstType.STATEMENT]: 'T'
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Record of {@link CstType} to {@link CstClass} mapping. */
|
|
42
|
+
const CST_TYPE_TO_CLASS: Record<CstType, CstClass> = {
|
|
43
|
+
[CstType.NOMINAL]: CstClass.NOMINAL,
|
|
44
|
+
[CstType.BASE]: CstClass.BASIC,
|
|
45
|
+
[CstType.CONSTANT]: CstClass.BASIC,
|
|
46
|
+
[CstType.STRUCTURED]: CstClass.BASIC,
|
|
47
|
+
[CstType.TERM]: CstClass.DERIVED,
|
|
48
|
+
[CstType.FUNCTION]: CstClass.DERIVED,
|
|
49
|
+
[CstType.AXIOM]: CstClass.STATEMENT,
|
|
50
|
+
[CstType.PREDICATE]: CstClass.DERIVED,
|
|
51
|
+
[CstType.STATEMENT]: CstClass.STATEMENT
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Checks if {@link Constituenta} is a schema issue. */
|
|
55
|
+
export function isSchemaIssue(cst: Constituenta): boolean {
|
|
56
|
+
if (
|
|
57
|
+
cst.homonyms.length > 0 ||
|
|
58
|
+
cst.formalDuplicates.length > 0 ||
|
|
59
|
+
cst.status === CstStatus.INCORRECT ||
|
|
60
|
+
cst.status === CstStatus.INCALCULABLE ||
|
|
61
|
+
cst.is_type_mismatch
|
|
62
|
+
) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
if (isBasicConcept(cst.cst_type) && !isLogical(cst.cst_type)) {
|
|
66
|
+
if (!cst.convention || !cst.term_resolved) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Infers the status of an expression based on parsing and value information. */
|
|
74
|
+
export function inferStatus(parse: boolean, value?: ValueClass | null): CstStatus {
|
|
75
|
+
if (!parse) {
|
|
76
|
+
return CstStatus.INCORRECT;
|
|
77
|
+
}
|
|
78
|
+
if (value === null) {
|
|
79
|
+
return CstStatus.INCALCULABLE;
|
|
80
|
+
}
|
|
81
|
+
if (value === ValueClass.PROPERTY) {
|
|
82
|
+
return CstStatus.PROPERTY;
|
|
83
|
+
}
|
|
84
|
+
return CstStatus.VERIFIED;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Infers type of constituent for a given template and arguments. */
|
|
88
|
+
export function inferTemplatedType(templateType: CstType, args: ArgumentValue[]): CstType {
|
|
89
|
+
if (args.length === 0 || args.some(arg => !arg.value)) {
|
|
90
|
+
return templateType;
|
|
91
|
+
} else if (templateType === CstType.PREDICATE) {
|
|
92
|
+
return CstType.AXIOM;
|
|
93
|
+
} else {
|
|
94
|
+
return CstType.TERM;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Checks if given expression is a template. */
|
|
99
|
+
export function inferTemplate(expression: string): boolean {
|
|
100
|
+
const match = expression.match(/R\d+/g);
|
|
101
|
+
return (match && match?.length > 0) ?? false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Infers the {@link CstClass} based on the provided {@link CstType} and template status. */
|
|
105
|
+
export function inferClass(type: CstType, isTemplate: boolean = false): CstClass {
|
|
106
|
+
if (isTemplate) {
|
|
107
|
+
return CstClass.TEMPLATE;
|
|
108
|
+
}
|
|
109
|
+
return CST_TYPE_TO_CLASS[type];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Check if {@link Constituenta} is a template or a category. */
|
|
113
|
+
export function isTemplateCst(cst: Constituenta): boolean {
|
|
114
|
+
return cst.cst_type === CstType.FUNCTION || cst.cst_type === CstType.PREDICATE || cst.cst_type === CstType.STATEMENT;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Apply filter based on start {@link Constituenta} type. */
|
|
118
|
+
export function applyFilterCategory(start: Constituenta, items: Constituenta[]): Constituenta[] {
|
|
119
|
+
const startIndex = items.indexOf(start);
|
|
120
|
+
if (startIndex === -1) {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
const nextCategoryIndex = items.findIndex((cst, index) => index > startIndex && cst.cst_type === CstType.STATEMENT);
|
|
124
|
+
|
|
125
|
+
return items.filter((_, index) => index >= startIndex && (nextCategoryIndex === -1 || index < nextCategoryIndex));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Prefix for alias indicating {@link CstType}. */
|
|
129
|
+
export function getCstTypePrefix(type: CstType): string {
|
|
130
|
+
return CST_TYPE_PREFIX[type];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Guess {@link CstType} from user input hint. */
|
|
134
|
+
export function guessCstType(hint: string): CstType | null {
|
|
135
|
+
if (hint.length !== 1) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
for (const [type, prefix] of Object.entries(CST_TYPE_PREFIX)) {
|
|
139
|
+
if (hint === prefix) {
|
|
140
|
+
return type as CstType;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Evaluate if {@link CstType} is basic concept. */
|
|
147
|
+
export function isBasicConcept(type: CstType): boolean {
|
|
148
|
+
switch (type) {
|
|
149
|
+
case CstType.NOMINAL:
|
|
150
|
+
case CstType.BASE:
|
|
151
|
+
case CstType.CONSTANT:
|
|
152
|
+
case CstType.STRUCTURED:
|
|
153
|
+
case CstType.AXIOM:
|
|
154
|
+
return true;
|
|
155
|
+
|
|
156
|
+
case CstType.TERM:
|
|
157
|
+
case CstType.FUNCTION:
|
|
158
|
+
case CstType.PREDICATE:
|
|
159
|
+
case CstType.STATEMENT:
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Evaluate if {@link CstType} is base set or constant set. */
|
|
165
|
+
export function isBaseSet(type: CstType): boolean {
|
|
166
|
+
switch (type) {
|
|
167
|
+
case CstType.BASE:
|
|
168
|
+
case CstType.CONSTANT:
|
|
169
|
+
return true;
|
|
170
|
+
|
|
171
|
+
case CstType.NOMINAL:
|
|
172
|
+
case CstType.STRUCTURED:
|
|
173
|
+
case CstType.AXIOM:
|
|
174
|
+
case CstType.TERM:
|
|
175
|
+
case CstType.FUNCTION:
|
|
176
|
+
case CstType.PREDICATE:
|
|
177
|
+
case CstType.STATEMENT:
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Evaluate if {@link CstType} is a function. */
|
|
183
|
+
export function isFunctional(type: CstType): boolean {
|
|
184
|
+
switch (type) {
|
|
185
|
+
case CstType.FUNCTION:
|
|
186
|
+
case CstType.PREDICATE:
|
|
187
|
+
return true;
|
|
188
|
+
|
|
189
|
+
case CstType.NOMINAL:
|
|
190
|
+
case CstType.BASE:
|
|
191
|
+
case CstType.CONSTANT:
|
|
192
|
+
case CstType.STRUCTURED:
|
|
193
|
+
case CstType.AXIOM:
|
|
194
|
+
case CstType.TERM:
|
|
195
|
+
case CstType.STATEMENT:
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Evaluate if {@link CstType} is logical. */
|
|
201
|
+
export function isLogical(type: CstType): boolean {
|
|
202
|
+
switch (type) {
|
|
203
|
+
case CstType.AXIOM:
|
|
204
|
+
case CstType.STATEMENT:
|
|
205
|
+
return true;
|
|
206
|
+
|
|
207
|
+
case CstType.NOMINAL:
|
|
208
|
+
case CstType.BASE:
|
|
209
|
+
case CstType.CONSTANT:
|
|
210
|
+
case CstType.STRUCTURED:
|
|
211
|
+
case CstType.TERM:
|
|
212
|
+
case CstType.FUNCTION:
|
|
213
|
+
case CstType.PREDICATE:
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Evaluate if {@link Constituenta} can be used produce structure. */
|
|
219
|
+
export function canProduceStructure(cst: Constituenta): boolean {
|
|
220
|
+
switch (cst.cst_type) {
|
|
221
|
+
case CstType.NOMINAL:
|
|
222
|
+
case CstType.BASE:
|
|
223
|
+
case CstType.CONSTANT:
|
|
224
|
+
case CstType.AXIOM:
|
|
225
|
+
case CstType.STATEMENT:
|
|
226
|
+
case CstType.PREDICATE:
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
if (!cst.effectiveType) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
if (cst.cst_type === CstType.FUNCTION) {
|
|
233
|
+
const result = (cst.effectiveType as EchelonFunctional).result;
|
|
234
|
+
return typeCanProduceStructure(result);
|
|
235
|
+
}
|
|
236
|
+
if (!isTypification(cst.effectiveType)) {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
return typeCanProduceStructure(cst.effectiveType as Typification);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Evaluate if {@link CstType} can have manual typification. */
|
|
243
|
+
export function canHaveManualTypification(type: CstType): boolean {
|
|
244
|
+
switch (type) {
|
|
245
|
+
case CstType.STRUCTURED:
|
|
246
|
+
case CstType.TERM:
|
|
247
|
+
case CstType.FUNCTION:
|
|
248
|
+
case CstType.PREDICATE:
|
|
249
|
+
return true;
|
|
250
|
+
|
|
251
|
+
case CstType.NOMINAL:
|
|
252
|
+
case CstType.BASE:
|
|
253
|
+
case CstType.CONSTANT:
|
|
254
|
+
case CstType.AXIOM:
|
|
255
|
+
case CstType.STATEMENT:
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Validate new alias against {@link CstType} and {@link RSForm}. */
|
|
261
|
+
export function validateNewAlias(alias: string, type: CstType, schema: RSForm): boolean {
|
|
262
|
+
if (alias.length < 2) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
const prefix = getCstTypePrefix(type);
|
|
266
|
+
if (!alias.startsWith(prefix)) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
if (schema.cstByAlias.has(alias)) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
if (!/^\d+$/.exec(alias.substring(prefix.length))) {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Generate alias for new {@link Constituenta} of a given {@link CstType} for current {@link RSForm}. */
|
|
279
|
+
export function generateAlias(type: CstType, schema: RSForm, takenAliases: string[] = []): string {
|
|
280
|
+
const prefix = getCstTypePrefix(type);
|
|
281
|
+
if (schema.items.length <= 0) {
|
|
282
|
+
return `${prefix}1`;
|
|
283
|
+
}
|
|
284
|
+
let index = schema.items.reduce((prev, cst, index) => {
|
|
285
|
+
if (cst.cst_type !== type) {
|
|
286
|
+
return prev;
|
|
287
|
+
}
|
|
288
|
+
index = Number(cst.alias.slice(1 - cst.alias.length)) + 1;
|
|
289
|
+
return Math.max(prev, index);
|
|
290
|
+
}, 1);
|
|
291
|
+
let alias = `${prefix}${index}`;
|
|
292
|
+
while (takenAliases.includes(alias)) {
|
|
293
|
+
index = index + 1;
|
|
294
|
+
alias = `${prefix}${index}`;
|
|
295
|
+
}
|
|
296
|
+
return alias;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Id to pass as `insert_after` when creating a new {@link Constituenta} spawned by `targetId`:
|
|
301
|
+
* after the last existing child of that spawner, or after the spawner itself if none.
|
|
302
|
+
*/
|
|
303
|
+
export function inferNewSpawnPosition(schema: RSForm, targetId: number): number {
|
|
304
|
+
let last: number | null = null;
|
|
305
|
+
for (const cst of schema.items) {
|
|
306
|
+
if (cst.spawner === targetId) {
|
|
307
|
+
last = cst.id;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return last ?? targetId;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Sorts library items relevant for InlineSynthesis with specified {@link RSForm}. */
|
|
314
|
+
export function sortItemsForInlineSynthesis(receiver: RSForm, items: readonly LibraryItem[]): LibraryItem[] {
|
|
315
|
+
const result = items.filter(item => item.location === receiver.location);
|
|
316
|
+
for (const item of items) {
|
|
317
|
+
if (item.visible && item.owner === item.owner && !result.includes(item)) {
|
|
318
|
+
result.push(item);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
for (const item of items) {
|
|
322
|
+
if (item.visible && !result.includes(item)) {
|
|
323
|
+
result.push(item);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
for (const item of items) {
|
|
327
|
+
if (!result.includes(item)) {
|
|
328
|
+
result.push(item);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return result;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Remove alias from expression. */
|
|
335
|
+
export function removeAliasReference(expression: string, alias: string): string {
|
|
336
|
+
const result = expression.replaceAll(new RegExp(`\\b${alias}\\b`, 'g'), 'DEL');
|
|
337
|
+
return result === 'DEL' ? '' : result;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Add alias to expression. */
|
|
341
|
+
export function addAliasReference(expression: string, alias: string): string {
|
|
342
|
+
return expression + ' ' + alias;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Returns expected {@link TypeClass} of formal definition for {@link CstType}. */
|
|
346
|
+
export function typeClassForCstType(cstType: CstType): TypeClass {
|
|
347
|
+
switch (cstType) {
|
|
348
|
+
case CstType.NOMINAL:
|
|
349
|
+
case CstType.BASE:
|
|
350
|
+
case CstType.CONSTANT:
|
|
351
|
+
case CstType.STRUCTURED:
|
|
352
|
+
case CstType.TERM:
|
|
353
|
+
return TypeClass.typification;
|
|
354
|
+
case CstType.FUNCTION:
|
|
355
|
+
return TypeClass.function;
|
|
356
|
+
case CstType.PREDICATE:
|
|
357
|
+
return TypeClass.predicate;
|
|
358
|
+
case CstType.AXIOM:
|
|
359
|
+
case CstType.STATEMENT:
|
|
360
|
+
return TypeClass.logic;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Checks whether a constituenta type supports formal definitions. */
|
|
365
|
+
export function canHaveFormalDefinition(cstType: CstType): boolean {
|
|
366
|
+
return cstType !== CstType.BASE && cstType !== CstType.CONSTANT;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Analyze expression for {@link RSForm}. */
|
|
370
|
+
export function getAnalysisFor(expression: string, cstType: CstType, schema: RSForm, alias?: string): AnalysisFull {
|
|
371
|
+
if (!canHaveFormalDefinition(cstType)) {
|
|
372
|
+
if (expression.trim().length === 0) {
|
|
373
|
+
const fallbackAlias = alias && alias.length > 0 ? alias : 'X0';
|
|
374
|
+
const type = cstType === CstType.BASE ? bool(basic(fallbackAlias)) : bool(constant(fallbackAlias));
|
|
375
|
+
return {
|
|
376
|
+
success: true,
|
|
377
|
+
type,
|
|
378
|
+
valueClass: ValueClass.VALUE,
|
|
379
|
+
errors: [],
|
|
380
|
+
ast: null
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
return {
|
|
384
|
+
success: false,
|
|
385
|
+
type: null,
|
|
386
|
+
valueClass: null,
|
|
387
|
+
errors: [
|
|
388
|
+
{
|
|
389
|
+
code: RSErrorCode.definitionNotAllowed,
|
|
390
|
+
from: 0,
|
|
391
|
+
to: Math.max(0, expression.length - 1)
|
|
392
|
+
}
|
|
393
|
+
],
|
|
394
|
+
ast: null
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
return schema.analyzer.checkFull(expression, {
|
|
398
|
+
expected: typeClassForCstType(cstType),
|
|
399
|
+
isDomain: cstType === CstType.STRUCTURED
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Calculate statistics for {@link RSForm}. */
|
|
404
|
+
export function calculateSchemaStats(target: RSForm): RSFormStats {
|
|
405
|
+
const items = target.items;
|
|
406
|
+
return {
|
|
407
|
+
count_all: items.length,
|
|
408
|
+
count_crucial: items.reduce((sum, cst) => sum + (cst.crucial ? 1 : 0), 0),
|
|
409
|
+
|
|
410
|
+
step_complexity: items.reduce((sum, cst) => sum + calculateStepComplexity(cst), 0),
|
|
411
|
+
|
|
412
|
+
count_problematic: items.reduce((sum, cst) => sum + (isSchemaIssue(cst) ? 1 : 0), 0),
|
|
413
|
+
count_homonyms: items.reduce((sum, cst) => sum + (cst.homonyms.length > 0 ? 1 : 0), 0),
|
|
414
|
+
count_formal_duplicates: items.reduce((sum, cst) => sum + (cst.formalDuplicates.length > 0 ? 1 : 0), 0),
|
|
415
|
+
count_missing_convention: items.reduce((sum, cst) => sum + (isMissingConvention(cst) ? 1 : 0), 0),
|
|
416
|
+
count_type_mismatch: items.reduce((sum, cst) => sum + (cst.is_type_mismatch ? 1 : 0), 0),
|
|
417
|
+
|
|
418
|
+
count_incorrect: items.reduce((sum, cst) => sum + (cst.status === CstStatus.INCORRECT ? 1 : 0), 0),
|
|
419
|
+
count_property: items.reduce((sum, cst) => sum + (cst.analysis?.valueClass === ValueClass.PROPERTY ? 1 : 0), 0),
|
|
420
|
+
count_incalculable: items.reduce(
|
|
421
|
+
(sum, cst) => sum + (cst.analysis?.success && cst.analysis.valueClass === null ? 1 : 0),
|
|
422
|
+
0
|
|
423
|
+
),
|
|
424
|
+
count_inherited: items.reduce((sum, cst) => sum + (cst.is_inherited ? 1 : 0), 0),
|
|
425
|
+
|
|
426
|
+
count_text_term: items.reduce((sum, cst) => sum + (cst.term_raw ? 1 : 0), 0),
|
|
427
|
+
count_definition: items.reduce((sum, cst) => sum + (cst.definition_raw ? 1 : 0), 0),
|
|
428
|
+
count_convention: items.reduce((sum, cst) => sum + (isBasicConcept(cst.cst_type) && cst.convention ? 1 : 0), 0),
|
|
429
|
+
count_comment: items.reduce((sum, cst) => sum + (!isBasicConcept(cst.cst_type) && !cst.convention ? 1 : 0), 0),
|
|
430
|
+
|
|
431
|
+
count_base: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.BASE ? 1 : 0), 0),
|
|
432
|
+
count_constant: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.CONSTANT ? 1 : 0), 0),
|
|
433
|
+
count_structured: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.STRUCTURED ? 1 : 0), 0),
|
|
434
|
+
count_axiom: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.AXIOM ? 1 : 0), 0),
|
|
435
|
+
count_term: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.TERM ? 1 : 0), 0),
|
|
436
|
+
count_function: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.FUNCTION ? 1 : 0), 0),
|
|
437
|
+
count_predicate: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.PREDICATE ? 1 : 0), 0),
|
|
438
|
+
count_statement: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.STATEMENT ? 1 : 0), 0),
|
|
439
|
+
count_nominal: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.NOMINAL ? 1 : 0), 0)
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Finds {@link Constituenta} by structure path. */
|
|
444
|
+
export function findCstByStructure(schema: RSForm, target: Constituenta, path: TypePath): Constituenta | null {
|
|
445
|
+
for (const cst of schema.items) {
|
|
446
|
+
if (cst.spawner === target.id && cst.spawner_path) {
|
|
447
|
+
if (cst.spawner_path.length === path.length && cst.spawner_path.every((v, i) => v === path[i])) {
|
|
448
|
+
return cst;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** Retrieves name for piece of target {@link Constituenta} structure. */
|
|
456
|
+
export function getStructureName(schema: RSForm, target: Constituenta, path: TypePath): string {
|
|
457
|
+
const representation = findCstByStructure(schema, target, path);
|
|
458
|
+
if (representation) {
|
|
459
|
+
return `${representation.alias}: ${representation.term_resolved}`;
|
|
460
|
+
}
|
|
461
|
+
if (!isTypification(target.effectiveType)) {
|
|
462
|
+
return '';
|
|
463
|
+
}
|
|
464
|
+
const type = applyPath(target.effectiveType as Typification, path);
|
|
465
|
+
if (type?.typeID === TypeID.basic) {
|
|
466
|
+
const cst = schema.cstByAlias.get(type.baseID);
|
|
467
|
+
if (cst?.term_resolved) {
|
|
468
|
+
return `${cst.alias}: ${cst.term_resolved}`;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return '';
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// ========= Internals =====
|
|
475
|
+
function calculateStepComplexity(cst: Constituenta): number {
|
|
476
|
+
if (cst.cst_type === CstType.AXIOM || cst.cst_type === CstType.NOMINAL || !isBasicConcept(cst.cst_type)) {
|
|
477
|
+
return 0;
|
|
478
|
+
}
|
|
479
|
+
if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT || !cst.effectiveType) {
|
|
480
|
+
return 1;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const type = cst.effectiveType as Typification;
|
|
484
|
+
return calculateTypificationComplexity(type) + 1;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function calculateTypificationComplexity(type: Typification): number {
|
|
488
|
+
switch (type.typeID) {
|
|
489
|
+
case TypeID.basic:
|
|
490
|
+
case TypeID.integer:
|
|
491
|
+
case TypeID.anyTypification:
|
|
492
|
+
return 0;
|
|
493
|
+
case TypeID.tuple:
|
|
494
|
+
return (
|
|
495
|
+
type.factors.length + type.factors.reduce((sum, factor) => sum + calculateTypificationComplexity(factor), 0)
|
|
496
|
+
);
|
|
497
|
+
case TypeID.collection:
|
|
498
|
+
if (type.base.typeID === TypeID.tuple) {
|
|
499
|
+
let sum = 0;
|
|
500
|
+
type.base.factors.forEach(factor => {
|
|
501
|
+
sum += calculateTypificationComplexity(factor);
|
|
502
|
+
});
|
|
503
|
+
return sum + type.base.factors.length;
|
|
504
|
+
} else if (type.base.typeID === TypeID.collection) {
|
|
505
|
+
return calculateTypificationComplexity(type.base) + 1;
|
|
506
|
+
}
|
|
507
|
+
return 0;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function isMissingConvention(cst: Constituenta): boolean {
|
|
512
|
+
if (isBasicConcept(cst.cst_type) && !isLogical(cst.cst_type)) {
|
|
513
|
+
if (!cst.convention || !cst.term_resolved) {
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/** Evaluate if {@link Typification} can be used to produce structure. */
|
|
521
|
+
function typeCanProduceStructure(type: Typification): boolean {
|
|
522
|
+
if (type.typeID === TypeID.basic || type.typeID === TypeID.integer || type.typeID === TypeID.anyTypification) {
|
|
523
|
+
return false;
|
|
524
|
+
} else if (type.typeID === TypeID.tuple) {
|
|
525
|
+
return true;
|
|
526
|
+
} else {
|
|
527
|
+
return (
|
|
528
|
+
type.base.typeID !== TypeID.basic &&
|
|
529
|
+
type.base.typeID !== TypeID.integer &&
|
|
530
|
+
type.base.typeID !== TypeID.anyTypification
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
}
|