Skip to content

Commit

Permalink
refactor: move unique label into unordered folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed May 1, 2024
1 parent fd5d943 commit 7641c16
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 40 deletions.
6 changes: 2 additions & 4 deletions matching/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
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;
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,
Expand All @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions matching/src/unordered/assignment_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -56,7 +60,7 @@ pub fn calculate_matchings<'a>(
fn solve_assignment_problem<'a>(
left: &'a CSTNode,
right: &'a CSTNode,
children_matchings: Vec<Vec<(usize, Matchings<'a>)>>
children_matchings: Vec<Vec<(usize, Matchings<'a>)>>,
) -> Matchings<'a> {
let m = children_matchings.len();
let n = children_matchings[0].len();
Expand Down
16 changes: 15 additions & 1 deletion matching/src/unordered/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
pub mod assignment_problem;
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."),
}
}
Original file line number Diff line number Diff line change
@@ -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>(

Check warning on line 7 in matching/src/unordered/unique_label.rs

View workflow job for this annotation

GitHub Actions / test

function `calculate_matchings` is never used

Check failure on line 7 in matching/src/unordered/unique_label.rs

View workflow job for this annotation

GitHub Actions / clippy

function `calculate_matchings` is never used

Check warning on line 7 in matching/src/unordered/unique_label.rs

View workflow job for this annotation

GitHub Actions / build

function `calculate_matchings` is never used
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,
Expand All @@ -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)
Expand All @@ -74,6 +53,6 @@ pub fn unordered_tree_matching<'a>(

result
}
(_, _) => panic!("Invalid configuration reached"),
_ => unreachable!("Unordered matching is only supported for non-terminals."),
}
}
14 changes: 7 additions & 7 deletions merge/src/unordered_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 7641c16

Please sign in to comment.