Add a node to the graph
(&mut self, node: WorkflowNode)
| 157 | |
| 158 | /// Add a node to the graph |
| 159 | pub fn add_node(&mut self, node: WorkflowNode) -> GraphBitResult<()> { |
| 160 | let node_id = node.id.clone(); |
| 161 | |
| 162 | if self.nodes.contains_key(&node_id) { |
| 163 | let incoming_name = node.name.clone(); |
| 164 | let existing_name = self |
| 165 | .nodes |
| 166 | .get(&node_id) |
| 167 | .map(|n| n.name.clone()) |
| 168 | .unwrap_or_else(|| "<unknown>".to_string()); |
| 169 | return Err(GraphBitError::graph(format!( |
| 170 | "Node already exists: id={node_id} (existing name='{existing_name}', incoming name='{incoming_name}'). Hint: create a fresh Node instance; do not add the same Node object twice." |
| 171 | ))); |
| 172 | } |
| 173 | |
| 174 | let graph_index = self.graph.add_node(node.clone()); |
| 175 | self.node_map.insert(node_id.clone(), graph_index); |
| 176 | self.index_to_id.insert(graph_index, node_id.clone()); |
| 177 | self.name_to_id |
| 178 | .entry(node.name.clone()) |
| 179 | .or_insert_with(|| node_id.clone()); |
| 180 | self.nodes.insert(node_id, node); |
| 181 | |
| 182 | // Invalidate caches since graph structure changed |
| 183 | self.invalidate_caches(); |
| 184 | |
| 185 | Ok(()) |
| 186 | } |
| 187 | |
| 188 | /// Add an edge between two nodes |
| 189 | pub fn add_edge(&mut self, from: NodeId, to: NodeId, edge: WorkflowEdge) -> GraphBitResult<()> { |