1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::diff::*;
use crate::levenshtein::distance;
use std::collections::{BTreeMap, HashSet};
pub type Mapping<'a> = BTreeMap<&'a str, &'a str>;
#[derive(Debug, PartialEq)]
pub struct Matching<'a> {
pub from: &'a str,
pub to: &'a str,
}
impl<'a> Matching<'a> {
pub fn new(from: &'a str, to: &'a str) -> Matching<'a> {
Matching { from, to }
}
}
#[derive(Debug, PartialEq)]
pub enum Match<'a> {
Full(Matching<'a>),
Partial(Matching<'a>),
}
pub fn match_graphs<'a>(d1: &'a DiffGraph<'_>, d2: &'a DiffGraph<'_>) -> Vec<Match<'a>> {
let mut mapping: BTreeMap<&str, &str> = get_initial_mapping(d1, d2);
let mut matches: Vec<Match> = mapping
.iter()
.map(|(from, to)| Match::Full(Matching { from, to }))
.collect();
let rev_adj_list1 = d1.graph.rev_adj_list();
let rev_adj_list2 = d2.graph.rev_adj_list();
let mut matched_labels_in_2: HashSet<&str> = mapping.iter().map(|(_, v)| *v).collect();
let mut prev_mapping = mapping.clone();
loop {
let mut new_mapping: Mapping = BTreeMap::new();
for (k, v) in prev_mapping.iter() {
let parents_in_1 = rev_adj_list1.get(k).unwrap();
let parents_in_2 = rev_adj_list2.get(v).unwrap();
for n in parents_in_1.iter() {
if mapping.contains_key(n) {
continue;
}
if let Some(lab) = select(d1, d2, n, &parents_in_2, false) {
if !matched_labels_in_2.contains(lab) {
matched_labels_in_2.insert(lab);
new_mapping.insert(n, lab);
}
}
}
}
let new_matches = get_new_matches(&mapping, &new_mapping);
if new_matches.len() == 0 {
break;
}
for mch in new_matches {
mapping.insert(mch.from, mch.to);
matches.push(Match::Partial(mch));
}
prev_mapping = new_mapping;
}
matches
}
fn get_initial_mapping<'a>(d1: &'a DiffGraph<'_>, d2: &'a DiffGraph<'_>) -> Mapping<'a> {
let mut mapping: BTreeMap<&str, &str> = BTreeMap::new();
let mut reverse_mapping: BTreeMap<&str, &str> = BTreeMap::new();
let g2_labels: Vec<&str> = d2.graph.nodes.iter().map(|n| n.label.as_str()).collect();
for node in d1.graph.nodes.iter() {
if let Some(matched_lab) = select(d1, d2, &node.label, &g2_labels, true) {
if let Some(lab_in_1) = reverse_mapping.get(matched_lab) {
let dist_already = dist_bw_nodes(d1, d2, lab_in_1, matched_lab);
let dist_new = dist_bw_nodes(d1, d2, &node.label, matched_lab);
if dist_new > dist_already {
continue;
}
mapping.remove(lab_in_1);
}
mapping.insert(&node.label, matched_lab);
reverse_mapping.insert(matched_lab, &node.label);
}
}
mapping
}
fn dist_bw_nodes(d1: &DiffGraph<'_>, d2: &DiffGraph<'_>, n1: &str, n2: &str) -> Option<usize> {
let node1 = d1.graph.get_node_by_label(n1).unwrap();
let node2 = d2.graph.get_node_by_label(n2).unwrap();
let tup1 = (
d1.dist_start[n1] as i64,
d1.dist_end[n1] as i64,
node1.stmts.len() as i64,
);
let tup2 = (
d2.dist_start[n2] as i64,
d2.dist_end[n2] as i64,
node2.stmts.len() as i64,
);
let s1 = node1.stmts.join("");
let s2 = node2.stmts.join("");
let dist = distance(&s1, &s2);
Some(
((tup1.0 - tup2.0).pow(2) + (tup1.1 - tup2.1).pow(2) + (tup1.2 - tup2.2).pow(2)) as usize
+ dist,
)
}
fn select<'a>(
d1: &'a DiffGraph<'_>,
d2: &'a DiffGraph<'_>,
n: &'a str,
list_of_labs: &[&'a str],
use_text_dist_filter: bool,
) -> Option<&'a str> {
let node = d1.graph.get_node_by_label(n).unwrap();
let node_stmt_len = node.stmts.len();
let node_stmts = node.stmts.join("");
list_of_labs
.iter()
.filter(|lab| {
let other_node = d2.graph.get_node_by_label(lab).unwrap();
(other_node.stmts.len() as i64 - node.stmts.len() as i64).abs() <= 2
})
.filter(|lab| {
if !use_text_dist_filter {
return true;
}
let other_node_stmts = d2.graph.get_node_by_label(lab).unwrap().stmts.join("");
distance(&node_stmts, &other_node_stmts) < 2 * node_stmt_len
})
.min_by_key(|x| dist_bw_nodes(d1, d2, n, x))
.map(|x| *x)
}
fn get_new_matches<'a>(mapping: &Mapping<'a>, new_mapping: &Mapping<'a>) -> Vec<Matching<'a>> {
let mut changed = Vec::new();
for (k, v) in new_mapping.iter() {
if !mapping.contains_key(k) {
changed.push(Matching { from: k, to: v })
}
}
changed
}