-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathConfigPreprocessorComponent.ts
More file actions
314 lines (284 loc) · 12.6 KB
/
ConfigPreprocessorComponent.ts
File metadata and controls
314 lines (284 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import type { Resource, RdfObjectLoader } from 'rdf-object';
import type { Logger } from 'winston';
import { IRIS_OWL } from '../rdf/Iris';
import { uniqueTypes } from '../rdf/ResourceUtil';
import { ErrorResourcesContext } from '../util/ErrorResourcesContext';
import { GenericsContext } from './GenericsContext';
import type { IConfigPreprocessorTransform, IConfigPreprocessor } from './IConfigPreprocessor';
import type { ParameterHandler } from './ParameterHandler';
/**
* Handles config that refer to a component as type.
* The component may have parameters that can be applied on the config.
*/
export class ConfigPreprocessorComponent implements IConfigPreprocessor<IComponentConfigPreprocessorHandleResponse> {
public readonly objectLoader: RdfObjectLoader;
protected readonly componentResources: Record<string, Resource>;
protected readonly runTypeConfigs: Record<string, Resource[]>;
protected readonly parameterHandler: ParameterHandler;
protected readonly logger: Logger;
public constructor(options: IComponentConfigPreprocessorOptions) {
this.objectLoader = options.objectLoader;
this.componentResources = options.componentResources;
this.runTypeConfigs = options.runTypeConfigs;
this.parameterHandler = options.parameterHandler;
this.logger = options.logger;
}
public canHandle(config: Resource): IComponentConfigPreprocessorHandleResponse | undefined {
if (!config.property.requireName) {
// Collect all component types from the resource
const componentTypes: Resource[] = uniqueTypes(config, this.componentResources);
// Require either exactly one component type, or a requireName
if (componentTypes.length > 1) {
throw new ErrorResourcesContext(`Detected more than one component types for config "${config.value}"`, {
componentTypes: `[${componentTypes.map(resource => resource.value)}]`,
config,
});
}
if (componentTypes.length === 0) {
throw new ErrorResourcesContext(`Could not find (valid) component types for config "${config.value}" among its types, or a requireName`, {
configTypes: `${config.properties.types.map(resource => resource.value)}`,
config,
});
}
// If we have a referred component type, add it to the factory options
const component = componentTypes[0];
const module = component.property.module;
if (!module) {
throw new ErrorResourcesContext(`No module was found for the component "${component.value}"`, { config });
}
// Save this config so that other configs may inherit params from it in the future.
if (!this.runTypeConfigs[component.value]) {
this.runTypeConfigs[component.value] = [];
}
// Only save configs with a given id once.
if (!this.runTypeConfigs[component.value].some(resource => resource.term.equals(config.term))) {
this.runTypeConfigs[component.value].push(config);
}
return {
module,
component,
};
}
}
public transform(config: Resource, handleResponse: IComponentConfigPreprocessorHandleResponse):
IConfigPreprocessorTransform {
// Inherit parameter values
this.inheritParameterValues(config, handleResponse.component);
// Add all required config properties
const configRaw = this.objectLoader.createCompactedResource({});
if (config.isA('Instance')) {
configRaw.properties.types.push(this.objectLoader.createCompactedResource('oo:ComponentInstance'));
}
// Add reference to the original (config) instance, which we need to determine the instanceId @see ConfigConstructor
configRaw.property.originalInstance = config;
const requireName = handleResponse.component.property.requireName || handleResponse.module.property.requireName;
if (!requireName) {
throw new ErrorResourcesContext(`Could not find a requireName in either the config's module or component`, {
module: handleResponse.module,
component: handleResponse.component,
config,
});
}
configRaw.property.requireName = requireName;
const requireElement = handleResponse.component.property.requireElement;
if (requireElement) {
configRaw.property.requireElement = requireElement;
}
configRaw.property.arguments = this.transformConstructorArguments(config, handleResponse);
// Validate the input config
this.validateConfig(config, handleResponse);
return { rawConfig: configRaw, finishTransformation: true };
}
protected createGenericsContext(
handleResponse: IComponentConfigPreprocessorHandleResponse,
config: Resource,
): GenericsContext {
// Create a new generics context for the component's generic type parameters
const genericsContext = new GenericsContext(
this.objectLoader,
handleResponse.component.properties.genericTypeParameters,
);
// If the config has a genericTypeInstancesComponentScope, it will also have genericTypeInstances.
// In that case, we bind these instances to the component's generic type parameters within the context.
// (these values may have been set during generic param type-checking in
// ParameterPropertyHandlerRange#hasParamValueValidType)
if (config.property.genericTypeInstancesComponentScope &&
handleResponse.component.value === config.property.genericTypeInstancesComponentScope.value) {
const conflict = genericsContext.bindComponentGenericTypes(
handleResponse.component,
config.properties.genericTypeInstances,
{ config },
(subType, superType) => this.parameterHandler.parameterPropertyHandlerRange
.hasType(
subType,
superType,
genericsContext,
config.property.genericTypeInstancesComponentScope,
config.properties.genericTypeInstances,
{ config },
),
);
if (conflict) {
throw new ErrorResourcesContext(conflict.description, { cause: conflict });
}
}
return genericsContext;
}
/**
* Determine the constructor arguments of the given config.
* @param config A config.
* @param handleResponse Return value of the {#canHandle}.
*/
public transformConstructorArguments(
config: Resource,
handleResponse: IComponentConfigPreprocessorHandleResponse,
): Resource {
const entries: Resource[] = [];
const genericsContext = this.createGenericsContext(handleResponse, config);
for (const fieldData of handleResponse.component.properties.parameters) {
const field = this.objectLoader.createCompactedResource({});
field.property.key = this.objectLoader.createCompactedResource(`"${fieldData.term.value}"`);
const value = this.parameterHandler
.applyParameterValues(handleResponse.component, fieldData, config, genericsContext);
if (value) {
field.property.value = value;
}
entries.push(field);
}
// Create a single-arg hash constructor, and add all params as key-value pairs
const param0 = this.objectLoader.createCompactedResource({
fields: {
list: entries,
},
});
// Create constructor arguments list
return this.objectLoader.createCompactedResource({
list: [
param0,
],
});
}
/**
* Let this config inherit parameter values from previously instantiated configs.
* This will check for inheritanceValues that are defined on the component,
* which can refer to parameters from other components.
*
* For example, assume we had previously instantiated a component X with param P set to 'value'.
* If we instantiate component Y, which is defined to inherit values from param P of X,
* then it will automatically inherit this param P set to 'value'.
*
* This can effectively mutate the given config resource.
* @param config The config
* @param component The component
*/
public inheritParameterValues(config: Resource, component: Resource): void {
// Iterate over all params in the instantiating component
for (const parameter of component.properties.parameters) {
// Collect all InheritanceValue's (=owl:Restriction)
const inheritanceValueDefinitions: Resource[] = parameter.properties.inheritValues
.reduce((acc: Resource[], clazz: Resource) => {
if (clazz.properties.types.reduce((subAcc: boolean, type: Resource) => subAcc ||
type.value === IRIS_OWL.Restriction, false)) {
acc.push(clazz);
}
return acc;
}, []);
// Check the validity of all definitions
for (const inheritanceValueDefinition of inheritanceValueDefinitions) {
// Check if 'from' refers to a component
if (inheritanceValueDefinition.property.from) {
// Check if 'onParameter' refers to a parameter
if (!inheritanceValueDefinition.property.onParameter) {
throw new ErrorResourcesContext(`Missing onParameter property on parameter value inheritance definition`, {
parameter,
config,
component,
});
}
// Iterate over all components to inherit from
for (const componentType of inheritanceValueDefinition.properties.from) {
if (componentType.type !== 'NamedNode') {
throw new ErrorResourcesContext(`Detected invalid from term type "${componentType.type}" on parameter value inheritance definition`, {
parameter,
config,
component,
});
}
// Iterate over all instantiations of the referenced component
const typeInstances: Resource[] = this.runTypeConfigs[componentType.value];
if (typeInstances) {
for (const instance of typeInstances) {
// Iterate over all parameters to inherit from
for (const parentParameter of inheritanceValueDefinition.properties.onParameter) {
if (parentParameter.type !== 'NamedNode') {
throw new ErrorResourcesContext(`Detected invalid onParameter term type "${parentParameter.type}" on parameter value inheritance definition`, {
parentParameter,
config,
component,
});
}
// If the previous instance had a value for this parameter, copy it to our current config
if (instance.property[parentParameter.value]) {
// Copy the parameters
for (const value of instance.properties[parentParameter.value]) {
if (!config.properties[parentParameter.value].includes(value)) {
config.properties[parentParameter.value].push(value);
}
}
// Also add the parameter to the parameter type list
// This is needed to ensure that the param value will be instantiated during mapping
if (!component.properties.parameters.includes(parentParameter)) {
component.properties.parameters.push(parentParameter);
}
}
}
}
}
}
} else {
throw new ErrorResourcesContext(`Missing from property on parameter value inheritance definition`, {
parameter,
config,
component,
});
}
}
}
}
/**
* Run checks to see if the given config is valid.
* @param config The original config.
* @param handleResponse The handle response.
*/
public validateConfig(
config: Resource,
handleResponse: IComponentConfigPreprocessorHandleResponse,
): void {
// Determine all valid parameter ids
const validParameters: Record<string, boolean> = {};
for (const param of handleResponse.component.properties.parameters) {
validParameters[param.value] = true;
}
// Emit warning on undefined parameters inside the component's scope
const prefix = handleResponse.component.value;
for (const property of Object.keys(config.property)) {
if (property.startsWith(prefix) && !validParameters[property]) {
this.logger.warn(`Detected potentially invalid component parameter '${property}' in a config`);
}
}
}
public reset(): void {
// There is nothing to reset here
}
}
export interface IComponentConfigPreprocessorOptions {
objectLoader: RdfObjectLoader;
componentResources: Record<string, Resource>;
runTypeConfigs: Record<string, Resource[]>;
parameterHandler: ParameterHandler;
logger: Logger;
}
export interface IComponentConfigPreprocessorHandleResponse {
module: Resource;
component: Resource;
}