Composite graph node with internal entry and exit ports. Graph is both a node and a container. It can be nested inside another graph.
| 10 | from masfactory.core.node_template import NodeTemplate |
| 11 | |
| 12 | class Graph(BaseGraph): |
| 13 | """ |
| 14 | Composite graph node with internal entry and exit ports. |
| 15 | |
| 16 | Graph is both a node and a container. It can be nested inside another graph. |
| 17 | """ |
| 18 | def __init__( |
| 19 | self, |
| 20 | name, |
| 21 | pull_keys: dict[str, dict|str] | None = None, |
| 22 | push_keys: dict[str, dict|str] | None = None, |
| 23 | attributes: dict[str, object] | None = None, |
| 24 | edges: list[tuple[str, str] | tuple[str, str, dict[str, dict|str] ]]|None=None, |
| 25 | nodes: list[tuple[str, NodeTemplate]] | None = None, |
| 26 | build_func:Callable|None = None, |
| 27 | ): |
| 28 | """Create a composite graph node with internal entry/exit ports. |
| 29 | |
| 30 | Args: |
| 31 | name: Graph name. |
| 32 | pull_keys: Attribute pull rule for this graph. |
| 33 | push_keys: Attribute push rule for this graph. |
| 34 | attributes: Default attributes for this graph. |
| 35 | edges: Optional declarative edge definitions. Each item is either: |
| 36 | - `(from_name, to_name)` |
| 37 | - `(from_name, to_name, edge_keys)` |
| 38 | nodes: Optional declarative node definitions as `(name, NodeTemplate)` entries. |
| 39 | build_func: Optional custom build function executed before child build. |
| 40 | Signature: `(graph: Graph) -> None`. |
| 41 | """ |
| 42 | if attributes is None: |
| 43 | attributes = {} |
| 44 | super().__init__(name, pull_keys, push_keys, attributes, build_func=build_func) |
| 45 | self._init_nodes = nodes |
| 46 | self._init_edges = edges |
| 47 | class EntryNode(InternalGraphNode): |
| 48 | def __init__(self, name, gate_close_callback:Callable|None=None): |
| 49 | super().__init__(name, gate_close_callback) |
| 50 | self.input: dict[str,object] = {} |
| 51 | @masf_hook(Node.Hook.FORWARD) |
| 52 | def _forward(self,input:dict[str,object]) -> dict[str,object]: |
| 53 | return input |
| 54 | def _message_aggregate_in(self) -> dict[str,object]: |
| 55 | return self.input |
| 56 | def _update_gate_state(self): |
| 57 | pass |
| 58 | @masf_hook(Node.Hook.BUILD) |
| 59 | def build(self): |
| 60 | self._is_built = True |
| 61 | |
| 62 | class ExitNode(InternalGraphNode): |
| 63 | def __init__(self, name, gate_close_callback:Callable|None=None): |
| 64 | super().__init__(name, gate_close_callback) |
| 65 | self._output = {} |
| 66 | @masf_hook(Node.Hook.FORWARD) |
| 67 | def _forward(self,input:dict[str,object]) -> dict[str,object]: |
| 68 | return input |
| 69 | def _message_dispatch_out(self,message:dict[str,object]): |
no outgoing calls
no test coverage detected