@lionweb/core 0.7.0-beta.18 → 0.7.0-beta.19
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/CHANGELOG.md +1 -0
- package/package.json +3 -3
- package/src/deserializer.ts +224 -0
- package/src/dynamic-facade.ts +63 -0
- package/src/extraction.ts +31 -0
- package/src/functions.ts +28 -0
- package/src/handler.ts +57 -0
- package/src/index.ts +13 -0
- package/src/m1/reference-utils.ts +106 -0
- package/src/m3/README.md +16 -0
- package/src/m3/builtins.ts +170 -0
- package/src/m3/constraints.ts +109 -0
- package/src/m3/deserializer.ts +38 -0
- package/src/m3/facade.ts +130 -0
- package/src/m3/factory.ts +98 -0
- package/src/m3/feature-resolvers.ts +55 -0
- package/src/m3/functions.ts +379 -0
- package/src/m3/index.ts +12 -0
- package/src/m3/lioncore.ts +139 -0
- package/src/m3/reference-checker.ts +38 -0
- package/src/m3/serializer.ts +13 -0
- package/src/m3/symbol-table.ts +119 -0
- package/src/m3/types.ts +325 -0
- package/src/reading.ts +55 -0
- package/src/references.ts +31 -0
- package/src/serializer.ts +244 -0
- package/src/types.ts +11 -0
- package/src/version.ts +5 -0
- package/src/writing.ts +79 -0
package/CHANGELOG.md
CHANGED
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
* Fix a bug where some features’ values are serialized multiple times.
|
|
34
34
|
* Expose feature resolution, through the `featureResolversFor` function (and associated types), which performs proper checking.
|
|
35
35
|
This avoids undebuggable `undefined` dereferencing at runtime in `deltaDeserializer` and `eventToDeltaTranslator` functions.
|
|
36
|
+
* Package `src/` again (— i.e., don't ignore for NPM packaging.)
|
|
36
37
|
|
|
37
38
|
|
|
38
39
|
## 0.6.12
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lionweb/core",
|
|
3
|
-
"version": "0.7.0-beta.
|
|
3
|
+
"version": "0.7.0-beta.19",
|
|
4
4
|
"description": "LionWeb core for {Java|Type}Script",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"release": "npm publish"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@lionweb/json": "0.7.0-beta.
|
|
31
|
-
"@lionweb/ts-utils": "0.7.0-beta.
|
|
30
|
+
"@lionweb/json": "0.7.0-beta.19",
|
|
31
|
+
"@lionweb/ts-utils": "0.7.0-beta.19"
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import {
|
|
2
|
+
currentSerializationFormatVersion,
|
|
3
|
+
LionWebId,
|
|
4
|
+
LionWebJsonChunk,
|
|
5
|
+
LionWebJsonNode,
|
|
6
|
+
LionWebKey
|
|
7
|
+
} from "@lionweb/json"
|
|
8
|
+
import { byIdMap, groupBy, keepDefineds } from "@lionweb/ts-utils"
|
|
9
|
+
import { Writer } from "./writing.js"
|
|
10
|
+
import { defaultSimplisticHandler, SimplisticHandler } from "./handler.js"
|
|
11
|
+
import { BuiltinPropertyValueDeserializer } from "./m3/builtins.js"
|
|
12
|
+
import { MemoisingSymbolTable } from "./m3/symbol-table.js"
|
|
13
|
+
import { Classifier, Containment, Enumeration, Language, PrimitiveType, Property, Reference } from "./m3/types.js"
|
|
14
|
+
import { unresolved } from "./references.js"
|
|
15
|
+
import { Node } from "./types.js"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Interface for objects that expose a method to deserialize a property's value.
|
|
19
|
+
*/
|
|
20
|
+
export interface PropertyValueDeserializer {
|
|
21
|
+
deserializeValue(value: string | undefined, property: Property): unknown | undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Misspelled alias of {@link PropertyValueDeserializer}, kept for backward compatibility, and to be deprecated and removed later.
|
|
26
|
+
*/
|
|
27
|
+
export interface PrimitiveTypeDeserializer extends PropertyValueDeserializer {}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @return a deserialization of a {@link LionWebJsonChunk}
|
|
31
|
+
*
|
|
32
|
+
* @param serializationChunk - a {@link SerializedModel model} from its LionWeb serialization JSON format
|
|
33
|
+
* @param writer - a {@link Writer} that is used to instantiate nodes and set values on them
|
|
34
|
+
* @param languages - a {@link Language language} that the serialized model is expected to conform to
|
|
35
|
+
* @param dependentNodes - a collection of nodes from dependent models against which all references in the serialized model are supposed to resolve against
|
|
36
|
+
* @param propertyValueDeserializer - a deserializer for values of properties (by default a {@link BuiltinPropertyValueDeserializer})
|
|
37
|
+
* @param problemHandler - a handler for reporting problems (by default a {@link defaultSimplisticHandler})
|
|
38
|
+
*/
|
|
39
|
+
export const deserializeSerializationChunk = <NT extends Node>(
|
|
40
|
+
serializationChunk: LionWebJsonChunk,
|
|
41
|
+
writer: Writer<NT>,
|
|
42
|
+
languages: Language[],
|
|
43
|
+
// TODO facades <--> languages, so it's weird that it looks split up like this
|
|
44
|
+
dependentNodes: Node[],
|
|
45
|
+
// TODO (#13) see if you can turn this into [nodes: Node[], writer: Writer<Node>][] after all
|
|
46
|
+
propertyValueDeserializer: BuiltinPropertyValueDeserializer = new BuiltinPropertyValueDeserializer(),
|
|
47
|
+
problemHandler: SimplisticHandler = defaultSimplisticHandler
|
|
48
|
+
): NT[] => {
|
|
49
|
+
if (serializationChunk.serializationFormatVersion !== currentSerializationFormatVersion) {
|
|
50
|
+
problemHandler.reportProblem(
|
|
51
|
+
`can't deserialize from serialization format other than version "${currentSerializationFormatVersion}" - assuming that version`
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const symbolTable = new MemoisingSymbolTable(languages)
|
|
56
|
+
|
|
57
|
+
const { nodes: serializedNodes } = serializationChunk
|
|
58
|
+
|
|
59
|
+
const serializedNodeById = byIdMap(serializedNodes)
|
|
60
|
+
|
|
61
|
+
const deserializedNodeById: { [id: LionWebId]: NT } = {}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Instantiates a {@link Node} from the given {@link LionWebJsonNode},
|
|
65
|
+
* and stores it under its ID so references to it can be resolved.
|
|
66
|
+
* For every serialized node, only one instance will ever be constructed (through memoisation).
|
|
67
|
+
*/
|
|
68
|
+
const instantiateMemoised = (serNode: LionWebJsonNode, parent?: NT): NT | null => {
|
|
69
|
+
if (serNode.id in deserializedNodeById) {
|
|
70
|
+
return deserializedNodeById[serNode.id]
|
|
71
|
+
}
|
|
72
|
+
const node = instantiate(serNode, parent)
|
|
73
|
+
if (node !== null) {
|
|
74
|
+
deserializedNodeById[node.id] = node
|
|
75
|
+
}
|
|
76
|
+
return node
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
type ReferenceToInstall = [node: NT, feature: Reference, refId: LionWebId]
|
|
80
|
+
const referencesToInstall: ReferenceToInstall[] = []
|
|
81
|
+
|
|
82
|
+
const tryInstantiate = (
|
|
83
|
+
parent: NT | undefined,
|
|
84
|
+
classifier: Classifier,
|
|
85
|
+
id: LionWebId,
|
|
86
|
+
propertySettings: { [propertyKey: LionWebKey]: unknown }
|
|
87
|
+
): NT | null => {
|
|
88
|
+
try {
|
|
89
|
+
return writer.nodeFor(parent, classifier, id, propertySettings)
|
|
90
|
+
} catch (e: unknown) {
|
|
91
|
+
problemHandler.reportProblem(
|
|
92
|
+
`error occurred during instantiation of a node for classifier ${classifier.name} with meta-pointer (${classifier.language.key}, ${classifier.language.version}, ${classifier.key}); reason:`
|
|
93
|
+
)
|
|
94
|
+
problemHandler.reportProblem((e as Error).toString())
|
|
95
|
+
return null
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Instantiates a {@link Node} from its {@link LionWebJsonNode serialization}.
|
|
101
|
+
*/
|
|
102
|
+
const instantiate = (
|
|
103
|
+
{ id, classifier: classifierMetaPointer, properties, containments, references, annotations }: LionWebJsonNode,
|
|
104
|
+
parent?: NT
|
|
105
|
+
): NT | null => {
|
|
106
|
+
const classifier = symbolTable.entityMatching(classifierMetaPointer)
|
|
107
|
+
|
|
108
|
+
if (classifier === undefined || !(classifier instanceof Classifier)) {
|
|
109
|
+
problemHandler.reportProblem(
|
|
110
|
+
`can't deserialize node with id=${id}: can't find the classifier with key ${classifierMetaPointer.key} in language (${classifierMetaPointer.language}, ${classifierMetaPointer.version})`
|
|
111
|
+
)
|
|
112
|
+
return null
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const allFeatures = symbolTable.allFeaturesOfEntityMatching(classifierMetaPointer)
|
|
116
|
+
|
|
117
|
+
const propertySettings: { [propertyKey: LionWebKey]: unknown } = {}
|
|
118
|
+
|
|
119
|
+
const serializedPropertiesPerKey = properties === undefined ? {} : groupBy(properties, sp => sp.property.key) // (this assumes no duplicate keys among properties!)
|
|
120
|
+
if (properties !== undefined) {
|
|
121
|
+
allFeatures
|
|
122
|
+
.filter(feature => feature instanceof Property)
|
|
123
|
+
.map(feature => feature as Property)
|
|
124
|
+
.forEach(property => {
|
|
125
|
+
if (property.key in serializedPropertiesPerKey) {
|
|
126
|
+
const value = serializedPropertiesPerKey[property.key][0].value
|
|
127
|
+
if (property.type instanceof PrimitiveType) {
|
|
128
|
+
propertySettings[property.key] =
|
|
129
|
+
value === null ? undefined : propertyValueDeserializer.deserializeValue(value, property as Property)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
if (property.type instanceof Enumeration) {
|
|
133
|
+
const literal = property.type.literals.find(literal => literal.key === value)
|
|
134
|
+
if (literal !== undefined) {
|
|
135
|
+
propertySettings[property.key] = writer.encodingOf(literal)
|
|
136
|
+
}
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
// (property is not handled, because neither a primitive type nor of enumeration type)
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const node = tryInstantiate(parent, classifier, id, propertySettings)
|
|
145
|
+
if (node === null) {
|
|
146
|
+
return null
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const serializedContainmentsPerKey = containments === undefined ? {} : groupBy(containments, sp => sp.containment.key) // (this assumes no duplicate keys among containments!)
|
|
150
|
+
const serializedReferencesPerKey = references === undefined ? {} : groupBy(references, sp => sp.reference.key) // (this assumes no duplicate keys among references!)
|
|
151
|
+
|
|
152
|
+
allFeatures.forEach(feature => {
|
|
153
|
+
if (feature instanceof Property && properties !== undefined && feature.key in serializedPropertiesPerKey) {
|
|
154
|
+
writer.setFeatureValue(node, feature, propertySettings[feature.key])
|
|
155
|
+
} else if (feature instanceof Containment && containments !== undefined && feature.key in serializedContainmentsPerKey) {
|
|
156
|
+
const childIds = serializedContainmentsPerKey[feature.key].flatMap(serChildren => serChildren.children) as LionWebId[]
|
|
157
|
+
if (feature.multiple) {
|
|
158
|
+
childIds.forEach(childId => {
|
|
159
|
+
if (childId in serializedNodeById) {
|
|
160
|
+
writer.setFeatureValue(node, feature, instantiateMemoised(serializedNodeById[childId], node))
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
} else {
|
|
164
|
+
if (childIds.length > 0) {
|
|
165
|
+
// just set the 1st one:
|
|
166
|
+
const firstChildId = childIds[0]
|
|
167
|
+
if (firstChildId in serializedNodeById) {
|
|
168
|
+
writer.setFeatureValue(node, feature, instantiateMemoised(serializedNodeById[firstChildId], node))
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} else if (feature instanceof Reference && references !== undefined && feature.key in serializedReferencesPerKey) {
|
|
173
|
+
const serRefs = (serializedReferencesPerKey[feature.key] ?? []).flatMap(serReferences =>
|
|
174
|
+
serReferences.targets.map(t => t.reference)
|
|
175
|
+
)
|
|
176
|
+
referencesToInstall.push(
|
|
177
|
+
...(serRefs.filter(serRef => typeof serRef === "string") as LionWebId[]).map(
|
|
178
|
+
refId => [node, feature, refId] as ReferenceToInstall
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
node.annotations = keepDefineds(
|
|
185
|
+
annotations
|
|
186
|
+
.filter(annotationId => annotationId in serializedNodeById)
|
|
187
|
+
.map(annotationId => instantiateMemoised(serializedNodeById[annotationId]))
|
|
188
|
+
)
|
|
189
|
+
.map(annotation => annotation!)
|
|
190
|
+
|
|
191
|
+
return node
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const rootLikeNodes = keepDefineds(
|
|
195
|
+
serializedNodes
|
|
196
|
+
.filter(({ parent }) => parent === null || !(parent in serializedNodeById))
|
|
197
|
+
.map(serializedNode => instantiateMemoised(serializedNode))
|
|
198
|
+
)
|
|
199
|
+
.map(node => node!)
|
|
200
|
+
|
|
201
|
+
const dependentNodesById = byIdMap(dependentNodes)
|
|
202
|
+
|
|
203
|
+
referencesToInstall.forEach(([node, reference, refId]) => {
|
|
204
|
+
const target = deserializedNodeById[refId] ?? dependentNodesById[refId]
|
|
205
|
+
const value = (() => {
|
|
206
|
+
if (target === undefined) {
|
|
207
|
+
const metaTypeMessage = "concept" in node ? ` and (meta-)type ${node.concept}` : ""
|
|
208
|
+
problemHandler.reportProblem(
|
|
209
|
+
`couldn't resolve the target with id=${refId} of a "${reference.name}" reference on the node with id=${node.id}${metaTypeMessage}`
|
|
210
|
+
)
|
|
211
|
+
return unresolved
|
|
212
|
+
}
|
|
213
|
+
return target
|
|
214
|
+
})()
|
|
215
|
+
writer.setFeatureValue(node, reference, value)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
return rootLikeNodes
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Alias for {@link deserializeSerializationChunk}.
|
|
223
|
+
*/
|
|
224
|
+
export const deserializeChunk = deserializeSerializationChunk
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { LionWebKey } from "@lionweb/json"
|
|
2
|
+
import { builtinFeatures } from "./m3/builtins.js"
|
|
3
|
+
import { Classifier } from "./m3/types.js"
|
|
4
|
+
import { Reader, ResolveInfoDeducer } from "./reading.js"
|
|
5
|
+
import { Node } from "./types.js"
|
|
6
|
+
import { updateSettingsKeyBased, Writer } from "./writing.js"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Type definition for "dynamic nodes" that are not backed by specific types (e.g. classes).
|
|
11
|
+
*/
|
|
12
|
+
export type DynamicNode = Node & {
|
|
13
|
+
classifier: Classifier
|
|
14
|
+
settings: Record<string, unknown>
|
|
15
|
+
}
|
|
16
|
+
// TODO could also have properties, containments, references - mimicking the serialization
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const propertyGetterFor = (key: LionWebKey): ResolveInfoDeducer<DynamicNode> =>
|
|
20
|
+
(node) =>
|
|
21
|
+
(key in node.settings && typeof node.settings[key] === "string")
|
|
22
|
+
? node.settings[key] as string // FIXME type cast shouldn't be necessary
|
|
23
|
+
: undefined
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An implementation of {@link Reader} for {@link DynamicNode dynamic nodes}.
|
|
27
|
+
*/
|
|
28
|
+
export const dynamicReader: Reader<DynamicNode> = ({
|
|
29
|
+
classifierOf: (node) => node.classifier,
|
|
30
|
+
getFeatureValue: (node, feature) =>
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
(node.settings as any)[feature.key],
|
|
33
|
+
enumerationLiteralFrom: (value, enumeration) =>
|
|
34
|
+
enumeration.literals.find(({key}) => key === value)
|
|
35
|
+
?? null, // (undefined -> null)
|
|
36
|
+
resolveInfoFor: propertyGetterFor(builtinFeatures.inamed_name.key)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Alias for {@link Reader}, kept for backward compatibility, and to be deprecated and removed later.
|
|
41
|
+
*/
|
|
42
|
+
export const dynamicExtractionFacade = dynamicReader
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* An implementation of {@link Writer} for {@link DynamicNode dynamic nodes}.
|
|
46
|
+
*/
|
|
47
|
+
export const dynamicWriter: Writer<DynamicNode> = ({
|
|
48
|
+
nodeFor: (_parent, classifier, id, _propertySettings) => ({
|
|
49
|
+
id,
|
|
50
|
+
classifier,
|
|
51
|
+
settings: {}
|
|
52
|
+
} as DynamicNode),
|
|
53
|
+
setFeatureValue: (node, feature, value) => {
|
|
54
|
+
updateSettingsKeyBased(node.settings, feature, value)
|
|
55
|
+
},
|
|
56
|
+
encodingOf: ({key}) => key
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Alias for {@link Reader}, kept for backward compatibility, and to be deprecated and removed later.
|
|
61
|
+
*/
|
|
62
|
+
export const dynamicInstantiationFacade = dynamicReader
|
|
63
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { flatMapNonCyclingFollowing, trivialFlatMapper } from "@lionweb/ts-utils"
|
|
2
|
+
|
|
3
|
+
import { allFeaturesOf, isContainment } from "./m3/index.js"
|
|
4
|
+
import { Reader } from "./reading.js"
|
|
5
|
+
import { Node } from "./types.js"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Type def. for functions that extract {@link Node nodes} from a given one.
|
|
10
|
+
*/
|
|
11
|
+
export type NodesExtractor<NT extends Node> = (node: NT) => NT[]
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @return A function that extracts the children from a given node.
|
|
15
|
+
*/
|
|
16
|
+
export const childrenExtractorUsing = <NT extends Node>(reader: Reader<NT>): NodesExtractor<NT> =>
|
|
17
|
+
(node: NT): NT[] => [
|
|
18
|
+
...(allFeaturesOf(reader.classifierOf(node))
|
|
19
|
+
.filter(isContainment)
|
|
20
|
+
.flatMap((containment) => reader.getFeatureValue(node, containment) ?? [])),
|
|
21
|
+
// FIXME there's NO guarantee about the result of reader.getFeatureValue(node, containment) !!!
|
|
22
|
+
...node.annotations
|
|
23
|
+
] as NT[]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @return a function that extracts *all* nodes from a given start node - usually a root node.
|
|
28
|
+
*/
|
|
29
|
+
export const nodesExtractorUsing = <NT extends Node>(reader: Reader<NT>): NodesExtractor<NT> =>
|
|
30
|
+
flatMapNonCyclingFollowing(trivialFlatMapper, childrenExtractorUsing<NT>(reader))
|
|
31
|
+
|
package/src/functions.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { LionWebId } from "@lionweb/json"
|
|
2
|
+
import { flatMapNonCyclingFollowing, trivialFlatMapper } from "@lionweb/ts-utils"
|
|
3
|
+
import { Node } from "./types.js"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @return a list of itself and the ancestors of the given {@link Node node}, in anti-chronological order.
|
|
8
|
+
*/
|
|
9
|
+
export const containmentChain = (node: Node): Node[] => {
|
|
10
|
+
const getParent = (t: Node): Node[] => t.parent === undefined ? [] : [t.parent]
|
|
11
|
+
return flatMapNonCyclingFollowing(trivialFlatMapper, getParent)(node)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Maps an array of {@link Node AST nodes} or `null`s to their IDs.
|
|
17
|
+
* These `null`s might be the result of unresolved children.
|
|
18
|
+
*/
|
|
19
|
+
export const asIds = (nodeOrNulls: (Node | null)[]): (LionWebId | null)[] =>
|
|
20
|
+
nodeOrNulls.map((nodeOrNull) => nodeOrNull === null ? null : nodeOrNull.id)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @return the id of the given {@link Node node}.
|
|
25
|
+
*/
|
|
26
|
+
export const idOf = <T extends Node>({id}: T): LionWebId =>
|
|
27
|
+
id
|
|
28
|
+
|
package/src/handler.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simplistic handler to which problems that arise during deserialization,
|
|
3
|
+
* are reported as plain text by calling the `reportProblem` function.
|
|
4
|
+
*/
|
|
5
|
+
export interface SimplisticHandler {
|
|
6
|
+
reportProblem: (message: string) => void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A default simplistic handler that just outputs everything of the console.
|
|
11
|
+
*/
|
|
12
|
+
export const defaultSimplisticHandler: SimplisticHandler = {
|
|
13
|
+
reportProblem: (message) => {
|
|
14
|
+
console.log(message)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A simplistic handler that just accumulates problems (in terms of their messages).
|
|
21
|
+
*/
|
|
22
|
+
export class AccumulatingSimplisticHandler implements SimplisticHandler {
|
|
23
|
+
private _allProblems: string[] = []
|
|
24
|
+
reportProblem(message: string) {
|
|
25
|
+
this._allProblems.push(message)
|
|
26
|
+
}
|
|
27
|
+
get allProblems() {
|
|
28
|
+
return this._allProblems
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A simplistic handler that aggregates problems by their message.
|
|
35
|
+
* This is convenient for problems that arise many times during deserialization
|
|
36
|
+
* but produce the exact same message every time.
|
|
37
|
+
*/
|
|
38
|
+
export class AggregatingSimplisticHandler implements SimplisticHandler {
|
|
39
|
+
private messageByCount: { [message: string]: number } = {}
|
|
40
|
+
reportProblem(message: string) {
|
|
41
|
+
this.messageByCount[message] = (this.messageByCount[message] ?? 0) + 1
|
|
42
|
+
}
|
|
43
|
+
reportAllProblemsOnConsole(asTable = false) {
|
|
44
|
+
if (asTable) {
|
|
45
|
+
console.table(this.messageByCount)
|
|
46
|
+
} else {
|
|
47
|
+
Object.entries(this.messageByCount)
|
|
48
|
+
.forEach(([message, count]) => {
|
|
49
|
+
console.log(`${message} (${count})`)
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
allProblems() {
|
|
54
|
+
return { ...this.messageByCount }
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./deserializer.js"
|
|
2
|
+
export * from "./dynamic-facade.js"
|
|
3
|
+
export * from "./extraction.js"
|
|
4
|
+
export * from "./handler.js"
|
|
5
|
+
export * from "./functions.js"
|
|
6
|
+
export * from "./reading.js"
|
|
7
|
+
export * from "./references.js"
|
|
8
|
+
export * from "./serializer.js"
|
|
9
|
+
export * from "./types.js"
|
|
10
|
+
export * from "./version.js"
|
|
11
|
+
export * from "./writing.js"
|
|
12
|
+
export * from "./m1/reference-utils.js"
|
|
13
|
+
export * from "./m3/index.js"
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Reader } from "../reading.js"
|
|
2
|
+
import { allFeaturesOf, Reference } from "../m3/index.js"
|
|
3
|
+
import { Node } from "../types.js"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents information about a source and target node related through a {@link Reference}.
|
|
8
|
+
* An index of `null` means that the reference is (at most) single-valued.
|
|
9
|
+
*/
|
|
10
|
+
export class ReferenceValue<NT extends Node> {
|
|
11
|
+
constructor(
|
|
12
|
+
public readonly sourceNode: NT,
|
|
13
|
+
public readonly targetNode: NT,
|
|
14
|
+
public readonly reference: Reference,
|
|
15
|
+
public readonly index: number | null
|
|
16
|
+
) {}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Finds all references within the given scope, as {@link ReferenceValue reference values}.
|
|
22
|
+
* To search within all nodes under a collection of root nodes,
|
|
23
|
+
* use _child extraction_ to compute all nodes in the forest hanging off of those root nodes as scope.
|
|
24
|
+
* Note that any reference is found uniquely,
|
|
25
|
+
* i.e. the returned {@link ReferenceValue reference values} are pairwise distinct,
|
|
26
|
+
* even if the scope passed contains duplicate nodes.
|
|
27
|
+
*
|
|
28
|
+
* @param scope - the {@link Node nodes} that are searched for references
|
|
29
|
+
* @param reader - a {@link Reader} to reflect on nodes.
|
|
30
|
+
* _Note_ that it's assumed that its {@link getFeatureValue} function doesn't throw.
|
|
31
|
+
*/
|
|
32
|
+
export const referenceValues = <NT extends Node>(
|
|
33
|
+
scope: NT[],
|
|
34
|
+
reader: Reader<NT>
|
|
35
|
+
): ReferenceValue<NT>[] => {
|
|
36
|
+
const visit = (sourceNode: NT, reference: Reference): ReferenceValue<NT>[] => {
|
|
37
|
+
if (reference.multiple) {
|
|
38
|
+
const targetNodes = (reader.getFeatureValue(sourceNode, reference) ?? []) as NT[]
|
|
39
|
+
return targetNodes
|
|
40
|
+
.map((targetNode, index) =>
|
|
41
|
+
new ReferenceValue<NT>(sourceNode, targetNode, reference, index)
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const targetNode = reader.getFeatureValue(sourceNode, reference) as (NT | undefined)
|
|
46
|
+
if (targetNode !== undefined) {
|
|
47
|
+
return [new ReferenceValue<NT>(sourceNode, targetNode, reference, null)]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return []
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return [...new Set(scope)] // ~ .distinct()
|
|
54
|
+
.flatMap((sourceNode) =>
|
|
55
|
+
allFeaturesOf(reader.classifierOf(sourceNode))
|
|
56
|
+
.filter((feature) => feature instanceof Reference)
|
|
57
|
+
.map((feature) => feature as Reference)
|
|
58
|
+
.flatMap((reference) => visit(sourceNode, reference))
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Finds all references coming into the given target node or any of the given target nodes,
|
|
65
|
+
* within the given scope, as {@link ReferenceValue reference values}.
|
|
66
|
+
* To search within all nodes under a collection of root nodes,
|
|
67
|
+
* use _child extraction_ to compute all nodes in the forest hanging off of those root nodes as scope.
|
|
68
|
+
* Note that any reference is found uniquely,
|
|
69
|
+
* i.e. the returned {@link ReferenceValue reference values} are pairwise distinct,
|
|
70
|
+
* even if the given target nodes or scope contain duplicate nodes.
|
|
71
|
+
*
|
|
72
|
+
* @param targetNodeOrNodes - one or more target {@link Node nodes} for which the incoming references are searched
|
|
73
|
+
* @param scope - the {@link Node nodes} that are searched for references
|
|
74
|
+
* @param reader - a {@link Reader} to reflect on nodes.
|
|
75
|
+
* _Note_ that it's assumed that its {@link getFeatureValue} function doesn't throw.
|
|
76
|
+
*/
|
|
77
|
+
export const incomingReferences = <NT extends Node>(
|
|
78
|
+
targetNodeOrNodes: NT[] | NT,
|
|
79
|
+
scope: NT[],
|
|
80
|
+
reader: Reader<NT>
|
|
81
|
+
): ReferenceValue<NT>[] => {
|
|
82
|
+
const targetNodes = Array.isArray(targetNodeOrNodes) ? targetNodeOrNodes : [targetNodeOrNodes]
|
|
83
|
+
return referenceValues(scope, reader)
|
|
84
|
+
.filter((referenceValue) => targetNodes.indexOf(referenceValue.targetNode) > -1)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Finds all references to nodes that are not in the given scope, as {@link ReferenceValue reference values}.
|
|
90
|
+
* To search within all nodes under a collection of root nodes,
|
|
91
|
+
* use _child extraction_ to compute all nodes in the forest hanging off of those root nodes as scope.
|
|
92
|
+
* Note that any reference is found uniquely,
|
|
93
|
+
* i.e. the returned {@link ReferenceValue reference values} are pairwise distinct,
|
|
94
|
+
* even if the given scope contains duplicate nodes.
|
|
95
|
+
*
|
|
96
|
+
* @param scope - the {@link Node nodes} that form the scope of “reachable” nodes
|
|
97
|
+
* @param reader - a {@link Reader} to reflect on nodes.
|
|
98
|
+
* _Note_ that it's assumed that its {@link getFeatureValue} function doesn't throw.
|
|
99
|
+
*/
|
|
100
|
+
export const referencesToOutOfScopeNodes = <NT extends Node>(
|
|
101
|
+
scope: NT[],
|
|
102
|
+
reader: Reader<NT>
|
|
103
|
+
): ReferenceValue<NT>[] =>
|
|
104
|
+
referenceValues(scope, reader)
|
|
105
|
+
.filter((referenceValue) => scope.indexOf(referenceValue.targetNode) === -1)
|
|
106
|
+
|
package/src/m3/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# LionCore M3 metametamodel
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
## Aspects
|
|
5
|
+
|
|
6
|
+
* [TypeScript type definitions](./types.ts)
|
|
7
|
+
* [Factory](./factory.ts) for conveniently creating M3 instances
|
|
8
|
+
* [Classifiers built in (`LionCore-builtins`) to LionCore](./builtins.ts)
|
|
9
|
+
* [Facades specific for M3 instances](./facade.ts)
|
|
10
|
+
* Persistence: [serializer](./serializer.ts) and [deserializer](./deserializer.ts)
|
|
11
|
+
* [Constraints checker](./constraints.ts)
|
|
12
|
+
* Convenience/helper [functions](./functions.ts) defined on M3 concepts
|
|
13
|
+
* ([Reference checker](./reference-checker.ts))
|
|
14
|
+
|
|
15
|
+
An interesting place to start might be the [self-definition](./lioncore.ts) of LionCore (`LionCore-M3`) using its own [TypeScript type definitions](./types.ts).
|
|
16
|
+
|