Skip to main content

tcb/causality_checker/
petgraph.rs

1use super::causality_checker_structs::CheckNode;
2use crate::graph::middleware::dag::ArrayMap;
3use petgraph::dot::{Config, Dot};
4use petgraph::graph::NodeIndex;
5use petgraph::Graph;
6use std::fs::File;
7use std::io::Write;
8
9/**
10 * Writes to a file the graph built by the causality checker using petgraph format.
11 * This file can be visualized by oppening it in a program that can read this format.
12 * The graph from the checker is returned from the check_causal_delivery function call.
13 *
14 * # Arguments
15 *
16 * `dag` - Graph built by the causality checker.
17 *
18 * `filename` - Filename to write the output into.
19 */
20pub fn plot_graph(dag: ArrayMap<CheckNode>, filename: &String) {
21    let mut graph = Graph::<_, ()>::new();
22    let nmbr_nodes = dag.node_number();
23
24    for i in 0..nmbr_nodes {
25        let node = format!("({}, {})", dag[i].dot.id, dag[i].dot.counter);
26        graph.add_node(node);
27    }
28
29    for i in 0..nmbr_nodes {
30        for succ in &dag[i].successors {
31            graph.add_edge(NodeIndex::new(i), NodeIndex::new(*succ), ());
32        }
33    }
34
35    let dot = Dot::with_config(&graph, &[Config::EdgeNoLabel]);
36    let output = format!("{:?}", dot);
37    let mut file = File::create(filename.clone()).unwrap();
38
39    write!(file, "{}", output).unwrap();
40}