From e54988497a47a1a3b47abdc321666bbdfe27798a Mon Sep 17 00:00:00 2001 From: samwondim Date: Fri, 16 Aug 2024 11:23:39 +0300 Subject: [PATCH 1/3] [ENH] added Constraint node guard set visualization --- metta-run/src/formatters/commands.rs | 6 +- .../src/formatters/guardset_tree_formatter.rs | 127 ++++++++++++++++++ metta-run/src/formatters/mod.rs | 1 + 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 metta-run/src/formatters/guardset_tree_formatter.rs 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..d928349 --- /dev/null +++ b/metta-run/src/formatters/guardset_tree_formatter.rs @@ -0,0 +1,127 @@ + +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") { + let is_binary_tree = is_binary_tree(&line); + if !is_binary_tree { + format_tree(&line); + continue; + } + println!("{}", 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); +} + +fn is_binary_tree(tree: &str) -> bool { + let main_metta_functions = format!( + " + ; check the length of the list + (: length (-> (List $t) Number)) + (= (length Nil) 0) + (= (length (Cons $x $xs)) + (+ 1 (length $xs))) + + ; function to check if the tree is binary + (: check_binary_tree (-> Tree Bool)) + (= (check_binary_tree (TreeNode $value $guard_set $children)) + (== 2 (length $children))) + " + ); + + let tree = &tree.to_string().replace("[", "").replace("]", ""); + let getter_code = format!("{}\n !(check_binary_tree {}) ", main_metta_functions, tree); + let result = runners::python::run(None, &getter_code); + + if result == "[[False]]" { + return false; + } else { + return true; + } +} 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; From 9063708149202238bd0ac94e2259a6f5005580c6 Mon Sep 17 00:00:00 2001 From: samwondim Date: Fri, 16 Aug 2024 11:27:29 +0300 Subject: [PATCH 2/3] [ENH] add test case for guardset visualization --- metta-run/tests/resources/guardset.metta | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 metta-run/tests/resources/guardset.metta 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)))))) + From a6cb6516135c79cdb8187785ad430ca18ae09dd6 Mon Sep 17 00:00:00 2001 From: samwondim Date: Tue, 20 Aug 2024 14:08:34 +0300 Subject: [PATCH 3/3] [ENH] removed unnecessary is_binary_tree function --- .../src/formatters/guardset_tree_formatter.rs | 35 +------------------ 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/metta-run/src/formatters/guardset_tree_formatter.rs b/metta-run/src/formatters/guardset_tree_formatter.rs index d928349..9952fc4 100644 --- a/metta-run/src/formatters/guardset_tree_formatter.rs +++ b/metta-run/src/formatters/guardset_tree_formatter.rs @@ -1,16 +1,10 @@ - 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") { - let is_binary_tree = is_binary_tree(&line); - if !is_binary_tree { - format_tree(&line); - continue; - } - println!("{}", line); + format_tree(&line); } else { println!("{}", line); } @@ -98,30 +92,3 @@ fn format_tree(tree: &str) { print_tree(tree, 0, &get_guardset, &get_tree_head, &get_tree_tail); } - -fn is_binary_tree(tree: &str) -> bool { - let main_metta_functions = format!( - " - ; check the length of the list - (: length (-> (List $t) Number)) - (= (length Nil) 0) - (= (length (Cons $x $xs)) - (+ 1 (length $xs))) - - ; function to check if the tree is binary - (: check_binary_tree (-> Tree Bool)) - (= (check_binary_tree (TreeNode $value $guard_set $children)) - (== 2 (length $children))) - " - ); - - let tree = &tree.to_string().replace("[", "").replace("]", ""); - let getter_code = format!("{}\n !(check_binary_tree {}) ", main_metta_functions, tree); - let result = runners::python::run(None, &getter_code); - - if result == "[[False]]" { - return false; - } else { - return true; - } -}