Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial implementation of S3M merge algorithm #4

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ target/
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# End of https://www.toptal.com/developers/gitignore/api/rust
# End of https://www.toptal.com/developers/gitignore/api/rust

.vscode/
147 changes: 115 additions & 32 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
use matching::ordered_tree_matching;
use merge::merge;
use model::CSTNode;
use model::{CSTNode, Language};
use parsing::ParserConfiguration;

fn run_semi_structured_merge_on_revisions(
language: Language,
base: &str,
left: &str,
right: &str,
) -> Result<CSTNode, &'static str> {
let base_tree = parsing::parse_string(base, ParserConfiguration::from_language(language))?;
let left_tree = parsing::parse_string(left, ParserConfiguration::from_language(language))?;
let right_tree = parsing::parse_string(right, ParserConfiguration::from_language(language))?;

let matchings_left_base = ordered_tree_matching(&left_tree, &base_tree);
let matchings_right_base = ordered_tree_matching(&right_tree, &base_tree);
let matchings_left_right = ordered_tree_matching(&left_tree, &right_tree);

Ok(merge(
&base_tree,
&left_tree,
&right_tree,
&matchings_left_base,
&matchings_right_base,
&matchings_left_right,
))
}

fn main() {
let base = parsing::parse_string(
r#"
let base = r#"
public static interface HelloWorld {
void sayHello(String name);
}
"#,
parsing::ParserConfiguration::from_language(model::Language::Java),
)
.unwrap();
let left = parsing::parse_string(
r#"
"#;
let left = r#"
public static interface HelloWorld {
void sayHello(String name);
void sayBye(String name);
}
"#,
parsing::ParserConfiguration::from_language(model::Language::Java),
)
.unwrap();
let right = parsing::parse_string(
r#"
"#;
let right = r#"
public static interface HelloWorld {
void killAllHumans();
void sayHello(String name);
}
"#,
parsing::ParserConfiguration::from_language(model::Language::Java),
)
.unwrap();

let matchings_left_base = ordered_tree_matching(&left, &base);
let matchings_right_base = ordered_tree_matching(&right, &base);
let matchings_lef_right = ordered_tree_matching(&left, &right);
let result = merge(
&base,
&left,
&right,
&matchings_left_base,
&matchings_right_base,
&matchings_lef_right,
);
"#;

println!("{:#?}", pretty_print(result))
let result = run_semi_structured_merge_on_revisions(Language::Java, base, left, right);

println!("{:#?}", pretty_print(result.unwrap()))
}

pub fn pretty_print(node: CSTNode) -> String {
Expand All @@ -61,3 +64,83 @@ pub fn pretty_print(node: CSTNode) -> String {
}
}
}

#[cfg(test)]
mod tests {
use model::language::Language;
use model::CSTNode;
use parsing::ParserConfiguration;

use crate::run_semi_structured_merge_on_revisions;

fn parse_java_string(contents: &str) -> CSTNode {
parsing::parse_string(contents, ParserConfiguration::from_language(Language::Java)).unwrap()
}

#[test]
fn it_merges_three_java_revisions_that_are_equal() {
let code = r#"
public static interface HelloWorld {
void sayHello(String name);
void sayBye(String name);
}
"#;

let result = run_semi_structured_merge_on_revisions(Language::Java, code, code, code);

assert_eq!(parse_java_string(code), result.unwrap())
}

#[test]
fn it_merges_three_java_revisions_that_adds_the_same_node() {
let base = r#"
public static interface HelloWorld {
void sayBye(String name);
}
"#;

let parents = r#"
public static interface HelloWorld {
void sayHello(String name);
void sayBye(String name);
}
"#;

let merge = r#"
public static interface HelloWorld {
void sayHello(String name);
void sayBye(String name);
}
"#;

let result = run_semi_structured_merge_on_revisions(Language::Java, base, parents, parents);

assert_eq!(parse_java_string(merge), result.unwrap())
}

#[test]
fn it_merges_three_java_revisions_that_removes_the_same_node() {
let base = r#"
public static interface HelloWorld {
void sayHello(String name);
void sayBye(String name);
}
"#;

let parents = r#"
public static interface HelloWorld {
void sayBye(String name);
}
"#;

let merge = r#"
public static interface HelloWorld {
void sayBye(String name);
}
"#;

let result = run_semi_structured_merge_on_revisions(Language::Java, base, parents, parents);

assert_eq!(parse_java_string(merge), result.unwrap())
}
}
39 changes: 35 additions & 4 deletions matching/src/ordered_tree_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ fn ordered_tree_matching_helper(
}
}

let matching = MatchingEntry::new(
matrix_m[m][n] + root_matching,
2 * matrix_m[m][n] / (m + n) == 1,
);
let matching = MatchingEntry::new(matrix_m[m][n] + root_matching, left == right);
let mut result = HashMap::new();
result.insert(
UnorderedPair::new(left.to_owned(), right.to_owned()),
Expand Down Expand Up @@ -325,4 +322,38 @@ mod tests {
matchings.get_matching_entry(left, right)
)
}

#[test]
fn perfect_matching_deeper_nodes() {
let leaf = CSTNode::Terminal {
kind: "kind_b".into(),
value: "value_b".into(),
};

let intermediate = CSTNode::NonTerminal {
kind: "intermediate".into(),
children: vec![leaf],
};

let left = CSTNode::NonTerminal {
kind: "kind_a".to_owned(),
children: vec![intermediate.clone()],
};
let right = CSTNode::NonTerminal {
kind: "kind_a".to_owned(),
children: vec![intermediate.clone()],
};

let matchings = ordered_tree_matching(&left, &right);

assert_eq!(
Some(&MatchingEntry::new(2, true)),
matchings.get_matching_entry(intermediate.clone(), intermediate)
);

assert_eq!(
Some(&MatchingEntry::new(3, true)),
matchings.get_matching_entry(left, right)
)
}
}
Loading
Loading