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

fix(matching): Early return on matching if nodes have different kinds #35

Merged
merged 1 commit into from
Mar 1, 2024
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
21 changes: 21 additions & 0 deletions bin/tests/scenarios/jdime_matching_issue/base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.fosd.jdime.artifact;

import java.security.MessageDigest;

public abstract class Artifact<T extends Artifact<T>> implements Comparable<T>, StatisticsInterface {
public boolean hasChanges(Revision revision) {
if (this.revision.equals(revision)) {
return false;
}

if (!hasMatching(revision)) {
return true;
}

T match = getMatching(revision).getMatchingArtifact(this);

return getTreeSize() != match.getTreeSize() || Artifacts.bfsStream(self()).anyMatch(a -> {
return !a.hasMatching(revision) || !a.getMatching(revision).getMatchingArtifact(a).hashId().equals(a.hashId());
});
}
}
22 changes: 22 additions & 0 deletions bin/tests/scenarios/jdime_matching_issue/left.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.fosd.jdime.artifact;

import java.security.MessageDigest;

public abstract class Artifact<T extends Artifact<T>> implements Comparable<T>, StatisticsInterface {
public boolean hasChanges(Revision revision) {

if (this.revision.equals(revision)) {
return false;
}

if (!hasMatching(revision)) {
return true;
}

T match = getMatching(revision).getMatchingArtifact(this);

return getTreeSize() != match.getTreeSize() || Artifacts.bfsStream(self()).anyMatch(a -> {
return !a.hasMatching(revision) || !a.getMatching(revision).getMatchingArtifact(a).hashId().equals(a.hashId());
});
}
}
1 change: 1 addition & 0 deletions bin/tests/scenarios/jdime_matching_issue/merge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package de . fosd . jdime . artifact ; import java . security . MessageDigest ; public abstract class Artifact < T extends Artifact < T > > implements Comparable < T > , StatisticsInterface { public boolean hasChanges ( Revision revision ) { if ( this . revision . equals ( revision ) ) { return false ; } if ( ! hasMatching ( revision ) ) { return true ; } T match = getMatching ( revision ) . getMatchingArtifact ( this ) ; return getTreeSize ( ) != match . getTreeSize ( ) || ! getTreeHash ( ) . equals ( match . getTreeHash ( ) ) ; } }
20 changes: 20 additions & 0 deletions bin/tests/scenarios/jdime_matching_issue/right.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.fosd.jdime.artifact;

import java.security.MessageDigest;

public abstract class Artifact<T extends Artifact<T>> implements Comparable<T>, StatisticsInterface {
public boolean hasChanges(Revision revision) {

if (this.revision.equals(revision)) {
return false;
}

if (!hasMatching(revision)) {
return true;
}

T match = getMatching(revision).getMatchingArtifact(this);

return getTreeSize() != match.getTreeSize() || !getTreeHash().equals(match.getTreeHash());
}
}
8 changes: 8 additions & 0 deletions matching/src/ordered_tree_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
) => {
let root_matching: usize = (kind_left == kind_right).into();

// Node kinds do not match, so we return a matching with a score of 0
if root_matching == 0 {
return Matchings::from_single(
UnorderedPair(left, right),
MatchingEntry::new(0, false),
);
}

let m = children_left.len();
let n = children_right.len();

Expand Down Expand Up @@ -105,9 +113,9 @@

#[cfg(test)]
mod tests {
use crate::{matching_entry::MatchingEntry, *};

Check warning on line 116 in matching/src/ordered_tree_matching.rs

View workflow job for this annotation

GitHub Actions / test

the item `MatchingEntry` is imported redundantly
use model::{
cst_node::{NonTerminal, Terminal},

Check warning on line 118 in matching/src/ordered_tree_matching.rs

View workflow job for this annotation

GitHub Actions / test

the item `Terminal` is imported redundantly
language, CSTNode, Point,
};

Expand Down
Loading