diff --git a/metta-run/src/formatters/commands.rs b/metta-run/src/formatters/commands.rs index 0cd114c..0b3c82c 100644 --- a/metta-run/src/formatters/commands.rs +++ b/metta-run/src/formatters/commands.rs @@ -1,6 +1,6 @@ use clap::Subcommand; -use super::{binary_tree_formatter, constraint_tree_formatter}; +use super::{binary_tree_formatter, constraint_tree_formatter, guardset_tree_formatter}; #[derive(Subcommand, Debug, Clone)] pub enum FormatterCommands { @@ -9,11 +9,15 @@ pub enum FormatterCommands { #[command(name = "fct", about = "Format constraint tree")] Fct, + + #[command(name = "fgt", about = "Format constraint tree guardset")] + Fgt, } pub fn format(metta_output: String, command: FormatterCommands) { match command { FormatterCommands::Fbt => binary_tree_formatter::format(metta_output), FormatterCommands::Fct => constraint_tree_formatter::format(metta_output), + FormatterCommands::Fgt => guardset_tree_formatter::format(metta_output), } } diff --git a/metta-run/src/formatters/guardset_tree_formatter.rs b/metta-run/src/formatters/guardset_tree_formatter.rs new file mode 100644 index 0000000..9952fc4 --- /dev/null +++ b/metta-run/src/formatters/guardset_tree_formatter.rs @@ -0,0 +1,94 @@ +use crate::runners; + +pub fn format(_metta_output: String) { + //check if there are tree in metta output if there is tree format them + for line in _metta_output.lines() { + if line.starts_with("[(TreeNode") { + format_tree(&line); + } else { + println!("{}", line); + } + } +} + +fn format_tree(tree: &str) { + fn simplify_tree_node(input: &str) -> String { + let simplified_string = input.split(")").collect::>()[0].to_string() + ")"; + simplified_string + } + + fn remove_brackets(input: &str) -> String { + input.replace("[", "").replace("]", "").trim().to_string() + } + + let main_metta_functions = format!( + " + (:getGuardSet (-> Tree (List Tree))) + (= (getGuardSet (TreeNode $nodeVal $guardSet $children)) $guardSet) + (= (head (Cons $x $xs)) $x) + (= (tail (Cons $x $xs)) $xs) + " + ); + + let get_guardset = |input: &str| -> String { + let tree = remove_brackets(&input); + let getter_code = format!("{}\n !(getGuardSet {}) ", main_metta_functions, tree); + let result = runners::python::run(None, &getter_code); + remove_brackets(&result) + }; + + let get_tree_head = |input: &str| -> String { + let tree = remove_brackets(&input); + let getter_code = format!("{}\n !(head {}) ", main_metta_functions, tree); + let result = runners::python::run(None, &getter_code); + remove_brackets(&result) + }; + + let get_tree_tail = |input: &str| -> String { + let tree = remove_brackets(&input); + let getter_code = format!("{}\n !(tail {}) ", main_metta_functions, tree); + let result = runners::python::run(None, &getter_code); + remove_brackets(&result) + }; + + fn print_tree( + tree: &str, + indent: u32, + get_guardset: &dyn Fn(&str) -> String, + get_tree_head: &dyn Fn(&str) -> String, + get_tree_tail: &dyn Fn(&str) -> String, + ) { + if tree == "Nil" { + return; + } + + let current_node = simplify_tree_node(&tree); + let mut children = get_guardset(&tree); + + if indent == 0 { + println!("{}{}", " ".repeat(indent as usize), current_node); + } else { + println!("{}{}{}", " ".repeat(indent as usize), "├─", current_node); + } + + if children != "Nil" { + println!("{}{}", " ".repeat(indent as usize), "└──GuardSets's"); + } + + while children != "Nil" { + let child = get_tree_head(&children); + if child != "Nil" { + print_tree( + &child, + indent + 4, + &get_guardset, + &get_tree_head, + &get_tree_tail, + ); + } + children = get_tree_tail(&children); + } + } + + print_tree(tree, 0, &get_guardset, &get_tree_head, &get_tree_tail); +} diff --git a/metta-run/src/formatters/mod.rs b/metta-run/src/formatters/mod.rs index d1600a9..69db404 100644 --- a/metta-run/src/formatters/mod.rs +++ b/metta-run/src/formatters/mod.rs @@ -1,3 +1,4 @@ pub mod binary_tree_formatter; pub mod commands; pub mod constraint_tree_formatter; +pub mod guardset_tree_formatter; diff --git a/metta-run/tests/resources/guardset.metta b/metta-run/tests/resources/guardset.metta new file mode 100644 index 0000000..cf7d3b2 --- /dev/null +++ b/metta-run/tests/resources/guardset.metta @@ -0,0 +1,3 @@ +!(TreeNode (Value Nil False OR) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value D True LITERAL) Nil Nil) (Cons (TreeNode (Value E False LITERAL) Nil Nil) Nil))) + (Cons (TreeNode (Value A False LITERAL) Nil Nil) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil)))))) +