MCPcopy Create free account
hub / github.com/BUPT-GAMMA/MASFactory / RootGraph

Class RootGraph

masfactory/components/graphs/root_graph.py:5–77  ·  view source on GitHub ↗

Top-level executable graph. RootGraph is the user-facing entry point for building and invoking workflows.

Source from the content-addressed store, hash-verified

3from masfactory.core.node_template import NodeTemplate
4
5class RootGraph(Graph):
6 """
7 Top-level executable graph.
8
9 RootGraph is the user-facing entry point for building and invoking workflows.
10 """
11 def __init__(
12 self,
13 name: str,
14 attributes: dict[str, object] | None = None,
15 edges: list[tuple[str, str] | tuple[str, str, dict[str, dict|str] ]]|None=None,
16 nodes: list[tuple[str, NodeTemplate]] | None = None,
17 ):
18 """Create a RootGraph.
19
20 Args:
21 name: Graph name.
22 attributes: Default attributes for this graph.
23 edges: Optional declarative edge definitions (see `Graph.__init__`).
24 nodes: Optional declarative node definitions as `(name, NodeTemplate)` entries.
25 """
26 super().__init__(name=name, attributes=attributes, edges=edges, nodes=nodes)
27 self._input = {}
28 self._output = {}
29
30 def invoke(self, input: dict, attributes: dict[str, object] | None = None):
31 """
32 Execute the root graph.
33
34 Args:
35 input: Graph input payload.
36 attributes: Optional attribute overrides for this invocation.
37
38 Returns:
39 tuple[dict, dict[str, object]]: `(output, attributes_snapshot)`.
40 """
41 if not self.check_built():
42 raise RuntimeError("Graph is not built yet. Please build the graph first.")
43 if attributes is None:
44 attributes = {}
45 self._attributes_store = {**self._attributes_store, **attributes}
46 try:
47 from masfactory.visualizer import get_bridge # noqa: WPS433
48 except Exception:
49 get_bridge = None # type: ignore[assignment]
50 runtime = get_bridge() if get_bridge is not None else None
51 if runtime is not None:
52 runtime.begin_run(self, input=input)
53 self._input = input
54 self.execute(self.attributes)
55 if runtime is not None:
56 runtime.end_run(self, output=self._output if isinstance(self._output, dict) else None)
57 return self._output,self.attributes.copy()
58
59 def build(self):
60 if self._is_built:
61 return
62 super().build()

Callers 15

mainFunction · 0.90
workflow.pyFile · 0.90
build_graphFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected