Create graph_models.py
Browse files
engine_03/flow/graph_models.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass, field
|
| 2 |
+
from typing import List, Dict, Any, Optional
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@dataclass
|
| 6 |
+
class Node:
|
| 7 |
+
id: str
|
| 8 |
+
label: str = ""
|
| 9 |
+
type: str = ""
|
| 10 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class Edge:
|
| 15 |
+
source: str
|
| 16 |
+
target: str
|
| 17 |
+
label: str = ""
|
| 18 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@dataclass
|
| 22 |
+
class FlowGraph:
|
| 23 |
+
nodes: List[Node] = field(default_factory=list)
|
| 24 |
+
edges: List[Edge] = field(default_factory=list)
|
| 25 |
+
|
| 26 |
+
def find_node(self, node_id: str) -> Optional[Node]:
|
| 27 |
+
for n in self.nodes:
|
| 28 |
+
if n.id == node_id:
|
| 29 |
+
return n
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def table_to_flowgraph(nodes_table, edges_table) -> FlowGraph:
|
| 34 |
+
"""
|
| 35 |
+
Convert Gradio Dataframe tables into a FlowGraph instance.
|
| 36 |
+
nodes_table: list of [id, label, type]
|
| 37 |
+
edges_table: list of [source, target, label]
|
| 38 |
+
"""
|
| 39 |
+
graph = FlowGraph()
|
| 40 |
+
|
| 41 |
+
if nodes_table is None:
|
| 42 |
+
nodes_table = []
|
| 43 |
+
if edges_table is None:
|
| 44 |
+
edges_table = []
|
| 45 |
+
|
| 46 |
+
for row in nodes_table:
|
| 47 |
+
if not row:
|
| 48 |
+
continue
|
| 49 |
+
row = list(row) + ["", "", ""]
|
| 50 |
+
nid, label, ntype = row[0], row[1], row[2]
|
| 51 |
+
if not nid:
|
| 52 |
+
continue
|
| 53 |
+
graph.nodes.append(Node(id=str(nid), label=str(label or nid), type=str(ntype or "")))
|
| 54 |
+
|
| 55 |
+
for row in edges_table:
|
| 56 |
+
if not row:
|
| 57 |
+
continue
|
| 58 |
+
row = list(row) + ["", "", ""]
|
| 59 |
+
src, dst, elabel = row[0], row[1], row[2]
|
| 60 |
+
if not src or not dst:
|
| 61 |
+
continue
|
| 62 |
+
graph.edges.append(Edge(source=str(src), target=str(dst), label=str(elabel or "")))
|
| 63 |
+
|
| 64 |
+
return graph
|