1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
use std::{
collections::{hash_map::DefaultHasher, BTreeMap, HashSet},
hash::BuildHasher,
};
use crate::{Identifiable, Visitor};
#[derive(Debug)]
struct Node {
id: usize,
name: &'static str,
edges: Vec<usize>,
operation: Option<&'static str>,
dependency: Option<&'static str>,
}
impl Node {
fn node_identifier(&self) -> String {
format!("node_{}", self.id)
}
}
/// A [Visitor] which builds a `Graphviz` representation of a given graph.
///
/// ```
/// # use std::{collections::HashSet, hash::Hash, rc::Rc};
/// #
/// # use depends::{
/// # Dependency, HashValue, Resolve, UpdateDerived, UpdateInput,
/// # NodeHash, InputNode, DerivedNode, TargetMut, SingleRef,
/// # error::{EarlyExit, ResolveResult},
/// # derives::{Dependencies, Value, Operation},
/// # };
/// #
/// # #[derive(Value, Default, Hash, Debug)]
/// # pub struct NumberValue {
/// # value: i32,
/// # }
/// #
/// # impl UpdateInput for NumberValue {
/// # type Update = i32;
/// #
/// # fn update_mut(&mut self, input: Self::Update) {
/// # self.value = input;
/// # }
/// # }
/// # #[derive(Operation)]
/// # struct Add;
/// #
/// # impl UpdateDerived for Add {
/// # type Input<'a> = TwoNumbersRef<'a> where Self: 'a;
/// # type Target<'a> = TargetMut<'a, NumberValue> where Self: 'a;
/// #
/// # fn update_derived(
/// # TwoNumbersRef { left, right }: TwoNumbersRef<'_>,
/// # mut target: TargetMut<'_, NumberValue>,
/// # ) -> Result<(), EarlyExit> {
/// # target.value = left.value + right.value;
/// # Ok(())
/// # }
/// # }
/// #
/// # #[derive(Operation)]
/// # struct Square;
/// #
/// # impl UpdateDerived for Square{
/// # type Input<'a> = SingleRef<'a, NumberValue> where Self: 'a;
/// # type Target<'a> = TargetMut<'a, NumberValue> where Self: 'a;
/// #
/// # fn update_derived(
/// # input: Self::Input<'_>,
/// # mut target: TargetMut<'_, NumberValue>,
/// # ) -> Result<(), EarlyExit> {
/// # target.value = input.value.pow(2);
/// # Ok(())
/// # }
/// # }
/// #
/// # #[derive(Dependencies)]
/// # pub struct TwoNumbers {
/// # left: NumberValue,
/// # right: NumberValue,
/// # }
/// #
/// use depends::graphviz::GraphvizVisitor;
///
/// // Compose a graph.
/// let left = InputNode::new(NumberValue::default());
/// let left_squared = DerivedNode::new(
/// Dependency::new(Rc::clone(&left)),
/// Square,
/// NumberValue::default(),
/// );
/// let right = InputNode::new(NumberValue::default());
/// let two_numbers = TwoNumbers::init(left_squared, right);
/// let sum = DerivedNode::new(two_numbers, Add, NumberValue::default());
/// let sum_squared = DerivedNode::new(Dependency::new(sum), Square, NumberValue::default());
///
/// let mut visitor = GraphvizVisitor::new();
///
/// // Resolve the graph with this visitor.
/// // Be sure NOT to use `resolve_root`, as this will clear the visitor's state.
/// sum_squared.resolve(&mut visitor).unwrap();
///
/// println!("{}", visitor.render().unwrap());
/// // A Graphviz representation is now available on the visitor!
/// assert_eq!(
/// visitor.render().unwrap(),
/// r#"
/// digraph Dag {
/// node_0 [label="NumberValue"];
/// node_1 [label="NumberValue"];
/// node_0 -> node_1 [label="Square"];
/// node_2 [label="NumberValue"];
/// node_3 [label="NumberValue"];
/// node_1 -> node_3 [label="Add", class="TwoNumbersDep"];
/// node_2 -> node_3 [label="Add", class="TwoNumbersDep"];
/// node_4 [label="NumberValue"];
/// node_3 -> node_4 [label="Square"];
/// }
/// "#
/// .trim()
/// );
/// ```
#[derive(Debug, Default)]
pub struct GraphvizVisitor {
visitor: HashSet<usize>,
nodes: BTreeMap<usize, Node>,
stack: Vec<usize>,
}
impl GraphvizVisitor {
pub fn new() -> Self {
Self::default()
}
/// Render the visited graph to Graphviz DOT format. Returns [Option::None]
/// if no graph has been visited.
pub fn render(&self) -> Option<String> {
if self.nodes.is_empty() {
None
} else {
let mut lines = Vec::new();
lines.push(String::from("digraph Dag {"));
self.nodes.values().for_each(|n| {
lines.push(format!(" {} [label=\"{}\"];", n.node_identifier(), n.name));
// TODO: it would be nice to make this type-system enforced
// at the moment, it's not guaranteed by the type system
// that nodes.len() == 0 iff operation.is_none()
// this would likely be done by splitting `touch` in to
// `touch_input` and `touch_derived`
if let Some(op) = n.operation {
let class = n
.dependency
.map(|d| format!(", class=\"{}\"", d))
.unwrap_or_else(String::new);
let edge_label = format!("[label=\"{}\"{}]", op, class);
n.edges.iter().for_each(|c| {
lines.push(format!(
" {} -> {} {};",
self.nodes[c].node_identifier(),
n.node_identifier(),
edge_label
));
});
}
});
lines.push(String::from("}"));
Some(lines.join("\n"))
}
}
}
impl Visitor for GraphvizVisitor {
type Hasher = DefaultHasher;
fn visit<N>(&mut self, node: &N) -> bool
where
N: Identifiable,
{
self.visitor.visit(node)
}
fn clear(&mut self) {
self.visitor.clear();
self.nodes.clear();
}
fn touch<N>(&mut self, node: &N, operation: Option<&'static str>)
where
N: Identifiable,
{
self.stack.push(node.id());
self.nodes.entry(node.id()).or_insert_with(|| {
Node {
id: node.id(),
name: N::name(),
edges: Vec::default(),
operation,
dependency: None,
}
});
}
fn touch_dependency_group(&mut self, dep: &'static str) {
let last = self.stack.last().unwrap();
if let Some(n) = self.nodes.get_mut(last) {
n.dependency = Some(dep);
}
}
fn leave<N>(&mut self, node: &N)
where
N: Identifiable,
{
let last = self.stack.pop().unwrap();
assert_eq!(last, node.id());
if let Some(parent) = self.stack.last() {
if let Some(n) = self.nodes.get_mut(parent) {
n.edges.push(last)
}
}
}
fn hasher(&self) -> Self::Hasher {
self.visitor.hasher().build_hasher()
}
}