diff --git a/Cargo.lock b/Cargo.lock index a3292bb..546748e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,7 +89,6 @@ dependencies = [ "env_logger", "log", "matching", - "matching_handlers", "merge", "model", "parsing", @@ -341,7 +340,6 @@ name = "matching" version = "0.1.0" dependencies = [ "log", - "matching_handlers", "model", "parsing", "pathfinding", @@ -349,14 +347,6 @@ dependencies = [ "uuid", ] -[[package]] -name = "matching_handlers" -version = "0.1.0" -dependencies = [ - "log", - "model", -] - [[package]] name = "memchr" version = "2.6.4" @@ -370,7 +360,6 @@ dependencies = [ "diffy", "log", "matching", - "matching_handlers", "model", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index 6fe0713..df2fef0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,6 @@ members = [ "bin", "matching", - "matching_handlers", "merge", "parsing", "model" diff --git a/bin/Cargo.toml b/bin/Cargo.toml index a95745a..0176679 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -10,7 +10,6 @@ merge = { path = "../merge" } model = { path = "../model" } parsing = { path = "../parsing" } matching = { path = "../matching" } -matching_handlers = { path = "../matching_handlers" } assert_cmd = "2.0.12" clap = { version = "4.4.8", features = ["derive"] } log = { workspace = true } diff --git a/bin/src/control.rs b/bin/src/control.rs index 23c72c6..457429b 100644 --- a/bin/src/control.rs +++ b/bin/src/control.rs @@ -3,7 +3,7 @@ use std::{ fmt::{self, Display}, }; -use matching::{matching_configuration, MatchingEntry}; +use matching::MatchingEntry; use parsing::ParserConfiguration; #[derive(Debug)] @@ -62,27 +62,27 @@ pub fn run_tool_on_merge_scenario( let base_tree = parsing::parse_string(base, &parser_configuration).map_err(ExecutionError::ParsingError)?; log::info!("Finished parsing base file"); + log::info!("Started parsing left file"); let left_tree = parsing::parse_string(left, &parser_configuration).map_err(ExecutionError::ParsingError)?; log::info!("Finished parsing left file"); + log::info!("Started parsing right file"); let right_tree = parsing::parse_string(right, &parser_configuration) .map_err(ExecutionError::ParsingError)?; log::info!("Finished parsing right file"); - let matching_configuration = matching_configuration::MatchingConfiguration::from(language); log::info!("Started calculation of matchings between left and base"); - let matchings_left_base = - matching::calculate_matchings(&left_tree, &base_tree, &matching_configuration); + let matchings_left_base = matching::calculate_matchings(&left_tree, &base_tree); log::info!("Finished calculation of matchings between left and base"); + log::info!("Started calculation of matchings between right and base"); - let matchings_right_base = - matching::calculate_matchings(&right_tree, &base_tree, &matching_configuration); + let matchings_right_base = matching::calculate_matchings(&right_tree, &base_tree); log::info!("Finished calculation of matchings between right and base"); + log::info!("Started calculation of matchings between left and right"); - let matchings_left_right = - matching::calculate_matchings(&left_tree, &right_tree, &matching_configuration); + let matchings_left_right = matching::calculate_matchings(&left_tree, &right_tree); log::info!("Finished calculation of matchings between left and right"); log::info!("Starting merge of the trees"); @@ -119,10 +119,8 @@ pub fn run_diff_on_files( .map_err(ExecutionError::ParsingError)?; log::info!("Finished parsing right file"); - let matching_configuration = matching_configuration::MatchingConfiguration::from(language); log::info!("Started calculation of matchings between left and right"); - let matchings_left_right = - matching::calculate_matchings(&left_tree_root, &right_tree_root, &matching_configuration); + let matchings_left_right = matching::calculate_matchings(&left_tree_root, &right_tree_root); log::info!("Finished calculation of matchings between left and right"); Ok(matchings_left_right diff --git a/matching/Cargo.toml b/matching/Cargo.toml index 6f00aa7..69ec3d1 100644 --- a/matching/Cargo.toml +++ b/matching/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] model = { path = "../model" } -matching_handlers = { path = "../matching_handlers" } unordered-pair = "0.2.4" log = { workspace = true } pathfinding = "4.9.1" diff --git a/matching/src/lib.rs b/matching/src/lib.rs index a83ca78..574ebdf 100644 --- a/matching/src/lib.rs +++ b/matching/src/lib.rs @@ -1,13 +1,11 @@ mod matches; mod matching; -pub mod matching_configuration; mod matching_entry; mod matchings; pub mod ordered; pub mod unordered; use matches::Matches; -use matching_configuration::MatchingConfiguration; pub use matching_entry::MatchingEntry; pub use matchings::Matchings; use unordered_pair::UnorderedPair; @@ -15,7 +13,6 @@ use unordered_pair::UnorderedPair; pub fn calculate_matchings<'a>( left: &'a model::CSTNode, right: &'a model::CSTNode, - config: &'a MatchingConfiguration<'a>, ) -> Matchings<'a> { if !left.matches(right) { return Matchings::empty(); @@ -28,9 +25,9 @@ pub fn calculate_matchings<'a>( ) => { if non_terminal_left.are_children_unordered && non_terminal_right.are_children_unordered { - unordered::calculate_matchings(left, right, config) + unordered::calculate_matchings(left, right) } else { - ordered::calculate_matchings(left, right, config) + ordered::calculate_matchings(left, right) } } ( @@ -57,7 +54,7 @@ pub fn calculate_matchings<'a>( #[cfg(test)] mod tests { - use crate::{calculate_matchings, matching_configuration::MatchingConfiguration}; + use crate::calculate_matchings; use model::{cst_node::Terminal, CSTNode, Point}; #[test] @@ -79,8 +76,7 @@ mod tests { is_block_end_delimiter: false, }); - let matching_configuration = MatchingConfiguration::default(); - let matchings = calculate_matchings(&left, &right, &matching_configuration); + let matchings = calculate_matchings(&left, &right); let left_right_matching = matchings.get_matching_entry(&left, &right).unwrap(); assert_eq!(1, left_right_matching.score); diff --git a/matching/src/matching_configuration.rs b/matching/src/matching_configuration.rs deleted file mode 100644 index 5c2d170..0000000 --- a/matching/src/matching_configuration.rs +++ /dev/null @@ -1,23 +0,0 @@ -use matching_handlers::MatchingHandlers; -use model::Language; - -pub struct MatchingConfiguration<'a> { - #[allow(dead_code)] - pub(crate) handlers: MatchingHandlers<'a>, -} - -impl Default for MatchingConfiguration<'_> { - fn default() -> Self { - MatchingConfiguration::from(Language::Java) - } -} - -impl From for MatchingConfiguration<'_> { - fn from(language: Language) -> Self { - match language { - Language::Java => MatchingConfiguration { - handlers: MatchingHandlers::from(Language::Java), - }, - } - } -} diff --git a/matching/src/ordered/mod.rs b/matching/src/ordered/mod.rs index 23a5bb2..342bbeb 100644 --- a/matching/src/ordered/mod.rs +++ b/matching/src/ordered/mod.rs @@ -1,7 +1,4 @@ -use crate::{ - matches::Matches, matching_configuration::MatchingConfiguration, matching_entry::MatchingEntry, - Matchings, -}; +use crate::{matches::Matches, matching_entry::MatchingEntry, Matchings}; use model::{cst_node::NonTerminal, CSTNode}; use unordered_pair::UnorderedPair; @@ -21,11 +18,7 @@ impl<'a> Default for Entry<'a> { } } -pub fn calculate_matchings<'a>( - left: &'a CSTNode, - right: &'a CSTNode, - config: &'a MatchingConfiguration<'a>, -) -> Matchings<'a> { +pub fn calculate_matchings<'a>(left: &'a CSTNode, right: &'a CSTNode) -> Matchings<'a> { match (left, right) { ( CSTNode::NonTerminal(NonTerminal { @@ -50,7 +43,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, config); + let w = crate::calculate_matchings(left_child, right_child); let matching = w .get_matching_entry(left_child, right_child) .unwrap_or_default(); @@ -103,7 +96,6 @@ pub fn calculate_matchings<'a>( #[cfg(test)] mod tests { - use crate::MatchingConfiguration; use model::{ cst_node::{NonTerminal, Terminal}, language, CSTNode, Language, Point, @@ -138,8 +130,7 @@ mod tests { ..Default::default() }); - let matching_configuration = MatchingConfiguration::default(); - let matchings = super::calculate_matchings(&left, &right, &matching_configuration); + let matchings = super::calculate_matchings(&left, &right); let child_matching = matchings.get_matching_entry(&child, &child); assert!(child_matching.is_some()); @@ -185,8 +176,7 @@ mod tests { ..Default::default() }); - let matching_configuration = MatchingConfiguration::from(Language::Java); - let matchings = super::calculate_matchings(&left, &right, &matching_configuration); + let matchings = super::calculate_matchings(&left, &right); assert!(matchings .get_matching_entry(&left_child, &right_child) .is_none()) @@ -230,8 +220,7 @@ mod tests { ..Default::default() }); - let matching_configuration = MatchingConfiguration::from(language::Language::Java); - let matchings = super::calculate_matchings(&left, &right, &matching_configuration); + let matchings = super::calculate_matchings(&left, &right); let left_right_matchings = matchings.get_matching_entry(&left, &right).unwrap(); assert_eq!(2, left_right_matchings.score); @@ -268,8 +257,7 @@ mod tests { ..Default::default() }); - let matching_configuration = MatchingConfiguration::from(language::Language::Java); - let matchings = super::calculate_matchings(&left, &right, &matching_configuration); + let matchings = super::calculate_matchings(&left, &right); let left_right_matchings = matchings.get_matching_entry(&left, &right).unwrap(); assert_eq!(2, left_right_matchings.score); @@ -317,8 +305,7 @@ mod tests { ..Default::default() }); - let matching_configuration = MatchingConfiguration::default(); - let matchings = super::calculate_matchings(&left, &right, &matching_configuration); + let matchings = super::calculate_matchings(&left, &right); let intermediate_matching = matchings .get_matching_entry(&intermediate, &intermediate) diff --git a/matching/src/unordered/assignment_problem.rs b/matching/src/unordered/assignment_problem.rs index eb0d973..15ca7a4 100644 --- a/matching/src/unordered/assignment_problem.rs +++ b/matching/src/unordered/assignment_problem.rs @@ -4,12 +4,11 @@ use model::{cst_node::NonTerminal, CSTNode}; use pathfinding::{kuhn_munkres::Weights, matrix}; use unordered_pair::UnorderedPair; -use crate::{matching_configuration::MatchingConfiguration, MatchingEntry, Matchings}; +use crate::{MatchingEntry, Matchings}; pub fn calculate_matchings<'a>( left: &'a CSTNode<'a>, right: &'a CSTNode<'a>, - config: &'a MatchingConfiguration<'a>, ) -> crate::Matchings<'a> { match (left, right) { ( @@ -34,7 +33,7 @@ pub fn calculate_matchings<'a>( children_right .iter() .map(|right_child| { - let w = crate::calculate_matchings(left_child, right_child, config); + let w = crate::calculate_matchings(left_child, right_child); 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 b434d8f..db7a138 100644 --- a/matching/src/unordered/mod.rs +++ b/matching/src/unordered/mod.rs @@ -1,4 +1,3 @@ -use crate::matching_configuration::MatchingConfiguration; use model::cst_node::NonTerminal; mod assignment_problem; @@ -7,7 +6,6 @@ mod unique_label; pub fn calculate_matchings<'a>( 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)) => { @@ -17,14 +15,14 @@ pub fn calculate_matchings<'a>( left.kind(), right.kind() ); - unique_label::calculate_matchings(left, right, config) + unique_label::calculate_matchings(left, right) } else { log::debug!( "Matching children of \"{}\" with \"{}\" using assignment problem matching.", left.kind(), right.kind() ); - assignment_problem::calculate_matchings(left, right, config) + assignment_problem::calculate_matchings(left, right) } } _ => 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 917826f..c0b2d55 100644 --- a/matching/src/unordered/unique_label.rs +++ b/matching/src/unordered/unique_label.rs @@ -1,13 +1,9 @@ use model::{cst_node::NonTerminal, CSTNode}; use unordered_pair::UnorderedPair; -use crate::{matching_configuration::MatchingConfiguration, MatchingEntry, Matchings}; +use crate::{MatchingEntry, Matchings}; -pub fn calculate_matchings<'a>( - left: &'a CSTNode, - right: &'a CSTNode, - config: &'a MatchingConfiguration<'a>, -) -> crate::Matchings<'a> { +pub fn calculate_matchings<'a>(left: &'a CSTNode, right: &'a CSTNode) -> crate::Matchings<'a> { match (left, right) { ( CSTNode::NonTerminal(NonTerminal { @@ -39,8 +35,7 @@ pub fn calculate_matchings<'a>( }; if is_same_identifier { - let child_matchings = - crate::calculate_matchings(child_left, child_right, config); + let child_matchings = crate::calculate_matchings(child_left, child_right); if let Some(matching_entry) = child_matchings.get_matching_entry(child_left, child_right) diff --git a/matching/tests/perfect_matching.rs b/matching/tests/perfect_matching.rs index 4bffd4d..99f7dcd 100644 --- a/matching/tests/perfect_matching.rs +++ b/matching/tests/perfect_matching.rs @@ -1,4 +1,3 @@ -use matching::matching_configuration::MatchingConfiguration; use model::language::Language; use parsing::ParserConfiguration; @@ -43,8 +42,7 @@ fn the_perfect_matching_calculation_is_correct() -> Result<(), Box() -> MatchingHandlers<'a> { - let matching_handlers: MatchingHandlers<'a> = MatchingHandlers::new(); - matching_handlers -} diff --git a/matching_handlers/src/lib.rs b/matching_handlers/src/lib.rs deleted file mode 100644 index c2df8d3..0000000 --- a/matching_handlers/src/lib.rs +++ /dev/null @@ -1,51 +0,0 @@ -mod java; - -use std::collections::HashMap; - -use java::get_default_java_matching_handlers; -use model::{CSTNode, Language}; - -type MatchingHandler<'a> = fn(left: &'a CSTNode<'a>, right: &'a CSTNode<'a>) -> usize; - -pub struct MatchingHandlers<'a> { - matching_handlers: HashMap<&'static str, MatchingHandler<'a>>, -} - -impl<'a> Default for MatchingHandlers<'a> { - fn default() -> Self { - Self::new() - } -} - -impl<'a> MatchingHandlers<'a> { - pub fn new() -> Self { - Self { - matching_handlers: HashMap::new(), - } - } - - pub fn register(&mut self, key: &'static str, value: MatchingHandler<'a>) { - self.matching_handlers.insert(key, value); - } - - pub fn compute_matching_score( - &'a self, - left: &'a CSTNode, - right: &'a CSTNode, - ) -> Option { - if left.kind() != right.kind() { - None - } else { - let handler = &self.matching_handlers.get(left.kind())?; - Some(handler(left, right)) - } - } -} - -impl From for MatchingHandlers<'_> { - fn from(language: Language) -> Self { - match language { - Language::Java => get_default_java_matching_handlers(), - } - } -} diff --git a/merge/Cargo.toml b/merge/Cargo.toml index 770e5ad..f07a7d1 100644 --- a/merge/Cargo.toml +++ b/merge/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" [dependencies] model = { path = "../model" } matching = { path = "../matching" } -matching_handlers = { path = "../matching_handlers" } diffy = "0.3.0" log = { workspace = true } uuid = { workspace = true } diff --git a/merge/src/ordered_merge.rs b/merge/src/ordered_merge.rs index 24bd2a3..b455950 100644 --- a/merge/src/ordered_merge.rs +++ b/merge/src/ordered_merge.rs @@ -211,7 +211,7 @@ pub fn ordered_merge<'a>( mod tests { use std::vec; - use matching::{matching_configuration::MatchingConfiguration, ordered, Matchings}; + use matching::{ordered, Matchings}; use model::{cst_node::NonTerminal, cst_node::Terminal, CSTNode, Language, Point}; use crate::{MergeError, MergedCSTNode}; @@ -224,14 +224,9 @@ mod tests { parent_b: &'a CSTNode<'a>, expected_merge: &'a MergedCSTNode<'a>, ) -> Result<(), MergeError> { - let matching_configuration = MatchingConfiguration::from(Language::Java); - - 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 matchings_base_parent_a = ordered::calculate_matchings(base, parent_a); + let matchings_base_parent_b = ordered::calculate_matchings(base, parent_b); + let matchings_parents = ordered::calculate_matchings(parent_a, parent_b); let merged_tree = ordered_merge( parent_a.try_into().unwrap(), @@ -260,14 +255,9 @@ mod tests { parent_b: &CSTNode, expected_merge: &MergedCSTNode, ) -> Result<(), MergeError> { - let matching_configuration = MatchingConfiguration::from(Language::Java); - - 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 matchings_base_parent_a = ordered::calculate_matchings(base, parent_a); + let matchings_base_parent_b = ordered::calculate_matchings(base, parent_b); + let matchings_parents = ordered::calculate_matchings(parent_a, parent_b); let merged_tree = ordered_merge( parent_a.try_into().unwrap(), @@ -658,13 +648,9 @@ mod tests { ..Default::default() }); - let matching_configuration = MatchingConfiguration::from(Language::Java); - 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 matchings_base_parent_a = ordered::calculate_matchings(&base, &parent_a); + let matchings_base_parent_b = ordered::calculate_matchings(&base, &parent_b); + let matchings_parents = ordered::calculate_matchings(&parent_a, &parent_b); 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 4bcf505..ad40b42 100644 --- a/merge/src/unordered_merge.rs +++ b/merge/src/unordered_merge.rs @@ -138,9 +138,7 @@ pub fn unordered_merge<'a>( #[cfg(test)] mod tests { - use matching::{ - matching_configuration::MatchingConfiguration, unordered::calculate_matchings, Matchings, - }; + use matching::{unordered::calculate_matchings, Matchings}; use model::{ cst_node::{NonTerminal, Terminal}, CSTNode, Language, Point, @@ -156,11 +154,9 @@ mod tests { parent_b: &CSTNode, expected_merge: &MergedCSTNode, ) -> Result<(), MergeError> { - let matching_configuration = MatchingConfiguration::from(Language::Java); - - 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 matchings_base_parent_a = calculate_matchings(base, parent_a); + let matchings_base_parent_b = calculate_matchings(base, parent_b); + let matchings_parents = calculate_matchings(parent_a, parent_b); let merged_tree = unordered_merge( parent_a.try_into().unwrap(), @@ -189,11 +185,9 @@ mod tests { parent_b: &CSTNode, expected_merge: &MergedCSTNode, ) -> Result<(), MergeError> { - let matching_configuration = MatchingConfiguration::from(Language::Java); - - 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 matchings_base_parent_a = calculate_matchings(base, parent_a); + let matchings_base_parent_b = calculate_matchings(base, parent_b); + let matchings_parents = calculate_matchings(parent_a, parent_b); let merged_tree = unordered_merge( parent_a.try_into().unwrap(),