bookdata/graph/
gml.rs

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
use std::fs::File;
use std::io::prelude::*;
use std::io::BufWriter;
use std::path::Path;

use anyhow::Result;
use log::*;
use petgraph::visit::*;

use crate::ids::codes::ns_of_book_code;

use super::{IdGraph, IdNode};

fn gml_begin<W: Write>(w: &mut W) -> Result<()> {
    writeln!(w, "graph [")?;
    Ok(())
}

fn gml_end<W: Write>(w: &mut W) -> Result<()> {
    writeln!(w, "]")?;
    Ok(())
}

fn gml_node<W: Write>(w: &mut W, graph: &IdGraph, v: IdNode) -> Result<()> {
    let node = graph.node_weight(v).unwrap();
    writeln!(w, "  node [")?;
    writeln!(w, "    id {}", node.code)?;
    let ns = ns_of_book_code(node.code).unwrap();
    writeln!(w, "    namespace \"{}\"", ns.name())?;
    if let Some(ref l) = node.label {
        writeln!(w, "    label \"{}\"", l)?;
    }
    writeln!(w, "  ]")?;
    Ok(())
}

fn gml_edge<W: Write>(w: &mut W, graph: &IdGraph, sv: IdNode, dv: IdNode) -> Result<()> {
    let src = graph.node_weight(sv).unwrap();
    let dst = graph.node_weight(dv).unwrap();
    writeln!(w, "  edge [")?;
    writeln!(w, "    source {}", src.code)?;
    writeln!(w, "    target {}", dst.code)?;
    writeln!(w, "  ]")?;
    Ok(())
}

/// Save a graph to a GML file.
pub fn save_gml<P: AsRef<Path>>(graph: &IdGraph, path: P) -> Result<()> {
    info!("saving graph to {}", path.as_ref().to_string_lossy());
    let out = File::create(path)?;
    let mut out = BufWriter::new(out);
    gml_begin(&mut out)?;
    for n in graph.node_indices() {
        gml_node(&mut out, graph, n)?;
    }
    for e in graph.edge_references() {
        gml_edge(&mut out, graph, e.source(), e.target())?;
    }
    gml_end(&mut out)?;
    Ok(())
}