|
| 1 | +use crate::runners; |
| 2 | + |
| 3 | +pub fn format(_metta_output: String) { |
| 4 | + //check if there are tree in metta output if there is tree format them |
| 5 | + for line in _metta_output.lines() { |
| 6 | + if line.starts_with("[(TreeNode") { |
| 7 | + format_tree(&line); |
| 8 | + } else { |
| 9 | + println!("{}", line); |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +fn format_tree(tree: &str) { |
| 15 | + fn simplify_tree_node(input: &str) -> String { |
| 16 | + let simplified_string = input.split(")").collect::<Vec<&str>>()[0].to_string() + ")"; |
| 17 | + simplified_string |
| 18 | + } |
| 19 | + |
| 20 | + fn remove_brackets(input: &str) -> String { |
| 21 | + input.replace("[", "").replace("]", "").trim().to_string() |
| 22 | + } |
| 23 | + |
| 24 | + let main_metta_functions = format!( |
| 25 | + " |
| 26 | + (:getGuardSet (-> Tree (List Tree))) |
| 27 | + (= (getGuardSet (TreeNode $nodeVal $guardSet $children)) $guardSet) |
| 28 | + (= (head (Cons $x $xs)) $x) |
| 29 | + (= (tail (Cons $x $xs)) $xs) |
| 30 | + " |
| 31 | + ); |
| 32 | + |
| 33 | + let get_guardset = |input: &str| -> String { |
| 34 | + let tree = remove_brackets(&input); |
| 35 | + let getter_code = format!("{}\n !(getGuardSet {}) ", main_metta_functions, tree); |
| 36 | + let result = runners::python::run(None, &getter_code); |
| 37 | + remove_brackets(&result) |
| 38 | + }; |
| 39 | + |
| 40 | + let get_tree_head = |input: &str| -> String { |
| 41 | + let tree = remove_brackets(&input); |
| 42 | + let getter_code = format!("{}\n !(head {}) ", main_metta_functions, tree); |
| 43 | + let result = runners::python::run(None, &getter_code); |
| 44 | + remove_brackets(&result) |
| 45 | + }; |
| 46 | + |
| 47 | + let get_tree_tail = |input: &str| -> String { |
| 48 | + let tree = remove_brackets(&input); |
| 49 | + let getter_code = format!("{}\n !(tail {}) ", main_metta_functions, tree); |
| 50 | + let result = runners::python::run(None, &getter_code); |
| 51 | + remove_brackets(&result) |
| 52 | + }; |
| 53 | + |
| 54 | + fn print_tree( |
| 55 | + tree: &str, |
| 56 | + indent: u32, |
| 57 | + get_guardset: &dyn Fn(&str) -> String, |
| 58 | + get_tree_head: &dyn Fn(&str) -> String, |
| 59 | + get_tree_tail: &dyn Fn(&str) -> String, |
| 60 | + ) { |
| 61 | + if tree == "Nil" { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + let current_node = simplify_tree_node(&tree); |
| 66 | + let mut children = get_guardset(&tree); |
| 67 | + |
| 68 | + if indent == 0 { |
| 69 | + println!("{}{}", " ".repeat(indent as usize), current_node); |
| 70 | + } else { |
| 71 | + println!("{}{}{}", " ".repeat(indent as usize), "├─", current_node); |
| 72 | + } |
| 73 | + |
| 74 | + if children != "Nil" { |
| 75 | + println!("{}{}", " ".repeat(indent as usize), "└──GuardSets's"); |
| 76 | + } |
| 77 | + |
| 78 | + while children != "Nil" { |
| 79 | + let child = get_tree_head(&children); |
| 80 | + if child != "Nil" { |
| 81 | + print_tree( |
| 82 | + &child, |
| 83 | + indent + 4, |
| 84 | + &get_guardset, |
| 85 | + &get_tree_head, |
| 86 | + &get_tree_tail, |
| 87 | + ); |
| 88 | + } |
| 89 | + children = get_tree_tail(&children); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + print_tree(tree, 0, &get_guardset, &get_tree_head, &get_tree_tail); |
| 94 | +} |
0 commit comments