Skip to content

Commit

Permalink
graphviz output
Browse files Browse the repository at this point in the history
  • Loading branch information
lorepozo committed Mar 11, 2017
1 parent 0f4d703 commit 690c5bb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/knowledge.rs
@@ -1,5 +1,6 @@
extern crate rand;

use std::io::Write;
use std::cmp::{min, max};
use std::collections::{HashSet, HashMap};
use std::cell::RefCell;
Expand Down Expand Up @@ -333,6 +334,37 @@ impl Network {
current_epoch: epoch,
}
}
/// dot writes the network in the graphviz DOT language.
fn dot<W>(&self, w: &mut W) -> ::std::io::Result<()>
where W: Write
{
let net = self.net.borrow();
let mut body = String::new();
for id in 0..net.graph.len() {
body.push_str(format!(" N{} [shape=box,label={:?}];\n",
id,
&net.graph[id].data.as_str().clone())
.as_str());
}
body.pop();
let mut edges = net.graph
.iter()
.flat_map(|item| {
let id = item.id;
item.adj.iter().map(move |&o| {
let mut v = vec![id, o];
v.sort();
(v[0], v[1])
})
})
.collect::<Vec<_>>();
edges.sort();
edges.dedup();
for (i, o) in edges {
body.push_str(format!("\n N{} -- N{};", i, o).as_str());
}
write!(w, "graph G {{\n{}\n}}\n", body)
}
}

/// MechanismRegistry maintains a set of mechanisms used by the knowledge
Expand Down Expand Up @@ -394,4 +426,10 @@ impl<'a> Skn<'a> {
}
}
}
/// dot writes the network in the graphviz DOT language.
pub fn dot<W>(&self, w: &mut W) -> ::std::io::Result<()>
where W: Write
{
self.network.dot(w)
}
}
3 changes: 2 additions & 1 deletion src/main.rs
Expand Up @@ -15,7 +15,8 @@ fn main() {
let mut skn = knowledge::Skn::new(embryo, t);
skn.register("ec", &mech);
skn.run();
println!("{:?}", skn)
let mut w = ::std::io::stdout();
skn.dot(&mut w).unwrap();
}

#[cfg(test)]
Expand Down

0 comments on commit 690c5bb

Please sign in to comment.