Adds an operation node to the graph and returns it. # Arguments `node_dependencies` - vector of nodes necessary to perform the given operation `graph_dependencies` - vector of graphs necessary to perform the given operation `operation` - operation performed by the node # Returns New operation node that gets added
(
&self,
node_dependencies: Vec<Node>,
graph_dependencies: Vec<Graph>,
operation: Operation,
)
| 3358 | /// |
| 3359 | /// New operation node that gets added |
| 3360 | pub fn add_node( |
| 3361 | &self, |
| 3362 | node_dependencies: Vec<Node>, |
| 3363 | graph_dependencies: Vec<Graph>, |
| 3364 | operation: Operation, |
| 3365 | ) -> Result<Node> { |
| 3366 | if self.is_finalized() { |
| 3367 | return Err(runtime_error!("Can't add a node to a finalized graph")); |
| 3368 | } |
| 3369 | for dependency in &node_dependencies { |
| 3370 | if dependency.get_graph() != *self |
| 3371 | || dependency.get_id() >= self.body.borrow().nodes.len() as u64 |
| 3372 | || self.body.borrow().nodes[dependency.get_id() as usize] != *dependency |
| 3373 | { |
| 3374 | return Err(runtime_error!( |
| 3375 | "Can't add a node with invalid node dependencies" |
| 3376 | )); |
| 3377 | } |
| 3378 | } |
| 3379 | for dependency in &graph_dependencies { |
| 3380 | if !dependency.is_finalized() { |
| 3381 | return Err(runtime_error!( |
| 3382 | "Can't add a node with not finilized graph dependency" |
| 3383 | )); |
| 3384 | } |
| 3385 | if dependency.get_id() >= self.get_id() { |
| 3386 | return Err(runtime_error!( |
| 3387 | "Can't add a node with graph dependency with bigger id. {} >= {}", |
| 3388 | dependency.get_id(), |
| 3389 | self.get_id() |
| 3390 | )); |
| 3391 | } |
| 3392 | if dependency.get_context() != self.get_context() { |
| 3393 | return Err(runtime_error!( |
| 3394 | "Can't add a node with graph dependency from different context" |
| 3395 | )); |
| 3396 | } |
| 3397 | } |
| 3398 | let id = self.body.borrow().nodes.len() as u64; |
| 3399 | let result = Node { |
| 3400 | body: Arc::new(AtomicRefCell::new(NodeBody { |
| 3401 | graph: self.downgrade(), |
| 3402 | node_dependencies: node_dependencies.iter().map(|n| n.downgrade()).collect(), |
| 3403 | graph_dependencies: graph_dependencies.iter().map(|g| g.downgrade()).collect(), |
| 3404 | operation, |
| 3405 | id, |
| 3406 | })), |
| 3407 | }; |
| 3408 | { |
| 3409 | let mut cell = self.body.borrow_mut(); |
| 3410 | cell.nodes.push(result.clone()); |
| 3411 | } |
| 3412 | let mut context_has_type_checker = false; |
| 3413 | { |
| 3414 | let context = self.get_context(); |
| 3415 | let mut context_cell = context.body.borrow_mut(); |
| 3416 | let type_checker = &mut context_cell.type_checker; |
| 3417 | if type_checker.is_some() { |
no test coverage detected