diff options
Diffstat (limited to 'graph/src')
-rw-r--r-- | graph/src/labelled/binary.rs | 47 | ||||
-rw-r--r-- | graph/src/lib.rs | 6 |
2 files changed, 49 insertions, 4 deletions
diff --git a/graph/src/labelled/binary.rs b/graph/src/labelled/binary.rs index 201dda2..4ec7378 100644 --- a/graph/src/labelled/binary.rs +++ b/graph/src/labelled/binary.rs @@ -203,8 +203,51 @@ impl<T: GraphLabel> Graph for PLGraph<T> { unimplemented!("use a `PLGBuilderMut` instead") } - fn print_viz(&self, _filename: &str) -> Result<(), std::io::Error> { - todo!() + fn print_viz(&self, filename: &str) -> Result<(), std::io::Error> { + let filename = format!("output/{filename}"); + + let preamble = "digraph nfa { + fontname=\"Helvetica,Arial,sans-serif\" + node [fontname=\"Helvetica,Arial,sans-serif\"] + edge [fontname=\"Helvetica,Arial,sans-serif\"] + rankdir=LR;\n"; + + let mut post = String::new(); + + for node in self.nodes() { + post.push_str(&format!( + " {node} [label = \"{}\"]\n", + match self.vertex_label(node) { + Ok(Some(label)) => { + format!("{label}") + } + _ => { + " ε ".to_owned() + } + } + )); + } + + for (source, target) in self.edges() { + post.push_str(&format!(" {source} -> {target}\n")); + } + + post.push_str("}\n"); + + let result = format!("{preamble}{post}"); + + if std::fs::metadata(&filename).is_ok() { + std::fs::remove_file(&filename)?; + } + + let mut file = std::fs::File::options() + .write(true) + .create(true) + .open(&filename)?; + + use std::io::Write; + + file.write_all(result.as_bytes()) } } diff --git a/graph/src/lib.rs b/graph/src/lib.rs index 2a0c50d..87f39c0 100644 --- a/graph/src/lib.rs +++ b/graph/src/lib.rs @@ -129,8 +129,10 @@ pub trait Graph: Default { let result = format!("{preamble}{post}"); - if std::fs::metadata(filename).is_ok() { - std::fs::remove_file(filename)?; + let filename = format!("output/{filename}"); + + if std::fs::metadata(&filename).is_ok() { + std::fs::remove_file(&filename)?; } let mut file = std::fs::File::options() |