Skip to content

Commit

Permalink
Refactor traverse method to avoid code duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Sophie Frasnedo <[email protected]>
  • Loading branch information
So-Fras committed Dec 7, 2023
1 parent 1168c00 commit 9ea2ce6
Showing 1 changed file with 9 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,29 @@ public static Collection<VoltageLevel> getNextDepthVoltageLevels(Network network
return voltageLevelSet;
}

private static void traverseVoltageLevels(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels) {
private static void traverseVoltageLevels(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels, Predicate<VoltageLevel> predicate) {
if (depth < 0) {
return;
}
Set<VoltageLevel> nextDepthVoltageLevels = new HashSet<>();
for (VoltageLevel vl : voltageLevelsDepth) {
if (!visitedVoltageLevels.contains(vl)) {
if (!visitedVoltageLevels.contains(vl) && predicate.test(vl)) {
visitedVoltageLevels.add(vl);
vl.visitEquipments(new VlVisitor(nextDepthVoltageLevels, visitedVoltageLevels));
}
}
traverseVoltageLevels(nextDepthVoltageLevels, depth - 1, visitedVoltageLevels);
traverseVoltageLevels(nextDepthVoltageLevels, depth - 1, visitedVoltageLevels, predicate);
}

private static void traverseVoltageLevels(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels) {
traverseVoltageLevels(voltageLevelsDepth, depth, visitedVoltageLevels, NO_FILTER);
}

private static void traverseVoltageLevelsWithVoltageFilter(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels,
double lowNominalVoltageBound, double highNominalVoltageBound) {
if (depth < 0) {
return;
}

Set<VoltageLevel> nextDepthVoltageLevels = new HashSet<>();
for (VoltageLevel vl : voltageLevelsDepth) {
if (!visitedVoltageLevels.contains(vl)
&& vl.getNominalV() >= lowNominalVoltageBound
&& vl.getNominalV() <= highNominalVoltageBound) {
traverseVoltageLevel(visitedVoltageLevels, nextDepthVoltageLevels, vl);
}
}
traverseVoltageLevelsWithVoltageFilter(nextDepthVoltageLevels, depth - 1, visitedVoltageLevels, lowNominalVoltageBound, highNominalVoltageBound);

}

private static void traverseVoltageLevel(Set<VoltageLevel> visitedVoltageLevels, Set<VoltageLevel> nextDepthVoltageLevels, VoltageLevel vl) {
visitedVoltageLevels.add(vl);
vl.visitEquipments(new VlVisitor(nextDepthVoltageLevels, visitedVoltageLevels));
Predicate<VoltageLevel> voltageFilterPredicate = voltageLevel -> voltageLevel.getNominalV() >= lowNominalVoltageBound && voltageLevel.getNominalV() <= highNominalVoltageBound;
traverseVoltageLevels(voltageLevelsDepth, depth, visitedVoltageLevels, voltageFilterPredicate);
}

public static VoltageLevelFilter createNominalVoltageFilter(Network network, List<String> voltageLevelIds,
Expand Down

0 comments on commit 9ea2ce6

Please sign in to comment.