Skip to content

Commit b239b83

Browse files
authored
Merge pull request #8 from iCog-Labs-Dev/feature/guardSetFormatter
Feature/guard set formatter
2 parents 59ff910 + a6cb651 commit b239b83

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

metta-run/src/formatters/commands.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clap::Subcommand;
22

3-
use super::{binary_tree_formatter, constraint_tree_formatter};
3+
use super::{binary_tree_formatter, constraint_tree_formatter, guardset_tree_formatter};
44

55
#[derive(Subcommand, Debug, Clone)]
66
pub enum FormatterCommands {
@@ -9,11 +9,15 @@ pub enum FormatterCommands {
99

1010
#[command(name = "fct", about = "Format constraint tree")]
1111
Fct,
12+
13+
#[command(name = "fgt", about = "Format constraint tree guardset")]
14+
Fgt,
1215
}
1316

1417
pub fn format(metta_output: String, command: FormatterCommands) {
1518
match command {
1619
FormatterCommands::Fbt => binary_tree_formatter::format(metta_output),
1720
FormatterCommands::Fct => constraint_tree_formatter::format(metta_output),
21+
FormatterCommands::Fgt => guardset_tree_formatter::format(metta_output),
1822
}
1923
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

metta-run/src/formatters/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod binary_tree_formatter;
22
pub mod commands;
33
pub mod constraint_tree_formatter;
4+
pub mod guardset_tree_formatter;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!(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)))
2+
(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))))))
3+

0 commit comments

Comments
 (0)