Skip to content

Commit

Permalink
fix regulating by setting phasetapchanger equals and hashcode
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <[email protected]>
  • Loading branch information
EtienneLt committed Oct 28, 2024
1 parent 8b94024 commit 516b0af
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ abstract class AbstractTapChanger<H extends TapChangerParent, C extends Abstract
return parent.getTransformer();
}

protected H getParent() {
return parent;
}

protected Resource<?> getResource() {
return getTransformer().getResource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import com.powsybl.iidm.network.*;
import com.powsybl.network.store.model.*;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -148,4 +146,35 @@ public static void validateStep(TapChangerStepAttributes step, TapChangerParent
throw new ValidationException(parent, "step alpha is not set");
}
}

// equals and hashcode are override because of ValidationUtil.checkPhaseTapChangerRegulation that
// remove the tap changer of the list of allRegulatingTapChanger
// and it does not work if equals and hascode check this.attributesGetter
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PhaseTapChangerImpl that = (PhaseTapChangerImpl) o;
// check phase tap changer are on same leg
if (!Objects.equals(that.getTransformer().getClass(), getTransformer().getClass())) {
return false;
}
if (that.getTransformer() instanceof ThreeWindingsTransformerImpl &&
!Objects.equals(((ThreeWindingsTransformerImpl.LegImpl) parent).getSide(),
((ThreeWindingsTransformerImpl.LegImpl) that.getParent()).getSide())) {
return false;
}
return Objects.equals(getTransformer().getId(), that.getTransformer().getId()) &&
Objects.equals(getRegulationMode(), that.getRegulationMode()) &&
Objects.equals(getRegulationValue(), that.getRegulationValue());
}

@Override
public int hashCode() {
return Objects.hash(getParent(), getTransformer().getId(), getRegulationMode(), getRegulationValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import com.powsybl.iidm.network.*;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -37,6 +35,19 @@ void testTapChangerRemoval() {
assertNull(twtWithPhaseTapChanger.getPhaseTapChanger());
}

@Test
void testPhaseTapChangerEqualsAndHashCode() {
Network network = createNetwork();
TwoWindingsTransformer twtWithPhaseTapChanger = network.getTwoWindingsTransformer("a708c3bc-465d-4fe7-b6ef-6fa6408a62b0");
Set<PhaseTapChanger> phaseTapChangers = new HashSet<>();
phaseTapChangers.add(twtWithPhaseTapChanger.getPhaseTapChanger());

// use the equals and the hashcode of PhaseTapChangerImpl
phaseTapChangers.remove(twtWithPhaseTapChanger.getPhaseTapChanger());

assertEquals(0, phaseTapChangers.size());
}

@Test
void testTapChangerStepsReplacement() {
Network network = createNetwork();
Expand Down

0 comments on commit 516b0af

Please sign in to comment.