Skip to content

Commit

Permalink
Enable support for runnables in SwcInternalBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
cogu committed Jun 24, 2024
1 parent 10240a8 commit c37a7eb
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 22 deletions.
70 changes: 48 additions & 22 deletions src/autosar/xml/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -5992,28 +5992,6 @@ def __init__(self,
self._assign_optional("scope", scope, ar_enum.VariableAccessScope)


class SwcInternalBehavior(Identifiable):
"""
Complex type AR:SWC-INTERNAL-BEHAVIOR
Tag variants: 'SWC-INTERNAL-BEHAVIOR'
This is just a placeholder. Will be implemented later.
"""

def __init__(self,
name: str,
**kwargs) -> None:
super().__init__(name, **kwargs)

def ref(self) -> SwcInternalBehaviorRef | None:
"""
Returns a reference to this element or
None if the element is not yet part of a package
"""
ref_str = self._calc_ref_string()
return None if ref_str is None else SwcInternalBehaviorRef(ref_str)


class SwcImplementation(Implementation):
"""
Complex type AR:SWC-IMPLEMENTATION
Expand Down Expand Up @@ -6212,3 +6190,51 @@ class RTEEvent(Identifiable):
"""
Group AR:RTE-EVENT
"""


class InternalBehavior(Identifiable):
"""
Group AR:INTERNAL-BEHAVIOR
This is just a placeholder. Will be implemented later.
"""


class SwcInternalBehavior(InternalBehavior):
"""
Complex type AR:SWC-INTERNAL-BEHAVIOR
Tag variants: 'SWC-INTERNAL-BEHAVIOR'
Implementation is very limited for now
"""

def __init__(self,
name: str,
runnables: RunnableEntity | list[RunnableEntity] | None = None,
**kwargs) -> None:
super().__init__(name, **kwargs)

self.runnables: list[RunnableEntity] = [] # .RUNNABLES

if runnables is not None:
if isinstance(runnables, list):
for runnable in runnables:
self.append_runnable(runnable)
else:
self.append_runnable(runnables)

def ref(self) -> SwcInternalBehaviorRef | None:
"""
Returns a reference to this element or
None if the element is not yet part of a package
"""
ref_str = self._calc_ref_string()
return None if ref_str is None else SwcInternalBehaviorRef(ref_str)

def append_runnable(self, runnable: RunnableEntity) -> None:
"""
Appends runnable to internal list of runnables
"""
if isinstance(runnable, RunnableEntity):
self.runnables.append(runnable)
else:
raise TypeError(f"runnable must be of type RunnableEntity. Got {str(type(runnable))}")
42 changes: 42 additions & 0 deletions src/autosar/xml/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4514,5 +4514,47 @@ def _read_swc_internal_behavior(self, xml_element: ElementTree.Element) -> ar_el
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_internal_behavior_group(child_elements, data)
self._read_swc_internal_behavior_group(child_elements, data)
self._report_unprocessed_elements(child_elements)
return ar_element.SwcInternalBehavior(**data)

def _read_internal_behavior_group(self, child_elements: ChildElementMap, data: dict) -> None:
"""
Reads group AR:INTERNAL-BEHAVIOR
Will be implemented in a future version
"""
child_elements.skip("CONSTANT-MEMORYS")
child_elements.skip("CONSTANT-VALUE-MAPPING-REFS")
child_elements.skip("DATA-TYPE-MAPPING-REFS")
child_elements.skip("EXCLUSIVE-AREAS")
child_elements.skip("EXCLUSIVE-AREA-NESTING-ORDERS")
child_elements.skip("STATIC-MEMORYS")

def _read_swc_internal_behavior_group(self, child_elements: ChildElementMap, data: dict) -> None:
"""
Reads group AR:SWC-INTERNAL-BEHAVIOR
Most of it will be implemented in a future version
"""
child_elements.skip("AR-TYPED-PER-INSTANCE-MEMORYS")
child_elements.skip("EVENTS")
child_elements.skip("EXCLUSIVE-AREA-POLICYS")
child_elements.skip("EXPLICIT-INTER-RUNNABLE-VARIABLES")
child_elements.skip("HANDLE-TERMINATION-AND-RESTART")
child_elements.skip("INCLUDED-DATA-TYPE-SETS")
child_elements.skip("INCLUDED-MODE-DECLARATION-GROUP-SETS")
child_elements.skip("INSTANTIATION-DATA-DEF-PROPSS")
child_elements.skip("PER-INSTANCE-MEMORYS")
child_elements.skip("PER-INSTANCE-PARAMETERS")
child_elements.skip("PORT-API-OPTIONS")
xml_child = child_elements.get("RUNNABLES")
if xml_child is not None:
runnables = []
for xml_grand_child in xml_child.findall("./RUNNABLE-ENTITY"):
runnables.append(self._read_runnable_entity(xml_grand_child))
data["runnables"] = runnables
child_elements.skip("SERVICE-DEPENDENCYS")
child_elements.skip("SHARED-PARAMETERS")
child_elements.skip("SUPPORTS-MULTIPLE-INSTANTIATION")
child_elements.skip("VARIATION-POINT-PROXYS")
child_elements.skip("VARIATION-POINT")
19 changes: 19 additions & 0 deletions src/autosar/xml/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3869,4 +3869,23 @@ def _write_swc_internal_behavior(self, elem: ar_element.SwcInternalBehavior) ->
self._write_referrable(elem)
self._write_multilanguage_referrable(elem)
self._write_identifiable(elem)
self._write_internal_behavior_group(elem)
self._write_swc_internal_behavior_group(elem)
self._leave_child()

def _write_internal_behavior_group(self, elem: ar_element.InternalBehavior) -> None:
"""
Writes group AR:INTERNAL-BEHAVIOR
This is just a placeholder. Will be implemented later.
"""

def _write_swc_internal_behavior_group(self, elem: ar_element.SwcInternalBehavior) -> None:
"""
Writes group AR:SWC-INTERNAL-BEHAVIOR
Most of it will be implemented in a future version
"""
if elem.runnables:
self._add_child("RUNNABLES")
for runnable in elem.runnables:
self._write_runnable_entity(runnable)
self._leave_child()
104 changes: 104 additions & 0 deletions tests/xml/test_swc_internal_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,5 +711,109 @@ def test_create_sw_addr_method_from_string(self):
self.assertEqual(ref.dest, ar_enum.IdentifiableSubTypes.SW_ADDR_METHOD)


class TestSwcInternalBehavior(unittest.TestCase):
"""
Most elements are not implemented yet
"""

def test_name_only(self):
writer = autosar.xml.Writer()
element = ar_element.SwcInternalBehavior('MyName')
xml = '''<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>MyName</SHORT-NAME>
</SWC-INTERNAL-BEHAVIOR>'''
self.assertEqual(writer.write_str_elem(element), xml)
reader = autosar.xml.Reader()
elem: ar_element.SwcInternalBehavior = reader.read_str_elem(xml)
self.assertIsInstance(elem, ar_element.SwcInternalBehavior)
self.assertEqual(elem.name, 'MyName')
self.assertEqual(elem.short_name, 'MyName')

# Base class elements
# IMPLEMENT LATER: CONSTANT-MEMORYS
# IMPLEMENT LATER: CONSTANT-VALUE-MAPPING-REFS
# IMPLEMENT LATER: DATA-TYPE-MAPPING-REFS
# IMPLEMENT LATER: EXCLUSIVE-AREAS
# IMPLEMENT LATER: EXCLUSIVE-AREA-NESTING-ORDERS
# IMPLEMENT LATER: STATIC-MEMORYS
# Class elements
# IMPLEMENT LATER: AR-TYPED-PER-INSTANCE-MEMORYS
# IMPLEMENT LATER: EVENTS
# IMPLEMENT LATER: EXCLUSIVE-AREA-POLICYS
# IMPLEMENT LATER: EXPLICIT-INTER-RUNNABLE-VARIABLES
# IMPLEMENT LATER: HANDLE-TERMINATION-AND-RESTART
# IMPLEMENT LATER: IMPLICIT-INTER-RUNNABLE-VARIABLES
# IMPLEMENT LATER: INCLUDED-DATA-TYPE-SETS
# IMPLEMENT LATER: INCLUDED-MODE-DECLARATION-GROUP-SETS
# IMPLEMENT LATER: INSTANTIATION-DATA-DEF-PROPSS
# IMPLEMENT LATER: PER-INSTANCE-MEMORYS
# IMPLEMENT LATER: PER-INSTANCE-PARAMETERS
# IMPLEMENT LATER: PORT-API-OPTIONS

# RUNNABLES

def test_runnables_from_element(self):
writer = autosar.xml.Writer()
element = ar_element.SwcInternalBehavior('MyName', runnables=ar_element.RunnableEntity("MyRunnable"))
xml = '''<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>MyName</SHORT-NAME>
<RUNNABLES>
<RUNNABLE-ENTITY>
<SHORT-NAME>MyRunnable</SHORT-NAME>
</RUNNABLE-ENTITY>
</RUNNABLES>
</SWC-INTERNAL-BEHAVIOR>'''
self.assertEqual(writer.write_str_elem(element), xml)
reader = autosar.xml.Reader()
elem: ar_element.SwcInternalBehavior = reader.read_str_elem(xml)
self.assertIsInstance(elem, ar_element.SwcInternalBehavior)
self.assertEqual(len(elem.runnables), 1)
runnable = elem.runnables[0]
self.assertIsInstance(runnable, ar_element.RunnableEntity)

def test_runnables_from_list(self):
writer = autosar.xml.Writer()
runnable1 = ar_element.RunnableEntity("MyRunnable1")
runnable2 = ar_element.RunnableEntity("MyRunnable2")
runnable3 = ar_element.RunnableEntity("MyRunnable3")
element = ar_element.SwcInternalBehavior('MyName', runnables=[runnable1,
runnable2,
runnable3])
xml = '''<SWC-INTERNAL-BEHAVIOR>
<SHORT-NAME>MyName</SHORT-NAME>
<RUNNABLES>
<RUNNABLE-ENTITY>
<SHORT-NAME>MyRunnable1</SHORT-NAME>
</RUNNABLE-ENTITY>
<RUNNABLE-ENTITY>
<SHORT-NAME>MyRunnable2</SHORT-NAME>
</RUNNABLE-ENTITY>
<RUNNABLE-ENTITY>
<SHORT-NAME>MyRunnable3</SHORT-NAME>
</RUNNABLE-ENTITY>
</RUNNABLES>
</SWC-INTERNAL-BEHAVIOR>'''
self.assertEqual(writer.write_str_elem(element), xml)
reader = autosar.xml.Reader()
elem: ar_element.SwcInternalBehavior = reader.read_str_elem(xml)
self.assertIsInstance(elem, ar_element.SwcInternalBehavior)
self.assertEqual(len(elem.runnables), 3)
runnable = elem.runnables[0]
self.assertIsInstance(runnable, ar_element.RunnableEntity)
self.assertEqual(runnable.name, "MyRunnable1")
runnable = elem.runnables[1]
self.assertIsInstance(runnable, ar_element.RunnableEntity)
self.assertEqual(runnable.name, "MyRunnable2")
runnable = elem.runnables[2]
self.assertIsInstance(runnable, ar_element.RunnableEntity)
self.assertEqual(runnable.name, "MyRunnable3")

# IMPLEMENT LATER: SERVICE-DEPENDENCYS"
# IMPLEMENT LATER: SHARED-PARAMETERS
# IMPLEMENT LATER: SUPPORTS-MULTIPLE-INSTANTIATION
# NOT SUPPORTED: VARIATION-POINT-PROXYS
# NOT SUPPORTED: VARIATION-POINT


if __name__ == '__main__':
unittest.main()

0 comments on commit c37a7eb

Please sign in to comment.