Skip to content

Commit

Permalink
refactor: move declaration of handlers into matching configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed May 2, 2024
1 parent 7f6173a commit 6432bf3
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 170 deletions.
25 changes: 6 additions & 19 deletions bin/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 7 additions & 14 deletions matching/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ 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;

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) {
(
Expand All @@ -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)
}
}
(
Expand Down Expand Up @@ -87,7 +80,7 @@ mod tests {

let binding = MatchingHandlers::from(Language::Java);

Check warning on line 81 in matching/src/lib.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `binding`
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)),
Expand Down Expand Up @@ -116,7 +109,7 @@ mod tests {

let binding = MatchingHandlers::from(Language::Java);

Check warning on line 110 in matching/src/lib.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `binding`
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)),
Expand Down Expand Up @@ -145,7 +138,7 @@ mod tests {

let binding = MatchingHandlers::from(Language::Java);

Check warning on line 139 in matching/src/lib.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `binding`
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)),
Expand Down Expand Up @@ -174,7 +167,7 @@ mod tests {

let binding = MatchingHandlers::from(Language::Java);

Check warning on line 168 in matching/src/lib.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `binding`
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)),
Expand Down
9 changes: 6 additions & 3 deletions matching/src/matching_configuration.rs
Original file line number Diff line number Diff line change
@@ -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<Language> for MatchingConfiguration {
impl From<Language> for MatchingConfiguration<'_> {
fn from(language: Language) -> Self {
match language {
Language::Java => MatchingConfiguration {
Expand All @@ -24,6 +26,7 @@ impl From<Language> for MatchingConfiguration {
"method_declaration",
]
.into(),
handlers: MatchingHandlers::from(Language::Java),
},
}
}
Expand Down
44 changes: 14 additions & 30 deletions matching/src/ordered/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
(
Expand All @@ -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());

Expand All @@ -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();
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)),
Expand Down
15 changes: 4 additions & 11 deletions matching/src/unordered/assignment_problem.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::cmp::max;

use matching_handlers::MatchingHandlers;
use model::{cst_node::NonTerminal, CSTNode};
use pathfinding::{kuhn_munkres::Weights, matrix};
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) {
(
Expand All @@ -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();
Expand Down
11 changes: 5 additions & 6 deletions matching/src/unordered/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
Expand Down
12 changes: 3 additions & 9 deletions matching/src/unordered/unique_label.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use matching_handlers::MatchingHandlers;
use model::{cst_node::NonTerminal, CSTNode};
use unordered_pair::UnorderedPair;

Expand All @@ -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) {
(
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 6432bf3

Please sign in to comment.