Skip to content

Commit

Permalink
feat(fkl): add basic nested for style
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 25, 2022
1 parent 6acad2e commit 8b66068
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
8 changes: 8 additions & 0 deletions crates/fkl_codegen_dot/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Graph {
nodes: Vec<Node>,
edges: Vec<Edge>,
node_styles: Vec<String>,
graph_style: Vec<String>,
subgraph: Vec<Subgraph>,
}

Expand All @@ -24,6 +25,7 @@ impl Graph {
nodes: Vec::new(),
edges: Vec::new(),
node_styles: vec![],
graph_style: vec![],
subgraph: Vec::new(),
}
}
Expand Down Expand Up @@ -63,6 +65,8 @@ impl Graph {
pub fn use_default_style(&mut self) {
self.set_shape("box");
self.set_style("filled");

self.graph_style.push("component=true".to_string());
}
}

Expand All @@ -71,6 +75,10 @@ impl Display for Graph {
let space = indent(1);
out.write_str(&format!("digraph {} {{\n", self.name))?;

if !self.graph_style.is_empty() {
out.write_str(&format!("{}{};\n", space, self.graph_style.join("")))?;
}

if !self.node_styles.is_empty() {
out.write_str(&format!("{}node [{}];\n", space, self.node_styles.join(" ")))?;
}
Expand Down
1 change: 1 addition & 0 deletions crates/fkl_codegen_dot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ mod tests {
graph.add_node(Node::new("a"));

assert_eq!(format!("{}", graph), r#"digraph graph_width_rect_shape_style {
component=true;
node [shape=box style=filled];
a [label="a"];
}"#);
Expand Down
4 changes: 4 additions & 0 deletions crates/fkl_codegen_dot/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ impl Node {
pub fn new(name: &str) -> Self {
Node { name: name.to_string(), label: name.to_string() }
}

pub fn label(name: &str, label: &str) -> Self {
Node { name: name.to_string(), label: label.to_string() }
}
}

impl fmt::Display for Node {
Expand Down
44 changes: 43 additions & 1 deletion crates/fkl_parser_wasm/src/dot_gen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use fkl_dot::graph::Graph;
use fkl_dot::node::Node;
use fkl_dot::subgraph::Subgraph;
use fkl_parser::mir::{ConnectionDirection, ContextMap, ContextRelation};
use crate::bc_edge_style;
use crate::bc_edge_style::BcEdgeStyle;
Expand All @@ -9,7 +10,19 @@ pub(crate) fn to_dot(context_map: &ContextMap) -> String {
graph.use_default_style();

for bc in &context_map.contexts {
graph.add_node(Node::new(&bc.name))
// add context.entities to subgraph
let mut subgraph = Subgraph::new(&bc.name, &bc.name);
for aggregate in &bc.aggregates {
let mut aggregate_graph = Subgraph::new(&format!("aggregate_{}", aggregate.name), &format!("Aggregate {}", aggregate.name));

aggregate.entities.iter().for_each(|entity| {
aggregate_graph.add_node(Node::label(&format!("entity_{}", entity.name), &entity.name));
});

subgraph.add_subgraph(aggregate_graph);
}

graph.add_subgraph(subgraph);
}

for rel in &context_map.relations {
Expand Down Expand Up @@ -54,3 +67,32 @@ fn create_graph_edge_style(bc_style: BcEdgeStyle) -> Vec<String> {

style
}

#[cfg(test)]
mod test {
use fkl_parser::parse;
use crate::dot_gen::to_dot;

#[test]
fn nested_entity() {
let input = r#"
ContextMap TicketBooking {
Reservation -> Cinema;
Reservation -> Movie;
Reservation -> User;
}
Context Reservation {
Aggregate Reservation;
}
Aggregate Reservation {
Entity Ticket, Reservation;
}
"#;

let context_map = parse(input).unwrap();
let dot = to_dot(&context_map);
println!("{}", dot);
}
}
1 change: 1 addition & 0 deletions crates/fkl_parser_wasm/www/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ let parser = new FklParser(template);
console.log(parser.parse());
console.log(parser.to_dot());

document.writeln(parser.to_dot());

0 comments on commit 8b66068

Please sign in to comment.