-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
315 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ Cargo.lock | |
# Added by cargo | ||
|
||
/target | ||
|
||
# Generated .dot files for Graphviz | ||
*.dot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use crate::manager::Result; | ||
|
||
pub trait Dot { | ||
fn draw(&self, writer: &mut DotWriter); | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct DotWriter { | ||
nodes: Vec<(usize, NodeType)>, | ||
edges: Vec<((usize, Option<usize>), usize)>, | ||
} | ||
|
||
pub enum NodeType { | ||
Box(String), | ||
Circle(u32), | ||
Record(String, String), | ||
} | ||
|
||
impl NodeType { | ||
fn shape(&self) -> String { | ||
let shape_type = match self { | ||
NodeType::Box(_) => "box", | ||
NodeType::Record(_, _) => "record", | ||
NodeType::Circle(_) => "circle", | ||
} | ||
.to_owned(); | ||
|
||
format!("shape={shape_type}") | ||
} | ||
|
||
fn label(&self) -> String { | ||
match self { | ||
NodeType::Record(fst, snd) => format!("label=\"<f0> {fst} | <f1> {snd}\""), | ||
NodeType::Circle(label) => format!("label=\"{label}\""), | ||
NodeType::Box(_) => String::new(), | ||
} | ||
} | ||
|
||
fn metadata() -> String { | ||
"height=.25 width=.2".to_owned() | ||
} | ||
} | ||
|
||
impl DotWriter { | ||
#[must_use] | ||
pub fn new() -> DotWriter { | ||
DotWriter::default() | ||
} | ||
|
||
pub fn add_node(&mut self, node_idx: usize, node_type: NodeType) { | ||
self.nodes.push((node_idx, node_type)); | ||
} | ||
|
||
pub fn add_edge(&mut self, from: (usize, Option<usize>), to: usize) { | ||
self.edges.push((from, to)); | ||
} | ||
|
||
/// # Errors | ||
/// Function returns an error if the writing to a file or flushing fails. | ||
pub fn write(&self, writer: &mut dyn std::io::Write) -> Result<()> { | ||
write!(writer, "digraph sdd {{\n overlap=false")?; | ||
|
||
for (node, node_type) in &self.nodes { | ||
write!( | ||
writer, | ||
"\n {} [{} {} {}]", | ||
node, | ||
node_type.shape(), | ||
node_type.label(), | ||
NodeType::metadata(), | ||
)?; | ||
} | ||
|
||
for ((from, from_child), to) in &self.edges { | ||
if let Some(from_child) = from_child { | ||
// TODO: Make the edge begin in the middle of the child. | ||
write!(writer, "\n {from}:f{from_child} -> {to} [arrowsize=.50]")?; | ||
} else { | ||
write!(writer, "\n {from} -> {to} [arrowsize=.50]")?; | ||
} | ||
} | ||
|
||
write!(writer, "\n}}")?; | ||
writer.flush()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ pub mod options; | |
pub mod sdd; | ||
#[macro_use] | ||
pub mod util; | ||
pub mod dot_writer; | ||
pub mod vtree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.