sodg/dot.rs
1// Copyright (c) 2022-2023 Yegor Bugayenko
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included
11// in all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::Sodg;
22use itertools::Itertools;
23
24impl Sodg {
25 /// Print SODG as a DOT graph.
26 ///
27 /// For example, for this code:
28 ///
29 /// ```
30 /// use sodg::Hex;
31 /// use sodg::Sodg;
32 /// let mut g = Sodg::empty();
33 /// g.add(0).unwrap();
34 /// g.put(0, &Hex::from_str_bytes("hello")).unwrap();
35 /// g.add(1).unwrap();
36 /// g.bind(0, 1, "foo").unwrap();
37 /// g.bind(0, 1, "bar").unwrap();
38 /// let dot = g.to_dot();
39 /// println!("{}", dot);
40 /// ```
41 ///
42 /// The printout will look approximately like this:
43 ///
44 /// ```text
45 /// digraph {
46 /// v0[shape=circle,label="ν0"];
47 /// v0 -> v1 [label="bar"];
48 /// v0 -> v1 [label="foo"];
49 /// v1[shape=circle,label="ν1"];
50 /// }
51 /// ```
52 #[must_use]
53 pub fn to_dot(&self) -> String {
54 let mut lines: Vec<String> = vec![];
55 lines.push(
56 "/* Render it at https://dreampuf.github.io/GraphvizOnline/ */
57digraph {
58 node [fixedsize=true,width=1,fontname=\"Arial\"];
59 edge [fontname=\"Arial\"];"
60 .to_string(),
61 );
62 for (v, vtx) in self
63 .vertices
64 .iter()
65 .sorted_by_key(|(v, _)| <&u32>::clone(v))
66 {
67 lines.push(format!(
68 " v{v}[shape=circle,label=\"ν{v}\"{}]; {}",
69 if vtx.data.is_empty() {
70 ""
71 } else {
72 ",color=\"#f96900\""
73 },
74 if vtx.data.is_empty() {
75 String::new()
76 } else {
77 format!("/* {} */", vtx.data)
78 }
79 ));
80 for e in vtx.edges.iter().sorted_by_key(|e| e.a.clone()) {
81 lines.push(format!(
82 " v{v} -> v{} [label=\"{}\"{}{}];",
83 e.to,
84 e.a,
85 if e.a.starts_with('ρ') || e.a.starts_with('σ') {
86 ",color=gray,fontcolor=gray"
87 } else {
88 ""
89 },
90 if e.a.starts_with('π') {
91 ",style=dashed"
92 } else {
93 ""
94 }
95 ));
96 }
97 }
98 lines.push("}\n".to_string());
99 lines.join("\n")
100 }
101}
102
103#[cfg(test)]
104use crate::Hex;
105
106#[cfg(test)]
107use anyhow::Result;
108
109#[test]
110fn simple_graph_to_dot() -> Result<()> {
111 let mut g = Sodg::empty();
112 g.add(0)?;
113 g.put(0, &Hex::from_str_bytes("hello"))?;
114 g.add(1)?;
115 g.bind(0, 1, "foo")?;
116 let dot = g.to_dot();
117 assert!(dot.contains("shape=circle,label=\"ν0\""));
118 Ok(())
119}