Skip to content

Commit

Permalink
Refactor code related to messages with implicit size
Browse files Browse the repository at this point in the history
Ref. #736
  • Loading branch information
treiher committed Dec 21, 2021
1 parent f97206f commit 60722f1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
7 changes: 1 addition & 6 deletions rflx/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,7 @@ def __create_message(self, message: Message) -> None:
sequence_fields[f] = t
if isinstance(t, Opaque):
opaque_fields.append(f)
if any(
bool(
l.size.findall(lambda x: x in [expr.Size("Message"), expr.Last("Message")])
)
for l in message.incoming(f)
):
if any(l.has_implicit_size for l in message.incoming(f)):
fields_with_implicit_size.append(f)
else:
fields_with_explicit_size.append(f)
Expand Down
11 changes: 3 additions & 8 deletions rflx/generator/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
)
from rflx.common import unique
from rflx.const import BUILTINS_PACKAGE
from rflx.model import FINAL, Enumeration, Field, Link, Message, Opaque, Scalar, Sequence, Type
from rflx.model import FINAL, Enumeration, Field, Message, Opaque, Scalar, Sequence, Type

from . import common, const

Expand Down Expand Up @@ -264,11 +264,6 @@ def substituted(expression: expr.Expr) -> Expr:
.ada_expr()
)

def has_implicit_size(link: Link) -> bool:
return bool(
link.size.findall(lambda x: x in [expr.Size("Message"), expr.Last("Message")])
)

target_links = [
(target, list(links))
for target, links in itertools.groupby(message.outgoing(field), lambda x: x.target)
Expand Down Expand Up @@ -297,13 +292,13 @@ def has_implicit_size(link: Link) -> bool:
size = explicit_size
else:
if len(links) == 1:
size = implicit_size if has_implicit_size(links[0]) else explicit_size
size = implicit_size if links[0].has_implicit_size else explicit_size
else:
size = If(
[
(
substituted(l.condition),
implicit_size if has_implicit_size(l) else explicit_size,
implicit_size if l.has_implicit_size else explicit_size,
)
for l in links
],
Expand Down
22 changes: 9 additions & 13 deletions rflx/model/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def __eq__(self, other: object) -> bool:
def __hash__(self) -> int:
return 0

@property
def has_implicit_size(self) -> bool:
return bool(self.size.findall(lambda x: x in [expr.Size("Message"), expr.Last("Message")]))


def valid_message_field_types(message: "AbstractMessage") -> bool:
for t in message.types.values():
Expand Down Expand Up @@ -630,7 +634,7 @@ def _validate_link_aspects(self) -> None:
]
)

if link.size.findall(lambda x: x in {expr.Size("Message"), expr.Last("Message")}):
if link.has_implicit_size:
if any(l.target != FINAL for l in self.outgoing(link.target)):
self.error.extend(
[
Expand Down Expand Up @@ -868,15 +872,7 @@ def has_fixed_size(self) -> bool:

@property
def has_implicit_size(self) -> bool:
"""
Return true if the message has a implicit size.
All messages containing a reference to `Message` in a size aspect are considered as
having a implicit size.
"""
return all(
v.identifier != ID("Message") for l in self.structure for v in l.size.variables()
)
return any(l.has_implicit_size for l in self.structure)

@property
def is_definite(self) -> bool:
Expand All @@ -888,7 +884,7 @@ def is_definite(self) -> bool:
"""
return (
len(self.paths(FINAL)) <= 1
and self.has_implicit_size
and not self.has_implicit_size
and all(
not l.size.findall(lambda x: isinstance(x, (expr.First, expr.Last)))
for l in self.structure
Expand Down Expand Up @@ -1016,7 +1012,7 @@ def max_size(self) -> expr.Number:
if not self.structure:
return expr.Number(0)

if not self.has_implicit_size:
if self.has_implicit_size:
fail(
"unable to calculate maximum size of message with implicit size",
Subsystem.MODEL,
Expand All @@ -1035,7 +1031,7 @@ def max_field_sizes(self) -> Dict[Field, expr.Number]:
if not self.structure:
return {}

if not self.has_implicit_size:
if self.has_implicit_size:
fail(
"unable to calculate maximum field sizes of message with implicit size",
Subsystem.MODEL,
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/model/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2672,11 +2672,11 @@ def test_has_fixed_size() -> None:


def test_has_implicit_size() -> None:
assert NULL_MESSAGE.has_implicit_size
assert FIXED_SIZE_MESSAGE.has_implicit_size
assert TLV_MESSAGE.has_implicit_size
assert not ETHERNET_FRAME.has_implicit_size
assert SEQUENCE_MESSAGE.has_implicit_size
assert not NULL_MESSAGE.has_implicit_size
assert not FIXED_SIZE_MESSAGE.has_implicit_size
assert not TLV_MESSAGE.has_implicit_size
assert ETHERNET_FRAME.has_implicit_size
assert not SEQUENCE_MESSAGE.has_implicit_size


def test_is_definite() -> None:
Expand Down

0 comments on commit 60722f1

Please sign in to comment.