From 6432bf36f05fa14ab7b8181323fe72a7af9b1a07 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Wed, 1 May 2024 21:32:32 -0300 Subject: [PATCH] refactor: move declaration of handlers into matching configuration --- bin/src/control.rs | 25 ++----- matching/src/lib.rs | 21 ++---- matching/src/matching_configuration.rs | 9 ++- matching/src/ordered/mod.rs | 44 ++++-------- matching/src/unordered/assignment_problem.rs | 15 ++-- matching/src/unordered/mod.rs | 11 ++- matching/src/unordered/unique_label.rs | 12 +--- merge/src/ordered_merge.rs | 75 +++++--------------- merge/src/unordered_merge.rs | 27 ++----- 9 files changed, 69 insertions(+), 170 deletions(-) diff --git a/bin/src/control.rs b/bin/src/control.rs index fc621e9..8c1f51d 100644 --- a/bin/src/control.rs +++ b/bin/src/control.rs @@ -62,26 +62,13 @@ pub fn run_tool_on_merge_scenario( let right_tree = parsing::parse_string(right, &parser_configuration) .map_err(ExecutionError::ParsingError)?; - let matching_handlers = matching_handlers::MatchingHandlers::from(language); let matching_configuration = matching_configuration::MatchingConfiguration::from(language); - let matchings_left_base = matching::calculate_matchings( - &left_tree, - &base_tree, - &matching_handlers, - &matching_configuration, - ); - let matchings_right_base = matching::calculate_matchings( - &right_tree, - &base_tree, - &matching_handlers, - &matching_configuration, - ); - let matchings_left_right = matching::calculate_matchings( - &left_tree, - &right_tree, - &matching_handlers, - &matching_configuration, - ); + let matchings_left_base = + matching::calculate_matchings(&left_tree, &base_tree, &matching_configuration); + let matchings_right_base = + matching::calculate_matchings(&right_tree, &base_tree, &matching_configuration); + let matchings_left_right = + matching::calculate_matchings(&left_tree, &right_tree, &matching_configuration); let result = merge::merge( &base_tree, diff --git a/matching/src/lib.rs b/matching/src/lib.rs index 9cc348a..9d47ef1 100644 --- a/matching/src/lib.rs +++ b/matching/src/lib.rs @@ -7,7 +7,6 @@ pub mod unordered; use matching_configuration::MatchingConfiguration; pub use matching_entry::MatchingEntry; -use matching_handlers::MatchingHandlers; pub use matchings::Matchings; use model::cst_node::Terminal; use unordered_pair::UnorderedPair; @@ -15,8 +14,7 @@ use unordered_pair::UnorderedPair; pub fn calculate_matchings<'a>( left: &'a model::CSTNode, right: &'a model::CSTNode, - matching_handlers: &'a MatchingHandlers<'a>, - matching_configuration: &'a MatchingConfiguration, + config: &'a MatchingConfiguration<'a>, ) -> Matchings<'a> { match (left, right) { ( @@ -25,14 +23,9 @@ pub fn calculate_matchings<'a>( ) => { if non_terminal_left.are_children_unordered && non_terminal_right.are_children_unordered { - unordered::calculate_matchings( - left, - right, - matching_handlers, - matching_configuration, - ) + unordered::calculate_matchings(left, right, config) } else { - ordered::calculate_matchings(left, right, matching_handlers, matching_configuration) + ordered::calculate_matchings(left, right, config) } } ( @@ -87,7 +80,7 @@ mod tests { let binding = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::default(); - let matchings = calculate_matchings(&left, &right, &binding, &matching_configuration); + let matchings = calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(1, true)), @@ -116,7 +109,7 @@ mod tests { let binding = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::default(); - let matchings = calculate_matchings(&left, &right, &binding, &matching_configuration); + let matchings = calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(0, false)), @@ -145,7 +138,7 @@ mod tests { let binding = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::default(); - let matchings = calculate_matchings(&left, &right, &binding, &matching_configuration); + let matchings = calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(0, false)), @@ -174,7 +167,7 @@ mod tests { let binding = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::default(); - let matchings = calculate_matchings(&left, &right, &binding, &matching_configuration); + let matchings = calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(0, false)), diff --git a/matching/src/matching_configuration.rs b/matching/src/matching_configuration.rs index 1f8472b..91d2c32 100644 --- a/matching/src/matching_configuration.rs +++ b/matching/src/matching_configuration.rs @@ -1,18 +1,20 @@ +use matching_handlers::MatchingHandlers; use model::Language; use std::collections::HashSet; -pub struct MatchingConfiguration { +pub struct MatchingConfiguration<'a> { pub(crate) delimiters: HashSet<&'static str>, pub(crate) kinds_with_label: HashSet<&'static str>, + pub(crate) handlers: MatchingHandlers<'a>, } -impl Default for MatchingConfiguration { +impl Default for MatchingConfiguration<'_> { fn default() -> Self { MatchingConfiguration::from(Language::Java) } } -impl From for MatchingConfiguration { +impl From for MatchingConfiguration<'_> { fn from(language: Language) -> Self { match language { Language::Java => MatchingConfiguration { @@ -24,6 +26,7 @@ impl From for MatchingConfiguration { "method_declaration", ] .into(), + handlers: MatchingHandlers::from(Language::Java), }, } } diff --git a/matching/src/ordered/mod.rs b/matching/src/ordered/mod.rs index 6053599..403d17b 100644 --- a/matching/src/ordered/mod.rs +++ b/matching/src/ordered/mod.rs @@ -1,6 +1,5 @@ use crate::{ - matching_configuration::MatchingConfiguration, matching_entry::MatchingEntry, MatchingHandlers, - Matchings, + matching_configuration::MatchingConfiguration, matching_entry::MatchingEntry, Matchings, }; use model::{cst_node::NonTerminal, CSTNode}; use unordered_pair::UnorderedPair; @@ -24,8 +23,7 @@ impl<'a> Default for Entry<'a> { pub fn calculate_matchings<'a>( left: &'a CSTNode, right: &'a CSTNode, - matching_handlers: &'a MatchingHandlers<'a>, - matching_configuration: &'a MatchingConfiguration, + config: &'a MatchingConfiguration<'a>, ) -> Matchings<'a> { match (left, right) { ( @@ -38,7 +36,8 @@ pub fn calculate_matchings<'a>( .. }), ) => { - let root_matching: usize = matching_handlers + let root_matching: usize = config + .handlers .compute_matching_score(left, right) .unwrap_or((left.kind() == right.kind()).into()); @@ -58,12 +57,7 @@ pub fn calculate_matchings<'a>( let left_child = children_left.get(i - 1).unwrap(); let right_child = children_right.get(j - 1).unwrap(); - let w = crate::calculate_matchings( - left_child, - right_child, - matching_handlers, - matching_configuration, - ); + let w = crate::calculate_matchings(left_child, right_child, config); let matching = w .get_matching_entry(left_child, right_child) .unwrap_or_default(); @@ -122,7 +116,7 @@ mod tests { use crate::{matching_entry::MatchingEntry, *}; use model::{ cst_node::{NonTerminal, Terminal}, - language, CSTNode, Point, + language, CSTNode, Language, Point, }; #[test] @@ -152,10 +146,8 @@ mod tests { children: vec![child.clone()], }); - let binding = MatchingHandlers::from(language::Language::Java); let matching_configuration = MatchingConfiguration::default(); - let matchings = - super::calculate_matchings(&left, &right, &binding, &matching_configuration); + let matchings = super::calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(1, true)), @@ -199,10 +191,8 @@ mod tests { end_position: Point { row: 0, column: 7 }, }); - let binding = MatchingHandlers::from(language::Language::Java); - let matching_configuration = MatchingConfiguration::default(); - let matchings = - super::calculate_matchings(&left, &right, &binding, &matching_configuration); + let matching_configuration = MatchingConfiguration::from(Language::Java); + let matchings = super::calculate_matchings(&left, &right, &matching_configuration); assert_eq!( None, @@ -246,10 +236,8 @@ mod tests { children: vec![common_child.clone(), unique_right_child], }); - let binding = MatchingHandlers::from(language::Language::Java); - let matching_configuration = MatchingConfiguration::default(); - let matchings = - super::calculate_matchings(&left, &right, &binding, &matching_configuration); + let matching_configuration = MatchingConfiguration::from(language::Language::Java); + let matchings = super::calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(2, false)), @@ -285,10 +273,8 @@ mod tests { children: vec![common_child.clone()], }); - let binding = MatchingHandlers::from(language::Language::Java); - let matching_configuration = MatchingConfiguration::default(); - let matchings = - super::calculate_matchings(&left, &right, &binding, &matching_configuration); + let matching_configuration = MatchingConfiguration::from(language::Language::Java); + let matchings = super::calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(2, true)), @@ -333,10 +319,8 @@ mod tests { children: vec![intermediate.clone()], }); - let binding = MatchingHandlers::from(language::Language::Java); let matching_configuration = MatchingConfiguration::default(); - let matchings = - super::calculate_matchings(&left, &right, &binding, &matching_configuration); + let matchings = super::calculate_matchings(&left, &right, &matching_configuration); assert_eq!( Some(&MatchingEntry::new(2, true)), diff --git a/matching/src/unordered/assignment_problem.rs b/matching/src/unordered/assignment_problem.rs index 1e0c565..bba1ec0 100644 --- a/matching/src/unordered/assignment_problem.rs +++ b/matching/src/unordered/assignment_problem.rs @@ -1,6 +1,5 @@ use std::cmp::max; -use matching_handlers::MatchingHandlers; use model::{cst_node::NonTerminal, CSTNode}; use pathfinding::{kuhn_munkres::Weights, matrix}; use unordered_pair::UnorderedPair; @@ -8,10 +7,9 @@ use unordered_pair::UnorderedPair; use crate::{matching_configuration::MatchingConfiguration, MatchingEntry, Matchings}; pub fn calculate_matchings<'a>( - left: &'a CSTNode, - right: &'a CSTNode, - matching_handlers: &'a MatchingHandlers<'a>, - matching_configuration: &'a MatchingConfiguration, + left: &'a CSTNode<'a>, + right: &'a CSTNode<'a>, + config: &'a MatchingConfiguration<'a>, ) -> crate::Matchings<'a> { match (left, right) { ( @@ -36,12 +34,7 @@ pub fn calculate_matchings<'a>( children_right .iter() .map(|right_child| { - let w = crate::calculate_matchings( - left_child, - right_child, - matching_handlers, - matching_configuration, - ); + let w = crate::calculate_matchings(left_child, right_child, config); let matching = w .get_matching_entry(left_child, right_child) .unwrap_or_default(); diff --git a/matching/src/unordered/mod.rs b/matching/src/unordered/mod.rs index f211958..95107b7 100644 --- a/matching/src/unordered/mod.rs +++ b/matching/src/unordered/mod.rs @@ -5,19 +5,18 @@ 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>, - config: &'a MatchingConfiguration, + left: &'a model::CSTNode<'a>, + right: &'a model::CSTNode<'a>, + config: &'a MatchingConfiguration<'a>, ) -> crate::Matchings<'a> { match (left, right) { (model::CSTNode::NonTerminal(left_nt), model::CSTNode::NonTerminal(right_nt)) => { if all_children_labeled(left_nt, config) && all_children_labeled(right_nt, config) { log::debug!("Using unique label matching."); - unique_label::calculate_matchings(left, right, matching_handlers, config) + unique_label::calculate_matchings(left, right, config) } else { log::debug!("Using assignment problem matching."); - assignment_problem::calculate_matchings(left, right, matching_handlers, config) + assignment_problem::calculate_matchings(left, right, config) } } _ => unreachable!("Unordered matching is only supported for non-terminals."), diff --git a/matching/src/unordered/unique_label.rs b/matching/src/unordered/unique_label.rs index 082bc26..d89b63a 100644 --- a/matching/src/unordered/unique_label.rs +++ b/matching/src/unordered/unique_label.rs @@ -1,4 +1,3 @@ -use matching_handlers::MatchingHandlers; use model::{cst_node::NonTerminal, CSTNode}; use unordered_pair::UnorderedPair; @@ -7,8 +6,7 @@ use crate::{matching_configuration::MatchingConfiguration, MatchingEntry, Matchi pub fn calculate_matchings<'a>( left: &'a CSTNode, right: &'a CSTNode, - matching_handlers: &'a MatchingHandlers<'a>, - matching_configuration: &'a MatchingConfiguration, + matching_configuration: &'a MatchingConfiguration<'a>, ) -> crate::Matchings<'a> { match (left, right) { ( @@ -30,12 +28,8 @@ pub fn calculate_matchings<'a>( for child_left in children_left { for child_right in children_right { - let child_matchings = crate::calculate_matchings( - child_left, - child_right, - matching_handlers, - matching_configuration, - ); + let child_matchings = + crate::calculate_matchings(child_left, child_right, matching_configuration); if let Some(matching_entry) = child_matchings.get_matching_entry(child_left, child_right) diff --git a/merge/src/ordered_merge.rs b/merge/src/ordered_merge.rs index e57adb7..cdf34fb 100644 --- a/merge/src/ordered_merge.rs +++ b/merge/src/ordered_merge.rs @@ -228,27 +228,14 @@ mod tests { parent_b: &'a CSTNode<'a>, expected_merge: &'a MergedCSTNode<'a>, ) -> Result<(), MergeError> { - let matching_handlers = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::from(Language::Java); - let matchings_base_parent_a = ordered::calculate_matchings( - base, - parent_a, - &matching_handlers, - &matching_configuration, - ); - let matchings_base_parent_b = ordered::calculate_matchings( - base, - parent_b, - &matching_handlers, - &matching_configuration, - ); - let matchings_parents = ordered::calculate_matchings( - parent_a, - parent_b, - &matching_handlers, - &matching_configuration, - ); + let matchings_base_parent_a = + ordered::calculate_matchings(base, parent_a, &matching_configuration); + let matchings_base_parent_b = + ordered::calculate_matchings(base, parent_b, &matching_configuration); + let matchings_parents = + ordered::calculate_matchings(parent_a, parent_b, &matching_configuration); let merged_tree = ordered_merge( parent_a.try_into().unwrap(), @@ -277,27 +264,14 @@ mod tests { parent_b: &CSTNode, expected_merge: &MergedCSTNode, ) -> Result<(), MergeError> { - let matching_handlers = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::from(Language::Java); - let matchings_base_parent_a = ordered::calculate_matchings( - base, - parent_a, - &matching_handlers, - &matching_configuration, - ); - let matchings_base_parent_b = ordered::calculate_matchings( - base, - parent_b, - &matching_handlers, - &matching_configuration, - ); - let matchings_parents = ordered::calculate_matchings( - parent_a, - parent_b, - &matching_handlers, - &matching_configuration, - ); + let matchings_base_parent_a = + ordered::calculate_matchings(base, parent_a, &matching_configuration); + let matchings_base_parent_b = + ordered::calculate_matchings(base, parent_b, &matching_configuration); + let matchings_parents = + ordered::calculate_matchings(parent_a, parent_b, &matching_configuration); let merged_tree = ordered_merge( parent_a.try_into().unwrap(), @@ -670,26 +644,13 @@ mod tests { })], }); - let matching_handlers = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::from(Language::Java); - let matchings_base_parent_a = ordered::calculate_matchings( - &base, - &parent_a, - &matching_handlers, - &matching_configuration, - ); - let matchings_base_parent_b = ordered::calculate_matchings( - &base, - &parent_b, - &matching_handlers, - &matching_configuration, - ); - let matchings_parents = ordered::calculate_matchings( - &parent_a, - &parent_b, - &matching_handlers, - &matching_configuration, - ); + let matchings_base_parent_a = + ordered::calculate_matchings(&base, &parent_a, &matching_configuration); + let matchings_base_parent_b = + ordered::calculate_matchings(&base, &parent_b, &matching_configuration); + let matchings_parents = + ordered::calculate_matchings(&parent_a, &parent_b, &matching_configuration); let merged_tree = ordered_merge( (&parent_a).try_into().unwrap(), diff --git a/merge/src/unordered_merge.rs b/merge/src/unordered_merge.rs index e924ce6..355ba4d 100644 --- a/merge/src/unordered_merge.rs +++ b/merge/src/unordered_merge.rs @@ -162,16 +162,9 @@ mod tests { let matching_handlers = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::from(Language::Java); - let matchings_base_parent_a = - calculate_matchings(base, parent_a, &matching_handlers, &matching_configuration); - let matchings_base_parent_b = - calculate_matchings(base, parent_b, &matching_handlers, &matching_configuration); - let matchings_parents = calculate_matchings( - parent_a, - parent_b, - &matching_handlers, - &matching_configuration, - ); + let matchings_base_parent_a = calculate_matchings(base, parent_a, &matching_configuration); + let matchings_base_parent_b = calculate_matchings(base, parent_b, &matching_configuration); + let matchings_parents = calculate_matchings(parent_a, parent_b, &matching_configuration); let merged_tree = unordered_merge( parent_a.try_into().unwrap(), @@ -200,19 +193,11 @@ mod tests { parent_b: &CSTNode, expected_merge: &MergedCSTNode, ) -> Result<(), MergeError> { - let matching_handlers = MatchingHandlers::from(Language::Java); let matching_configuration = MatchingConfiguration::from(Language::Java); - let matchings_base_parent_a = - calculate_matchings(base, parent_a, &matching_handlers, &matching_configuration); - let matchings_base_parent_b = - calculate_matchings(base, parent_b, &matching_handlers, &matching_configuration); - let matchings_parents = calculate_matchings( - parent_a, - parent_b, - &matching_handlers, - &matching_configuration, - ); + let matchings_base_parent_a = calculate_matchings(base, parent_a, &matching_configuration); + let matchings_base_parent_b = calculate_matchings(base, parent_b, &matching_configuration); + let matchings_parents = calculate_matchings(parent_a, parent_b, &matching_configuration); let merged_tree = unordered_merge( parent_a.try_into().unwrap(),