Skip to content

Commit

Permalink
feat: cover more cases in merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Aug 2, 2023
1 parent 9b0debc commit 405fb48
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
merge = { path = "../merge" }
model = { path = "../model" }
parsing = { path = "../parsing" }
matching = { path = "../matching" }
Expand Down
33 changes: 28 additions & 5 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use matching::ordered_tree_matching;
use merge::merge;
use model::CSTNode;

fn main() {
let base = parsing::parse_string(
Expand Down Expand Up @@ -31,10 +33,31 @@ fn main() {
)
.unwrap();

let result = ordered_tree_matching(&left, &base);
let matchings_left_base = ordered_tree_matching(&left, &base);
let matchings_right_base = ordered_tree_matching(&right, &base);
let matchings_lef_right = ordered_tree_matching(&left, &right);
let result = merge(
&base,
&left,
&right,
&matchings_left_base,
&matchings_right_base,
&matchings_lef_right,
);

// result.into_iter().for_each(|(pair, matching)| {
// println!("{:#?}", pair);
// println!("{:?}", matching);
// });
println!("{:#?}", pretty_print(result))
}

pub fn pretty_print(node: CSTNode) -> String {
match node {
CSTNode::Terminal { value, .. } => value,
CSTNode::NonTerminal { children, .. } => {
children.iter().fold(String::new(), |acc, current| {
let mut result = acc.to_owned();
result.push_str(" ");
result.push_str(&pretty_print(current.clone()));
result
})
}
}
}
1 change: 1 addition & 0 deletions matching/src/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use model::CSTNode;
pub struct Matching<'a> {
pub matching_node: &'a CSTNode,
pub score: usize,
pub is_perfect_match: bool,
}
8 changes: 6 additions & 2 deletions matching/src/matching_entry.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MatchingEntry {
pub score: usize,
pub is_perfect_match: bool,
}

impl MatchingEntry {
pub fn with_score(score: usize) -> Self {
return MatchingEntry { score };
pub fn new(score: usize, is_perfect_match: bool) -> Self {
return MatchingEntry {
score,
is_perfect_match,
};
}
}
6 changes: 4 additions & 2 deletions matching/src/matchings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Matchings {
Matching {
matching_node,
score: matching.score,
is_perfect_match: matching.is_perfect_match,
}
})
}
Expand Down Expand Up @@ -64,13 +65,14 @@ mod tests {
let mut matchings = HashMap::new();
matchings.insert(
UnorderedPair(a_node.clone(), a_node.clone()),
MatchingEntry::with_score(1),
MatchingEntry::new(1, true),
);

assert_eq!(
Some(Matching {
matching_node: &a_node,
score: 1
score: 1,
is_perfect_match: true
}),
Matchings::new(matchings).find_matching_for(&a_node)
)
Expand Down
50 changes: 37 additions & 13 deletions matching/src/ordered_tree_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ fn ordered_tree_matching_helper(
}
}

let matching = MatchingEntry {
score: matrix_m[m][n] + root_matching,
};
let matching = MatchingEntry::new(
matrix_m[m][n] + root_matching,
2 * matrix_m[m][n] / (m + n) == 1,
);
let mut result = HashMap::new();
result.insert(
UnorderedPair::new(left.to_owned(), right.to_owned()),
Expand All @@ -126,19 +127,18 @@ fn ordered_tree_matching_helper(
},
) => {
let mut result = HashMap::new();
let is_perfetch_match = kind_left == kind_right && value_left == value_right;
result.insert(
UnorderedPair::new(left.to_owned(), right.to_owned()),
MatchingEntry {
score: (kind_left == kind_right && value_left == value_right).into(),
},
MatchingEntry::new(is_perfetch_match.into(), is_perfetch_match),
);
result
}
(_, _) => {
let mut result = HashMap::new();
result.insert(
UnorderedPair::new(left.to_owned(), right.to_owned()),
MatchingEntry { score: 0 },
MatchingEntry::new(0, false),
);
result
}
Expand All @@ -164,7 +164,7 @@ mod tests {
let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::with_score(1)),
Some(&MatchingEntry::new(1, true)),
matchings.get_matching_entry(left, right)
)
}
Expand All @@ -183,7 +183,7 @@ mod tests {
let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::with_score(0)),
Some(&MatchingEntry::new(0, false)),
matchings.get_matching_entry(left, right)
)
}
Expand All @@ -202,7 +202,7 @@ mod tests {
let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::with_score(0)),
Some(&MatchingEntry::new(0, false)),
matchings.get_matching_entry(left, right)
)
}
Expand All @@ -221,7 +221,7 @@ mod tests {
let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::with_score(0)),
Some(&MatchingEntry::new(0, false)),
matchings.get_matching_entry(left, right)
)
}
Expand All @@ -244,7 +244,7 @@ mod tests {
let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::with_score(1)),
Some(&MatchingEntry::new(1, true)),
matchings.get_matching_entry(child.clone(), child)
)
}
Expand Down Expand Up @@ -297,7 +297,31 @@ mod tests {
let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::with_score(2)),
Some(&MatchingEntry::new(2, false)),
matchings.get_matching_entry(left, right)
)
}

#[test]
fn perfect_matching_deep_nodes() {
let common_child = CSTNode::Terminal {
kind: "kind_b".into(),
value: "value_b".into(),
};

let left = CSTNode::NonTerminal {
kind: "kind_a".to_owned(),
children: vec![common_child.clone()],
};
let right = CSTNode::NonTerminal {
kind: "kind_a".to_owned(),
children: vec![common_child.clone()],
};

let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::new(2, true)),
matchings.get_matching_entry(left, right)
)
}
Expand Down
Loading

0 comments on commit 405fb48

Please sign in to comment.