Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ eyre = "0.6.8"
fastnbt = "2"
fastanvil = "0.26"
indicatif = { version = "0.17.9", features = ["rayon"] }
indexmap = "2.0.0"
itertools = "0.11.0"
petgraph = "0.6.3"
quine-mc_cluskey = "0.2.4"
Expand Down
5 changes: 3 additions & 2 deletions src/bin/check_nbt_world_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ fn main() -> eyre::Result<()> {
.map(|node| (node.id, node.kind.name()))
.collect::<HashMap<GraphNodeId, String>>();

let cyclic_components = kosaraju_scc(&graph.to_petgraph_only_edges())
let petgraph = graph.to_petgraph_only_edges();
let cyclic_components = kosaraju_scc(&petgraph)
.into_iter()
.map(|component| {
component
.into_iter()
.map(|node| node.index())
.map(|node| petgraph[node])
.filter(|id| world_graph.positions.contains_key(id))
.collect::<Vec<_>>()
})
Expand Down
40 changes: 18 additions & 22 deletions src/graph/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::cluster::ClusteredGraph;
use super::logic::LogicGraph;
use super::module::{GraphModule, GraphWithSubGraphs};
use super::world::WorldGraph;
use super::{Graph, GraphNode, GraphNodeId, SubGraph};
use super::{Graph, GraphNodeId, GraphNodeRef, SubGraph};
use crate::graph::module::GraphModulePortTarget;

pub struct GraphvizBuilder<'a> {
Expand Down Expand Up @@ -135,7 +135,7 @@ digraph {graph_name} {{
}
}

fn print_node(&self, node: &GraphNode) -> String {
fn print_node(&self, node: GraphNodeRef<'_>) -> String {
let name = if self.show_node_id {
format!("{} #{}", node.kind.name(), node.id)
} else {
Expand Down Expand Up @@ -268,10 +268,14 @@ digraph {graph_name} {{
.nodes
.iter()
.flat_map(|node| {
node.inputs.iter().map(|input| {
let in_suffix = format!("in{}_{}", input, node.id);
format!(" node{}:out->node{}:{}\n", input, node.id, in_suffix)
})
let node_id = node.id;
node.inputs
.iter()
.map(move |input| {
let in_suffix = format!("in{}_{}", input, node_id);
format!(" node{}:out->node{}:{}\n", input, node_id, in_suffix)
})
.collect_vec()
})
.collect::<Vec<_>>()
.join("")
Expand Down Expand Up @@ -521,15 +525,11 @@ mod tests {

#[test]
fn table_graphviz_shows_node_tags() {
let graph = Graph {
nodes: vec![GraphNode {
id: 0,
kind: GraphNodeKind::None,
tag: "Folded RS latch feedback SCC [1, 2, 3, 4]".to_owned(),
..Default::default()
}],
let graph = Graph::from_nodes(vec![GraphNode {
kind: GraphNodeKind::None,
tag: "Folded RS latch feedback SCC [1, 2, 3, 4]".to_owned(),
..Default::default()
};
}]);

let dot = GraphvizBuilder::default()
.with_graph(&graph)
Expand All @@ -541,15 +541,11 @@ mod tests {

#[test]
fn graphviz_can_hide_node_tags() {
let graph = Graph {
nodes: vec![GraphNode {
id: 0,
kind: GraphNodeKind::None,
tag: "debug source tag".to_owned(),
..Default::default()
}],
let graph = Graph::from_nodes(vec![GraphNode {
kind: GraphNodeKind::None,
tag: "debug source tag".to_owned(),
..Default::default()
};
}]);

let dot = GraphvizBuilder::default()
.with_graph(&graph)
Expand Down
39 changes: 10 additions & 29 deletions src/graph/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,18 @@ impl LogicGraph {
where
I: IntoIterator<Item = (String, GraphNodeId)>,
{
let mut next_id = self.graph.max_node_id().map_or(0, |id| id + 1);
for (name, source_id) in outputs {
if self.find_node_by_id(source_id).is_none() {
eyre::bail!("cannot attach output {name}: missing source node {source_id}");
}

self.graph.nodes.push(GraphNode {
id: next_id,
self.graph.add_node(GraphNode {
kind: GraphNodeKind::Output(name),
inputs: vec![source_id],
..Default::default()
});
next_id += 1;
}

self.graph.nodes.sort_by_key(|node| node.id);
self.graph.build_outputs();
self.graph.build_producers();
self.graph.build_consumers();
Expand All @@ -140,7 +136,7 @@ impl LogicGraph {
.filter_map(|node| match &node.kind {
GraphNodeKind::Output(name) => output_source_ids
.contains(&node.inputs[0])
.then_some(name.as_str()),
.then_some(name.clone()),
_ => None,
})
.collect::<Vec<_>>();
Expand All @@ -150,6 +146,7 @@ impl LogicGraph {
}

output_names.sort();
let output_names = output_names.iter().map(String::as_str).collect_vec();
table.select_outputs(&output_names)
}
}
Expand Down Expand Up @@ -310,9 +307,8 @@ fn permuted_output_table_set(
#[derive(Default)]
pub struct LogicGraphBuilder {
stmt: String,
node_id: usize,
ptr: usize,
nodes: Vec<GraphNode>,
graph: Graph,
inputs: HashMap<String, GraphNodeId>,
}

Expand Down Expand Up @@ -347,19 +343,9 @@ impl LogicGraphBuilder {
pub fn build(mut self, output_name: String) -> eyre::Result<LogicGraph> {
self.do_parse(output_name);

let mut graph = Graph {
nodes: self.nodes.clone(),
..Default::default()
};
graph.build_outputs();

Ok(LogicGraph { graph })
}
self.graph.build_outputs();

fn next_id(&mut self) -> usize {
let id = self.node_id;
self.node_id += 1;
id
Ok(LogicGraph { graph: self.graph })
}

fn next_ptr(&mut self) -> usize {
Expand All @@ -369,14 +355,11 @@ impl LogicGraphBuilder {
}

fn new_node(&mut self, kind: GraphNodeKind, inputs: Vec<GraphNodeId>) -> GraphNodeId {
let node = GraphNode {
id: self.next_id(),
self.graph.add_node(GraphNode {
kind,
inputs,
..Default::default()
};
self.nodes.push(node);
self.nodes.last().unwrap().clone().id
})
}

fn new_input_node(&mut self, name: String) -> GraphNodeId {
Expand Down Expand Up @@ -780,8 +763,7 @@ mod tests {

let splits = finish.graph.split_with_outputs();

let mut graph: Graph = (&finish.graph.split_with_outputs()[0]).into();
graph = graph.rebuild_node_ids();
let graph: Graph = (&finish.graph.split_with_outputs()[0]).into();
println!("{}", graph.to_graphviz());

let mut transform = LogicGraphTransformer::new(LogicGraph { graph });
Expand All @@ -792,8 +774,7 @@ mod tests {

#[allow(clippy::needless_range_loop)]
for index in 1..2 {
let mut graph: Graph = (&splits[index]).into();
graph = graph.rebuild_node_ids();
let graph: Graph = (&splits[index]).into();
println!("{}", graph.to_graphviz());

let mut transform = LogicGraphTransformer::new(LogicGraph { graph });
Expand Down
Loading
Loading