Skip to content

Commit 016d144

Browse files
committed
Refactoring template API
- Template classes can now choose if created element is added to package - Change argument type in element template create-method - Rename init_package_map in Workspace class to create_package_map - Create example of creating composition using element template
1 parent 9e7c81d commit 016d144

16 files changed

+344
-123
lines changed

examples/template/demo_system/component.py

+78-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Component type templates
33
"""
4-
# flake8: noqa
54
# pylint: disable=C0103, C0301
65

76

@@ -12,17 +11,16 @@
1211
NAMESPACE = "Default"
1312

1413

15-
def create_ReceiverComponent(_0: str,
14+
def create_ReceiverComponent(_0: ar_element.Package,
1615
workspace: ar_workspace.Workspace,
1716
deps: dict[str, ar_element.ARElement] | None,
1817
**_1) -> ar_element.ApplicationSoftwareComponentType:
1918
"""
2019
Create Receiver component type
2120
"""
22-
2321
timer_interface = deps[portinterface.FreeRunningTimer_I.ref(workspace)]
24-
vehicle_speed_interface = deps[portinterface.EngineSpeed_I.ref(workspace)]
25-
engine_speed_interface = deps[portinterface.VehicleSpeed_I.ref(workspace)]
22+
vehicle_speed_interface = deps[portinterface.VehicleSpeed_I.ref(workspace)]
23+
engine_speed_interface = deps[portinterface.EngineSpeed_I.ref(workspace)]
2624
engine_speed_init = deps[constants.EngineSpeed_IV.ref(workspace)]
2725
vehicle_speed_init = deps[constants.VehicleSpeed_IV.ref(workspace)]
2826
swc = ar_element.ApplicationSoftwareComponentType("ReceiverComponent")
@@ -43,14 +41,82 @@ def create_ReceiverComponent(_0: str,
4341
return swc
4442

4543

44+
def create_TimerComponent(_0: ar_element.Package,
45+
workspace: ar_workspace.Workspace,
46+
deps: dict[str, ar_element.ARElement] | None,
47+
**_1) -> ar_element.ApplicationSoftwareComponentType:
48+
"""
49+
Create TimerComponent component type
50+
"""
51+
timer_interface = deps[portinterface.FreeRunningTimer_I.ref(workspace)]
52+
swc = ar_element.ApplicationSoftwareComponentType("TimerComponent")
53+
swc.create_provide_port("FreeRunningTimer", timer_interface, com_spec={"GetTime": {"queue_length": 1},
54+
"IsTimerElapsed": {"queue_length": 1}
55+
})
56+
swc.create_internal_behavior()
57+
return swc
58+
59+
60+
def create_composition_component(package: ar_element.Package,
61+
workspace: ar_workspace.Workspace,
62+
deps: dict[str, ar_element.ARElement] | None,
63+
**_1) -> ar_element.ApplicationSoftwareComponentType:
64+
"""
65+
Creates a composition component
66+
The created swc must be manually added to the package, otherwise the
67+
swc.create_connector will not work properly.
68+
To disable automatic adding of the SWC by the workspace, set append_to_package to False
69+
in the template constructor.
70+
"""
71+
engine_speed_interface = deps[portinterface.EngineSpeed_I.ref(workspace)]
72+
vehicle_speed_interface = deps[portinterface.VehicleSpeed_I.ref(workspace)]
73+
engine_speed_init = deps[constants.EngineSpeed_IV.ref(workspace)]
74+
vehicle_speed_init = deps[constants.VehicleSpeed_IV.ref(workspace)]
75+
receiver_component = workspace.find(ReceiverComponent.ref(workspace))
76+
timer_component = workspace.find(TimerComponent.ref(workspace))
77+
assert isinstance(receiver_component, ar_element.ApplicationSoftwareComponentType)
78+
assert isinstance(timer_component, ar_element.ApplicationSoftwareComponentType)
79+
swc = ar_element.CompositionSwComponentType("CompositionComponent")
80+
swc.create_require_port("EngineSpeed", engine_speed_interface, com_spec={"init_value": engine_speed_init.ref(),
81+
"uses_end_to_end_protection": False})
82+
swc.create_require_port("VehicleSpeed", vehicle_speed_interface, com_spec={"init_value": vehicle_speed_init.ref(),
83+
"uses_end_to_end_protection": False})
84+
85+
swc.create_component_prototype(receiver_component)
86+
swc.create_component_prototype(timer_component)
87+
# Element must be added to workspace before connectors can be created
88+
package.append(swc)
89+
swc.create_connector("TimerComponent/FreeRunningTimer", "ReceiverComponent/FreeRunningTimer", workspace)
90+
swc.create_connector("VehicleSpeed", "ReceiverComponent/VehicleSpeed", workspace)
91+
swc.create_connector("EngineSpeed", "ReceiverComponent/EngineSpeed", workspace)
92+
return swc
93+
94+
4695
ReceiverComponent = factory.GenericComponentTypeTemplate("ReceiverComponent",
47-
NAMESPACE,
48-
create_ReceiverComponent,
49-
depends=[portinterface.EngineSpeed_I,
50-
portinterface.VehicleSpeed_I,
51-
portinterface.FreeRunningTimer_I,
52-
constants.EngineSpeed_IV,
53-
constants.VehicleSpeed_IV,])
96+
NAMESPACE,
97+
create_ReceiverComponent,
98+
depends=[portinterface.EngineSpeed_I,
99+
portinterface.VehicleSpeed_I,
100+
portinterface.FreeRunningTimer_I,
101+
constants.EngineSpeed_IV,
102+
constants.VehicleSpeed_IV])
54103
ReceiverComponent_Implementation = factory.SwcImplementationTemplate("ReceiverComponent_Implementation",
55104
NAMESPACE,
56105
component_type=ReceiverComponent)
106+
TimerComponent = factory.GenericComponentTypeTemplate("TimerComponent",
107+
NAMESPACE,
108+
create_TimerComponent,
109+
depends=[portinterface.FreeRunningTimer_I])
110+
TimerComponent_Implementation = factory.SwcImplementationTemplate("TimerComponent_Implementation",
111+
NAMESPACE,
112+
component_type=TimerComponent)
113+
CompositionComponent = factory.GenericComponentTypeTemplate("CompositionComponent",
114+
NAMESPACE,
115+
create_composition_component,
116+
depends=[portinterface.EngineSpeed_I,
117+
portinterface.VehicleSpeed_I,
118+
constants.EngineSpeed_IV,
119+
constants.VehicleSpeed_IV,
120+
ReceiverComponent_Implementation,
121+
TimerComponent_Implementation],
122+
append_to_package=False)

examples/template/demo_system/factory.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self,
2727
self.native_declaration = native_declaration
2828

2929
def create(self,
30-
element_ref: str,
30+
package: ar_element.Package,
3131
workspace: ar_workspace.Workspace,
3232
dependencies: dict[str, ar_element.ARElement] | None,
3333
**kwargs) -> ar_element.SwBaseType:
@@ -58,7 +58,7 @@ def __init__(self,
5858
self.upper_limit = upper_limit
5959

6060
def create(self,
61-
element_ref: str,
61+
package: ar_element.Package,
6262
workspace: ar_workspace.Workspace,
6363
dependencies: dict[str, ar_element.ARElement] | None,
6464
**kwargs) -> ar_element.DataConstraint:
@@ -87,7 +87,7 @@ def __init__(self,
8787
self.desc = desc
8888

8989
def create(self,
90-
element_ref: str,
90+
package: ar_element.Package,
9191
workspace: ar_workspace.Workspace,
9292
dependencies: dict[str, ar_element.ARElement] | None,
9393
**kwargs) -> ar_element.CompuMethod:
@@ -133,7 +133,7 @@ def __init__(self,
133133
self.type_emitter = type_emitter
134134

135135
def create(self,
136-
element_ref: str,
136+
package: ar_element.Package,
137137
workspace: ar_workspace.Workspace,
138138
dependencies: dict[str, ar_element.ARElement] | None,
139139
**kwargs) -> ar_element.ImplementationDataType:
@@ -205,7 +205,7 @@ def _create_compu_method(self,
205205
return CompuMethodEnumTemplate(element_name + suffix, namespace_name, value_table, auto_label=False)
206206

207207
def create(self,
208-
element_ref: str,
208+
package: ar_element.Package,
209209
workspace: ar_workspace.Workspace,
210210
dependencies: dict[str, ar_element.ARElement] | None,
211211
**kwargs) -> ar_element.ImplementationDataType:
@@ -242,7 +242,7 @@ def __init__(self,
242242
self.initial_mode_name = initial_mode_name
243243

244244
def create(self,
245-
element_ref: str,
245+
package: ar_element.Package,
246246
workspace: ar_workspace.Workspace,
247247
dependencies: dict[str, ar_element.ARElement] | None,
248248
**kwargs) -> ar_element.ModeDeclarationGroup:
@@ -251,7 +251,7 @@ def create(self,
251251
"""
252252
initial_mode_ref = None
253253
if self.initial_mode_name is not None:
254-
initial_mode_ref = '/'.join([element_ref, self.initial_mode_name])
254+
initial_mode_ref = '/'.join([str(package.ref()), self.element_name, self.initial_mode_name])
255255
elem = ar_element.ModeDeclarationGroup(name=self.element_name,
256256
mode_declarations=self.mode_declarations,
257257
initial_mode_ref=initial_mode_ref)
@@ -276,7 +276,7 @@ def __init__(self,
276276
self.is_service = is_service
277277

278278
def create(self,
279-
element_ref: str,
279+
package: ar_element.Package,
280280
workspace: ar_workspace.Workspace,
281281
dependencies: dict[str, ar_element.ARElement] | None,
282282
**kwargs) -> ar_element.ModeSwitchInterface:
@@ -288,8 +288,8 @@ def create(self,
288288
return elem
289289

290290

291-
CreateFuncType = Callable[[str, ar_workspace.Workspace, dict[str, ar_element.ARElement] | None],
292-
ar_element.PortInterface]
291+
CreateFuncType = Callable[[ar_element.Package, ar_workspace.Workspace, dict[str, ar_element.ARElement] | None],
292+
ar_element.ARElement]
293293

294294

295295
class GenericPortInterfaceTemplate(ar_template.ElementTemplate):
@@ -306,14 +306,14 @@ def __init__(self,
306306
self.create_func = create_func
307307

308308
def create(self,
309-
element_ref: str,
309+
package: ar_element.Package,
310310
workspace: ar_workspace.Workspace,
311311
dependencies: dict[str, ar_element.ARElement] | None,
312312
**kwargs) -> ar_element.PortInterface:
313313
"""
314314
Create method
315315
"""
316-
elem = self.create_func(element_ref, workspace, dependencies, **kwargs)
316+
elem = self.create_func(package, workspace, dependencies, **kwargs)
317317
return elem
318318

319319

@@ -333,7 +333,7 @@ def __init__(self,
333333
self.data_element_name = data_element_name
334334

335335
def create(self,
336-
element_ref: str,
336+
package: ar_element.Package,
337337
workspace: ar_workspace.Workspace,
338338
dependencies: dict[str, ar_element.ARElement] | None,
339339
**kwargs) -> ar_element.SenderReceiverInterface:
@@ -361,19 +361,20 @@ def __init__(self,
361361
element_name: str,
362362
namespace_name: str,
363363
create_func: CreateFuncType,
364-
depends: list[TemplateBase] | None = None) -> None:
365-
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPONENT_TYPE, depends)
364+
depends: list[TemplateBase] | None = None,
365+
append_to_package: bool = True) -> None:
366+
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPONENT_TYPE, depends, append_to_package)
366367
self.create_func = create_func
367368

368369
def create(self,
369-
element_ref: str,
370+
package: ar_element.Package,
370371
workspace: ar_workspace.Workspace,
371372
dependencies: dict[str, ar_element.ARElement] | None,
372373
**kwargs) -> ar_element.PortInterface:
373374
"""
374375
Create method
375376
"""
376-
elem = self.create_func(element_ref, workspace, dependencies, **kwargs)
377+
elem = self.create_func(package, workspace, dependencies, **kwargs)
377378
return elem
378379

379380

@@ -390,7 +391,7 @@ def __init__(self,
390391
self.component_type = component_type
391392

392393
def create(self,
393-
_0: str,
394+
_0: ar_element.Package,
394395
workspace: ar_workspace.Workspace,
395396
dependencies: dict[str, ar_element.ARElement] | None,
396397
**kwargs) -> ar_element.SwBaseType:
@@ -417,7 +418,7 @@ def __init__(self,
417418
self.value = value
418419

419420
def create(self,
420-
_0: str,
421+
_0: ar_element.Package,
421422
_1: ar_workspace.Workspace,
422423
_2: dict[str, ar_element.ARElement] | None,
423424
**_3) -> ar_element.SwBaseType:

examples/template/generate_xml_using_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def apply_component_types(workspace: ar_workspace.Workspace):
2222
"""
2323
Applies component type templates
2424
"""
25-
workspace.apply(component.ReceiverComponent_Implementation)
25+
workspace.apply(component.CompositionComponent)
2626

2727

2828
def main():

examples/template/generate_xml_without_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def apply_component_types(workspace: ar_workspace.Workspace):
6767
"""
6868
Applies component type templates
6969
"""
70-
workspace.apply(component.ReceiverComponent_Implementation)
70+
workspace.apply(component.CompositionComponent)
7171

7272

7373
def main():
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<AUTOSAR xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_00051.xsd" xmlns="http://autosar.org/schema/r4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<AR-PACKAGES>
4+
<AR-PACKAGE>
5+
<SHORT-NAME>ComponentTypes</SHORT-NAME>
6+
<ELEMENTS>
7+
<COMPOSITION-SW-COMPONENT-TYPE>
8+
<SHORT-NAME>CompositionComponent</SHORT-NAME>
9+
<PORTS>
10+
<R-PORT-PROTOTYPE>
11+
<SHORT-NAME>EngineSpeed</SHORT-NAME>
12+
<REQUIRED-COM-SPECS>
13+
<NONQUEUED-RECEIVER-COM-SPEC>
14+
<DATA-ELEMENT-REF DEST="VARIABLE-DATA-PROTOTYPE">/PortInterfaces/EngineSpeed_I/EngineSpeed</DATA-ELEMENT-REF>
15+
<USES-END-TO-END-PROTECTION>false</USES-END-TO-END-PROTECTION>
16+
<INIT-VALUE>
17+
<CONSTANT-REFERENCE>
18+
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/EngineSpeed_IV</CONSTANT-REF>
19+
</CONSTANT-REFERENCE>
20+
</INIT-VALUE>
21+
</NONQUEUED-RECEIVER-COM-SPEC>
22+
</REQUIRED-COM-SPECS>
23+
<REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/PortInterfaces/EngineSpeed_I</REQUIRED-INTERFACE-TREF>
24+
</R-PORT-PROTOTYPE>
25+
<R-PORT-PROTOTYPE>
26+
<SHORT-NAME>VehicleSpeed</SHORT-NAME>
27+
<REQUIRED-COM-SPECS>
28+
<NONQUEUED-RECEIVER-COM-SPEC>
29+
<DATA-ELEMENT-REF DEST="VARIABLE-DATA-PROTOTYPE">/PortInterfaces/VehicleSpeed_I/VehicleSpeed</DATA-ELEMENT-REF>
30+
<USES-END-TO-END-PROTECTION>false</USES-END-TO-END-PROTECTION>
31+
<INIT-VALUE>
32+
<CONSTANT-REFERENCE>
33+
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/VehicleSpeed_IV</CONSTANT-REF>
34+
</CONSTANT-REFERENCE>
35+
</INIT-VALUE>
36+
</NONQUEUED-RECEIVER-COM-SPEC>
37+
</REQUIRED-COM-SPECS>
38+
<REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/PortInterfaces/VehicleSpeed_I</REQUIRED-INTERFACE-TREF>
39+
</R-PORT-PROTOTYPE>
40+
</PORTS>
41+
<COMPONENTS>
42+
<SW-COMPONENT-PROTOTYPE>
43+
<SHORT-NAME>ReceiverComponent</SHORT-NAME>
44+
<TYPE-TREF DEST="APPLICATION-SW-COMPONENT-TYPE">/ComponentTypes/ReceiverComponent</TYPE-TREF>
45+
</SW-COMPONENT-PROTOTYPE>
46+
<SW-COMPONENT-PROTOTYPE>
47+
<SHORT-NAME>TimerComponent</SHORT-NAME>
48+
<TYPE-TREF DEST="APPLICATION-SW-COMPONENT-TYPE">/ComponentTypes/TimerComponent</TYPE-TREF>
49+
</SW-COMPONENT-PROTOTYPE>
50+
</COMPONENTS>
51+
<CONNECTORS>
52+
<ASSEMBLY-SW-CONNECTOR>
53+
<SHORT-NAME>TimerComponent_FreeRunningTimer_ReceiverComponent_FreeRunningTimer</SHORT-NAME>
54+
<PROVIDER-IREF>
55+
<CONTEXT-COMPONENT-REF DEST="SW-COMPONENT-PROTOTYPE">/ComponentTypes/CompositionComponent/TimerComponent</CONTEXT-COMPONENT-REF>
56+
<TARGET-P-PORT-REF DEST="P-PORT-PROTOTYPE">/ComponentTypes/TimerComponent/FreeRunningTimer</TARGET-P-PORT-REF>
57+
</PROVIDER-IREF>
58+
<REQUESTER-IREF>
59+
<CONTEXT-COMPONENT-REF DEST="SW-COMPONENT-PROTOTYPE">/ComponentTypes/CompositionComponent/ReceiverComponent</CONTEXT-COMPONENT-REF>
60+
<TARGET-R-PORT-REF DEST="R-PORT-PROTOTYPE">/ComponentTypes/ReceiverComponent/FreeRunningTimer</TARGET-R-PORT-REF>
61+
</REQUESTER-IREF>
62+
</ASSEMBLY-SW-CONNECTOR>
63+
<DELEGATION-SW-CONNECTOR>
64+
<SHORT-NAME>VehicleSpeed_ReceiverComponent_VehicleSpeed</SHORT-NAME>
65+
<INNER-PORT-IREF>
66+
<R-PORT-IN-COMPOSITION-INSTANCE-REF>
67+
<CONTEXT-COMPONENT-REF DEST="SW-COMPONENT-PROTOTYPE">/ComponentTypes/CompositionComponent/ReceiverComponent</CONTEXT-COMPONENT-REF>
68+
<TARGET-R-PORT-REF DEST="R-PORT-PROTOTYPE">/ComponentTypes/ReceiverComponent/VehicleSpeed</TARGET-R-PORT-REF>
69+
</R-PORT-IN-COMPOSITION-INSTANCE-REF>
70+
</INNER-PORT-IREF>
71+
<OUTER-PORT-REF DEST="R-PORT-PROTOTYPE">/ComponentTypes/CompositionComponent/VehicleSpeed</OUTER-PORT-REF>
72+
</DELEGATION-SW-CONNECTOR>
73+
<DELEGATION-SW-CONNECTOR>
74+
<SHORT-NAME>EngineSpeed_ReceiverComponent_EngineSpeed</SHORT-NAME>
75+
<INNER-PORT-IREF>
76+
<R-PORT-IN-COMPOSITION-INSTANCE-REF>
77+
<CONTEXT-COMPONENT-REF DEST="SW-COMPONENT-PROTOTYPE">/ComponentTypes/CompositionComponent/ReceiverComponent</CONTEXT-COMPONENT-REF>
78+
<TARGET-R-PORT-REF DEST="R-PORT-PROTOTYPE">/ComponentTypes/ReceiverComponent/EngineSpeed</TARGET-R-PORT-REF>
79+
</R-PORT-IN-COMPOSITION-INSTANCE-REF>
80+
</INNER-PORT-IREF>
81+
<OUTER-PORT-REF DEST="R-PORT-PROTOTYPE">/ComponentTypes/CompositionComponent/EngineSpeed</OUTER-PORT-REF>
82+
</DELEGATION-SW-CONNECTOR>
83+
</CONNECTORS>
84+
</COMPOSITION-SW-COMPONENT-TYPE>
85+
</ELEMENTS>
86+
</AR-PACKAGE>
87+
</AR-PACKAGES>
88+
</AUTOSAR>

0 commit comments

Comments
 (0)