From 159ff7fd6adb5ead0f740e8accc71bec4a2e6f95 Mon Sep 17 00:00:00 2001 From: Gordon Fraser Date: Mon, 9 Aug 2021 13:08:57 +0200 Subject: [PATCH 1/4] Define copy constructor that initialises all attributes of RefactorSequence copies, including rank and distance --- .../chromosomes/RefactorSequence.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java index 984601d15..044c1e61a 100644 --- a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java +++ b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java @@ -108,6 +108,20 @@ public RefactorSequence(Program originalProgram, Mutation muta this.refactoringFinders = refactoringFinders; } + /** + * Copy constructor + * + * @param other RefactoringSequence to copy + */ + RefactorSequence(RefactorSequence other) { + super(other); + this.originalProgram = other.originalProgram; + this.productions = new LinkedList<>(other.productions); + this.refactoringFinders = other.refactoringFinders; + this.setRank(other.getRank()); + this.setDistance(other.getDistance()); + } + /** * Apply the refactoring sequence to a given program, without modifying the original program. * @@ -145,8 +159,7 @@ private Refactoring getExecutedRefactoring(Program program, Integer nthProductio @Override public RefactorSequence copy() { - return new RefactorSequence(originalProgram, getMutation(), getCrossover(), - new ArrayList<>(productions), refactoringFinders); + return new RefactorSequence(this); } /* From 5b10d7a29dee91e867abf3c36a6413b8240d4473 Mon Sep 17 00:00:00 2001 From: Gordon Fraser Date: Mon, 9 Aug 2021 14:36:02 +0200 Subject: [PATCH 2/4] Move initialisation of rank/distance to where it belongs --- .../refactor/metaheuristics/chromosomes/RefactorSequence.java | 2 -- .../litterbox/refactor/metaheuristics/chromosomes/Solution.java | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java index 044c1e61a..2479fdca1 100644 --- a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java +++ b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java @@ -118,8 +118,6 @@ public RefactorSequence(Program originalProgram, Mutation muta this.originalProgram = other.originalProgram; this.productions = new LinkedList<>(other.productions); this.refactoringFinders = other.refactoringFinders; - this.setRank(other.getRank()); - this.setDistance(other.getDistance()); } /** diff --git a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/Solution.java b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/Solution.java index 2d6c1780d..baecfda9e 100644 --- a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/Solution.java +++ b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/Solution.java @@ -72,6 +72,8 @@ protected Solution(Mutation mutation, Crossover crossover, Map(other.getFitnessMap()); + this.rank = other.getRank(); + this.distance = other.getDistance(); } /** From 7a2623bbc819c2b93292c25592d70f5770c19cd8 Mon Sep 17 00:00:00 2001 From: labrenz Date: Thu, 12 Aug 2021 21:16:55 +0200 Subject: [PATCH 3/4] Correct BinaryRankTournamentTest. --- .../metaheuristics/search_operators/BinaryRankTournament.java | 2 -- .../search_operators/BinaryRankTournamentTest.java | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java index a8c1057e1..3d9ce95a9 100644 --- a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java +++ b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java @@ -26,8 +26,6 @@ public class BinaryRankTournament> implements Selection { - public BinaryRankTournament() { - } /** * Selects a chromosome to be used as parent for mutation or crossover from the given non-null diff --git a/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java b/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java index 0bd305db2..8234b15e4 100644 --- a/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java +++ b/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java @@ -110,8 +110,8 @@ void testSelectionByDistance() { refactorSequence1.setDistance(2.0); mockedRandomness.when(() -> Randomness.nextInt(2)).thenReturn(0).thenReturn(1); RefactorSequence candidate2 = selection.apply(population); - assertNotSame(refactorSequence1, candidate1); - assertEquals(refactorSequence1, candidate1); + assertNotSame(refactorSequence1, candidate2); + assertEquals(refactorSequence1, candidate2); } @Test From 73d7da2c459129acd74ea546e813a069bdbad7d6 Mon Sep 17 00:00:00 2001 From: labrenz Date: Thu, 12 Aug 2021 21:16:55 +0200 Subject: [PATCH 4/4] Correct BinaryRankTournamentTest. --- .../refactor/metaheuristics/chromosomes/RefactorSequence.java | 1 - .../metaheuristics/search_operators/BinaryRankTournament.java | 2 -- .../search_operators/BinaryRankTournamentTest.java | 4 ++-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java index 2479fdca1..113a400dd 100644 --- a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java +++ b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/chromosomes/RefactorSequence.java @@ -25,7 +25,6 @@ import de.uni_passau.fim.se2.litterbox.refactor.refactorings.Refactoring; import de.uni_passau.fim.se2.litterbox.utils.PropertyLoader; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Objects; diff --git a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java index a8c1057e1..3d9ce95a9 100644 --- a/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java +++ b/src/main/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournament.java @@ -26,8 +26,6 @@ public class BinaryRankTournament> implements Selection { - public BinaryRankTournament() { - } /** * Selects a chromosome to be used as parent for mutation or crossover from the given non-null diff --git a/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java b/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java index 0bd305db2..8234b15e4 100644 --- a/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java +++ b/src/test/java/de/uni_passau/fim/se2/litterbox/refactor/metaheuristics/search_operators/BinaryRankTournamentTest.java @@ -110,8 +110,8 @@ void testSelectionByDistance() { refactorSequence1.setDistance(2.0); mockedRandomness.when(() -> Randomness.nextInt(2)).thenReturn(0).thenReturn(1); RefactorSequence candidate2 = selection.apply(population); - assertNotSame(refactorSequence1, candidate1); - assertEquals(refactorSequence1, candidate1); + assertNotSame(refactorSequence1, candidate2); + assertEquals(refactorSequence1, candidate2); } @Test