diff --git a/matching/src/lib.rs b/matching/src/lib.rs index 3fe849c..ae46dc8 100644 --- a/matching/src/lib.rs +++ b/matching/src/lib.rs @@ -1,9 +1,8 @@ -mod unordered; mod matching; mod matching_entry; mod matchings; mod ordered_tree_matching; -mod unordered_tree_matching; +pub mod unordered; pub use matching_entry::MatchingEntry; use matching_handlers::MatchingHandlers; @@ -11,7 +10,6 @@ pub use matchings::Matchings; use model::cst_node::Terminal; pub use ordered_tree_matching::ordered_tree_matching; use unordered_pair::UnorderedPair; -pub use unordered_tree_matching::unordered_tree_matching; pub fn calculate_matchings<'a>( left: &'a model::CSTNode, @@ -25,7 +23,7 @@ pub fn calculate_matchings<'a>( ) => { if non_terminal_left.are_children_unordered && non_terminal_right.are_children_unordered { - unordered::assignment_problem::calculate_matchings(left, right, matching_handlers) + unordered::calculate_matchings(left, right, matching_handlers) } else { ordered_tree_matching::ordered_tree_matching(left, right, matching_handlers) } diff --git a/matching/src/unordered/assignment_problem.rs b/matching/src/unordered/assignment_problem.rs index 6670916..30e5361 100644 --- a/matching/src/unordered/assignment_problem.rs +++ b/matching/src/unordered/assignment_problem.rs @@ -35,7 +35,11 @@ pub fn calculate_matchings<'a>( children_right .iter() .map(|right_child| { - let w = crate::calculate_matchings(left_child, right_child, matching_handlers); + let w = crate::calculate_matchings( + left_child, + right_child, + matching_handlers, + ); let matching = w .get_matching_entry(left_child, right_child) .unwrap_or_default(); @@ -56,7 +60,7 @@ pub fn calculate_matchings<'a>( fn solve_assignment_problem<'a>( left: &'a CSTNode, right: &'a CSTNode, - children_matchings: Vec)>> + children_matchings: Vec)>>, ) -> Matchings<'a> { let m = children_matchings.len(); let n = children_matchings[0].len(); diff --git a/matching/src/unordered/mod.rs b/matching/src/unordered/mod.rs index d98f2cd..cfe2332 100644 --- a/matching/src/unordered/mod.rs +++ b/matching/src/unordered/mod.rs @@ -1 +1,15 @@ -pub mod assignment_problem; \ No newline at end of file +mod assignment_problem; +mod unique_label; + +pub fn calculate_matchings<'a>( + left: &'a model::CSTNode, + right: &'a model::CSTNode, + matching_handlers: &'a matching_handlers::MatchingHandlers<'a>, +) -> crate::Matchings<'a> { + match (left, right) { + (model::CSTNode::NonTerminal(_), model::CSTNode::NonTerminal(_)) => { + assignment_problem::calculate_matchings(left, right, matching_handlers) + } + _ => unreachable!("Unordered matching is only supported for non-terminals."), + } +} diff --git a/matching/src/unordered_tree_matching.rs b/matching/src/unordered/unique_label.rs similarity index 63% rename from matching/src/unordered_tree_matching.rs rename to matching/src/unordered/unique_label.rs index 0c5d378..437c6a4 100644 --- a/matching/src/unordered_tree_matching.rs +++ b/matching/src/unordered/unique_label.rs @@ -1,36 +1,15 @@ use matching_handlers::MatchingHandlers; -use model::{ - cst_node::{NonTerminal, Terminal}, - CSTNode, -}; +use model::{cst_node::NonTerminal, CSTNode}; use unordered_pair::UnorderedPair; -use crate::{calculate_matchings, MatchingEntry, Matchings}; +use crate::{MatchingEntry, Matchings}; -pub fn unordered_tree_matching<'a>( +pub fn calculate_matchings<'a>( left: &'a CSTNode, right: &'a CSTNode, matching_handlers: &'a MatchingHandlers<'a>, ) -> crate::Matchings<'a> { match (left, right) { - ( - CSTNode::Terminal(Terminal { - kind: kind_left, - value: value_left, - .. - }), - CSTNode::Terminal(Terminal { - kind: kind_right, - value: value_right, - .. - }), - ) => { - let is_perfetch_match = kind_left == kind_right && value_left == value_right; - Matchings::from_single( - UnorderedPair(left, right), - MatchingEntry::new(is_perfetch_match.into(), is_perfetch_match), - ) - } ( CSTNode::NonTerminal(NonTerminal { kind: kind_left, @@ -51,7 +30,7 @@ pub fn unordered_tree_matching<'a>( for child_left in children_left { for child_right in children_right { let child_matchings = - calculate_matchings(child_left, child_right, matching_handlers); + crate::calculate_matchings(child_left, child_right, matching_handlers); if let Some(matching_entry) = child_matchings.get_matching_entry(child_left, child_right) @@ -74,6 +53,6 @@ pub fn unordered_tree_matching<'a>( result } - (_, _) => panic!("Invalid configuration reached"), + _ => unreachable!("Unordered matching is only supported for non-terminals."), } } diff --git a/merge/src/unordered_merge.rs b/merge/src/unordered_merge.rs index acaac9a..b481360 100644 --- a/merge/src/unordered_merge.rs +++ b/merge/src/unordered_merge.rs @@ -138,7 +138,7 @@ pub fn unordered_merge<'a>( #[cfg(test)] mod tests { - use matching::{unordered_tree_matching, Matchings}; + use matching::{unordered::calculate_matchings, Matchings}; use matching_handlers::MatchingHandlers; use model::{ cst_node::{NonTerminal, Terminal}, @@ -157,9 +157,9 @@ mod tests { ) -> Result<(), MergeError> { let matching_handlers = MatchingHandlers::from(Language::Java); - let matchings_base_parent_a = unordered_tree_matching(base, parent_a, &matching_handlers); - let matchings_base_parent_b = unordered_tree_matching(base, parent_b, &matching_handlers); - let matchings_parents = unordered_tree_matching(parent_a, parent_b, &matching_handlers); + let matchings_base_parent_a = calculate_matchings(base, parent_a, &matching_handlers); + let matchings_base_parent_b = calculate_matchings(base, parent_b, &matching_handlers); + let matchings_parents = calculate_matchings(parent_a, parent_b, &matching_handlers); let merged_tree = unordered_merge( parent_a.try_into().unwrap(), @@ -190,9 +190,9 @@ mod tests { ) -> Result<(), MergeError> { let matching_handlers = MatchingHandlers::from(Language::Java); - let matchings_base_parent_a = unordered_tree_matching(base, parent_a, &matching_handlers); - let matchings_base_parent_b = unordered_tree_matching(base, parent_b, &matching_handlers); - let matchings_parents = unordered_tree_matching(parent_a, parent_b, &matching_handlers); + let matchings_base_parent_a = calculate_matchings(base, parent_a, &matching_handlers); + let matchings_base_parent_b = calculate_matchings(base, parent_b, &matching_handlers); + let matchings_parents = calculate_matchings(parent_a, parent_b, &matching_handlers); let merged_tree = unordered_merge( parent_a.try_into().unwrap(),