Skip to content

Commit

Permalink
Add event creation examples
Browse files Browse the repository at this point in the history
- Examples using event creation API
- Add logic for adding numeric suffix to event names
  • Loading branch information
cogu committed Oct 13, 2024
1 parent 330367c commit 4f16712
Show file tree
Hide file tree
Showing 18 changed files with 427 additions and 52 deletions.
10 changes: 10 additions & 0 deletions examples/template/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@
element_types = "SwComponentType"
suffix_filters = ["_Implementation"]

[behavior]
background_event_prefix = "BT_"
data_receive_error_event_prefix = "DRET_"
data_receive_event_prefix = "DRT_"
init_event_prefix = "IT_"
operation_invoked_event_prefix = "OIT_"
swc_mode_manager_error_event_prefix = "MMET_"
swc_mode_switch_event_prefix = "MST_"
timing_event_prefix = "TMT_"

53 changes: 40 additions & 13 deletions examples/template/demo_system/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@


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

NAMESPACE = "Default"


def create_ReceiverComponent(_0: ar_element.Package,
def create_ReceiverComponent(package: ar_element.Package,
workspace: ar_workspace.Workspace,
deps: dict[str, ar_element.ARElement] | None,
**_1) -> ar_element.ApplicationSoftwareComponentType:
"""
Create Receiver component type
Receiver ports:
- EngineSpeed
- VehicleSpeed
Client ports:
- FreeRunningTimer
"""
timer_interface = deps[portinterface.FreeRunningTimer_I.ref(workspace)]
vehicle_speed_interface = deps[portinterface.VehicleSpeed_I.ref(workspace)]
engine_speed_interface = deps[portinterface.EngineSpeed_I.ref(workspace)]
ecu_mode_interface = deps[portinterface.EcuM_CurrentMode_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_name = "ReceiverComponent"
swc = ar_element.ApplicationSoftwareComponentType(swc_name)
package.append(swc)
swc.create_require_port("EcuM_CurrentMode", ecu_mode_interface, com_spec={"enhanced_mode_api": False,
"supports_async": False})
swc.create_require_port("EngineSpeed", engine_speed_interface, com_spec={"init_value": engine_speed_init.ref(),
"alive_timeout": 0,
"enable_update": False,
Expand All @@ -37,30 +47,48 @@ def create_ReceiverComponent(_0: ar_element.Package,
"handle_never_received": False
})
swc.create_require_port("FreeRunningTimer", timer_interface)
swc.create_internal_behavior()
init_runnable_name = swc_name + '_Init'
periodic_runnable_name = swc_name + '_Run'
behavior = swc.create_internal_behavior()
behavior.create_runnable(init_runnable_name)
behavior.create_runnable(periodic_runnable_name)
behavior.create_swc_mode_mode_switch_event(init_runnable_name,
"EcuM_CurrentMode/RUN",
ar_enum.ModeActivationKind.ON_ENTRY)
behavior.create_timing_event(periodic_runnable_name, 20.0 / 1000)
return swc


def create_TimerComponent(_0: ar_element.Package,
def create_TimerComponent(package: ar_element.Package,
workspace: ar_workspace.Workspace,
deps: dict[str, ar_element.ARElement] | None,
**_1) -> ar_element.ApplicationSoftwareComponentType:
**_0) -> ar_element.ApplicationSoftwareComponentType:
"""
Create TimerComponent component type
"""
timer_interface = deps[portinterface.FreeRunningTimer_I.ref(workspace)]
swc = ar_element.ApplicationSoftwareComponentType("TimerComponent")
package.append(swc)
swc.create_provide_port("FreeRunningTimer", timer_interface, com_spec={"GetTime": {"queue_length": 1},
"IsTimerElapsed": {"queue_length": 1}
})
swc.create_internal_behavior()
behavior = swc.create_internal_behavior()
init_runnable_name = swc.name + "_Init"
get_time_runnable_name = swc.name + "_GetTime"
timer_elapsed_runnable_name = swc.name + "_IsTimerElapsed"
behavior.create_runnable(init_runnable_name)
behavior.create_runnable(get_time_runnable_name, reentrancy_level=ar_enum.ReentrancyLevel.NON_REENTRANT)
behavior.create_runnable(timer_elapsed_runnable_name, reentrancy_level=ar_enum.ReentrancyLevel.NON_REENTRANT)
behavior.create_init_event(init_runnable_name)
behavior.create_operation_invoked_event(get_time_runnable_name, "FreeRunningTimer/GetTime")
behavior.create_operation_invoked_event(timer_elapsed_runnable_name, "FreeRunningTimer/IsTimerElapsed")
return swc


def create_composition_component(package: ar_element.Package,
workspace: ar_workspace.Workspace,
deps: dict[str, ar_element.ARElement] | None,
**_1) -> ar_element.ApplicationSoftwareComponentType:
**_0) -> ar_element.ApplicationSoftwareComponentType:
"""
Creates a composition component
The created swc must be manually added to the package, otherwise the
Expand All @@ -77,15 +105,14 @@ def create_composition_component(package: ar_element.Package,
assert isinstance(receiver_component, ar_element.ApplicationSoftwareComponentType)
assert isinstance(timer_component, ar_element.ApplicationSoftwareComponentType)
swc = ar_element.CompositionSwComponentType("CompositionComponent")
package.append(swc)
swc.create_require_port("EngineSpeed", engine_speed_interface, com_spec={"init_value": engine_speed_init.ref(),
"uses_end_to_end_protection": False})
swc.create_require_port("VehicleSpeed", vehicle_speed_interface, com_spec={"init_value": vehicle_speed_init.ref(),
"uses_end_to_end_protection": False})

swc.create_component_prototype(receiver_component)
swc.create_component_prototype(timer_component)
# Element must be added to workspace before connectors can be created
package.append(swc)
swc.create_connector("TimerComponent/FreeRunningTimer", "ReceiverComponent/FreeRunningTimer", workspace)
swc.create_connector("VehicleSpeed", "ReceiverComponent/VehicleSpeed", workspace)
swc.create_connector("EngineSpeed", "ReceiverComponent/EngineSpeed", workspace)
Expand All @@ -95,7 +122,8 @@ def create_composition_component(package: ar_element.Package,
ReceiverComponent = factory.GenericComponentTypeTemplate("ReceiverComponent",
NAMESPACE,
create_ReceiverComponent,
depends=[portinterface.EngineSpeed_I,
depends=[portinterface.EcuM_CurrentMode_I,
portinterface.EngineSpeed_I,
portinterface.VehicleSpeed_I,
portinterface.FreeRunningTimer_I,
constants.EngineSpeed_IV,
Expand All @@ -118,5 +146,4 @@ def create_composition_component(package: ar_element.Package,
constants.EngineSpeed_IV,
constants.VehicleSpeed_IV,
ReceiverComponent_Implementation,
TimerComponent_Implementation],
append_to_package=False)
TimerComponent_Implementation])
2 changes: 1 addition & 1 deletion examples/template/demo_system/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def __init__(self,
namespace_name: str,
create_func: CreateFuncType,
depends: list[TemplateBase] | None = None,
append_to_package: bool = True) -> 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

Expand Down
2 changes: 1 addition & 1 deletion examples/template/demo_system/portinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_free_running_timer_interface(_0: str,
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)
EcuM_CurrentMode_I = factory.ModeSwitchInterfaceTemplate("EcuM_CurrentMode_I", 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,
Expand Down
16 changes: 16 additions & 0 deletions examples/template/generate_xml_without_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ def create_namespaces(workspace: ar_workspace.Workspace):
base_ref="/")


def create_behavior_settings(workspace: ar_workspace.Workspace):
"""
Define default event name prefixess
"""
workspace.behavior_settings.update({
"background_event_prefix": "BT_",
"data_receive_error_event_prefix": "DRET_",
"data_receive_event_prefix": "DRT_",
"init_event_prefix": "IT_",
"operation_invoked_event_prefix": "OIT_",
"swc_mode_manager_error_event_prefix": "MMET_",
"swc_mode_switch_event_prefix": "MST_",
"timing_event_prefix": "TMT_"})


def create_documents(workspace: ar_workspace.Workspace) -> None:
"""
Creates documents
Expand Down Expand Up @@ -67,6 +82,7 @@ def main():
"""Main"""
workspace = ar_workspace.Workspace(document_root="generated")
create_namespaces(workspace)
create_behavior_settings(workspace)
create_documents(workspace)
apply_platform_types(workspace)
apply_component_types(workspace)
Expand Down
37 changes: 37 additions & 0 deletions examples/template/generated/PortInterfaces.arxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<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">
<AR-PACKAGES>
<AR-PACKAGE>
<SHORT-NAME>ModeDclrGroups</SHORT-NAME>
<ELEMENTS>
<MODE-DECLARATION-GROUP>
<SHORT-NAME>EcuM_Mode</SHORT-NAME>
<INITIAL-MODE-REF DEST="MODE-DECLARATION">/ModeDclrGroups/EcuM_Mode/STARTUP</INITIAL-MODE-REF>
<MODE-DECLARATIONS>
<MODE-DECLARATION>
<SHORT-NAME>STARTUP</SHORT-NAME>
</MODE-DECLARATION>
<MODE-DECLARATION>
<SHORT-NAME>RUN</SHORT-NAME>
</MODE-DECLARATION>
<MODE-DECLARATION>
<SHORT-NAME>POST_RUN</SHORT-NAME>
</MODE-DECLARATION>
<MODE-DECLARATION>
<SHORT-NAME>SLEEP</SHORT-NAME>
</MODE-DECLARATION>
<MODE-DECLARATION>
<SHORT-NAME>WAKEUP</SHORT-NAME>
</MODE-DECLARATION>
<MODE-DECLARATION>
<SHORT-NAME>SHUTDOWN</SHORT-NAME>
</MODE-DECLARATION>
</MODE-DECLARATIONS>
</MODE-DECLARATION-GROUP>
</ELEMENTS>
</AR-PACKAGE>
<AR-PACKAGE>
<SHORT-NAME>PortInterfaces</SHORT-NAME>
<ELEMENTS>
Expand All @@ -22,6 +51,14 @@
</VARIABLE-DATA-PROTOTYPE>
</DATA-ELEMENTS>
</SENDER-RECEIVER-INTERFACE>
<MODE-SWITCH-INTERFACE>
<SHORT-NAME>EcuM_CurrentMode_I</SHORT-NAME>
<IS-SERVICE>true</IS-SERVICE>
<MODE-GROUP>
<SHORT-NAME>currentMode</SHORT-NAME>
<TYPE-TREF DEST="MODE-DECLARATION-GROUP">/ModeDclrGroups/EcuM_Mode</TYPE-TREF>
</MODE-GROUP>
</MODE-SWITCH-INTERFACE>
<CLIENT-SERVER-INTERFACE>
<SHORT-NAME>FreeRunningTimer_I</SHORT-NAME>
<OPERATIONS>
Expand Down
38 changes: 38 additions & 0 deletions examples/template/generated/ReceiverComponent.arxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
<APPLICATION-SW-COMPONENT-TYPE>
<SHORT-NAME>ReceiverComponent</SHORT-NAME>
<PORTS>
<R-PORT-PROTOTYPE>
<SHORT-NAME>EcuM_CurrentMode</SHORT-NAME>
<REQUIRED-COM-SPECS>
<MODE-SWITCH-RECEIVER-COM-SPEC>
<ENHANCED-MODE-API>false</ENHANCED-MODE-API>
<MODE-GROUP-REF DEST="MODE-DECLARATION-GROUP-PROTOTYPE">/PortInterfaces/EcuM_CurrentMode_I/currentMode</MODE-GROUP-REF>
<SUPPORTS-ASYNCHRONOUS-MODE-SWITCH>false</SUPPORTS-ASYNCHRONOUS-MODE-SWITCH>
</MODE-SWITCH-RECEIVER-COM-SPEC>
</REQUIRED-COM-SPECS>
<REQUIRED-INTERFACE-TREF DEST="MODE-SWITCH-INTERFACE">/PortInterfaces/EcuM_CurrentMode_I</REQUIRED-INTERFACE-TREF>
</R-PORT-PROTOTYPE>
<R-PORT-PROTOTYPE>
<SHORT-NAME>EngineSpeed</SHORT-NAME>
<REQUIRED-COM-SPECS>
Expand Down Expand Up @@ -51,6 +62,33 @@
<INTERNAL-BEHAVIORS>
<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>ReceiverComponent_InternalBehavior</SHORT-NAME>
<EVENTS>
<SWC-MODE-SWITCH-EVENT>
<SHORT-NAME>MST_ReceiverComponent_Init</SHORT-NAME>
<START-ON-EVENT-REF DEST="RUNNABLE-ENTITY">/ComponentTypes/ReceiverComponent/ReceiverComponent_InternalBehavior/ReceiverComponent_Init</START-ON-EVENT-REF>
<ACTIVATION>ON-ENTRY</ACTIVATION>
<MODE-IREFS>
<MODE-IREF>
<CONTEXT-PORT-REF DEST="R-PORT-PROTOTYPE">/ComponentTypes/ReceiverComponent/EcuM_CurrentMode</CONTEXT-PORT-REF>
<CONTEXT-MODE-DECLARATION-GROUP-PROTOTYPE-REF DEST="MODE-DECLARATION-GROUP-PROTOTYPE">/PortInterfaces/EcuM_CurrentMode_I/currentMode</CONTEXT-MODE-DECLARATION-GROUP-PROTOTYPE-REF>
<TARGET-MODE-DECLARATION-REF DEST="MODE-DECLARATION">/ModeDclrGroups/EcuM_Mode/RUN</TARGET-MODE-DECLARATION-REF>
</MODE-IREF>
</MODE-IREFS>
</SWC-MODE-SWITCH-EVENT>
<TIMING-EVENT>
<SHORT-NAME>TMT_ReceiverComponent_Run</SHORT-NAME>
<START-ON-EVENT-REF DEST="RUNNABLE-ENTITY">/ComponentTypes/ReceiverComponent/ReceiverComponent_InternalBehavior/ReceiverComponent_Run</START-ON-EVENT-REF>
<PERIOD>0.02</PERIOD>
</TIMING-EVENT>
</EVENTS>
<RUNNABLES>
<RUNNABLE-ENTITY>
<SHORT-NAME>ReceiverComponent_Init</SHORT-NAME>
</RUNNABLE-ENTITY>
<RUNNABLE-ENTITY>
<SHORT-NAME>ReceiverComponent_Run</SHORT-NAME>
</RUNNABLE-ENTITY>
</RUNNABLES>
</SWC-INTERNAL-BEHAVIOR>
</INTERNAL-BEHAVIORS>
</APPLICATION-SW-COMPONENT-TYPE>
Expand Down
35 changes: 35 additions & 0 deletions examples/template/generated/TimerComponent.arxml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,41 @@
<INTERNAL-BEHAVIORS>
<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>TimerComponent_InternalBehavior</SHORT-NAME>
<EVENTS>
<INIT-EVENT>
<SHORT-NAME>IT_TimerComponent_Init</SHORT-NAME>
<START-ON-EVENT-REF DEST="RUNNABLE-ENTITY">/ComponentTypes/TimerComponent/TimerComponent_InternalBehavior/TimerComponent_Init</START-ON-EVENT-REF>
</INIT-EVENT>
<OPERATION-INVOKED-EVENT>
<SHORT-NAME>OIT_TimerComponent_GetTime_FreeRunningTimer_GetTime</SHORT-NAME>
<START-ON-EVENT-REF DEST="RUNNABLE-ENTITY">/ComponentTypes/TimerComponent/TimerComponent_InternalBehavior/TimerComponent_GetTime</START-ON-EVENT-REF>
<OPERATION-IREF>
<CONTEXT-P-PORT-REF DEST="P-PORT-PROTOTYPE">/ComponentTypes/TimerComponent/FreeRunningTimer</CONTEXT-P-PORT-REF>
<TARGET-PROVIDED-OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/GetTime</TARGET-PROVIDED-OPERATION-REF>
</OPERATION-IREF>
</OPERATION-INVOKED-EVENT>
<OPERATION-INVOKED-EVENT>
<SHORT-NAME>OIT_TimerComponent_IsTimerElapsed_FreeRunningTimer_IsTimerElapsed</SHORT-NAME>
<START-ON-EVENT-REF DEST="RUNNABLE-ENTITY">/ComponentTypes/TimerComponent/TimerComponent_InternalBehavior/TimerComponent_IsTimerElapsed</START-ON-EVENT-REF>
<OPERATION-IREF>
<CONTEXT-P-PORT-REF DEST="P-PORT-PROTOTYPE">/ComponentTypes/TimerComponent/FreeRunningTimer</CONTEXT-P-PORT-REF>
<TARGET-PROVIDED-OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/IsTimerElapsed</TARGET-PROVIDED-OPERATION-REF>
</OPERATION-IREF>
</OPERATION-INVOKED-EVENT>
</EVENTS>
<RUNNABLES>
<RUNNABLE-ENTITY>
<SHORT-NAME>TimerComponent_Init</SHORT-NAME>
</RUNNABLE-ENTITY>
<RUNNABLE-ENTITY>
<SHORT-NAME>TimerComponent_GetTime</SHORT-NAME>
<REENTRANCY-LEVEL>NON-REENTRANT</REENTRANCY-LEVEL>
</RUNNABLE-ENTITY>
<RUNNABLE-ENTITY>
<SHORT-NAME>TimerComponent_IsTimerElapsed</SHORT-NAME>
<REENTRANCY-LEVEL>NON-REENTRANT</REENTRANCY-LEVEL>
</RUNNABLE-ENTITY>
</RUNNABLES>
</SWC-INTERNAL-BEHAVIOR>
</INTERNAL-BEHAVIORS>
</APPLICATION-SW-COMPONENT-TYPE>
Expand Down
Loading

0 comments on commit 4f16712

Please sign in to comment.