Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up dotrender #33

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/lib/ast/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,7 @@ let pass_one_on_list inputs section_list =

(* ----- public interface ----- *)

let to_list ast =
(* in days of yore this was by section, but for now we do it by executable block *)
List.map
(fun x ->
let _hbid, h = x in
Commandgroup.v (Hyperblock.context h) (Hyperblock.commands h))
ast.nodes
let to_list ast = List.map snd ast.nodes

let of_sharkdown ~template_markdown =
let metadata, sections =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ast/ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ val block_by_id : t -> block_id -> Hyperblock.t option
val find_hyperblock_from_block : t -> Block.t -> Hyperblock.t option
val find_dependencies : t -> block_id -> Hyperblock.t list

val to_list : t -> Commandgroup.t list
val to_list : t -> Hyperblock.t list
(** Convert the AST to a list of command blocks. *)
8 changes: 0 additions & 8 deletions src/lib/ast/commandgroup.ml

This file was deleted.

12 changes: 0 additions & 12 deletions src/lib/ast/commandgroup.mli

This file was deleted.

49 changes: 38 additions & 11 deletions src/lib/dotrenderer.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module DatafileSet = Set.Make (Datafile)

let render_command_to_dot ppf command =
(* let node_style = process_style node.style in *)
(* TODO - some commands like littlejohn get different box styles*)
let process_index = Leaf.id command in
List.iter
(fun datafile ->
Expand All @@ -26,34 +24,63 @@ let render_command_to_dot ppf command =
(Leaf.outputs command);
Format.fprintf ppf "\n"

let render_publish_to_dot ppf command =
let process_index = Leaf.id command in
(* In publish blocks the targets end up as commands where the name is the target *)
Format.fprintf ppf "\tn%d[shape=\"cylinder\",label=\"%s\"];\n" process_index
(Command.name (Leaf.command command));
List.iter
(fun datafile ->
let label =
match Datafile.subpath datafile with
| Some x -> Fmt.str ",label=\"%s\"" x
| None -> ""
in
Format.fprintf ppf "\tn%d->n%d[penwidth=\"2.0\"%s];\n"
(Datafile.id datafile) process_index label)
(Leaf.inputs command)

let datafile_to_dot ppf datafile =
Format.fprintf ppf "\tn%d[shape=\"cylinder\",label=\"%s\"];\n"
(Datafile.id datafile)
(Fpath.to_string (Datafile.path datafile))

let render_ast_to_dot ppf ast : unit =
let render_ast_to_dot ppf hyperblocks : unit =
Format.fprintf ppf "digraph{\n";
List.concat_map
(fun group ->
let commands = Commandgroup.children group in
(fun hb ->
let commands = Ast.Hyperblock.commands hb in
List.concat_map
(fun command ->
let inputs = Leaf.inputs command and outputs = Leaf.outputs command in
List.concat [ inputs; outputs ])
commands)
ast
hyperblocks
|> DatafileSet.of_list
|> DatafileSet.iter (datafile_to_dot ppf);

List.iteri
(fun i group ->
let name = Commandgroup.name group
and commands = Commandgroup.children group in
(fun i hb ->
let kind = Block.kind (Ast.Hyperblock.block hb) in
let name, style =
match kind with
| `Publish -> ("Publish", "bold")
| _ -> (Ast.Hyperblock.context hb, "solid")
and commands = Ast.Hyperblock.commands hb in
Format.fprintf ppf "subgraph \"cluster_%d\" {\n" i;
Format.fprintf ppf "\tstyle = %s\n" style;
Format.fprintf ppf "\tlabel = \"%s\"\n" name;
List.iter (render_command_to_dot ppf) commands;

let renderer =
match kind with
| `Run -> render_command_to_dot
| `Publish -> render_publish_to_dot
| _ -> fun _a _b -> ()
in
List.iter (renderer ppf) commands;

Format.fprintf ppf "}\n")
ast;
hyperblocks;
Format.fprintf ppf "}\n"

let render ~template_markdown =
Expand Down
21 changes: 12 additions & 9 deletions src/test/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ $ python3 something.py /data/something.txt
|}
in
let test = Shark.Ast.of_sharkdown ~template_markdown in
let groups = Shark.Ast.to_list test in
Alcotest.(check int) "Single command group expected" 1 (List.length groups);
let leaves = Shark.Commandgroup.children (List.nth groups 0) in
let hyperblocks = Shark.Ast.to_list test in
Alcotest.(check int)
"Single command group expected" 1 (List.length hyperblocks);
let leaves = Shark.Ast.Hyperblock.commands (List.nth hyperblocks 0) in
Alcotest.(check int) "Single command expected" 1 (List.length leaves)

let test_multicommand_block () =
Expand All @@ -24,9 +25,10 @@ $ python3 else.py /data/something.txt
|}
in
let test = Shark.Ast.of_sharkdown ~template_markdown in
let groups = Shark.Ast.to_list test in
Alcotest.(check int) "Single command group expected" 1 (List.length groups);
let leaves = Shark.Commandgroup.children (List.nth groups 0) in
let hyperblocks = Shark.Ast.to_list test in
Alcotest.(check int)
"Single command group expected" 1 (List.length hyperblocks);
let leaves = Shark.Ast.Hyperblock.commands (List.nth hyperblocks 0) in
Alcotest.(check int) "Single command expected" 2 (List.length leaves)

let test_single_block_no_obvious_side_effects () =
Expand All @@ -38,9 +40,10 @@ $ python3 something.py
|}
in
let test = Shark.Ast.of_sharkdown ~template_markdown in
let groups = Shark.Ast.to_list test in
Alcotest.(check int) "Single command group expected" 1 (List.length groups);
let leaves = Shark.Commandgroup.children (List.nth groups 0) in
let hyperblocks = Shark.Ast.to_list test in
Alcotest.(check int)
"Single command group expected" 1 (List.length hyperblocks);
let leaves = Shark.Ast.Hyperblock.commands (List.nth hyperblocks 0) in
Alcotest.(check int) "Single command expected" 1 (List.length leaves)

let tests =
Expand Down
25 changes: 25 additions & 0 deletions src/test/expect/test_dot.expected
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,30 @@ digraph{
n51[shape="cylinder",label="/data/tmf/123/pairs"];
n53[shape="cylinder",label="/data/tmf/123/additionality.csv"];
subgraph "cluster_0" {
style = solid
label = "JRC"
n4[shape="box",label="methods.inputs.download_jrc_data"];
n4->n2[penwidth="2.0"];
n4->n3[penwidth="2.0"];

}
subgraph "cluster_1" {
style = solid
label = "JRC"
n3->n6[penwidth="2.0"];
n6[shape="box",label="methods.inputs.generate_fine_circular_coverage"];
n6->n5[penwidth="2.0"];

}
subgraph "cluster_2" {
style = solid
label = "Ecoregions"
n8[shape="box",label="methods.inputs.download_shapefiles"];
n8->n7[penwidth="2.0"];

}
subgraph "cluster_3" {
style = solid
label = "Ecoregions"
n7->n10[penwidth="2.0"];
n3->n10[penwidth="2.0"];
Expand All @@ -57,12 +61,14 @@ subgraph "cluster_3" {

}
subgraph "cluster_4" {
style = solid
label = "ACCESS"
n12[shape="box",label="methods.inputs.download_accessibility"];
n12->n11[penwidth="2.0"];

}
subgraph "cluster_5" {
style = solid
label = "ACCESS"
n11->n14[penwidth="2.0"];
n3->n14[penwidth="2.0"];
Expand All @@ -71,26 +77,30 @@ subgraph "cluster_5" {

}
subgraph "cluster_6" {
style = solid
label = "Country boarders"
n16[shape="box",label="methods.inputs.osm_countries"];
n16->n15[penwidth="2.0"];

}
subgraph "cluster_7" {
style = solid
label = "Make variations on project shapes"
n0->n18[penwidth="2.0"];
n18[shape="box",label="methods.inputs.generate_boundary"];
n18->n17[penwidth="2.0"];

}
subgraph "cluster_8" {
style = solid
label = "Make variations on project shapes"
n0->n20[penwidth="2.0"];
n20[shape="box",label="methods.inputs.generate_leakage"];
n20->n19[penwidth="2.0"];

}
subgraph "cluster_9" {
style = solid
label = "Make LUC tiles"
n17->n22[penwidth="2.0"];
n3->n22[penwidth="2.0"];
Expand All @@ -99,19 +109,22 @@ subgraph "cluster_9" {

}
subgraph "cluster_10" {
style = solid
label = "GEDI data"
n17->n24[penwidth="2.0"];
n24[shape="box",label="methods.inputs.download_gedi_data"];
n24->n23[penwidth="2.0"];

}
subgraph "cluster_11" {
style = solid
label = "GEDI data"
n23->n25[penwidth="2.0"];
n25[shape="box",label="methods.inputs.import_gedi_data"];

}
subgraph "cluster_12" {
style = solid
label = "GEDI data"
n17->n27[penwidth="2.0"];
n21->n27[penwidth="2.0"];
Expand All @@ -120,6 +133,7 @@ subgraph "cluster_12" {

}
subgraph "cluster_13" {
style = solid
label = "GEDI data"
n0->n29[penwidth="2.0"];
n15->n29[penwidth="2.0"];
Expand All @@ -128,6 +142,7 @@ subgraph "cluster_13" {

}
subgraph "cluster_14" {
style = solid
label = "GEDI data"
n0->n31[penwidth="2.0"];
n28->n31[penwidth="2.0"];
Expand All @@ -139,6 +154,7 @@ subgraph "cluster_14" {

}
subgraph "cluster_15" {
style = solid
label = "Elevation and slope data"
n0->n34[penwidth="2.0"];
n30->n34[penwidth="2.0"];
Expand All @@ -148,13 +164,15 @@ subgraph "cluster_15" {

}
subgraph "cluster_16" {
style = solid
label = "Elevation and slope data"
n33->n36[penwidth="2.0"];
n36[shape="box",label="methods.inputs.generate_slope"];
n36->n35[penwidth="2.0"];

}
subgraph "cluster_17" {
style = solid
label = "Elevation and slope data"
n3->n38[penwidth="2.0"];
n33->n38[penwidth="2.0"];
Expand All @@ -168,6 +186,7 @@ subgraph "cluster_17" {

}
subgraph "cluster_18" {
style = solid
label = "Country raster"
n3->n42[penwidth="2.0"];
n30->n42[penwidth="2.0"];
Expand All @@ -177,6 +196,7 @@ subgraph "cluster_18" {

}
subgraph "cluster_19" {
style = solid
label = "Calculate set K"
n0->n44[penwidth="2.0"];
n3->n44[penwidth="2.0"];
Expand All @@ -191,6 +211,7 @@ subgraph "cluster_19" {

}
subgraph "cluster_20" {
style = solid
label = "Calculate set M"
n43->n46[penwidth="2.0"];
n30->n46[penwidth="2.0"];
Expand All @@ -206,13 +227,15 @@ subgraph "cluster_20" {

}
subgraph "cluster_21" {
style = solid
label = "Calculate set M"
n45->n48[penwidth="2.0"];
n48[shape="box",label="methods.matching.build_m_raster"];
n48->n47[penwidth="2.0"];

}
subgraph "cluster_22" {
style = solid
label = "Calculate set M"
n47->n50[penwidth="2.0"];
n30->n50[penwidth="2.0"];
Expand All @@ -228,6 +251,7 @@ subgraph "cluster_22" {

}
subgraph "cluster_23" {
style = solid
label = "Find pairs"
n43->n52[penwidth="2.0"];
n49->n52[penwidth="2.0"];
Expand All @@ -236,6 +260,7 @@ subgraph "cluster_23" {

}
subgraph "cluster_24" {
style = solid
label = "Calculate additionality"
n0->n54[penwidth="2.0"];
n26->n54[penwidth="2.0"];
Expand Down
Loading