Skip to content

Commit

Permalink
Improvements to component examples
Browse files Browse the repository at this point in the history
- Fix an xml-related issue with missing tag INTERNAL-BEHAVIORS
- Add support for package-to-document mapping action
- Examples now create default internal behavior and SWC implementation
- Examples use document root option to select default xml output folder
  • Loading branch information
cogu committed Mar 18, 2024
1 parent ef97b94 commit c2cf9ee
Show file tree
Hide file tree
Showing 14 changed files with 453 additions and 286 deletions.
22 changes: 10 additions & 12 deletions examples/xml/component/application_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,23 @@ def create_application_component(workspace: autosar.xml.Workspace):
swc.create_provide_port("EngineSpeed", engine_speed_interface, com_spec={"init_value": engine_speed_init.ref(),
"uses_end_to_end_protection": False,
})
swc.create_internal_behavior()
workspace.add_element("ComponentTypes", swc)
impl = ar_element.SwcImplementation("SenderComponent_Implementation", behavior_ref=swc.internal_behavior.ref())
workspace.add_element("ComponentTypes", impl)


def save_xml_files(workspace: autosar.xml.Workspace):
"""
Saves workspace as XML documents
"""
interface_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'portinterfaces.arxml'))
platform_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'platform.arxml'))
component_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'sender_component.arxml'))
constant_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'constants.arxml'))
workspace.create_document(interface_document_path, packages="/PortInterfaces")
workspace.create_document(constant_document_path, packages="/Constants")
workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform")
workspace.create_document(component_document_path, packages="/ComponentTypes")
workspace.set_document_root(os.path.abspath(os.path.join(os.path.dirname(__file__), "data")))
workspace.create_document("portinterfaces.arxml", packages="/PortInterfaces")
workspace.create_document("constants.arxml", packages="/Constants")
workspace.create_document("platform.arxml", packages="/AUTOSAR_Platform")
workspace.create_document_mapping(package_ref="/ComponentTypes",
element_types=ar_element.SwComponentType,
suffix_filters=["_Implementation"])
workspace.write_documents()


Expand Down
25 changes: 13 additions & 12 deletions examples/xml/component/composition_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def create_receiver_component(workspace: autosar.xml.Workspace):
"handle_never_received": False
})
swc.create_require_port("FreeRunningTimer", timer_interface, com_spec={"GetTime": {}, "IsTimerElapsed": {}})
swc.create_internal_behavior()
workspace.add_element("ComponentTypes", swc)
impl = ar_element.SwcImplementation("ReceiverComponent_Implementation", behavior_ref=swc.internal_behavior.ref())
workspace.add_element("ComponentTypes", impl)


def create_server_component(workspace: autosar.xml.Workspace):
Expand All @@ -123,7 +126,10 @@ def create_server_component(workspace: autosar.xml.Workspace):
swc.create_provide_port("FreeRunningTimer", timer_interface, com_spec={"GetTime": {"queue_length": 1},
"IsTimerElapsed": {"queue_length": 1}
})
swc.create_internal_behavior()
workspace.add_element("ComponentTypes", swc)
impl = ar_element.SwcImplementation("TimerComponent_Implementation", behavior_ref=swc.internal_behavior.ref())
workspace.add_element("ComponentTypes", impl)


def create_composition_component(workspace: autosar.xml.Workspace):
Expand Down Expand Up @@ -153,18 +159,13 @@ def save_xml_files(workspace: autosar.xml.Workspace):
"""
Saves workspace as XML documents
"""
interface_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'portinterfaces.arxml'))
platform_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'platform.arxml'))
component_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'composition.arxml'))
constant_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'constants.arxml'))
workspace.create_document(interface_document_path, packages="/PortInterfaces")
workspace.create_document(constant_document_path, packages="/Constants")
workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform")
workspace.create_document(component_document_path, packages="/ComponentTypes")
workspace.set_document_root(os.path.abspath(os.path.join(os.path.dirname(__file__), "data")))
workspace.create_document("portinterfaces.arxml", packages="/PortInterfaces")
workspace.create_document("constants.arxml", packages="/Constants")
workspace.create_document("platform.arxml", packages="/AUTOSAR_Platform")
workspace.create_document_mapping(package_ref="/ComponentTypes",
element_types=ar_element.SwComponentType,
suffix_filters=["_Implementation"])
workspace.write_documents()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,6 @@
<AR-PACKAGE>
<SHORT-NAME>ComponentTypes</SHORT-NAME>
<ELEMENTS>
<APPLICATION-SW-COMPONENT-TYPE>
<SHORT-NAME>ReceiverComponent</SHORT-NAME>
<PORTS>
<R-PORT-PROTOTYPE>
<SHORT-NAME>VehicleSpeed</SHORT-NAME>
<REQUIRED-COM-SPECS>
<NONQUEUED-RECEIVER-COM-SPEC>
<DATA-ELEMENT-REF DEST="VARIABLE-DATA-PROTOTYPE">/PortInterfaces/VehicleSpeed_I/VehicleSpeed</DATA-ELEMENT-REF>
<USES-END-TO-END-PROTECTION>false</USES-END-TO-END-PROTECTION>
<ALIVE-TIMEOUT>0</ALIVE-TIMEOUT>
<ENABLE-UPDATE>false</ENABLE-UPDATE>
<HANDLE-NEVER-RECEIVED>false</HANDLE-NEVER-RECEIVED>
<INIT-VALUE>
<CONSTANT-REFERENCE>
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/VehicleSpeed_IV</CONSTANT-REF>
</CONSTANT-REFERENCE>
</INIT-VALUE>
</NONQUEUED-RECEIVER-COM-SPEC>
</REQUIRED-COM-SPECS>
<REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/PortInterfaces/VehicleSpeed_I</REQUIRED-INTERFACE-TREF>
</R-PORT-PROTOTYPE>
<R-PORT-PROTOTYPE>
<SHORT-NAME>EngineSpeed</SHORT-NAME>
<REQUIRED-COM-SPECS>
<NONQUEUED-RECEIVER-COM-SPEC>
<DATA-ELEMENT-REF DEST="VARIABLE-DATA-PROTOTYPE">/PortInterfaces/EngineSpeed_I/EngineSpeed</DATA-ELEMENT-REF>
<USES-END-TO-END-PROTECTION>false</USES-END-TO-END-PROTECTION>
<ALIVE-TIMEOUT>0</ALIVE-TIMEOUT>
<ENABLE-UPDATE>false</ENABLE-UPDATE>
<HANDLE-NEVER-RECEIVED>false</HANDLE-NEVER-RECEIVED>
<INIT-VALUE>
<CONSTANT-REFERENCE>
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/EngineSpeed_IV</CONSTANT-REF>
</CONSTANT-REFERENCE>
</INIT-VALUE>
</NONQUEUED-RECEIVER-COM-SPEC>
</REQUIRED-COM-SPECS>
<REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/PortInterfaces/EngineSpeed_I</REQUIRED-INTERFACE-TREF>
</R-PORT-PROTOTYPE>
<R-PORT-PROTOTYPE>
<SHORT-NAME>FreeRunningTimer</SHORT-NAME>
<REQUIRED-COM-SPECS>
<CLIENT-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/GetTime</OPERATION-REF>
</CLIENT-COM-SPEC>
<CLIENT-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/IsTimerElapsed</OPERATION-REF>
</CLIENT-COM-SPEC>
</REQUIRED-COM-SPECS>
<REQUIRED-INTERFACE-TREF DEST="CLIENT-SERVER-INTERFACE">/PortInterfaces/FreeRunningTimer_I</REQUIRED-INTERFACE-TREF>
</R-PORT-PROTOTYPE>
</PORTS>
</APPLICATION-SW-COMPONENT-TYPE>
<APPLICATION-SW-COMPONENT-TYPE>
<SHORT-NAME>TimerComponent</SHORT-NAME>
<PORTS>
<P-PORT-PROTOTYPE>
<SHORT-NAME>FreeRunningTimer</SHORT-NAME>
<PROVIDED-COM-SPECS>
<SERVER-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/GetTime</OPERATION-REF>
<QUEUE-LENGTH>1</QUEUE-LENGTH>
</SERVER-COM-SPEC>
<SERVER-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/IsTimerElapsed</OPERATION-REF>
<QUEUE-LENGTH>1</QUEUE-LENGTH>
</SERVER-COM-SPEC>
</PROVIDED-COM-SPECS>
<PROVIDED-INTERFACE-TREF DEST="CLIENT-SERVER-INTERFACE">/PortInterfaces/FreeRunningTimer_I</PROVIDED-INTERFACE-TREF>
</P-PORT-PROTOTYPE>
</PORTS>
</APPLICATION-SW-COMPONENT-TYPE>
<COMPOSITION-SW-COMPONENT-TYPE>
<SHORT-NAME>CompositionComponent</SHORT-NAME>
<PORTS>
Expand Down
72 changes: 72 additions & 0 deletions examples/xml/component/data/ReceiverComponent.arxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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>ComponentTypes</SHORT-NAME>
<ELEMENTS>
<APPLICATION-SW-COMPONENT-TYPE>
<SHORT-NAME>ReceiverComponent</SHORT-NAME>
<PORTS>
<R-PORT-PROTOTYPE>
<SHORT-NAME>VehicleSpeed</SHORT-NAME>
<REQUIRED-COM-SPECS>
<NONQUEUED-RECEIVER-COM-SPEC>
<DATA-ELEMENT-REF DEST="VARIABLE-DATA-PROTOTYPE">/PortInterfaces/VehicleSpeed_I/VehicleSpeed</DATA-ELEMENT-REF>
<USES-END-TO-END-PROTECTION>false</USES-END-TO-END-PROTECTION>
<ALIVE-TIMEOUT>0</ALIVE-TIMEOUT>
<ENABLE-UPDATE>false</ENABLE-UPDATE>
<HANDLE-NEVER-RECEIVED>false</HANDLE-NEVER-RECEIVED>
<INIT-VALUE>
<CONSTANT-REFERENCE>
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/VehicleSpeed_IV</CONSTANT-REF>
</CONSTANT-REFERENCE>
</INIT-VALUE>
</NONQUEUED-RECEIVER-COM-SPEC>
</REQUIRED-COM-SPECS>
<REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/PortInterfaces/VehicleSpeed_I</REQUIRED-INTERFACE-TREF>
</R-PORT-PROTOTYPE>
<R-PORT-PROTOTYPE>
<SHORT-NAME>EngineSpeed</SHORT-NAME>
<REQUIRED-COM-SPECS>
<NONQUEUED-RECEIVER-COM-SPEC>
<DATA-ELEMENT-REF DEST="VARIABLE-DATA-PROTOTYPE">/PortInterfaces/EngineSpeed_I/EngineSpeed</DATA-ELEMENT-REF>
<USES-END-TO-END-PROTECTION>false</USES-END-TO-END-PROTECTION>
<ALIVE-TIMEOUT>0</ALIVE-TIMEOUT>
<ENABLE-UPDATE>false</ENABLE-UPDATE>
<HANDLE-NEVER-RECEIVED>false</HANDLE-NEVER-RECEIVED>
<INIT-VALUE>
<CONSTANT-REFERENCE>
<CONSTANT-REF DEST="CONSTANT-SPECIFICATION">/Constants/EngineSpeed_IV</CONSTANT-REF>
</CONSTANT-REFERENCE>
</INIT-VALUE>
</NONQUEUED-RECEIVER-COM-SPEC>
</REQUIRED-COM-SPECS>
<REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/PortInterfaces/EngineSpeed_I</REQUIRED-INTERFACE-TREF>
</R-PORT-PROTOTYPE>
<R-PORT-PROTOTYPE>
<SHORT-NAME>FreeRunningTimer</SHORT-NAME>
<REQUIRED-COM-SPECS>
<CLIENT-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/GetTime</OPERATION-REF>
</CLIENT-COM-SPEC>
<CLIENT-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/IsTimerElapsed</OPERATION-REF>
</CLIENT-COM-SPEC>
</REQUIRED-COM-SPECS>
<REQUIRED-INTERFACE-TREF DEST="CLIENT-SERVER-INTERFACE">/PortInterfaces/FreeRunningTimer_I</REQUIRED-INTERFACE-TREF>
</R-PORT-PROTOTYPE>
</PORTS>
<INTERNAL-BEHAVIORS>
<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>ReceiverComponent_InternalBehavior</SHORT-NAME>
</SWC-INTERNAL-BEHAVIOR>
</INTERNAL-BEHAVIORS>
</APPLICATION-SW-COMPONENT-TYPE>
<SWC-IMPLEMENTATION>
<SHORT-NAME>ReceiverComponent_Implementation</SHORT-NAME>
<BEHAVIOR-REF DEST="SWC-INTERNAL-BEHAVIOR">/ComponentTypes/ReceiverComponent/ReceiverComponent_InternalBehavior</BEHAVIOR-REF>
</SWC-IMPLEMENTATION>
</ELEMENTS>
</AR-PACKAGE>
</AR-PACKAGES>
</AUTOSAR>
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@
<PROVIDED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/PortInterfaces/EngineSpeed_I</PROVIDED-INTERFACE-TREF>
</P-PORT-PROTOTYPE>
</PORTS>
<INTERNAL-BEHAVIORS>
<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>SenderComponent_InternalBehavior</SHORT-NAME>
</SWC-INTERNAL-BEHAVIOR>
</INTERNAL-BEHAVIORS>
</APPLICATION-SW-COMPONENT-TYPE>
<SWC-IMPLEMENTATION>
<SHORT-NAME>SenderComponent_Implementation</SHORT-NAME>
<BEHAVIOR-REF DEST="SWC-INTERNAL-BEHAVIOR">/ComponentTypes/SenderComponent/SenderComponent_InternalBehavior</BEHAVIOR-REF>
</SWC-IMPLEMENTATION>
</ELEMENTS>
</AR-PACKAGE>
</AR-PACKAGES>
Expand Down
38 changes: 38 additions & 0 deletions examples/xml/component/data/TimerComponent.arxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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>ComponentTypes</SHORT-NAME>
<ELEMENTS>
<APPLICATION-SW-COMPONENT-TYPE>
<SHORT-NAME>TimerComponent</SHORT-NAME>
<PORTS>
<P-PORT-PROTOTYPE>
<SHORT-NAME>FreeRunningTimer</SHORT-NAME>
<PROVIDED-COM-SPECS>
<SERVER-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/GetTime</OPERATION-REF>
<QUEUE-LENGTH>1</QUEUE-LENGTH>
</SERVER-COM-SPEC>
<SERVER-COM-SPEC>
<OPERATION-REF DEST="CLIENT-SERVER-OPERATION">/PortInterfaces/FreeRunningTimer_I/IsTimerElapsed</OPERATION-REF>
<QUEUE-LENGTH>1</QUEUE-LENGTH>
</SERVER-COM-SPEC>
</PROVIDED-COM-SPECS>
<PROVIDED-INTERFACE-TREF DEST="CLIENT-SERVER-INTERFACE">/PortInterfaces/FreeRunningTimer_I</PROVIDED-INTERFACE-TREF>
</P-PORT-PROTOTYPE>
</PORTS>
<INTERNAL-BEHAVIORS>
<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>TimerComponent_InternalBehavior</SHORT-NAME>
</SWC-INTERNAL-BEHAVIOR>
</INTERNAL-BEHAVIORS>
</APPLICATION-SW-COMPONENT-TYPE>
<SWC-IMPLEMENTATION>
<SHORT-NAME>TimerComponent_Implementation</SHORT-NAME>
<BEHAVIOR-REF DEST="SWC-INTERNAL-BEHAVIOR">/ComponentTypes/TimerComponent/TimerComponent_InternalBehavior</BEHAVIOR-REF>
</SWC-IMPLEMENTATION>
</ELEMENTS>
</AR-PACKAGE>
</AR-PACKAGES>
</AUTOSAR>
4 changes: 2 additions & 2 deletions run_examples.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ python examples\xml\port_interface\client_server_interface.py
python examples\xml\port_interface\mode_switch_interface.py
python examples\xml\component\application_component.py
python examples\xml\component\composition_component.py
python examples\xml\template\generate_xml_using_config.py
python examples\xml\template\generate_xml_without_config.py
python examples\template\generate_xml_using_config.py
python examples\template\generate_xml_without_config.py
python examples\generator\data_types\gen_type_defs_scalar.py
python examples\generator\data_types\gen_type_defs_array.py
python examples\generator\data_types\gen_type_defs_record.py
Expand Down
45 changes: 5 additions & 40 deletions src/autosar/xml/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
AUTOSAR Document
"""
# pylint: disable=R0801
from typing import Any
from autosar.base import DEFAULT_SCHEMA_VERSION
import autosar.xml.element as ar_element


Expand All @@ -23,49 +23,14 @@ def schema_file(self) -> str:
return f'AUTOSAR_{self.schema_version:05d}.xsd'


class Document(DocumentMeta):
class Document(ar_element.PackageCollection, DocumentMeta):
"""
Implements AUTOSAR root description element
"""

def __init__(self, packages: list[ar_element.Package] | None = None, schema_version=51) -> None:
super().__init__(schema_version)
def __init__(self, packages: list[ar_element.Package] | None = None, schema_version=DEFAULT_SCHEMA_VERSION) -> None:
ar_element.PackageCollection.__init__(self, packages)
DocumentMeta.__init__(self, schema_version)
self.file_info_comment = None # .FILE-INFO-COMMENT
self.admin_data = None # .ADMIN-DATA
self.introduction = None # .INTRODUCTION
self.packages: list[ar_element.Package] = [] # .PACKAGES
self._package_map = {} # internal package map
if packages is not None:
for package in packages:
self.append(package)

def append(self, package: ar_element.Package):
"""
Appends package to this document and
appropriately updates reference links
"""
if isinstance(package, ar_element.Package):
if package.name in self._package_map:
raise ValueError(
f"Package with SHORT-NAME '{package.name}' already exists")
package.parent = self
self.packages.append(package)
self._package_map[package.name] = package

def find(self, ref: str) -> Any:
"""
Finds item by reference
"""
if ref.startswith('/'):
ref = ref[1:]
parts = ref.partition('/')
package = self._package_map.get(parts[0], None)
if (package is not None) and (len(parts[2]) > 0):
return package.find(parts[2])
return package

def update_ref_parts(self, ref_parts: list[str]):
"""
Utility method used generating XML references
"""
ref_parts.append('')
Loading

0 comments on commit c2cf9ee

Please sign in to comment.