Skip to content

Feature/guard set formatter #8

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

Merged
merged 3 commits into from
Aug 20, 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
6 changes: 5 additions & 1 deletion metta-run/src/formatters/commands.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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),
}
}
94 changes: 94 additions & 0 deletions metta-run/src/formatters/guardset_tree_formatter.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<&str>>()[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);
}
1 change: 1 addition & 0 deletions metta-run/src/formatters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod binary_tree_formatter;
pub mod commands;
pub mod constraint_tree_formatter;
pub mod guardset_tree_formatter;
3 changes: 3 additions & 0 deletions metta-run/tests/resources/guardset.metta
Original file line number Diff line number Diff line change
@@ -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))))))

Loading