Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion ymmsl/v0_2/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections import OrderedDict
from collections import OrderedDict, defaultdict
from copy import copy
from enum import Enum
from typing import Any, List, Sequence, TypeAlias, cast
Expand Down Expand Up @@ -448,6 +448,7 @@ def check_consistent(self) -> List[str]:
This checks:
- that every conduit is connected to two existing ports on existing
components, or on the model itself.
- that every receiving port is connected by at most one conduit.

Returns a list of errors, or an empty list if none were found.
"""
Expand All @@ -460,6 +461,25 @@ def check_consistent(self) -> List[str]:
errors.extend(self._check_sending_side(conduit, model_receiving_ports))
errors.extend(self._check_receiving_side(conduit, model_sending_ports))

errors.extend(self._check_one_conduit_per_receiver())

return errors

def _check_one_conduit_per_receiver(self) -> List[str]:
"""Check that no receiving port is connected by more than one conduit."""
cdts_by_receiver: defaultdict[Reference, List[Conduit]] = defaultdict(list)
for conduit in self.conduits:
cdts_by_receiver[conduit.receiver].append(conduit)

errors = list()
for receiver, conduits in cdts_by_receiver.items():
if len(conduits) > 1:
conduits_str = ", ".join(str(conduit) for conduit in conduits)
errors.append(
f"Receiving port {receiver} is connected by multiple conduits"
f" ({conduits_str}), but a receiving port can only be connected"
" by one conduit."
)
return errors

def _check_sending_side(
Expand Down
21 changes: 21 additions & 0 deletions ymmsl/v0_2/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,24 @@ def test_conduits_inconsistent() -> None:

errors = model.check_consistent()
assert len(errors) == 6


def test_conduits_multiple_to_one_receiver() -> None:
a = Component("a", Ports(o_f=["out1", "out2"]), "a_impl")
b = Component("b", Ports(f_init=["in"]), "b_impl")
conduits = [Conduit("a.out1", "b.in"), Conduit("a.out2", "b.in")]
model = Model("m", components=[a, b], conduits=conduits)

errors = model.check_consistent()
assert len(errors) == 1
assert "b.in" in errors[0]


def test_conduits_multiple_to_one_receiving_model_port() -> None:
a = Component("a", Ports(o_f=["out1", "out2"]), "a_impl")
conduits = [Conduit("a.out1", "final"), Conduit("a.out2", "final")]
model = Model("m", Ports(o_f=["final"]), components=[a], conduits=conduits)

errors = model.check_consistent()
assert len(errors) == 1
assert "final" in errors[0]