Skip to content

Commit

Permalink
unfreeze OdxLinkRef and derive RelatedDiagCommRef from it
Browse files Browse the repository at this point in the history
the main use case for frozen dataclasses is that they are
hashable. Since this is not a concern for `OdxLinkRef` objects, let's
unfreeze it and allow derived classes.

Besides `RelatedDiagCommRef`, there is also `ComparamRef` which should
be derived from `OdxLinkRef`, but this is more problematic because
`ComparamRef` contains quite a few attributes which the user is
usually interested in that are not in the `Comparam` specification
object (e.g., the actual value) of the communication parameter.

As usual, thanks [at]kayoub5 for the nudge.

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Gerrit Ecke <[email protected]>
  • Loading branch information
andlaus committed Oct 31, 2023
1 parent f5397ea commit 1d1b201
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 61 deletions.
18 changes: 9 additions & 9 deletions examples/somersaultecu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ class SomersaultSID(IntEnum):
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1609,7 +1609,7 @@ class SomersaultSID(IntEnum):
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1643,7 +1643,7 @@ class SomersaultSID(IntEnum):
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1686,7 +1686,7 @@ class SomersaultSID(IntEnum):
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1718,7 +1718,7 @@ class SomersaultSID(IntEnum):
description="<p>Do a forward flip.</p>",
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1769,7 +1769,7 @@ class SomersaultSID(IntEnum):
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1814,7 +1814,7 @@ class SomersaultSID(IntEnum):
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1862,7 +1862,7 @@ class SomersaultSID(IntEnum):
semantic=None,
functional_class_refs=[],
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
pre_condition_state_refs=[],
state_transition_refs=[],
diagnostic_class=None,
Expand Down Expand Up @@ -2233,7 +2233,7 @@ class SomersaultSID(IntEnum):
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down
51 changes: 13 additions & 38 deletions odxtools/diagcomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,18 @@ class DiagClassType(Enum):


@dataclass
class RelatedDiagCommRef:
diag_comm_ref: OdxLinkRef
class RelatedDiagCommRef(OdxLinkRef):
relation_type: str

@property
def diag_comm(self) -> "DiagComm":
return self._diag_comm

@property
def short_name(self) -> str:
return self.diag_comm.short_name

def _build_odxlinks(self) -> Dict[OdxLinkId, Any]:
return {}

def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
self._diag_comm = odxlinks.resolve(self.diag_comm_ref, DiagComm)

def _resolve_snrefs(self, diag_layer: "DiagLayer") -> None:
pass

@staticmethod
def from_et(et_element: ElementTree.Element,
doc_frags: List[OdxDocFragment]) -> "RelatedDiagCommRef":
diag_comm_ref = OdxLinkRef.from_et(et_element, doc_frags)
def from_et( # type: ignore[override]
et_element: ElementTree.Element,
doc_frags: List[OdxDocFragment]) -> "RelatedDiagCommRef":
kwargs = dataclass_fields_asdict(odxrequire(OdxLinkRef.from_et(et_element, doc_frags)))

relation_type = odxrequire(et_element.findtext("RELATION-TYPE"))

return RelatedDiagCommRef(diag_comm_ref=diag_comm_ref, relation_type=relation_type)
return RelatedDiagCommRef(relation_type=relation_type, **kwargs)


@dataclass
Expand All @@ -77,7 +60,7 @@ class DiagComm(IdentifiableElement):
functional_class_refs: List[OdxLinkRef]
audience: Optional[Audience]
protocol_snrefs: List[str]
related_diag_comm_refs_raw: List[RelatedDiagCommRef]
related_diag_comm_refs: List[RelatedDiagCommRef]
pre_condition_state_refs: Iterable[OdxLinkRef]
state_transition_refs: Iterable[OdxLinkRef]

Expand Down Expand Up @@ -109,7 +92,7 @@ def from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFragment]) ->
for el in et_element.iterfind("PROTOCOL-SNREFS/PROTOCOL-SNREF")
]

related_diag_comm_refs_raw = [
related_diag_comm_refs = [
RelatedDiagCommRef.from_et(el, doc_frags)
for el in et_element.iterfind("RELATED-DIAG-COMM-REFS/RELATED-DIAG-COMM-REF")
]
Expand Down Expand Up @@ -144,7 +127,7 @@ def from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFragment]) ->
functional_class_refs=functional_class_refs,
audience=audience,
protocol_snrefs=protocol_snrefs,
related_diag_comm_refs_raw=related_diag_comm_refs_raw,
related_diag_comm_refs=related_diag_comm_refs,
pre_condition_state_refs=pre_condition_state_refs,
state_transition_refs=state_transition_refs,
semantic=semantic,
Expand All @@ -163,8 +146,8 @@ def protocols(self) -> NamedItemList["DiagLayer"]:
return self._protocols

@property
def related_diag_comm_refs(self) -> NamedItemList[RelatedDiagCommRef]:
return self._related_diag_comm_refs
def related_diag_comms(self) -> NamedItemList["DiagComm"]:
return self._related_diag_comms

@property
def pre_condition_states(self) -> NamedItemList[State]:
Expand Down Expand Up @@ -198,9 +181,6 @@ def _build_odxlinks(self) -> Dict[OdxLinkId, Any]:
if self.audience is not None:
result.update(self.audience._build_odxlinks())

for rdc in self.related_diag_comm_refs_raw:
result.update(rdc._build_odxlinks())

return result

def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
Expand All @@ -210,13 +190,11 @@ def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
if self.audience:
self.audience._resolve_odxlinks(odxlinks)

for rdc in self.related_diag_comm_refs_raw:
rdc._resolve_odxlinks(odxlinks)

for sdg in self.sdgs:
sdg._resolve_odxlinks(odxlinks)

self._related_diag_comm_refs = NamedItemList(self.related_diag_comm_refs_raw)
self._related_diag_comms = NamedItemList(
[odxlinks.resolve(dc_ref, DiagComm) for dc_ref in self.related_diag_comm_refs])
self._functional_classes = NamedItemList(
[odxlinks.resolve(fc_ref, FunctionalClass) for fc_ref in self.functional_class_refs])
self._pre_condition_states = NamedItemList(
Expand All @@ -231,9 +209,6 @@ def _resolve_snrefs(self, diag_layer: "DiagLayer") -> None:
if self.audience:
self.audience._resolve_snrefs(diag_layer)

for rdc in self.related_diag_comm_refs_raw:
rdc._resolve_snrefs(diag_layer)

for sdg in self.sdgs:
sdg._resolve_snrefs(diag_layer)

Expand Down
2 changes: 1 addition & 1 deletion odxtools/odxlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def from_et(et: ElementTree.Element,
return OdxLinkId(local_id, doc_fragments)


@dataclass(frozen=True)
@dataclass
class OdxLinkRef:
"""A reference to an ODX object.
Expand Down
2 changes: 1 addition & 1 deletion odxtools/templates/macros/printDiagComm.xml.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
{%- if dc.related_diag_comm_refs %}
<RELATED-DIAG-COMM-REFS>
{%- for ref in dc.related_diag_comm_refs %}
<RELATED-DIAG-COMM-REF ID-REF="{{ref.diag_comm_ref.ref_id}}">
<RELATED-DIAG-COMM-REF ID-REF="{{ref.ref_id}}">
<RELATION-TYPE>{{ref.relation_type}}</RELATION-TYPE>
</RELATED-DIAG-COMM-REF>
{%- endfor %}
Expand Down
16 changes: 8 additions & 8 deletions tests/test_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_prefix_tree_construction(self) -> None:
addressing_raw=None,
transmission_mode_raw=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_prefix_tree_construction(self) -> None:
pre_condition_state_refs=[],
state_transition_refs=[],
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -286,7 +286,7 @@ def test_decode_request_coded_const(self) -> None:
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -427,7 +427,7 @@ def test_decode_request_coded_const_undefined_byte_position(self) -> None:
long_name=None,
description=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -611,7 +611,7 @@ def test_decode_request_structure(self) -> None:
long_name=None,
description=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -813,7 +813,7 @@ def test_decode_request_end_of_pdu_field(self) -> None:
long_name=None,
description=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -971,7 +971,7 @@ def test_decode_request_linear_compu_method(self) -> None:
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def test_decode_response(self) -> None:
description=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ecu_variant_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def ident_service(monkeypatch: pytest.MonkeyPatch, dummy_response: Response) ->
semantic=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down Expand Up @@ -118,7 +118,7 @@ def supplier_service(monkeypatch: pytest.MonkeyPatch, dummy_response: Response)
semantic=None,
admin_data=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
diagnostic_class=None,
is_mandatory_raw=None,
is_executable_raw=None,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_singleecujob.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Context(NamedTuple):
semantic=None,
functional_class_refs=[OdxLinkRef.from_id(self.context.extensiveTask.odx_id)],
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
pre_condition_state_refs=[],
state_transition_refs=[],
diagnostic_class=None,
Expand Down Expand Up @@ -341,7 +341,7 @@ def test_default_lists(self) -> None:
semantic=None,
audience=None,
protocol_snrefs=[],
related_diag_comm_refs_raw=[],
related_diag_comm_refs=[],
pre_condition_state_refs=[],
state_transition_refs=[],
prog_codes=[
Expand Down

0 comments on commit 1d1b201

Please sign in to comment.