From 273fe7ebca6b947c4964bc912624ee57a72dcf6c Mon Sep 17 00:00:00 2001 From: Seyon Sivarajah Date: Thu, 6 Jun 2024 14:26:13 +0100 Subject: [PATCH] add order link iterators and fix detected bug --- hugr-py/src/hugr/_dfg.py | 2 +- hugr-py/src/hugr/_hugr.py | 7 ++++++- hugr-py/tests/test_hugr_build.py | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/hugr-py/src/hugr/_dfg.py b/hugr-py/src/hugr/_dfg.py index 539dfa499..f083e8ae0 100644 --- a/hugr-py/src/hugr/_dfg.py +++ b/hugr-py/src/hugr/_dfg.py @@ -76,7 +76,7 @@ def _wire_up(self, node: Node, ports: Iterable[Wire]): node_ancestor = _ancestral_sibling(self.hugr, src.node, node) if node_ancestor is None: raise NoSiblingAncestor(src.node.idx, node.idx) - if node_ancestor != src.node: + if node_ancestor != node: self.add_state_order(src.node, node_ancestor) self.hugr.add_link(src, node.inp(i)) diff --git a/hugr-py/src/hugr/_hugr.py b/hugr-py/src/hugr/_hugr.py index 577c79086..db40a9f2b 100644 --- a/hugr-py/src/hugr/_hugr.py +++ b/hugr-py/src/hugr/_hugr.py @@ -283,6 +283,12 @@ def linked_ports(self, port: OutPort | InPort): # TODO: single linked port + def outgoing_order_links(self, node: Node) -> Iterable[Node]: + return (p.node for p in self.linked_ports(node.out(-1))) + + def incoming_order_links(self, node: Node) -> Iterable[Node]: + return (p.node for p in self.linked_ports(node.inp(-1))) + def _node_links( self, node: Node, links: dict[_SubPort[P], _SubPort[K]] ) -> Iterable[tuple[P, list[K]]]: @@ -292,7 +298,6 @@ def _node_links( return # iterate over known offsets for offset in range(self.num_ports(node, direction)): - # TODO should this also look for -1 state order edges? port = cast(P, node.port(offset, direction)) yield port, list(self._linked_ports(port, links)) diff --git a/hugr-py/tests/test_hugr_build.py b/hugr-py/tests/test_hugr_build.py index 841039769..e13c2198d 100644 --- a/hugr-py/tests/test_hugr_build.py +++ b/hugr-py/tests/test_hugr_build.py @@ -237,9 +237,13 @@ def test_build_inter_graph(): h.set_outputs(nested.root, b) + _validate(h.hugr, True) + assert _SubPort(h.input_node.out(-1)) in h.hugr._links assert h.hugr.num_outgoing(h.input_node) == 2 # doesn't count state order - _validate(h.hugr, True) + assert len(list(h.hugr.outgoing_order_links(h.input_node))) == 1 + assert len(list(h.hugr.incoming_order_links(nested.root))) == 1 + assert len(list(h.hugr.incoming_order_links(h.output_node))) == 0 def test_ancestral_sibling():