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

Add support for NOT_EXISTS operator. #16

Merged
merged 1 commit into from
Apr 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.stream.Collectors;

import static org.gridsuite.filter.utils.expertfilter.ExpertFilterUtils.getFieldValue;
import static org.gridsuite.filter.utils.expertfilter.OperatorType.NOT_EXISTS;
import static org.gridsuite.filter.utils.expertfilter.OperatorType.isMultipleCriteriaOperator;

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ public DataType getDataType() {
public boolean evaluateRule(Identifiable<?> identifiable, FilterLoader filterLoader, Map<UUID, FilterEquipments> cachedUuidFilters) {
Double identifiableValue = getNumberValue(getFieldValue(this.getField(), null, identifiable));
if (Double.isNaN(identifiableValue)) {
return false;
return this.getOperator() == NOT_EXISTS;
}
Double filterValue = this.getValue();
Set<Double> filterValues = this.getValues();
Expand All @@ -69,6 +70,7 @@ public boolean evaluateRule(Identifiable<?> identifiable, FilterLoader filterLoa
}
case LOWER -> identifiableValue.compareTo(filterValue) < 0;
case EXISTS -> true; // We return true here because we already test above if identifiableValue is NaN.
case NOT_EXISTS -> false; // if true, checked above
case IN -> filterValues.contains(identifiableValue);
case NOT_IN -> !filterValues.contains(identifiableValue);
default ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.*;

import static org.gridsuite.filter.utils.expertfilter.ExpertFilterUtils.getFieldValue;
import static org.gridsuite.filter.utils.expertfilter.OperatorType.NOT_EXISTS;
import static org.gridsuite.filter.utils.expertfilter.OperatorType.isMultipleCriteriaOperator;

/**
Expand Down Expand Up @@ -58,14 +59,15 @@ public DataType getDataType() {
public boolean evaluateRule(Identifiable<?> identifiable, FilterLoader filterLoader, Map<UUID, FilterEquipments> cachedUuidFilters) {
String identifiableValue = getFieldValue(this.getField(), null, identifiable);
if (identifiableValue == null) {
return false;
return this.getOperator() == NOT_EXISTS;
}
return switch (this.getOperator()) {
case IS -> identifiableValue.equalsIgnoreCase(this.getValue());
case CONTAINS -> StringUtils.containsIgnoreCase(identifiableValue, this.getValue());
case BEGINS_WITH -> StringUtils.startsWithIgnoreCase(identifiableValue, this.getValue());
case ENDS_WITH -> StringUtils.endsWithIgnoreCase(identifiableValue, this.getValue());
case EXISTS -> !StringUtils.isEmpty(identifiableValue);
case NOT_EXISTS -> false; // if true, checked above
case IN -> this.getValues().contains(identifiableValue);
case NOT_IN -> !this.getValues().contains(identifiableValue);
default -> throw new PowsyblException(this.getOperator() + " operator not supported with " + this.getDataType() + " rule data type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum OperatorType {
NOT_IN,
// Number and String
EXISTS,
NOT_EXISTS,
// Number
LOWER,
LOWER_OR_EQUALS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,33 @@ private static Stream<Arguments> provideArgumentsForGeneratorTest() {
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, gen, true),
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, gen1, false),

// --- NOT_EXISTS --- //
// Generator fields
Arguments.of(NOT_EXISTS, FieldType.MIN_P, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.MIN_P, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.MAX_P, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.MAX_P, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.TARGET_V, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.TARGET_V, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.TARGET_P, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.TARGET_P, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.TARGET_Q, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.TARGET_Q, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.RATED_S, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.RATED_S, null, null, gen1, true),
// GeneratorStartup extension fields
Arguments.of(NOT_EXISTS, FieldType.PLANNED_ACTIVE_POWER_SET_POINT, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.PLANNED_ACTIVE_POWER_SET_POINT, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.MARGINAL_COST, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.MARGINAL_COST, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.PLANNED_OUTAGE_RATE, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.PLANNED_OUTAGE_RATE, null, null, gen1, true),
Arguments.of(NOT_EXISTS, FieldType.FORCED_OUTAGE_RATE, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.FORCED_OUTAGE_RATE, null, null, gen1, true),
// VoltageLevel fields
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, gen, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, gen1, true),

// --- IN --- //
// Generator fields
Arguments.of(IN, FieldType.MIN_P, null, Set.of(-600.0, -500.0, -400.0), gen, true),
Expand Down Expand Up @@ -546,6 +573,16 @@ private static Stream<Arguments> provideArgumentsForLoadTest() {
Arguments.of(EXISTS, FieldType.Q0, null, null, load, true),
Arguments.of(EXISTS, FieldType.Q0, null, null, load1, false),

// --- NOT_EXISTS --- //
// VoltageLevel fields
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, load, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, load1, true),
// Load fields
Arguments.of(NOT_EXISTS, FieldType.P0, null, null, load, false),
Arguments.of(NOT_EXISTS, FieldType.P0, null, null, load1, true),
Arguments.of(NOT_EXISTS, FieldType.Q0, null, null, load, false),
Arguments.of(NOT_EXISTS, FieldType.Q0, null, null, load1, true),

// --- IN --- //
// VoltageLevel fields
Arguments.of(IN, FieldType.NOMINAL_VOLTAGE, null, Set.of(12.0, 13.0, 14.0), load, true),
Expand Down Expand Up @@ -625,6 +662,11 @@ private static Stream<Arguments> provideArgumentsForBusTest() {
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, bus, true),
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, bus1, false),

// --- NOT_EXISTS --- //
// VoltageLevel fields
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, bus, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, bus1, true),

// --- IN --- //
// VoltageLevel fields
Arguments.of(IN, FieldType.NOMINAL_VOLTAGE, null, Set.of(12.0, 13.0, 14.0), bus, true),
Expand Down Expand Up @@ -698,6 +740,11 @@ private static Stream<Arguments> provideArgumentsForBusBarSectionTest() {
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, busbarSection, true),
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, busbarSection1, false),

// --- NOT_EXISTS --- //
// VoltageLevel fields
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, busbarSection, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, busbarSection1, true),

// --- IN --- //
// VoltageLevel fields
Arguments.of(IN, FieldType.NOMINAL_VOLTAGE, null, Set.of(12.0, 13.0, 14.0), busbarSection, true),
Expand Down Expand Up @@ -912,6 +959,11 @@ public ShuntCompensatorLinearModel setMaximumSectionCount(int i) {
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, shuntCompensator, true),
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, shuntCompensator1, false),

// --- NOT_EXISTS --- //
// VoltageLevel fields
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, shuntCompensator, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, shuntCompensator1, true),

// Shunt Compensator Fields

// --- IN --- //
Expand Down Expand Up @@ -1167,6 +1219,14 @@ private static Stream<Arguments> provideArgumentsForLinesTest() {
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE_2, null, null, line, true),
Arguments.of(EXISTS, FieldType.NOMINAL_VOLTAGE_2, null, null, line1, false),

// --- NOT_EXISTS --- //
// VoltageLevel fields
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_1, null, null, line, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_1, null, null, line1, true),

Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_2, null, null, line, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_2, null, null, line1, true),

// --- IN --- //
// VoltageLevel fields
Arguments.of(IN, FieldType.NOMINAL_VOLTAGE_1, null, Set.of(12., 13., 14.), line, true),
Expand Down Expand Up @@ -1356,6 +1416,19 @@ private static Stream<Arguments> provideArgumentsForBatteryTest() {
Arguments.of(EXISTS, FieldType.TARGET_P, null, null, battery1, false),
Arguments.of(EXISTS, FieldType.TARGET_Q, null, null, battery, true),
Arguments.of(EXISTS, FieldType.TARGET_Q, null, null, battery1, false),
// --- NOT_EXISTS --- //
// VoltageLevel fields
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, battery, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, battery1, true),
//Battery fields
Arguments.of(NOT_EXISTS, FieldType.MIN_P, null, null, battery, false),
Arguments.of(NOT_EXISTS, FieldType.MIN_P, null, null, battery1, true),
Arguments.of(NOT_EXISTS, FieldType.MAX_P, null, null, battery, false),
Arguments.of(NOT_EXISTS, FieldType.MAX_P, null, null, battery1, true),
Arguments.of(NOT_EXISTS, FieldType.TARGET_P, null, null, battery, false),
Arguments.of(NOT_EXISTS, FieldType.TARGET_P, null, null, battery1, true),
Arguments.of(NOT_EXISTS, FieldType.TARGET_Q, null, null, battery, false),
Arguments.of(NOT_EXISTS, FieldType.TARGET_Q, null, null, battery1, true),

// --- IN --- //
// VoltageLevel fields
Expand Down Expand Up @@ -1473,6 +1546,14 @@ private static Stream<Arguments> provideArgumentsForVoltageLevelTest() {
Arguments.of(EXISTS, FieldType.HIGH_VOLTAGE_LIMIT, null, null, voltageLevel, true),
Arguments.of(EXISTS, FieldType.HIGH_VOLTAGE_LIMIT, null, null, voltageLevel1, false),

// --- NOT_EXISTS --- //
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, voltageLevel, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE, null, null, voltageLevel1, true),
Arguments.of(NOT_EXISTS, FieldType.LOW_VOLTAGE_LIMIT, null, null, voltageLevel, false),
Arguments.of(NOT_EXISTS, FieldType.LOW_VOLTAGE_LIMIT, null, null, voltageLevel1, true),
Arguments.of(NOT_EXISTS, FieldType.HIGH_VOLTAGE_LIMIT, null, null, voltageLevel, false),
Arguments.of(NOT_EXISTS, FieldType.HIGH_VOLTAGE_LIMIT, null, null, voltageLevel1, true),

// --- IN --- //
Arguments.of(IN, FieldType.NOMINAL_VOLTAGE, null, Set.of(12.0, 13.0, 14.0), voltageLevel, true),
Arguments.of(IN, FieldType.NOMINAL_VOLTAGE, null, Set.of(12.0, 14.0), voltageLevel, false),
Expand Down Expand Up @@ -1527,6 +1608,8 @@ private static Stream<Arguments> provideArgumentsForTwoWindingTransformerTest()
Mockito.when(twoWindingsTransformer1.getG()).thenReturn(Double.NaN);
Mockito.when(twoWindingsTransformer1.getB()).thenReturn(Double.NaN);
Mockito.when(twoWindingsTransformer1.getRatedS()).thenReturn(Double.NaN);
Mockito.when(twoWindingsTransformer1.getRatedU1()).thenReturn(Double.NaN);
Mockito.when(twoWindingsTransformer1.getRatedU2()).thenReturn(Double.NaN);

// Terminal fields
Terminal terminal1 = Mockito.mock(Terminal.class);
Expand Down Expand Up @@ -1769,9 +1852,34 @@ private static Stream<Arguments> provideArgumentsForTwoWindingTransformerTest()
Arguments.of(EXISTS, FieldType.RATED_S, null, null, twoWindingsTransformer, true),
Arguments.of(EXISTS, FieldType.RATED_S, null, null, twoWindingsTransformer1, false),
Arguments.of(EXISTS, FieldType.RATED_VOLTAGE_1, null, null, twoWindingsTransformer, true),
Arguments.of(EXISTS, FieldType.RATED_VOLTAGE_1, null, null, twoWindingsTransformer1, true),
Arguments.of(EXISTS, FieldType.RATED_VOLTAGE_1, null, null, twoWindingsTransformer1, false),
Arguments.of(EXISTS, FieldType.RATED_VOLTAGE_2, null, null, twoWindingsTransformer, true),
Arguments.of(EXISTS, FieldType.RATED_VOLTAGE_2, null, null, twoWindingsTransformer1, true),
Arguments.of(EXISTS, FieldType.RATED_VOLTAGE_2, null, null, twoWindingsTransformer1, false),

// --- NOT_EXISTS --- //
// Terminal
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_1, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_1, null, null, twoWindingsTransformer1, true),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_2, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.NOMINAL_VOLTAGE_2, null, null, twoWindingsTransformer1, true),
// RatioTapChanger fields
Arguments.of(NOT_EXISTS, FieldType.RATIO_TARGET_V, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.RATIO_TARGET_V, null, null, twoWindingsTransformer1, true),
// TwoWindingsTransformer fields
Arguments.of(NOT_EXISTS, FieldType.SERIE_RESISTANCE, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.SERIE_RESISTANCE, null, null, twoWindingsTransformer1, true),
Arguments.of(NOT_EXISTS, FieldType.SERIE_REACTANCE, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.SERIE_REACTANCE, null, null, twoWindingsTransformer1, true),
Arguments.of(NOT_EXISTS, FieldType.MAGNETIZING_CONDUCTANCE, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.MAGNETIZING_CONDUCTANCE, null, null, twoWindingsTransformer1, true),
Arguments.of(NOT_EXISTS, FieldType.MAGNETIZING_SUSCEPTANCE, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.MAGNETIZING_SUSCEPTANCE, null, null, twoWindingsTransformer1, true),
Arguments.of(NOT_EXISTS, FieldType.RATED_S, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.RATED_S, null, null, twoWindingsTransformer1, true),
Arguments.of(NOT_EXISTS, FieldType.RATED_VOLTAGE_1, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.RATED_VOLTAGE_1, null, null, twoWindingsTransformer1, true),
Arguments.of(NOT_EXISTS, FieldType.RATED_VOLTAGE_2, null, null, twoWindingsTransformer, false),
Arguments.of(NOT_EXISTS, FieldType.RATED_VOLTAGE_2, null, null, twoWindingsTransformer1, true),

// --- IN --- //
// Terminal
Expand Down
Loading
Loading