Skip to content

Commit 9e7c81d

Browse files
committed
Add example how to create component from template class
1 parent c2cf9ee commit 9e7c81d

13 files changed

+430
-173
lines changed

examples/template/config.toml

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ComponentType = "/ComponentTypes"
88
ModeDeclaration = "/ModeDclrGroups"
99
PortInterface = "/PortInterfaces"
10+
Constant = "/Constants"
1011
base_ref = "/"
1112

1213
[namespace.AUTOSAR_Platform]
@@ -24,4 +25,11 @@
2425
[document.DataTypes]
2526
packages = "/DataTypes"
2627

28+
[document.Constants]
29+
packages = "/Constants"
30+
31+
[document_mapping.ComponentType]
32+
package_ref = "/ComponentTypes"
33+
element_types = "SwComponentType"
34+
suffix_filters = ["_Implementation"]
2735

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Component type templates
3+
"""
4+
# flake8: noqa
5+
# pylint: disable=C0103, C0301
6+
7+
8+
import autosar.xml.element as ar_element
9+
import autosar.xml.workspace as ar_workspace
10+
from . import factory, portinterface, constants
11+
12+
NAMESPACE = "Default"
13+
14+
15+
def create_ReceiverComponent(_0: str,
16+
workspace: ar_workspace.Workspace,
17+
deps: dict[str, ar_element.ARElement] | None,
18+
**_1) -> ar_element.ApplicationSoftwareComponentType:
19+
"""
20+
Create Receiver component type
21+
"""
22+
23+
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)]
26+
engine_speed_init = deps[constants.EngineSpeed_IV.ref(workspace)]
27+
vehicle_speed_init = deps[constants.VehicleSpeed_IV.ref(workspace)]
28+
swc = ar_element.ApplicationSoftwareComponentType("ReceiverComponent")
29+
swc.create_require_port("EngineSpeed", engine_speed_interface, com_spec={"init_value": engine_speed_init.ref(),
30+
"alive_timeout": 0,
31+
"enable_update": False,
32+
"uses_end_to_end_protection": False,
33+
"handle_never_received": False
34+
})
35+
swc.create_require_port("VehicleSpeed", vehicle_speed_interface, com_spec={"init_value": vehicle_speed_init.ref(),
36+
"alive_timeout": 0,
37+
"enable_update": False,
38+
"uses_end_to_end_protection": False,
39+
"handle_never_received": False
40+
})
41+
swc.create_require_port("FreeRunningTimer", timer_interface)
42+
swc.create_internal_behavior()
43+
return swc
44+
45+
46+
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,])
54+
ReceiverComponent_Implementation = factory.SwcImplementationTemplate("ReceiverComponent_Implementation",
55+
NAMESPACE,
56+
component_type=ReceiverComponent)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Constants templates
3+
"""
4+
# flake8: noqa
5+
# pylint: disable=C0103, C0301
6+
7+
from . import factory
8+
9+
NAMESPACE = "Default"
10+
11+
EngineSpeed_IV = factory.ConstantTemplate("EngineSpeed_IV", NAMESPACE, 65535)
12+
VehicleSpeed_IV = factory.ConstantTemplate("VehicleSpeed_IV", NAMESPACE, 65535)

examples/template/demo_system/factory.py

+112-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Example template classes
33
"""
4-
from typing import Callable
4+
from typing import Any, Callable
55
import autosar.xml.template as ar_template
66
import autosar.xml.element as ar_element
77
import autosar.xml.enumeration as ar_enum
@@ -294,7 +294,7 @@ def create(self,
294294

295295
class GenericPortInterfaceTemplate(ar_template.ElementTemplate):
296296
"""
297-
Generic element template
297+
Generic port interface element template
298298
"""
299299

300300
def __init__(self,
@@ -315,3 +315,113 @@ def create(self,
315315
"""
316316
elem = self.create_func(element_ref, workspace, dependencies, **kwargs)
317317
return elem
318+
319+
320+
class SenderReceiverInterfaceTemplate(ar_template.ElementTemplate):
321+
"""
322+
Sender receiver port interface template restricted to a single data element
323+
It is assumed that the element name ends with "_I"
324+
"""
325+
326+
def __init__(self,
327+
element_name: str,
328+
namespace_name: str,
329+
data_element_template: ar_template.ElementTemplate,
330+
data_element_name: str | None = None) -> None:
331+
super().__init__(element_name, namespace_name, ar_enum.PackageRole.PORT_INTERFACE, [data_element_template])
332+
self.data_element_template = data_element_template
333+
self.data_element_name = data_element_name
334+
335+
def create(self,
336+
element_ref: str,
337+
workspace: ar_workspace.Workspace,
338+
dependencies: dict[str, ar_element.ARElement] | None,
339+
**kwargs) -> ar_element.SenderReceiverInterface:
340+
"""
341+
Create method
342+
"""
343+
elem_type = dependencies[self.data_element_template.ref(workspace)]
344+
port_interface = ar_element.SenderReceiverInterface(self.element_name)
345+
elem_name = self.data_element_name
346+
if elem_name is None:
347+
if self.element_name.endswith("_I"):
348+
elem_name = self.element_name[:-2]
349+
else:
350+
elem_name = self.element_name
351+
port_interface.create_data_element(elem_name, type_ref=elem_type.ref())
352+
return port_interface
353+
354+
355+
class GenericComponentTypeTemplate(ar_template.ElementTemplate):
356+
"""
357+
Generic component type element template
358+
"""
359+
360+
def __init__(self,
361+
element_name: str,
362+
namespace_name: str,
363+
create_func: CreateFuncType,
364+
depends: list[TemplateBase] | None = None) -> None:
365+
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPONENT_TYPE, depends)
366+
self.create_func = create_func
367+
368+
def create(self,
369+
element_ref: str,
370+
workspace: ar_workspace.Workspace,
371+
dependencies: dict[str, ar_element.ARElement] | None,
372+
**kwargs) -> ar_element.PortInterface:
373+
"""
374+
Create method
375+
"""
376+
elem = self.create_func(element_ref, workspace, dependencies, **kwargs)
377+
return elem
378+
379+
380+
class SwcImplementationTemplate(ar_template.ElementTemplate):
381+
"""
382+
SwcImplementation template
383+
"""
384+
385+
def __init__(self,
386+
element_name: str,
387+
namespace_name: str,
388+
component_type: ar_template.ElementTemplate) -> None:
389+
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPONENT_TYPE, [component_type])
390+
self.component_type = component_type
391+
392+
def create(self,
393+
_0: str,
394+
workspace: ar_workspace.Workspace,
395+
dependencies: dict[str, ar_element.ARElement] | None,
396+
**kwargs) -> ar_element.SwBaseType:
397+
"""
398+
Factory method for creating a new SwcImplementation
399+
"""
400+
swc_type = dependencies[self.component_type.ref(workspace)]
401+
elem = ar_element.SwcImplementation(self.element_name,
402+
behavior_ref=swc_type.internal_behavior.ref())
403+
404+
return elem
405+
406+
407+
class ConstantTemplate(ar_template.ElementTemplate):
408+
"""
409+
Constant template
410+
"""
411+
412+
def __init__(self,
413+
element_name: str,
414+
namespace_name: str,
415+
value: Any) -> None:
416+
super().__init__(element_name, namespace_name, ar_enum.PackageRole.CONSTANT)
417+
self.value = value
418+
419+
def create(self,
420+
_0: str,
421+
_1: ar_workspace.Workspace,
422+
_2: dict[str, ar_element.ARElement] | None,
423+
**_3) -> ar_element.SwBaseType:
424+
"""
425+
Factory method for creating a new SwcImplementation
426+
"""
427+
return ar_element.ConstantSpecification.make_constant(self.element_name, self.value)

examples/template/demo_system/portinterface.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
import autosar.xml.element as ar_element
99
import autosar.xml.workspace as ar_workspace
10-
from . import factory, mode
10+
from . import factory, mode, platform
1111

1212
NAMESPACE = "Default"
1313

14-
EcuM_CurrentMode = factory.ModeSwitchInterfaceTemplate("EcuM_CurrentMode", NAMESPACE, mode.EcuM_Mode, "currentMode", is_service=True)
15-
1614

1715
def create_NvMService_interface(element_ref: str,
1816
_1: ar_workspace.Workspace,
@@ -22,10 +20,36 @@ def create_NvMService_interface(element_ref: str,
2220
Create NvmService interface
2321
"""
2422

25-
interface = ar_element.ClientServerInterface("NvMService_I", is_service=True)
26-
e_not_ok = interface.create_possible_error("E_NOT_OK", 1)
27-
interface.create_operation("EraseBlock", possible_error_refs=element_ref + "/" + e_not_ok.name)
28-
return interface
23+
port_interface = ar_element.ClientServerInterface("NvMService_I", is_service=True)
24+
e_not_ok = port_interface.create_possible_error("E_NOT_OK", 1)
25+
port_interface.create_operation("EraseBlock", possible_error_refs=element_ref + "/" + e_not_ok.name)
26+
return port_interface
27+
2928

29+
def create_free_running_timer_interface(_0: str,
30+
workspace: ar_workspace.Workspace,
31+
deps: dict[str, ar_element.ARElement] | None,
32+
**_1) -> ar_element.ClientServerInterface:
33+
"""
34+
Creates client server interface FreeRunningTimer_I
35+
"""
36+
uint32_impl_type = deps[platform.ImplementationTypes.uint32.ref(workspace)]
37+
boolean_impl_type = deps[platform.ImplementationTypes.boolean.ref(workspace)]
38+
port_interface = ar_element.ClientServerInterface("FreeRunningTimer_I")
39+
operation = port_interface.create_operation("GetTime")
40+
operation.create_out_argument("value", type_ref=uint32_impl_type.ref())
41+
operation = port_interface.create_operation("IsTimerElapsed")
42+
operation.create_in_argument("startTime", type_ref=uint32_impl_type.ref())
43+
operation.create_in_argument("duration", type_ref=uint32_impl_type.ref())
44+
operation.create_out_argument("result", type_ref=boolean_impl_type.ref())
45+
return port_interface
3046

47+
EcuM_CurrentMode = factory.ModeSwitchInterfaceTemplate("EcuM_CurrentMode", NAMESPACE, mode.EcuM_Mode, "currentMode", is_service=True)
3148
NvMService_I = factory.GenericPortInterfaceTemplate("NvMService_I", NAMESPACE, create_NvMService_interface)
49+
FreeRunningTimer_I = factory.GenericPortInterfaceTemplate("FreeRunningTimer_I",
50+
NAMESPACE,
51+
create_free_running_timer_interface,
52+
depends=[platform.ImplementationTypes.uint32,
53+
platform.ImplementationTypes.boolean])
54+
VehicleSpeed_I = factory.SenderReceiverInterfaceTemplate("VehicleSpeed_I", NAMESPACE, platform.ImplementationTypes.uint16)
55+
EngineSpeed_I = factory.SenderReceiverInterfaceTemplate("EngineSpeed_I", NAMESPACE, platform.ImplementationTypes.uint16)

examples/template/generate_xml_using_config.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import os
6-
from demo_system import platform, datatype, portinterface
6+
from demo_system import platform, component
77
import autosar.xml.workspace as ar_workspace
88

99

@@ -18,19 +18,11 @@ def apply_platform_types(workspace: ar_workspace.Workspace):
1818
workspace.apply(platform.ImplementationTypes.uint64)
1919

2020

21-
def apply_data_types(workspace: ar_workspace.Workspace):
21+
def apply_component_types(workspace: ar_workspace.Workspace):
2222
"""
23-
Applies data type templates
23+
Applies component type templates
2424
"""
25-
workspace.apply(datatype.InactiveActive_T)
26-
27-
28-
def apply_portinterfaces(workspace: ar_workspace.Workspace):
29-
"""
30-
Applies mode templates
31-
"""
32-
workspace.apply(portinterface.EcuM_CurrentMode)
33-
workspace.apply(portinterface.NvMService_I)
25+
workspace.apply(component.ReceiverComponent_Implementation)
3426

3527

3628
def main():
@@ -39,8 +31,7 @@ def main():
3931
document_root = os.path.join(os.path.dirname(__file__), "generated")
4032
workspace = ar_workspace.Workspace(config_file_path, document_root=document_root)
4133
apply_platform_types(workspace)
42-
apply_data_types(workspace)
43-
apply_portinterfaces(workspace)
34+
apply_component_types(workspace)
4435
workspace.write_documents()
4536
print("Done")
4637

examples/template/generate_xml_without_config.py

+18-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"""
55

66
import os
7-
from demo_system import platform, datatype, portinterface
7+
from demo_system import platform, component
8+
import autosar.xml.element as ar_element
89
import autosar.xml.workspace as ar_workspace
910

1011

@@ -25,7 +26,9 @@ def create_namespaces(workspace: ar_workspace.Workspace):
2526
"CompuMethod": "/DataTypes/CompuMethods",
2627
"DataConstraint": "/DataTypes/DataConstrs",
2728
"ModeDeclaration": "/ModeDclrGroups",
28-
"PortInterface": "/PortInterfaces"},
29+
"PortInterface": "/PortInterfaces",
30+
"Constant": "/Constants",
31+
"ComponentType": "/ComponentTypes"},
2932
base_ref="/")
3033

3134

@@ -40,13 +43,13 @@ def create_documents(workspace: ar_workspace.Workspace) -> None:
4043
"""
4144
Creates documents
4245
"""
43-
sub_directory = "generated"
44-
platform_document_path = create_abs_path(sub_directory, "AUTOSAR_Platform.arxml")
45-
datatype_document_path = create_abs_path(sub_directory, "DataTypes.arxml")
46-
portinterface_document_path = create_abs_path(sub_directory, "PortInterfaces.arxml")
47-
workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform")
48-
workspace.create_document(datatype_document_path, packages="/DataTypes")
49-
workspace.create_document(portinterface_document_path, packages=["/ModeDclrGroups", "/PortInterfaces"])
46+
workspace.create_document("AUTOSAR_Platform.arxml", packages="/AUTOSAR_Platform")
47+
workspace.create_document("DataTypes.arxml", packages="/DataTypes")
48+
workspace.create_document("Constants.arxml", packages="/Constants")
49+
workspace.create_document("PortInterfaces.arxml", packages=["/ModeDclrGroups", "/PortInterfaces"])
50+
workspace.create_document_mapping(package_ref="/ComponentTypes",
51+
element_types=ar_element.SwComponentType,
52+
suffix_filters=["_Implementation"])
5053

5154

5255
def apply_platform_types(workspace: ar_workspace.Workspace):
@@ -60,29 +63,20 @@ def apply_platform_types(workspace: ar_workspace.Workspace):
6063
workspace.apply(platform.ImplementationTypes.uint64)
6164

6265

63-
def apply_data_types(workspace: ar_workspace.Workspace):
66+
def apply_component_types(workspace: ar_workspace.Workspace):
6467
"""
65-
Applies data type templates
68+
Applies component type templates
6669
"""
67-
workspace.apply(datatype.InactiveActive_T)
68-
69-
70-
def apply_portinterfaces(workspace: ar_workspace.Workspace):
71-
"""
72-
Applies mode templates
73-
"""
74-
workspace.apply(portinterface.EcuM_CurrentMode)
75-
workspace.apply(portinterface.NvMService_I)
70+
workspace.apply(component.ReceiverComponent_Implementation)
7671

7772

7873
def main():
7974
"""Main"""
80-
workspace = ar_workspace.Workspace()
75+
workspace = ar_workspace.Workspace(document_root="generated")
8176
create_namespaces(workspace)
82-
apply_platform_types(workspace)
83-
apply_data_types(workspace)
84-
apply_portinterfaces(workspace)
8577
create_documents(workspace)
78+
apply_platform_types(workspace)
79+
apply_component_types(workspace)
8680
workspace.write_documents()
8781
print("Done")
8882

0 commit comments

Comments
 (0)