Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@FieldNameConstants
// TODO convert to record when loadflow-server and computation lib stop to extends it
public class GlobalFilter {
private List<String> nominalV;
private List<List<Integer>> voltageRanges;
private List<Country> countryCode;
private List<UUID> substationOrVoltageLevelFilter; // list of generic filters containing only voltage level and substation filters
private List<UUID> genericFilter;
Expand All @@ -39,7 +39,7 @@ public class GlobalFilter {
* @return {@code true} if all filter parameters are empty, else {@code false}.
*/
public boolean isEmpty() {
return CollectionUtils.isEmpty(this.nominalV)
return CollectionUtils.isEmpty(this.voltageRanges)
&& CollectionUtils.isEmpty(this.countryCode)
&& CollectionUtils.isEmpty(this.genericFilter)
&& CollectionUtils.isEmpty(this.substationOrVoltageLevelFilter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public final class GlobalFilterUtils {
Expand All @@ -42,21 +41,20 @@ public static List<FieldType> getNominalVoltageFieldType(@Nonnull final Equipmen

/**
* Builds nominal voltage rules combining all relevant field types
* @see GlobalFilter#getNominalV()
* @see GlobalFilter#getVoltageRanges()
*/
@Nonnull
public static Optional<AbstractExpertRule> buildNominalVoltageRules(
@Nonnull final List<String> nominalVoltages, @Nonnull final EquipmentType equipmentType) {
@Nonnull final List<List<Integer>> voltageRanges, @Nonnull final EquipmentType equipmentType) {
final List<FieldType> fields = getNominalVoltageFieldType(equipmentType);
return ExpertFilterUtils.buildOrCombination(nominalVoltages.stream()
.filter(Predicate.not(String::isBlank))
.map(Double::valueOf)
.<AbstractExpertRule>mapMulti((value, accumulator) -> {
return ExpertFilterUtils.buildOrCombination(voltageRanges.stream()
.filter(CollectionUtils::isNotEmpty)
.<AbstractExpertRule>mapMulti((range, accumulator) -> {
for (final FieldType field : fields) {
accumulator.accept(NumberExpertRule.builder()
.value(value)
.values(range.stream().map(Integer::doubleValue).collect(Collectors.toSet()))
.field(field)
.operator(OperatorType.EQUALS)
.operator(OperatorType.BETWEEN)
.build());
}
}).toList());
Expand Down Expand Up @@ -232,8 +230,8 @@ public static ExpertFilter buildExpertFilter(@Nonnull final GlobalFilter globalF
}
}

if (globalFilter.getNominalV() != null) {
buildNominalVoltageRules(globalFilter.getNominalV(), equipmentType).ifPresent(andRules::add);
if (CollectionUtils.isNotEmpty(globalFilter.getVoltageRanges())) {
buildNominalVoltageRules(globalFilter.getVoltageRanges(), equipmentType).ifPresent(andRules::add);
}
if (globalFilter.getCountryCode() != null) {
buildCountryCodeRules(globalFilter.getCountryCode(), equipmentType).ifPresent(andRules::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,25 @@ private static <T> ThrowingConsumer<T>[] createAssertArray(final ThrowingConsume
class BuildNominalVoltageRules {
@ParameterizedTest
@MethodSource("expertRulesData")
void shouldCreateExpertRules(final List<String> nominalVoltages) {
void shouldCreateExpertRules(final List<List<Integer>> voltageRanges) {
testVariableOrCombinationRules(
GlobalFilterUtils.buildNominalVoltageRules(nominalVoltages, EquipmentType.VOLTAGE_LEVEL),
nominalVoltages.size(),
GlobalFilterUtils.buildNominalVoltageRules(voltageRanges, EquipmentType.VOLTAGE_LEVEL),
voltageRanges.size(),
NumberExpertRule.class,
createAssertArray(
ner -> assertThat(ner.getValue()).as("value").hasToString(nominalVoltages.getFirst()),
ner -> assertThat(ner.getValues()).as("values").containsExactlyInAnyOrderElementsOf(voltageRanges.getFirst().stream().map(Integer::doubleValue).toList()),
ner -> assertThat(ner.getField()).as("field").isEqualTo(FieldType.NOMINAL_VOLTAGE),
ner -> assertThat(ner.getOperator()).as("operator").isEqualTo(OperatorType.EQUALS)
ner -> assertThat(ner.getOperator()).as("operator").isEqualTo(OperatorType.BETWEEN)
),
nominalVoltages.stream().map(nv -> NumberExpertRule.builder().value(Double.valueOf(nv))
.field(FieldType.NOMINAL_VOLTAGE).operator(OperatorType.EQUALS).build()).collect(Collectors.toUnmodifiableList()));
voltageRanges.stream().map(vr -> NumberExpertRule.builder().values(vr.stream().map(Integer::doubleValue).collect(Collectors.toSet()))
.field(FieldType.NOMINAL_VOLTAGE).operator(OperatorType.BETWEEN).build()).collect(Collectors.toUnmodifiableList()));
}

private static Stream<Arguments> expertRulesData() {
return Stream.of(
Arguments.of(List.of()),
Arguments.of(List.of("300.0")),
Arguments.of(List.of("400.0", "225.0"))
Arguments.of(List.of(List.of(225, 400))),
Arguments.of(List.of(List.of(225, 400), List.of(300, 350)))
);
}
}
Expand Down Expand Up @@ -316,7 +316,7 @@ void shouldReturnResult() {
List.of(new IdentifierListFilterEquipmentAttributes("GEN1", 50.),
new IdentifierListFilterEquipmentAttributes("GEN2", 50.)
)));
final GlobalFilter globalFilter = new GlobalFilter(List.of("380", "225"), List.of(Country.FR, Country.BE), filterUuids, List.of(), Map.of());
final GlobalFilter globalFilter = new GlobalFilter(List.of(List.of(380, 400), List.of(225, 230)), List.of(Country.FR, Country.BE), filterUuids, List.of(), Map.of());
assertThat(GlobalFilterUtils.buildExpertFilter(globalFilter, EquipmentType.GENERATOR, filters, List.of()))
.as("result").isNotNull();
}
Expand All @@ -329,7 +329,7 @@ void shouldReturnResultWithGenericFilterTypeDifferentFromEquipmentType() {
List.of(new IdentifierListFilterEquipmentAttributes("GEN1", 50.),
new IdentifierListFilterEquipmentAttributes("GEN2", 50.)
)));
final GlobalFilter globalFilter = new GlobalFilter(List.of("380", "225"), List.of(Country.FR, Country.BE), filterUuids, List.of(), Map.of());
final GlobalFilter globalFilter = new GlobalFilter(List.of(List.of(380, 400), List.of(225, 230)), List.of(Country.FR, Country.BE), filterUuids, List.of(), Map.of());
assertThat(GlobalFilterUtils.buildExpertFilter(globalFilter, EquipmentType.GENERATOR, List.of(), filters))
.as("result").isNotNull();
}
Expand Down