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

Adds a new EXISTS expert filter operator and PLANNED_ACTIVE_POWER_SET… #80

Merged
merged 4 commits into from
Nov 28, 2023
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 @@ -43,13 +43,17 @@ public DataType getDataType() {
@Override
public boolean evaluateRule(Identifiable<?> identifiable) {
Double identifiableValue = getNumberValue(getFieldValue(this.getField(), identifiable));
if (Double.isNaN(identifiableValue)) {
return false;
}
Double filterValue = this.getValue();
return switch (this.getOperator()) {
case EQUALS -> identifiableValue.equals(filterValue);
case GREATER_OR_EQUALS -> identifiableValue.compareTo(filterValue) >= 0;
case GREATER -> identifiableValue.compareTo(filterValue) > 0;
case LOWER_OR_EQUALS -> identifiableValue.compareTo(filterValue) <= 0;
case LOWER -> identifiableValue.compareTo(filterValue) < 0;
case EXISTS -> true; // We return true here because we already test above if identifiableValue is NaN.
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 @@ -45,6 +45,7 @@ public boolean evaluateRule(Identifiable<?> identifiable) {
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);
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 @@ -8,6 +8,7 @@
package org.gridsuite.filter.server.repositories.proxies.expertfiler;

import com.powsybl.commons.PowsyblException;
import org.apache.commons.lang3.math.NumberUtils;
import org.gridsuite.filter.server.dto.criteriafilter.AbstractEquipmentFilterForm;
import org.gridsuite.filter.server.dto.AbstractFilter;
import org.gridsuite.filter.server.dto.expertfilter.ExpertFilter;
Expand Down Expand Up @@ -66,10 +67,16 @@ public static AbstractExpertRule entityToDto(ExpertRuleEntity filterEntity) {
.build();
}
case NUMBER -> {
Double newValue;
if (NumberUtils.isCreatable(filterEntity.getValue())) {
newValue = NumberUtils.createDouble(filterEntity.getValue());
} else {
newValue = Double.NaN;
}
return NumberExpertRule.builder()
.field(filterEntity.getField())
.operator(filterEntity.getOperator())
.value(Double.valueOf(filterEntity.getValue()))
.value(newValue)
.build();
}
case STRING -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.GeneratorStartup;

import java.util.Optional;

Expand Down Expand Up @@ -48,6 +49,13 @@ private static String getGeneratorFieldValue(FieldType field, Generator generato
case TARGET_P -> String.valueOf(generator.getTargetP());
case TARGET_Q -> String.valueOf(generator.getTargetQ());
case VOLTAGE_REGULATOR_ON -> String.valueOf(generator.isVoltageRegulatorOn());
case PLANNED_ACTIVE_POWER_SET_POINT -> {
GeneratorStartup generatorStartup = generator.getExtension(GeneratorStartup.class);
if (generatorStartup != null) {
yield String.valueOf(generatorStartup.getPlannedActivePowerSetpoint());
}
yield String.valueOf(Double.NaN);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public enum FieldType {
TARGET_Q,
ENERGY_SOURCE,
COUNTRY,
VOLTAGE_REGULATOR_ON
VOLTAGE_REGULATOR_ON,
PLANNED_ACTIVE_POWER_SET_POINT
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public enum OperatorType {
// Common
EQUALS,
NOT_EQUALS,
// Number and String
EXISTS,
// Number
LOWER,
LOWER_OR_EQUALS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.powsybl.iidm.network.EnergySource;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.extensions.GeneratorStartup;
import org.gridsuite.filter.server.dto.expertfilter.expertrule.*;
import org.gridsuite.filter.server.utils.expertfilter.CombinatorType;
import org.gridsuite.filter.server.utils.expertfilter.FieldType;
Expand All @@ -22,6 +23,7 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;

public class ExpertFilterUtilsTest {

Expand Down Expand Up @@ -214,4 +216,45 @@ public void testEvaluateExpertFilterIgnoreCase() {

assertTrue(result);
}

@Test
public void testEvaluateExpertFilterExists() {
List<AbstractExpertRule> numRules = new ArrayList<>();
numRules.add(NumberExpertRule.builder().field(FieldType.TARGET_V).operator(OperatorType.EXISTS).build());
CombinatorExpertRule numFilter = CombinatorExpertRule.builder().combinator(CombinatorType.AND).rules(numRules).build();

List<AbstractExpertRule> stringRules = new ArrayList<>();
stringRules.add(StringExpertRule.builder().field(FieldType.NAME).operator(OperatorType.EXISTS).build());
CombinatorExpertRule stringFilter = CombinatorExpertRule.builder().combinator(CombinatorType.AND).rules(stringRules).build();

// Test when value exists
assertTrue(numFilter.evaluateRule(gen));
assertTrue(stringFilter.evaluateRule(gen));

// Test when value does not exist
Mockito.when(gen.getTargetV()).thenReturn(Double.NaN);
assertFalse(numFilter.evaluateRule(gen));

Mockito.when(gen.getNameOrId()).thenReturn(null);
assertFalse(stringFilter.evaluateRule(gen));

Mockito.when(gen.getNameOrId()).thenReturn("");
assertFalse(stringFilter.evaluateRule(gen));
}

@Test
public void testEvaluateExpertFilterExtension() {
List<AbstractExpertRule> numRules = new ArrayList<>();
numRules.add(NumberExpertRule.builder().field(FieldType.PLANNED_ACTIVE_POWER_SET_POINT).operator(OperatorType.EXISTS).build());
CombinatorExpertRule numFilter = CombinatorExpertRule.builder().combinator(CombinatorType.AND).rules(numRules).build();

// Test when extension does not exist
assertFalse(numFilter.evaluateRule(gen));

// Test with extension
GeneratorStartup genStart = Mockito.mock(GeneratorStartup.class);
Mockito.when(genStart.getPlannedActivePowerSetpoint()).thenReturn(50.0);
Mockito.when(gen.getExtension(any())).thenReturn(genStart);
assertTrue(numFilter.evaluateRule(gen));
}
}
Loading