From 95194149291fd138cc0fe14001bb4d068de73cde Mon Sep 17 00:00:00 2001 From: Rahix Date: Mon, 28 Jul 2025 15:38:36 +0200 Subject: [PATCH] Push all ultimate elements to the bottom of the graph In dot rendering, put all ultimate elements (those that nothing further depends on) on the same rank, meaning the very bottom. This helps to visualize our ultimate goals more easily and also shows elements that are probably a requisite to something else that has not yet been modelled. --- techtree-manager/src/tree.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/techtree-manager/src/tree.rs b/techtree-manager/src/tree.rs index 0574f73..f87873c 100644 --- a/techtree-manager/src/tree.rs +++ b/techtree-manager/src/tree.rs @@ -160,6 +160,16 @@ impl Tree { Subtree::new_for_element(self, element) } + pub fn iter_ultimate_elements<'a>(&'a self) -> impl Iterator + 'a { + self.graph.node_indices().filter(|index| { + // If there are no incoming edges, then this is an ultimate element! + self.graph + .neighbors_directed(*index, petgraph::Direction::Incoming) + .next() + .is_none() + }) + } + pub fn to_dot(&self) -> String { let dot = petgraph::dot::Dot::with_attr_getters( &self.graph, @@ -173,9 +183,16 @@ impl Tree { &|_g, (_, element)| element.to_dot_node_attributes(None), ); + let ultimate_elements: Vec<_> = self + .iter_ultimate_elements() + .map(|idx| idx.index().to_string()) + .collect(); + let ultimate_element_list = ultimate_elements.join("; "); + format!( r#"digraph {{ ranksep=1.2 + {{ rank=same; {ultimate_element_list}; }} {:?} }} "#,