-
Notifications
You must be signed in to change notification settings - Fork 168
/
factory.py
428 lines (373 loc) · 17.2 KB
/
factory.py
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Example template classes
"""
from typing import Any, Callable
import autosar.xml.template as ar_template
import autosar.xml.element as ar_element
import autosar.xml.enumeration as ar_enum
from autosar.xml.template import TemplateBase
import autosar.xml.workspace as ar_workspace
class SwBaseTypeTemplate(ar_template.ElementTemplate):
"""
SwBaseType template
"""
def __init__(self,
element_name: str,
namespace_name: str,
bit_size: int = None,
encoding: str = None,
native_declaration: str = None) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.BASE_TYPE)
self.category = "FIXED_LENGTH"
self.bit_size = bit_size
self.encoding = encoding
self.native_declaration = native_declaration
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.SwBaseType:
"""
Factory method for SwBaseType
"""
elem = ar_element.SwBaseType(name=self.element_name,
category=self.category,
size=self.bit_size,
encoding=self.encoding,
native_declaration=self.native_declaration)
return elem
class DataConstraintInternalTemplate(ar_template.ElementTemplate):
"""
Template class for internal DataConstraint
"""
def __init__(self,
element_name: str,
namespace_name: str,
lower_limit: int | float,
upper_limit: int | float) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.DATA_CONSTRAINT)
self.lower_limit = lower_limit
self.upper_limit = upper_limit
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.DataConstraint:
"""
Factory method
"""
elem = ar_element.DataConstraint.make_internal(self.element_name, self.lower_limit, self.upper_limit)
return elem
class CompuMethodEnumTemplate(ar_template.ElementTemplate):
"""
Template class for Compumethods based on enumeration
"""
def __init__(self,
element_name: str,
namespace_name: str,
value_table: list[str] | list[tuple],
auto_label: bool = True,
desc: str | None = None,) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPU_METHOD)
self.category = "TEXTTABLE"
self.value_table = value_table
self.auto_label = auto_label
self.desc = desc
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.CompuMethod:
"""
Factory method
"""
computation = ar_element.Computation.make_value_table(self.value_table)
elem = ar_element.CompuMethod(name=self.element_name,
desc=self.desc,
category=self.category,
int_to_phys=computation)
return elem
class ImplementationValueTypeTemplate(ar_template.ElementTemplate):
"""
Template class for value-based implementation data type
that references a base type
"""
def __init__(self,
element_name: str,
namespace_name: str,
base_type: ar_template.ElementTemplate,
data_constraint: ar_template.ElementTemplate = None,
compu_method: ar_template.ElementTemplate = None,
desc: str | None = None,
calibration_access: ar_enum.SwCalibrationAccess | None = None,
type_emitter: str | None = None
) -> None:
dependencies = [base_type]
if data_constraint is not None:
dependencies.append(data_constraint)
if compu_method is not None:
dependencies.append(compu_method)
super().__init__(element_name, namespace_name, ar_enum.PackageRole.IMPLEMENTATION_DATA_TYPE, dependencies)
self.category = "VALUE"
self.desc = desc
self.base_type = base_type
self.data_constraint = data_constraint
self.compu_method = compu_method
self.calibration_access = calibration_access
self.type_emitter = type_emitter
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.ImplementationDataType:
"""
Factory method
"""
base_type_ref = self.base_type.ref(workspace)
compu_method_ref = None if self.compu_method is None else self.compu_method.ref(workspace)
data_constraint_ref = None if self.data_constraint is None else self.data_constraint.ref(workspace)
sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=base_type_ref,
compu_method_ref=compu_method_ref,
data_constraint_ref=data_constraint_ref,
calibration_access=self.calibration_access,
)
elem = ar_element.ImplementationDataType(name=self.element_name,
desc=self.desc,
category=self.category,
sw_data_def_props=sw_data_def_props,
type_emitter=self.type_emitter)
return elem
class ImplementationEnumDataTypeTemplate(ar_template.ElementTemplate):
"""
Template class for implementation types based on
an enumeration table
"""
def __init__(self,
element_name: str,
namespace_name: str,
base_type: ar_template.ElementTemplate,
value_table: list[str],
desc: str | None = None,
calibration_access: ar_enum.SwCalibrationAccess | None = None,
data_constraint_suffix="_DataConstr",
compu_method_suffix="",
) -> None:
data_constraint = self._create_data_constraint(element_name,
namespace_name,
data_constraint_suffix,
value_table)
compu_method = self._create_compu_method(element_name,
namespace_name,
compu_method_suffix,
value_table)
dependencies = [base_type, data_constraint, compu_method]
super().__init__(element_name, namespace_name, ar_enum.PackageRole.IMPLEMENTATION_DATA_TYPE, dependencies)
self.category = "VALUE"
self.desc = desc
self.base_type = base_type
self.data_constraint = data_constraint
self.compu_method = compu_method
self.calibration_access = calibration_access
def _create_data_constraint(self,
element_name: str,
namespace_name: str,
suffix: str,
value_table: list[str]) -> ar_template.ElementTemplate:
return DataConstraintInternalTemplate(element_name + suffix, namespace_name, 0, len(value_table) - 1)
def _create_compu_method(self,
element_name: str,
namespace_name: str,
suffix: str,
value_table: list[str]) -> ar_template.ElementTemplate:
return CompuMethodEnumTemplate(element_name + suffix, namespace_name, value_table, auto_label=False)
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.ImplementationDataType:
"""
Factory method
"""
base_type_ref = self.base_type.ref(workspace)
compu_method_ref = None if self.compu_method is None else self.compu_method.ref(workspace)
data_constraint_ref = None if self.data_constraint is None else self.data_constraint.ref(workspace)
sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=base_type_ref,
compu_method_ref=compu_method_ref,
data_constraint_ref=data_constraint_ref,
calibration_access=self.calibration_access,
)
elem = ar_element.ImplementationDataType(name=self.element_name,
desc=self.desc,
category=self.category,
sw_data_def_props=sw_data_def_props)
return elem
class ModeDeclarationGroupTemplate(ar_template.ElementTemplate):
"""
ModeDeclarationGroup template
"""
def __init__(self,
element_name: str,
namespace_name: str,
mode_declarations: list[str],
initial_mode_name: str | None = None) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.MODE_DECLARATION)
self.mode_declarations = list(mode_declarations)
self.initial_mode_name = initial_mode_name
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.ModeDeclarationGroup:
"""
Factory method for SwBaseType
"""
initial_mode_ref = None
if self.initial_mode_name is not None:
initial_mode_ref = '/'.join([str(package.ref()), self.element_name, self.initial_mode_name])
elem = ar_element.ModeDeclarationGroup(name=self.element_name,
mode_declarations=self.mode_declarations,
initial_mode_ref=initial_mode_ref)
return elem
class ModeSwitchInterfaceTemplate(ar_template.ElementTemplate):
"""
ModeDeclarationGroup template
"""
def __init__(self,
element_name: str,
namespace_name: str,
mode_declaration_group: ModeDeclarationGroupTemplate,
mode_group_name: str,
is_service: bool = False) -> None:
dependencies = [mode_declaration_group]
super().__init__(element_name, namespace_name, ar_enum.PackageRole.PORT_INTERFACE, dependencies)
self.mode_group_name = mode_group_name
self.mode_declaration_group = mode_declaration_group
self.is_service = is_service
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.ModeSwitchInterface:
"""
Element creation method
"""
elem = ar_element.ModeSwitchInterface(self.element_name, is_service=self.is_service)
elem.create_mode_group(self.mode_group_name, self.mode_declaration_group.ref(workspace))
return elem
CreateFuncType = Callable[[ar_element.Package, ar_workspace.Workspace, dict[str, ar_element.ARElement] | None],
ar_element.ARElement]
class GenericPortInterfaceTemplate(ar_template.ElementTemplate):
"""
Generic port interface element template
"""
def __init__(self,
element_name: str,
namespace_name: str,
create_func: CreateFuncType,
depends: list[TemplateBase] | None = None) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.PORT_INTERFACE, depends)
self.create_func = create_func
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.PortInterface:
"""
Create method
"""
elem = self.create_func(package, workspace, dependencies, **kwargs)
return elem
class SenderReceiverInterfaceTemplate(ar_template.ElementTemplate):
"""
Sender receiver port interface template restricted to a single data element
It is assumed that the element name ends with "_I"
"""
def __init__(self,
element_name: str,
namespace_name: str,
data_element_template: ar_template.ElementTemplate,
data_element_name: str | None = None) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.PORT_INTERFACE, [data_element_template])
self.data_element_template = data_element_template
self.data_element_name = data_element_name
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.SenderReceiverInterface:
"""
Create method
"""
elem_type = dependencies[self.data_element_template.ref(workspace)]
port_interface = ar_element.SenderReceiverInterface(self.element_name)
elem_name = self.data_element_name
if elem_name is None:
if self.element_name.endswith("_I"):
elem_name = self.element_name[:-2]
else:
elem_name = self.element_name
port_interface.create_data_element(elem_name, type_ref=elem_type.ref())
return port_interface
class GenericComponentTypeTemplate(ar_template.ElementTemplate):
"""
Generic component type element template
"""
def __init__(self,
element_name: str,
namespace_name: str,
create_func: CreateFuncType,
depends: list[TemplateBase] | None = None,
append_to_package: bool = False) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPONENT_TYPE, depends, append_to_package)
self.create_func = create_func
def create(self,
package: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.PortInterface:
"""
Create method
"""
elem = self.create_func(package, workspace, dependencies, **kwargs)
return elem
class SwcImplementationTemplate(ar_template.ElementTemplate):
"""
SwcImplementation template
"""
def __init__(self,
element_name: str,
namespace_name: str,
component_type: ar_template.ElementTemplate) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPONENT_TYPE, [component_type])
self.component_type = component_type
def create(self,
_0: ar_element.Package,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.SwBaseType:
"""
Factory method for creating a new SwcImplementation
"""
swc_type = dependencies[self.component_type.ref(workspace)]
elem = ar_element.SwcImplementation(self.element_name,
behavior_ref=swc_type.internal_behavior.ref())
return elem
class ConstantTemplate(ar_template.ElementTemplate):
"""
Constant template
"""
def __init__(self,
element_name: str,
namespace_name: str,
value: Any) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.CONSTANT)
self.value = value
def create(self,
_0: ar_element.Package,
_1: ar_workspace.Workspace,
_2: dict[str, ar_element.ARElement] | None,
**_3) -> ar_element.SwBaseType:
"""
Factory method for creating a new SwcImplementation
"""
return ar_element.ConstantSpecification.make_constant(self.element_name, self.value)