Skip to content

Commit f87f1aa

Browse files
authored
Merge pull request #23 from egraphs-good/saulshanabrook-patch-1
Allow arbitrary class data
2 parents 0581da5 + 51e0916 commit f87f1aa

File tree

6 files changed

+113
-70
lines changed

6 files changed

+113
-70
lines changed

src/graphviz.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt, io::Write};
1+
use std::{collections::HashMap, fmt, io::Write};
22

33
use crate::EGraph;
44
use graphviz_rust::{
@@ -39,16 +39,22 @@ impl EGraph {
3939
// and create mapping from each node ID to its class
4040
let mut node_to_class = std::collections::HashMap::new();
4141
for (node_id, node) in &self.nodes {
42-
let typ = self
43-
.class_data
44-
.get(&node.eclass)
45-
.and_then(|data| data.typ.clone());
42+
let class_data = self.class_data.get(&node.eclass);
43+
let typ = class_data.and_then(|data| data.typ.clone());
44+
let extra = class_data.and_then(|data| {
45+
if data.extra.is_empty() {
46+
None
47+
} else {
48+
Some(data.extra.clone())
49+
}
50+
});
4651
node_to_class.insert(node_id.clone(), node.eclass.clone());
4752
class_nodes
4853
.entry(typ)
4954
.or_insert_with(std::collections::HashMap::new)
5055
.entry(node.eclass.clone())
51-
.or_insert_with(Vec::new)
56+
.or_insert_with(|| (extra, Vec::new()))
57+
.1
5258
.push((node_id.clone(), node));
5359
}
5460
// 2. Start with configuration
@@ -84,7 +90,7 @@ impl EGraph {
8490
let next_color = (typ_colors.len() + INITIAL_COLOR) % N_COLORS;
8591
let color = typ_colors.entry(typ).or_insert(next_color);
8692
stmts.push(stmt!(attr!("fillcolor", color)));
87-
for (class_id, nodes) in class_to_node {
93+
for (class_id, (extra, nodes)) in class_to_node {
8894
let mut inner_stmts = vec![];
8995

9096
// Add nodes
@@ -113,14 +119,17 @@ impl EGraph {
113119
let outer_subgraph_id = quote(&format!("outer_{subgraph_id}"));
114120
let quoted_subgraph_id = quote(&subgraph_id);
115121

122+
let mut inner_subgraph = subgraph!(quoted_subgraph_id; subgraph!("", inner_stmts));
123+
if let Some(extra) = extra {
124+
inner_subgraph
125+
.add_stmt(stmt!(SubgraphAttributes::label(class_html_label(extra))));
126+
}
127+
// Nest in empty sub-graph so that we can use rank=same
128+
// https://stackoverflow.com/a/55562026/907060
116129
let subgraph = subgraph!(outer_subgraph_id;
117130
// Disable label for now, to reduce size
118131
// NodeAttributes::label(subgraph_html_label(&typ)),
119-
120-
// Nest in empty sub-graph so that we can use rank=same
121-
// https://stackoverflow.com/a/55562026/907060
122-
subgraph!(quoted_subgraph_id; subgraph!("", inner_stmts)),
123-
132+
inner_subgraph,
124133
// Make outer subgraph a cluster but make it invisible, so just used for padding
125134
// https://forum.graphviz.org/t/how-to-add-space-between-clusters/1209/3
126135
SubgraphAttributes::style(quote("invis")),
@@ -166,6 +175,20 @@ fn html_label(label: &str, n_args: usize) -> String {
166175
)
167176
}
168177

178+
fn class_html_label(extra: HashMap<String, String>) -> String {
179+
let rows = extra.iter().map(|(key, value)| {
180+
format!(
181+
"<TR><TD ALIGN=\"RIGHT\">{}</TD><TD ALIGN=\"LEFT\">{}</TD></TR>",
182+
Escape(key),
183+
Escape(value)
184+
)
185+
});
186+
format!(
187+
"<<TABLE BORDER=\"0\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"2\">{}</TABLE>>",
188+
rows.collect::<Vec<String>>().join("")
189+
)
190+
}
191+
169192
/// Adds double quotes and escapes the quotes in the string
170193
fn quote(s: &str) -> String {
171194
format!("{s:?}")

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod graphviz;
33

44
mod algorithms;
55

6+
use std::collections::HashMap;
67
use std::sync::Arc;
78

89
use indexmap::{map::Entry, IndexMap};
@@ -196,4 +197,7 @@ pub struct Class {
196197
pub struct ClassData {
197198
#[cfg_attr(feature = "serde", serde(rename = "type"))]
198199
pub typ: Option<String>,
200+
201+
#[cfg_attr(feature = "serde", serde(flatten))]
202+
pub extra: HashMap<String, String>,
199203
}

tests-viz/tiny-inlined-saturated.svg

Lines changed: 23 additions & 19 deletions
Loading

0 commit comments

Comments
 (0)