use std::fmt::{Display, Formatter, Result};
use crate::{egraph::EGraph, expr::Language};
pub struct Dot<'a, L: Language, M> {
egraph: &'a EGraph<L, M>,
}
impl<'a, L: Language, M> Dot<'a, L, M> {
pub fn new(egraph: &EGraph<L, M>) -> Dot<L, M> {
Dot { egraph }
}
}
impl<'a, L: Language, M> Display for Dot<'a, L, M> {
fn fmt(&self, f: &mut Formatter) -> Result {
writeln!(f, "digraph {{")?;
writeln!(f, " compound=true")?;
for class in self.egraph.classes() {
writeln!(f, " subgraph cluster_{} {{", class.id)?;
writeln!(f, " style=dotted")?;
for (i, node) in class.iter().enumerate() {
writeln!(f, " {}.{}[label = \"{}\"]", class.id, i, node.symbol())?;
}
writeln!(f, " }}")?;
}
for class in self.egraph.classes() {
for (i_in_class, node) in class.iter().enumerate() {
for (arg_i, child) in node.children().iter().enumerate() {
let child_leader = self.egraph.just_find(*child);
writeln!(
f,
" {}.{} -> {}.0 [lhead = cluster_{}, label = {}]",
class.id, i_in_class, child, child_leader, arg_i
)?;
}
}
}
write!(f, "}}")
}
}