-
Notifications
You must be signed in to change notification settings - Fork 17
/
Components.c
403 lines (335 loc) · 15.2 KB
/
Components.c
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/********************************************************************************
* Copyright (c) 2020 AVL List GmbH and others
*
* This program and the accompanying materials are made available under the
* terms of the Apache Software License 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include "reader/ssp/Components.h"
#include "reader/EnumMapping.h"
#include "reader/ssp/Ports.h"
#include "reader/ssp/Schema.h"
#include "reader/ssp/SpecificData.h"
#include "reader/ssp/Connections.h"
#include "reader/ssp/Parameters.h"
#include "reader/model/connections/ConnectionInput.h"
#include "reader/model/parameters/specific_data/FmuParameterInput.h"
#include "reader/model/components/specific_data/FmuInput.h"
#include "reader/model/components/specific_data/IntegratorInput.h"
#include "reader/model/components/specific_data/VectorIntegratorInput.h"
#include "reader/model/components/specific_data/ConstantInput.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
SSDComponentSpecificDataDefinition ssdCommonSpecificDataDefinition[] = {
{"application/x-fmu-sharedlibrary", "com.avl.model.connect.ssp.component.fmu", object_class_of(ComponentTypeFmu), object_class_of(FmuInput), SSDReadFmuData, SSDDefaultFmuData, object_class_of(PortInput), NULL, NULL, SSDSetFmuParameters, object_class_of(FmuParameterInput), "com.avl.model.connect.ssp.parameter.fmu", SSDReadFmuParameterData, SSDDefaultFmuParameterData},
{"application/avl-mcx-integrator", "com.avl.model.connect.ssp.component.integrator", object_class_of(ComponentTypeIntegrator), object_class_of(IntegratorInput), SSDReadIntegratorData, SSDDefaultData, object_class_of(PortInput), NULL, NULL, NULL, NULL, NULL, NULL, NULL},
{"application/avl-mcx-vector-integrator", "com.avl.model.connect.ssp.component.vector_integrator", object_class_of(ComponentTypeVectorIntegrator), object_class_of(VectorIntegratorInput), SSDReadVectorIntegratorData, SSDDefaultData, object_class_of(PortInput), NULL, NULL, NULL, NULL, NULL, NULL, NULL},
{"application/avl-mcx-constant", "com.avl.model.connect.ssp.component.constant", object_class_of(ComponentTypeConstant), object_class_of(ConstantInput), SSDReadConstantData, SSDDefaultData, object_class_of(PortInput), NULL, NULL, NULL, NULL, NULL, NULL, NULL},
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
};
SSDComponentSpecificDataDefinition * ssdMcxExeComponents[] = {
ssdCommonSpecificDataDefinition,
NULL
};
static ComponentResultsInput * SSDReadComponentResults(xmlNodePtr annotationNode) {
ComponentResultsInput * resultsInput = (ComponentResultsInput *) object_create(ComponentResultsInput);
InputElement * element = (InputElement *) resultsInput;
McxStatus retVal = RETURN_OK;
if (!resultsInput) {
return NULL;
}
element->type = INPUT_SSD;
element->context = (void *) annotationNode;
{
xmlNodePtr resultsNode = xml_child(annotationNode, "Results");
if (!resultsNode) {
retVal = xml_error_missing_child(annotationNode, "Results");
goto cleanup;
}
retVal = xml_validate_node(resultsNode, "com.avl.model.connect.ssp.component.results");
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_enum(resultsNode, "resultLevel", storeLevelMapping, (OPTIONAL_VALUE(int) *) &resultsInput->resultLevel);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_bool(resultsNode, "rtFactor", &resultsInput->rtFactor);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_double(resultsNode, "startTime", &resultsInput->startTime);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_double(resultsNode, "endTime", &resultsInput->endTime);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_double(resultsNode, "stepTime", &resultsInput->stepTime);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_size_t(resultsNode, "stepCount", &resultsInput->stepCount);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
}
cleanup:
if (retVal == RETURN_ERROR) {
object_destroy(resultsInput);
return NULL;
}
return resultsInput;
}
static ComponentInput * SSDReadComponent(xmlNodePtr componentNode, SSDComponentSpecificDataDefinition * components[], ObjectContainer * units) {
ComponentInput * componentInput = NULL;
SSDComponentSpecificDataDefinition * typeDefinition = NULL;
McxStatus retVal = RETURN_OK;
{
// Type of Component
retVal = xml_attr_enum_weak_ptr(componentNode, "type", (void **) &components[0], sizeof(SSDComponentSpecificDataDefinition), (void*)&typeDefinition, SSD_OPTIONAL);
if (retVal == RETURN_ERROR) {
goto cleanup;
} else if (retVal == RETURN_WARNING) {
// in case the type attribute is not present, SSP defines `application/x-fmu-sharedlibrary` as default
size_t i = 0;
for (i = 0; components[i]->mimeType != NULL; i++) {
if (!strcmp(components[i]->mimeType, "application/x-fmu-sharedlibrary")) {
typeDefinition = components[i];
break;
}
}
if (!typeDefinition) {
retVal = xml_error_generic(componentNode, "Unsupported component type application/x-fmu-sharedlibrary");
goto cleanup;
}
}
componentInput = (ComponentInput *) object_create_(typeDefinition->inputType);
if (!componentInput) {
retVal = RETURN_ERROR;
goto cleanup;
}
componentInput->type = (ComponentType *) object_create_(typeDefinition->compType);
if (!componentInput->type) {
retVal = RETURN_ERROR;
goto cleanup;
}
}
{
InputElement * element = (InputElement *) componentInput;
element->type = INPUT_SSD;
element->context = (void *) componentNode;
}
{
retVal = xml_attr_string(componentNode, "name", &componentInput->name, SSD_MANDATORY);
if (RETURN_ERROR == retVal) {
goto cleanup;
}
}
{
xmlNodePtr annotationNode = xml_annotation_of_type(componentNode, "com.avl.model.connect.ssp.component");
if (annotationNode) {
xmlNodePtr componentAnnotationNode = xml_child(annotationNode, "Component");
if (!componentAnnotationNode) {
retVal = xml_error_missing_child(annotationNode, "Component");
goto cleanup;
}
retVal = xml_validate_node(componentAnnotationNode, "com.avl.model.connect.ssp.component");
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_bool(componentAnnotationNode, "inputAtEndTime", &componentInput->inputAtEndTime);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_int(componentAnnotationNode, "triggerSequence", &componentInput->triggerSequence);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = xml_opt_attr_double(componentAnnotationNode, "deltaTime", &componentInput->deltaTime);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
}
}
{
// read ports:
//
// <Connectors> contain both inputs and outputs. They are
// <Connector> nodes, discerned by their kind attribute which
// is "input", "output", "parameter", etc.
xmlNodePtr connectorsNode = xml_child(componentNode, "Connectors");
if (connectorsNode) {
componentInput->inports = SSDReadComponentPorts(connectorsNode,
"input",
typeDefinition->portType,
typeDefinition->portAnnotationType,
typeDefinition->ReadPort,
units);
if (!componentInput->inports) {
retVal = RETURN_ERROR;
goto cleanup;
}
componentInput->outports = SSDReadComponentPorts(connectorsNode,
"output",
typeDefinition->portType,
typeDefinition->portAnnotationType,
typeDefinition->ReadPort,
units);
if (!componentInput->outports) {
retVal = RETURN_ERROR;
goto cleanup;
}
} else {
componentInput->inports = NULL;
componentInput->outports = NULL;
}
}
{
// read parameters
xmlNodePtr connectorsNode = xml_child(componentNode, "Connectors");
xmlNodePtr paramBindingsNode = xml_child(componentNode, "ParameterBindings");
retVal = SSDReadParameters(componentInput,
connectorsNode,
paramBindingsNode,
typeDefinition->SetParameters,
typeDefinition->parameterType,
typeDefinition->parameterAnnotationType,
typeDefinition->ReadParameter,
typeDefinition->DefaultParameter,
units);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
}
{
// read specific data
if (typeDefinition->annotationType) {
xmlNodePtr annotationNode = xml_annotation_of_type(componentNode, typeDefinition->annotationType);
if (typeDefinition->Read) {
if (annotationNode) {
xmlNodePtr specificDataNode = xml_child(annotationNode, "SpecificData");
if (!specificDataNode) {
retVal = xml_error_missing_child(annotationNode, "SpecificData");
goto cleanup;
}
retVal = xml_validate_node(specificDataNode, typeDefinition->annotationType);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
retVal = typeDefinition->Read(componentNode, specificDataNode, componentInput);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
} else {
if (typeDefinition->DefaultSpecificData) {
retVal = typeDefinition->DefaultSpecificData(componentNode, componentInput);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
} else {
retVal = xml_error_generic(componentNode, "Annotation %s not defined", typeDefinition->annotationType);
goto cleanup;
}
}
} else if (annotationNode) {
retVal = xml_error_unsupported_node(annotationNode);
goto cleanup;
}
} else {
if (typeDefinition->DefaultSpecificData) {
retVal = typeDefinition->DefaultSpecificData(componentNode, componentInput);
if (retVal == RETURN_ERROR) {
goto cleanup;
}
}
}
}
{
// read results
xmlNodePtr annotationNode = xml_annotation_of_type(componentNode, "com.avl.model.connect.ssp.component.results");
if (annotationNode) {
componentInput->results = SSDReadComponentResults(annotationNode);
if (!componentInput->results) {
goto cleanup;
}
} else {
componentInput->results = NULL;
}
}
cleanup:
if (retVal == RETURN_ERROR) {
object_destroy(componentInput);
return NULL;
}
return componentInput;
}
static MapStringInt ssdImplementationMapping[] = {
{"any", SSD_IMPLEMENTATION_ANY},
{"CoSimulation", SSD_IMPLEMENTATION_COSIM},
{"ModelExchange", SSD_IMPLEMENTATION_ME},
{NULL, 0}
};
ComponentsInput * SSDReadComponents(xmlNodePtr componentsNode,
SSDComponentSpecificDataDefinition * components[],
int processSubsystems,
SSDImplementation supportedImplementation,
ObjectContainer * units) {
ComponentsInput * componentsInput = (ComponentsInput *) object_create(ComponentsInput);
InputElement * element = (InputElement *) componentsInput;
McxStatus retVal = RETURN_OK;
if (!componentsInput) {
return NULL;
}
element->type = INPUT_SSD;
element->context = (void *) componentsNode;
{
size_t num = xml_num_children(componentsNode);
size_t i = 0;
for (i = 0; i < num; i++) {
xmlNodePtr componentNode = xml_child_by_index(componentsNode, i);
ComponentInput * component = NULL;
if (!strcmp(xml_node_get_name(componentNode), "Component")) {
SSDImplementation implementation = SSD_IMPLEMENTATION_ANY;
retVal = xml_attr_enum(componentNode, "implementation", ssdImplementationMapping, (int*)&implementation, SSD_OPTIONAL);
if (retVal == RETURN_ERROR) {
goto cleanup_1;
}
// check that the correct implementation type is chosen
if (implementation != SSD_IMPLEMENTATION_ANY && implementation != supportedImplementation) {
retVal = xml_error_generic(componentNode, "Implementation type not allowed");
goto cleanup_1;
}
component = SSDReadComponent(componentNode, components, units);
if (!component) {
goto cleanup_1;
}
} else {
xml_error_unsupported_node(componentNode);
goto cleanup_1;
}
retVal = componentsInput->components->PushBack(componentsInput->components, (Object *) component);
if (RETURN_ERROR == retVal) {
goto cleanup_1;
}
continue;
cleanup_1:
retVal = RETURN_ERROR;
object_destroy(component);
goto cleanup_0;
}
}
cleanup_0:
if (retVal == RETURN_ERROR) {
object_destroy(componentsInput);
return NULL;
}
return componentsInput;
}
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif /* __cplusplus */