diff --git a/ymmsl/v0_2/model.py b/ymmsl/v0_2/model.py index 524a495..771d436 100644 --- a/ymmsl/v0_2/model.py +++ b/ymmsl/v0_2/model.py @@ -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 @@ -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. """ @@ -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( diff --git a/ymmsl/v0_2/tests/test_model.py b/ymmsl/v0_2/tests/test_model.py index 2b5ab25..5df80d0 100644 --- a/ymmsl/v0_2/tests/test_model.py +++ b/ymmsl/v0_2/tests/test_model.py @@ -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]