Skip to content

Commit

Permalink
Implement DataReceivedEvent
Browse files Browse the repository at this point in the history
- Implement DataReceivedEvent
- Fixed element names in RModeInAtomicSwcInstanceRef
  • Loading branch information
cogu committed Jul 22, 2024
1 parent f33764e commit e6e0056
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Non-collectable elements are various sub-elements to collectable elements.
#### XML - Software component elements

* RModeInAtomicSwcInstanceRef | R-MODE-IN-ATOMIC-SWC-INSTANCE-REF
* RVariableInAtomicSwcInstanceRef | R-VARIABLE-IN-ATOMIC-SWC-INSTANCE-REF

#### XML - SWC internal behavior elements

* DataReceivedEvent | DATA-RECEIVED-EVENT
* InitEvent | INIT-EVENT
* InternalBehavior | SWC-INTERNAL-BEHAVIOR (Partly implemented)
* RunnableEntity | RUNNABLE-ENTITY
Expand Down
82 changes: 69 additions & 13 deletions src/autosar/xml/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -5233,19 +5233,40 @@ class RModeInAtomicSwcInstanceRef(ARObject):
"""

def __init__(self,
context_port_ref: AbstractRequiredPortPrototypeRef | None = None,
context_mode_declaration_group_prototype_ref: ModeDeclarationGroupPrototypeRef | None = None,
target_mode_declaration_ref: ModeDeclarationRef | None = None,
context_port: AbstractRequiredPortPrototypeRef | None = None,
context_mode_declaration_group_prototype: ModeDeclarationGroupPrototypeRef | None = None,
target_mode_declaration: ModeDeclarationRef | None = None,
) -> None:
self.context_port_ref: AbstractRequiredPortPrototypeRef | None = None # .CONTEXT-PORT-REF
# .CONTEXT-PORT-REF
self.context_port: AbstractRequiredPortPrototypeRef | None = None
# .CONTEXT-MODE-DECLARATION-GROUP-PROTOTYPE-REF
self.context_mode_declaration_group_prototype_ref: ModeDeclarationGroupPrototypeRef | None = None
self.target_mode_declaration_ref: ModeDeclarationRef | None = None # .TARGET-MODE-DECLARATION-REF
self._assign_optional("context_port_ref", context_port_ref, AbstractRequiredPortPrototypeRef)
self._assign_optional("context_mode_declaration_group_prototype_ref",
context_mode_declaration_group_prototype_ref,
self.context_mode_declaration_group_prototype: ModeDeclarationGroupPrototypeRef | None = None
# .TARGET-MODE-DECLARATION-REF
self.target_mode_declaration: ModeDeclarationRef | None = None
self._assign_optional("context_port", context_port, AbstractRequiredPortPrototypeRef)
self._assign_optional("context_mode_declaration_group_prototype",
context_mode_declaration_group_prototype,
ModeDeclarationGroupPrototypeRef)
self._assign_optional("target_mode_declaration_ref", target_mode_declaration_ref, ModeDeclarationRef)
self._assign_optional("target_mode_declaration", target_mode_declaration, ModeDeclarationRef)


class RVariableInAtomicSwcInstanceRef(ARObject):
"""
Complex type AR:R-VARIABLE-IN-ATOMIC-SWC-INSTANCE-REF
Tag variants: 'DATA-IREF'
"""

def __init__(self,
context_port: AbstractRequiredPortPrototypeRef | None = None,
target_data_element: VariableDataPrototypeRef | str | None = None,
) -> None:
# .CONTEXT-R-PORT-REF (Use same name as in RModeInAtomicSwcInstanceRef for consistency)
self.context_port: AbstractRequiredPortPrototypeRef | None = None
# .TARGET-DATA-ELEMENT-REF
self.target_data_element: VariableDataPrototypeRef | None = None
self._assign_optional("context_port", context_port, AbstractRequiredPortPrototypeRef)
self._assign_optional("target_data_element", target_data_element, VariableDataPrototypeRef)


# --- SWC internal behavior elements

Expand Down Expand Up @@ -5585,12 +5606,14 @@ class RteEvent(Identifiable):

def __init__(self,
name: str,
disabled_modes: RModeInAtomicSwcInstanceRef | list[RModeInAtomicSwcInstanceRef] | None = None,
start_on_event: RunnableEntityRef | None = None,
disabled_modes: RModeInAtomicSwcInstanceRef | list[RModeInAtomicSwcInstanceRef] | None = None,
**kwargs) -> None:
super().__init__(name, **kwargs)
self.disabled_modes: list[RModeInAtomicSwcInstanceRef] = [] # .DISABLED-MODE-IREFS
self.start_on_event: RunnableEntityRef | None = None # .START-ON-EVENT-REF
# .DISABLED-MODE-IREFS
self.disabled_modes: list[RModeInAtomicSwcInstanceRef] = []
# .START-ON-EVENT-REF
self.start_on_event: RunnableEntityRef | None = None
if disabled_modes is not None:
if isinstance(disabled_modes, RModeInAtomicSwcInstanceRef):
self.append_disabled_mode(disabled_modes)
Expand All @@ -5609,6 +5632,39 @@ def append_disabled_mode(self, disabled_mode: RModeInAtomicSwcInstanceRef) -> No
raise TypeError("disabled_mode must be of type RModeInAtomicSwcInstanceRef")


class DataReceivedEvent(RteEvent):
"""
Complex Type AR:DATA-RECEIVED-EVENT
Tag variants: 'DATA-RECEIVED-EVENT'
"""

def __init__(self,
name: str,
start_on_event: RunnableEntityRef | str | None = None,
data: RVariableInAtomicSwcInstanceRef | None = None,
**kwargs) -> None:
super().__init__(name, start_on_event, **kwargs)
# .DATA-IREF
self.data: RVariableInAtomicSwcInstanceRef | None = None
self._assign_optional_strict("data", data, RVariableInAtomicSwcInstanceRef)

@classmethod
def make(cls,
name: str,
start_on_event: RunnableEntityRef | str | None = None,
context_port: AbstractRequiredPortPrototypeRef | None = None,
target_data_element: VariableDataPrototypeRef | str | None = None,
**kwargs) -> "DataReceivedEvent":
"""
#convenience-method
Simplified creation method that automatically creates
and uses the necessary RVariableInAtomicSwcInstanceRef object
"""
data = RVariableInAtomicSwcInstanceRef(context_port, target_data_element)
return cls(name, start_on_event, data, **kwargs)


class InitEvent(RteEvent):
"""
Complex Type AR:INIT-EVENT
Expand Down
45 changes: 42 additions & 3 deletions src/autosar/xml/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ def __init__(self,
'DISABLED-MODE-IREF': self._read_rmode_in_atomic_swc_instance_ref,
'SWC-INTERNAL-BEHAVIOR': self._read_swc_internal_behavior,
'RUNNABLE-ENTITY': self._read_runnable_entity,
'DATA-RECEIVED-EVENT': self._read_data_received_event,
'INIT-EVENT': self._read_init_event,

}
self.switcher_all = {}
self.switcher_all.update(self.switcher_collectable)
Expand Down Expand Up @@ -4287,17 +4289,35 @@ def _read_rmode_in_atomic_swc_instance_ref(self,
child_elements = ChildElementMap(xml_element)
xml_child = child_elements.get("CONTEXT-PORT-REF")
if xml_child is not None:
data["context_port_ref"] = self._read_abstract_required_port_prototype_ref(xml_child)
data["context_port"] = self._read_abstract_required_port_prototype_ref(xml_child)
xml_child = child_elements.get("CONTEXT-MODE-DECLARATION-GROUP-PROTOTYPE-REF")
if xml_child is not None:
child_element = self._read_mode_declaration_group_prototype_ref(xml_child)
data["context_mode_declaration_group_prototype_ref"] = child_element
data["context_mode_declaration_group_prototype"] = child_element
xml_child = child_elements.get("TARGET-MODE-DECLARATION-REF")
if xml_child is not None:
data["target_mode_declaration_ref"] = self._read_mode_declaration_ref(xml_child)
data["target_mode_declaration"] = self._read_mode_declaration_ref(xml_child)
self._report_unprocessed_elements(child_elements)
return ar_element.RModeInAtomicSwcInstanceRef(**data)

def _read_rvariable_in_atomic_swc_instance_ref(self,
xml_element: ElementTree.Element
) -> ar_element.RVariableInAtomicSwcInstanceRef:
"""
Reads complex type AR:R-VARIABLE-IN-ATOMIC-SWC-INSTANCE-REF
Tag variants: 'DATA-IREF'
"""
data = {}
child_elements = ChildElementMap(xml_element)
xml_child = child_elements.get("CONTEXT-R-PORT-REF")
if xml_child is not None:
data["context_port"] = self._read_abstract_required_port_prototype_ref(xml_child)
xml_child = child_elements.get("TARGET-DATA-ELEMENT-REF")
if xml_child is not None:
data["target_data_element"] = self._read_variable_data_prototype_ref(xml_child)
self._report_unprocessed_elements(child_elements)
return ar_element.RVariableInAtomicSwcInstanceRef(**data)

# --- Internal Behavior elements

def _read_variable_in_impl_data_instance_ref(self,
Expand Down Expand Up @@ -4563,6 +4583,25 @@ def _read_rte_event(self, child_elements: ChildElementMap, data: dict) -> None:
data["start_on_event"] = self._read_runnable_entity_ref(xml_child)
child_elements.skip("VARIATION-POINT") # Not supported

def _read_data_received_event(self,
xml_element: ElementTree.Element
) -> ar_element.DataReceivedEvent:
"""
Reads complex Type AR:DATA-RECEIVED-EVENT
Tag variants: 'DATA-RECEIVED-EVENT'
"""
data = {}
child_elements = ChildElementMap(xml_element)
self._read_referrable(child_elements, data)
self._read_multi_language_referrable(child_elements, data)
self._read_identifiable(child_elements, xml_element.attrib, data)
self._read_rte_event(child_elements, data)
xml_child = child_elements.get("DATA-IREF")
if xml_child is not None:
data["data"] = self._read_rvariable_in_atomic_swc_instance_ref(xml_child)
self._report_unprocessed_elements(child_elements)
return ar_element.DataReceivedEvent(**data)

def _read_init_event(self,
xml_element: ElementTree.Element
) -> ar_element.InitEvent:
Expand Down
51 changes: 43 additions & 8 deletions src/autosar/xml/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def __init__(self,
'ExecutableEntityActivationReason': self._write_executable_entity_activation_reason,
'ExclusiveAreaRefConditional': self._write_exclusive_area_ref_conditional,
'RunnableEntity': self._write_runnable_entity,
'DataReceivedEvent': self._write_data_received_event,
'InitEvent': self._write_init_event,

}
Expand Down Expand Up @@ -3692,24 +3693,43 @@ def _write_swc_implementation_group(self, elem: ar_element.SwcImplementation) ->

def _write_rmode_in_atomic_swc_instance_ref(self, elem: ar_element.RModeInAtomicSwcInstanceRef, tag: str) -> None:
"""
Complex type AR:R-MODE-IN-ATOMIC-SWC-INSTANCE-REF
Writes complex type AR:R-MODE-IN-ATOMIC-SWC-INSTANCE-REF
Tag variants: 'DISABLED-MODE-IREF' | 'MODE-IREF'
"""
assert isinstance(elem, ar_element.RModeInAtomicSwcInstanceRef)
if elem.is_empty:
self._add_content(tag)
else:
self._add_child(tag)
if elem.context_port_ref is not None:
self._write_abstract_required_port_prototype_ref(elem.context_port_ref, "CONTEXT-PORT-REF")
if elem.context_mode_declaration_group_prototype_ref is not None:
self._write_mode_declaration_group_prototype_ref(elem.context_mode_declaration_group_prototype_ref,
if elem.context_port is not None:
self._write_abstract_required_port_prototype_ref(elem.context_port, "CONTEXT-PORT-REF")
if elem.context_mode_declaration_group_prototype is not None:
self._write_mode_declaration_group_prototype_ref(elem.context_mode_declaration_group_prototype,
"CONTEXT-MODE-DECLARATION-GROUP-PROTOTYPE-REF")
if elem.target_mode_declaration_ref is not None:
self._write_mode_declaration_ref(elem.target_mode_declaration_ref, "TARGET-MODE-DECLARATION-REF")
if elem.target_mode_declaration is not None:
self._write_mode_declaration_ref(elem.target_mode_declaration, "TARGET-MODE-DECLARATION-REF")
self._leave_child()

# SWC Internal behavior elements
def _write_rvariable_in_atomic_swc_instance_ref(self,
elem: ar_element.RVariableInAtomicSwcInstanceRef
) -> None:
"""
Writes complex type AR:R-VARIABLE-IN-ATOMIC-SWC-INSTANCE-REF
Tag variants: 'DATA-IREF'
"""
assert isinstance(elem, ar_element.RVariableInAtomicSwcInstanceRef)
tag = "DATA-IREF"
if elem.is_empty:
self._add_content(tag)
else:
self._add_child(tag)
if elem.context_port is not None:
self._write_abstract_required_port_prototype_ref(elem.context_port, "CONTEXT-R-PORT-REF")
if elem.target_data_element is not None:
self._write_variable_data_prototype_ref(elem.target_data_element, "TARGET-DATA-ELEMENT-REF")
self._leave_child()

# --- SWC Internal behavior elements

def _write_variable_in_impl_data_instance_ref(self,
elem: ar_element.ArVariableInImplementationDataInstanceRef,
Expand Down Expand Up @@ -3917,6 +3937,21 @@ def _write_rte_event(self, elem: ar_element.RteEvent) -> None:
if elem.start_on_event is not None:
self._write_runnable_entity_ref(elem.start_on_event, "START-ON-EVENT-REF")

def _write_data_received_event(self, elem: ar_element.DataReceivedEvent) -> None:
"""
Writes complex Type AR:DATA-RECEIVED-EVENT
Tag variants: 'DATA-RECEIVED-EVENT'
"""
assert isinstance(elem, ar_element.DataReceivedEvent)
self._add_child("DATA-RECEIVED-EVENT")
self._write_referrable(elem)
self._write_multilanguage_referrable(elem)
self._write_identifiable(elem)
self._write_rte_event(elem)
if elem.data is not None:
self._write_rvariable_in_atomic_swc_instance_ref(elem.data)
self._leave_child()

def _write_init_event(self, elem: ar_element.InitEvent) -> None:
"""
Writes complex type AR:INIT-EVENT
Expand Down
20 changes: 10 additions & 10 deletions tests/xml/test_software_component_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2066,10 +2066,10 @@ def test_empty(self):
elem: ar_element.RModeInAtomicSwcInstanceRef = reader.read_str_elem(xml)
self.assertIsInstance(elem, ar_element.RModeInAtomicSwcInstanceRef)

def test_context_port_ref(self):
def test_context_port(self):
ref_str = "/Components/ComponentName/RequirePortName"
port_ref = ar_element.PortPrototypeRef(ref_str, ar_enum.IdentifiableSubTypes.R_PORT_PROTOTYPE)
element = ar_element.RModeInAtomicSwcInstanceRef(context_port_ref=port_ref)
element = ar_element.RModeInAtomicSwcInstanceRef(context_port=port_ref)
writer = autosar.xml.Writer()
xml = f'''<DISABLED-MODE-IREF>
<CONTEXT-PORT-REF DEST="R-PORT-PROTOTYPE">{ref_str}</CONTEXT-PORT-REF>
Expand All @@ -2078,13 +2078,13 @@ def test_context_port_ref(self):
reader = autosar.xml.Reader()
elem: ar_element.RModeInAtomicSwcInstanceRef = reader.read_str_elem(xml)
self.assertIsInstance(elem, ar_element.RModeInAtomicSwcInstanceRef)
self.assertEqual(str(elem.context_port_ref), ref_str)
self.assertEqual(elem.context_port_ref.dest, ar_enum.IdentifiableSubTypes.R_PORT_PROTOTYPE)
self.assertEqual(str(elem.context_port), ref_str)
self.assertEqual(elem.context_port.dest, ar_enum.IdentifiableSubTypes.R_PORT_PROTOTYPE)

def test_context_mode_declaration_group_prototype_ref(self):
def test_context_mode_declaration_group_prototype(self):
ref_str = "/ModeDeclarationGroups/ModeDeclarationGroupName"
mode_decl_ref = ar_element.ModeDeclarationGroupPrototypeRef(ref_str)
element = ar_element.RModeInAtomicSwcInstanceRef(context_mode_declaration_group_prototype_ref=mode_decl_ref)
element = ar_element.RModeInAtomicSwcInstanceRef(context_mode_declaration_group_prototype=mode_decl_ref)
writer = autosar.xml.Writer()
tag = "CONTEXT-MODE-DECLARATION-GROUP-PROTOTYPE-REF"
xml = f'''<DISABLED-MODE-IREF>
Expand All @@ -2094,12 +2094,12 @@ def test_context_mode_declaration_group_prototype_ref(self):
reader = autosar.xml.Reader()
elem: ar_element.RModeInAtomicSwcInstanceRef = reader.read_str_elem(xml)
self.assertIsInstance(elem, ar_element.RModeInAtomicSwcInstanceRef)
self.assertEqual(str(elem.context_mode_declaration_group_prototype_ref), ref_str)
self.assertEqual(str(elem.context_mode_declaration_group_prototype), ref_str)

def test_target_mode_declaration_ref(self):
def test_target_mode_declaration(self):
ref_str = "/ModeDeclarationGroups/ModeDeclarationGroupName/ModeName"
mode_ref = ar_element.ModeDeclarationRef(ref_str)
element = ar_element.RModeInAtomicSwcInstanceRef(target_mode_declaration_ref=mode_ref)
element = ar_element.RModeInAtomicSwcInstanceRef(target_mode_declaration=mode_ref)
writer = autosar.xml.Writer()
xml = f'''<DISABLED-MODE-IREF>
<TARGET-MODE-DECLARATION-REF DEST="MODE-DECLARATION">{ref_str}</TARGET-MODE-DECLARATION-REF>
Expand All @@ -2108,7 +2108,7 @@ def test_target_mode_declaration_ref(self):
reader = autosar.xml.Reader()
elem: ar_element.RModeInAtomicSwcInstanceRef = reader.read_str_elem(xml)
self.assertIsInstance(elem, ar_element.RModeInAtomicSwcInstanceRef)
self.assertEqual(str(elem.target_mode_declaration_ref), ref_str)
self.assertEqual(str(elem.target_mode_declaration), ref_str)


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit e6e0056

Please sign in to comment.