Skip to content

Commit

Permalink
Add example how to create component from template class
Browse files Browse the repository at this point in the history
  • Loading branch information
cogu committed Mar 19, 2024
1 parent c2cf9ee commit 9e7c81d
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 173 deletions.
8 changes: 8 additions & 0 deletions examples/template/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ComponentType = "/ComponentTypes"
ModeDeclaration = "/ModeDclrGroups"
PortInterface = "/PortInterfaces"
Constant = "/Constants"
base_ref = "/"

[namespace.AUTOSAR_Platform]
Expand All @@ -24,4 +25,11 @@
[document.DataTypes]
packages = "/DataTypes"

[document.Constants]
packages = "/Constants"

[document_mapping.ComponentType]
package_ref = "/ComponentTypes"
element_types = "SwComponentType"
suffix_filters = ["_Implementation"]

56 changes: 56 additions & 0 deletions examples/template/demo_system/component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Component type templates
"""
# flake8: noqa
# pylint: disable=C0103, C0301


import autosar.xml.element as ar_element
import autosar.xml.workspace as ar_workspace
from . import factory, portinterface, constants

NAMESPACE = "Default"


def create_ReceiverComponent(_0: str,
workspace: ar_workspace.Workspace,
deps: dict[str, ar_element.ARElement] | None,
**_1) -> ar_element.ApplicationSoftwareComponentType:
"""
Create Receiver component type
"""

timer_interface = deps[portinterface.FreeRunningTimer_I.ref(workspace)]
vehicle_speed_interface = deps[portinterface.EngineSpeed_I.ref(workspace)]
engine_speed_interface = deps[portinterface.VehicleSpeed_I.ref(workspace)]
engine_speed_init = deps[constants.EngineSpeed_IV.ref(workspace)]
vehicle_speed_init = deps[constants.VehicleSpeed_IV.ref(workspace)]
swc = ar_element.ApplicationSoftwareComponentType("ReceiverComponent")
swc.create_require_port("EngineSpeed", engine_speed_interface, com_spec={"init_value": engine_speed_init.ref(),
"alive_timeout": 0,
"enable_update": False,
"uses_end_to_end_protection": False,
"handle_never_received": False
})
swc.create_require_port("VehicleSpeed", vehicle_speed_interface, com_spec={"init_value": vehicle_speed_init.ref(),
"alive_timeout": 0,
"enable_update": False,
"uses_end_to_end_protection": False,
"handle_never_received": False
})
swc.create_require_port("FreeRunningTimer", timer_interface)
swc.create_internal_behavior()
return swc


ReceiverComponent = factory.GenericComponentTypeTemplate("ReceiverComponent",
NAMESPACE,
create_ReceiverComponent,
depends=[portinterface.EngineSpeed_I,
portinterface.VehicleSpeed_I,
portinterface.FreeRunningTimer_I,
constants.EngineSpeed_IV,
constants.VehicleSpeed_IV,])
ReceiverComponent_Implementation = factory.SwcImplementationTemplate("ReceiverComponent_Implementation",
NAMESPACE,
component_type=ReceiverComponent)
12 changes: 12 additions & 0 deletions examples/template/demo_system/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Constants templates
"""
# flake8: noqa
# pylint: disable=C0103, C0301

from . import factory

NAMESPACE = "Default"

EngineSpeed_IV = factory.ConstantTemplate("EngineSpeed_IV", NAMESPACE, 65535)
VehicleSpeed_IV = factory.ConstantTemplate("VehicleSpeed_IV", NAMESPACE, 65535)
114 changes: 112 additions & 2 deletions examples/template/demo_system/factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Example template classes
"""
from typing import Callable
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
Expand Down Expand Up @@ -294,7 +294,7 @@ def create(self,

class GenericPortInterfaceTemplate(ar_template.ElementTemplate):
"""
Generic element template
Generic port interface element template
"""

def __init__(self,
Expand All @@ -315,3 +315,113 @@ def create(self,
"""
elem = self.create_func(element_ref, 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,
element_ref: str,
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) -> None:
super().__init__(element_name, namespace_name, ar_enum.PackageRole.COMPONENT_TYPE, depends)
self.create_func = create_func

def create(self,
element_ref: str,
workspace: ar_workspace.Workspace,
dependencies: dict[str, ar_element.ARElement] | None,
**kwargs) -> ar_element.PortInterface:
"""
Create method
"""
elem = self.create_func(element_ref, 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: str,
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: str,
_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)
38 changes: 31 additions & 7 deletions examples/template/demo_system/portinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

import autosar.xml.element as ar_element
import autosar.xml.workspace as ar_workspace
from . import factory, mode
from . import factory, mode, platform

NAMESPACE = "Default"

EcuM_CurrentMode = factory.ModeSwitchInterfaceTemplate("EcuM_CurrentMode", NAMESPACE, mode.EcuM_Mode, "currentMode", is_service=True)


def create_NvMService_interface(element_ref: str,
_1: ar_workspace.Workspace,
Expand All @@ -22,10 +20,36 @@ def create_NvMService_interface(element_ref: str,
Create NvmService interface
"""

interface = ar_element.ClientServerInterface("NvMService_I", is_service=True)
e_not_ok = interface.create_possible_error("E_NOT_OK", 1)
interface.create_operation("EraseBlock", possible_error_refs=element_ref + "/" + e_not_ok.name)
return interface
port_interface = ar_element.ClientServerInterface("NvMService_I", is_service=True)
e_not_ok = port_interface.create_possible_error("E_NOT_OK", 1)
port_interface.create_operation("EraseBlock", possible_error_refs=element_ref + "/" + e_not_ok.name)
return port_interface


def create_free_running_timer_interface(_0: str,
workspace: ar_workspace.Workspace,
deps: dict[str, ar_element.ARElement] | None,
**_1) -> ar_element.ClientServerInterface:
"""
Creates client server interface FreeRunningTimer_I
"""
uint32_impl_type = deps[platform.ImplementationTypes.uint32.ref(workspace)]
boolean_impl_type = deps[platform.ImplementationTypes.boolean.ref(workspace)]
port_interface = ar_element.ClientServerInterface("FreeRunningTimer_I")
operation = port_interface.create_operation("GetTime")
operation.create_out_argument("value", type_ref=uint32_impl_type.ref())
operation = port_interface.create_operation("IsTimerElapsed")
operation.create_in_argument("startTime", type_ref=uint32_impl_type.ref())
operation.create_in_argument("duration", type_ref=uint32_impl_type.ref())
operation.create_out_argument("result", type_ref=boolean_impl_type.ref())
return port_interface

EcuM_CurrentMode = factory.ModeSwitchInterfaceTemplate("EcuM_CurrentMode", NAMESPACE, mode.EcuM_Mode, "currentMode", is_service=True)
NvMService_I = factory.GenericPortInterfaceTemplate("NvMService_I", NAMESPACE, create_NvMService_interface)
FreeRunningTimer_I = factory.GenericPortInterfaceTemplate("FreeRunningTimer_I",
NAMESPACE,
create_free_running_timer_interface,
depends=[platform.ImplementationTypes.uint32,
platform.ImplementationTypes.boolean])
VehicleSpeed_I = factory.SenderReceiverInterfaceTemplate("VehicleSpeed_I", NAMESPACE, platform.ImplementationTypes.uint16)
EngineSpeed_I = factory.SenderReceiverInterfaceTemplate("EngineSpeed_I", NAMESPACE, platform.ImplementationTypes.uint16)
19 changes: 5 additions & 14 deletions examples/template/generate_xml_using_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import os
from demo_system import platform, datatype, portinterface
from demo_system import platform, component
import autosar.xml.workspace as ar_workspace


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


def apply_data_types(workspace: ar_workspace.Workspace):
def apply_component_types(workspace: ar_workspace.Workspace):
"""
Applies data type templates
Applies component type templates
"""
workspace.apply(datatype.InactiveActive_T)


def apply_portinterfaces(workspace: ar_workspace.Workspace):
"""
Applies mode templates
"""
workspace.apply(portinterface.EcuM_CurrentMode)
workspace.apply(portinterface.NvMService_I)
workspace.apply(component.ReceiverComponent_Implementation)


def main():
Expand All @@ -39,8 +31,7 @@ def main():
document_root = os.path.join(os.path.dirname(__file__), "generated")
workspace = ar_workspace.Workspace(config_file_path, document_root=document_root)
apply_platform_types(workspace)
apply_data_types(workspace)
apply_portinterfaces(workspace)
apply_component_types(workspace)
workspace.write_documents()
print("Done")

Expand Down
42 changes: 18 additions & 24 deletions examples/template/generate_xml_without_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"""

import os
from demo_system import platform, datatype, portinterface
from demo_system import platform, component
import autosar.xml.element as ar_element
import autosar.xml.workspace as ar_workspace


Expand All @@ -25,7 +26,9 @@ def create_namespaces(workspace: ar_workspace.Workspace):
"CompuMethod": "/DataTypes/CompuMethods",
"DataConstraint": "/DataTypes/DataConstrs",
"ModeDeclaration": "/ModeDclrGroups",
"PortInterface": "/PortInterfaces"},
"PortInterface": "/PortInterfaces",
"Constant": "/Constants",
"ComponentType": "/ComponentTypes"},
base_ref="/")


Expand All @@ -40,13 +43,13 @@ def create_documents(workspace: ar_workspace.Workspace) -> None:
"""
Creates documents
"""
sub_directory = "generated"
platform_document_path = create_abs_path(sub_directory, "AUTOSAR_Platform.arxml")
datatype_document_path = create_abs_path(sub_directory, "DataTypes.arxml")
portinterface_document_path = create_abs_path(sub_directory, "PortInterfaces.arxml")
workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform")
workspace.create_document(datatype_document_path, packages="/DataTypes")
workspace.create_document(portinterface_document_path, packages=["/ModeDclrGroups", "/PortInterfaces"])
workspace.create_document("AUTOSAR_Platform.arxml", packages="/AUTOSAR_Platform")
workspace.create_document("DataTypes.arxml", packages="/DataTypes")
workspace.create_document("Constants.arxml", packages="/Constants")
workspace.create_document("PortInterfaces.arxml", packages=["/ModeDclrGroups", "/PortInterfaces"])
workspace.create_document_mapping(package_ref="/ComponentTypes",
element_types=ar_element.SwComponentType,
suffix_filters=["_Implementation"])


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


def apply_data_types(workspace: ar_workspace.Workspace):
def apply_component_types(workspace: ar_workspace.Workspace):
"""
Applies data type templates
Applies component type templates
"""
workspace.apply(datatype.InactiveActive_T)


def apply_portinterfaces(workspace: ar_workspace.Workspace):
"""
Applies mode templates
"""
workspace.apply(portinterface.EcuM_CurrentMode)
workspace.apply(portinterface.NvMService_I)
workspace.apply(component.ReceiverComponent_Implementation)


def main():
"""Main"""
workspace = ar_workspace.Workspace()
workspace = ar_workspace.Workspace(document_root="generated")
create_namespaces(workspace)
apply_platform_types(workspace)
apply_data_types(workspace)
apply_portinterfaces(workspace)
create_documents(workspace)
apply_platform_types(workspace)
apply_component_types(workspace)
workspace.write_documents()
print("Done")

Expand Down
Loading

0 comments on commit 9e7c81d

Please sign in to comment.